Introduce GOVERNANCE document and empty RESOLUTIONS file.
[dbsrgits/DBIx-Class.git] / t / inflate / serialize.t
1 BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7
8 use DBICTest;
9
10 my $schema = DBICTest->init_schema();
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 DBICTest::Schema::Serialized->inflate_column( 'serialized',
34     { inflate => $selected->{inflater},
35       deflate => $selected->{deflater},
36     },
37 );
38 Class::C3->reinitialize if DBIx::Class::_ENV_::OLD_MRO;
39
40 my $struct_hash = {
41     a => 1,
42     b => [
43         { c => 2 },
44     ],
45     d => 3,
46 };
47
48 my $struct_array = [
49     'a',
50     {
51       b => 1,
52       c => 2,
53     },
54     'd',
55 ];
56
57 my $rs = $schema->resultset('Serialized');
58 my $inflated;
59
60 #======= testing hashref serialization
61
62 my $object = $rs->create( {
63     serialized => '',
64 } );
65 ok($object->update( { serialized => $struct_hash } ), 'hashref deflation');
66 ok($inflated = $object->serialized, 'hashref inflation');
67 is_deeply($inflated, $struct_hash, 'inflated hash matches original');
68
69 $object = $rs->create( {
70     serialized => '',
71 } );
72 $object->set_inflated_column('serialized', $struct_hash);
73 is_deeply($object->serialized, $struct_hash, 'inflated hash matches original');
74
75 $object = $rs->new({});
76 $object->serialized ($struct_hash);
77 $object->insert;
78 is_deeply (
79   $rs->find ({id => $object->id})->serialized,
80   $struct_hash,
81   'new/insert works',
82 );
83
84 #====== testing arrayref serialization
85
86 ok($object->update( { serialized => $struct_array } ), 'arrayref deflation');
87 ok($inflated = $object->serialized, 'arrayref inflation');
88 is_deeply($inflated, $struct_array, 'inflated array matches original');
89
90 $object = $rs->new({});
91 $object->serialized ($struct_array);
92 $object->insert;
93 is_deeply (
94   $rs->find ({id => $object->id})->serialized,
95   $struct_array,
96   'new/insert works',
97 );
98
99 #===== make sure make_column_dirty interacts reasonably with inflation
100 $object = $rs->search({}, { rows => 1 })->next;
101 $object->update ({serialized => { x => 'y'}});
102
103 $object->serialized->{x} = 'z'; # change state without notifying $object
104 ok (!$object->get_dirty_columns, 'no dirty columns yet');
105 is_deeply ($object->serialized, { x => 'z' }, 'object data correct');
106
107 $object->make_column_dirty('serialized');
108 $object->update;
109
110 is_deeply ($rs->first->serialized, { x => 'z' }, 'changes made it to the db' );
111
112 done_testing;