test fixups
[dbsrgits/DBIx-Class.git] / t / 68inflate_serialize.t
1 use strict;
2 use warnings;  
3
4 use Test::More;
5 use lib qw(t/lib);
6 use DBICTest;
7
8 my $schema = DBICTest->init_schema();
9
10 use Data::Dumper;
11
12 my @serializers = (
13     {   module => 'YAML.pm',
14         inflater => sub { YAML::Load (shift) },
15         deflater => sub { die "Expecting a reference" unless (ref $_[0]); YAML::Dump (shift) },
16     },
17     {   module => 'Storable.pm',
18         inflater => sub { Storable::thaw (shift) },
19         deflater => sub { die "Expecting a reference" unless (ref $_[0]); Storable::nfreeze (shift) },
20     },
21 );
22
23
24 my $selected;
25 foreach my $serializer (@serializers) {
26     eval { require $serializer->{module} };
27     unless ($@) {
28         $selected = $serializer;
29         last;
30     }
31 }
32
33 plan (skip_all => "No suitable serializer found") unless $selected;
34
35 plan (tests => 8);
36 DBICTest::Schema::Serialized->inflate_column( 'serialized',
37     { inflate => $selected->{inflater},
38       deflate => $selected->{deflater},
39     },
40 );
41 Class::C3->reinitialize;
42
43 my $struct_hash = {
44     a => 1,
45     b => [ 
46         { c => 2 },
47     ],
48     d => 3,
49 };
50
51 my $struct_array = [
52     'a', 
53     { 
54         b => 1,
55         c => 2
56     },
57     'd',
58 ];
59
60 my $rs = $schema->resultset('Serialized');
61 my $inflated;
62
63 #======= testing hashref serialization
64
65 my $object = $rs->create( { 
66     id => 1,
67     serialized => '',
68 } );
69 ok($object->update( { serialized => $struct_hash } ), 'hashref deflation');
70 ok($inflated = $object->serialized, 'hashref inflation');
71 is_deeply($inflated, $struct_hash, 'inflated hash matches original');
72
73 $object = $rs->create( { 
74     id => 2,
75     serialized => '',
76 } );
77 eval { $object->set_inflated_column('serialized', $struct_hash) };
78 ok(!$@, 'set_inflated_column to a hashref');
79 is_deeply($object->serialized, $struct_hash, 'inflated hash matches original');
80
81
82 #====== testing arrayref serialization
83
84 ok($object->update( { serialized => $struct_array } ), 'arrayref deflation');
85 ok($inflated = $object->serialized, 'arrayref inflation');
86 is_deeply($inflated, $struct_array, 'inflated array matches original');