File: serializer.t

package info (click to toggle)
libkiokudb-perl 0.57-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,396 kB
  • sloc: perl: 13,314; makefile: 12
file content (71 lines) | stat: -rw-r--r-- 1,626 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/perl

use strict;
use warnings;

use Test::More;
use Test::Moose;

use constant HAVE_YAML => eval { require YAML::XS } ? 1 : 0;

use KiokuDB::Serializer;
use KiokuDB::Serializer::JSON;
use KiokuDB::Serializer::Storable;
use if HAVE_YAML, 'KiokuDB::Serializer::YAML';

use KiokuDB::Entry;

sub KiokuDB::Entry::BUILD { shift->root }; # force building of root for is_deeply
$_->make_mutable, $_->make_immutable for KiokuDB::Entry->meta; # recreate new


{
    foreach my $serializer ( qw(JSON Storable), HAVE_YAML ? "YAML" : () ) {
        my $s = "KiokuDB::Serializer::$serializer"->new;

        does_ok( $s, "KiokuDB::Serializer" );
        does_ok( $s, "KiokuDB::Backend::Serialize" );

        my $entry = KiokuDB::Entry->new(
            class => "KiokuDB_Test_Foo",
            data  => { foo => "bar" },
        );

        my $ser = $s->serialize( $entry );

        ok( !ref($ser), "non ref" );
        ok( length($ser), "got data" );

        is_deeply( $s->deserialize($ser), $entry, "round tripping" );

        my $buf = '';

        open my $out, ">", \$buf;

        $s->serialize_to_stream($out, $entry) for 1 .. 3;

        close $out;

        ok( length($buf), "serialize_to_stream" );

        open my $in, "<", \$buf;

        my @entries;

        my $n;

        while ( my @got = $s->deserialize_from_stream($in) ) {
            $n++;
            push @entries, @got;
        }

        is( scalar(@entries), 3, "three entries from stream ($n reads)" );

        isa_ok( $_, "KiokuDB::Entry" ) for @entries;

        is_deeply( $entries[0], $entry, "round tripping" );
    }
}


done_testing;