updated my contact info
[dbsrgits/DBIx-Class.git] / t / run / 08inflate_serialize.tl
1 sub run_tests {
2 my $schema = shift;
3
4 use Data::Dumper;
5
6 my @serializers = (
7     {   module => 'YAML.pm',
8         inflater => sub { YAML::Load (shift) },
9         deflater => sub { die "Expecting a reference" unless (ref $_[0]); YAML::Dump (shift) },
10     },
11     {   module => 'Storable.pm',
12         inflater => sub { Storable::thaw (shift) },
13         deflater => sub { die "Expecting a reference" unless (ref $_[0]); Storable::nfreeze (shift) },
14     },
15 );
16
17
18 my $selected;
19 foreach my $serializer (@serializers) {
20     eval { require $serializer->{module} };
21     unless ($@) {
22         $selected = $serializer;
23         last;
24     }
25 }
26
27 plan (skip_all => "No suitable serializer found") unless $selected;
28
29 plan (tests => 6);
30 DBICTest::Schema::Serialized->inflate_column( 'serialized',
31     { inflate => $selected->{inflater},
32       deflate => $selected->{deflater},
33     },
34 );
35 Class::C3->reinitialize;
36
37 my $complex1 = {
38     id => 1,
39     serialized => {
40         a => 1,
41         b => [ 
42             { c => 2 },
43         ],
44         d => 3,
45     },
46 };
47
48 my $complex2 = {
49     id => 1,
50     serialized => [
51                 'a', 
52                 { b => 1, c => 2},
53                 'd',
54             ],
55 };
56
57 my $rs = $schema->resultset('Serialized');
58 my $entry = $rs->create({ id => 1, serialized => ''});
59
60 my $inflated;
61
62 ok($entry->update ({ %{$complex1} }), 'hashref deflation ok');
63 ok($inflated = $entry->serialized, 'hashref inflation ok');
64 is_deeply($inflated, $complex1->{serialized}, 'inflated hash matches original');
65
66 ok($entry->update ({ %{$complex2} }), 'arrayref deflation ok');
67 ok($inflated = $entry->serialized, 'arrayref inflation ok');
68 is_deeply($inflated, $complex2->{serialized}, 'inflated array matches original');
69
70 }
71
72 1;