Commit | Line | Data |
70350518 |
1 | use strict; |
2 | use warnings; |
3 | |
4 | use Test::More; |
5 | use lib qw(t/lib); |
6 | use DBICTest; |
7 | |
a47e1233 |
8 | my $schema = DBICTest->init_schema(); |
e9100ff7 |
9 | |
10 | use Data::Dumper; |
11 | |
12 | my @serializers = ( |
c9372242 |
13 | { module => 'YAML.pm', |
14 | inflater => sub { YAML::Load (shift) }, |
15 | deflater => sub { die "Expecting a reference" unless (ref $_[0]); YAML::Dump (shift) }, |
e9100ff7 |
16 | }, |
c9372242 |
17 | { module => 'Storable.pm', |
18 | inflater => sub { Storable::thaw (shift) }, |
19 | deflater => sub { die "Expecting a reference" unless (ref $_[0]); Storable::nfreeze (shift) }, |
e9100ff7 |
20 | }, |
21 | ); |
22 | |
23 | |
24 | my $selected; |
25 | foreach my $serializer (@serializers) { |
26 | eval { require $serializer->{module} }; |
27 | unless ($@) { |
c9372242 |
28 | $selected = $serializer; |
29 | last; |
e9100ff7 |
30 | } |
31 | } |
32 | |
33 | plan (skip_all => "No suitable serializer found") unless $selected; |
34 | |
e9100ff7 |
35 | DBICTest::Schema::Serialized->inflate_column( 'serialized', |
36 | { inflate => $selected->{inflater}, |
37 | deflate => $selected->{deflater}, |
38 | }, |
39 | ); |
40 | Class::C3->reinitialize; |
41 | |
8b621a87 |
42 | my $struct_hash = { |
43 | a => 1, |
c9372242 |
44 | b => [ |
8b621a87 |
45 | { c => 2 }, |
46 | ], |
47 | d => 3, |
e9100ff7 |
48 | }; |
49 | |
8b621a87 |
50 | my $struct_array = [ |
c9372242 |
51 | 'a', |
52 | { |
53 | b => 1, |
54 | c => 2, |
8b621a87 |
55 | }, |
56 | 'd', |
57 | ]; |
e9100ff7 |
58 | |
59 | my $rs = $schema->resultset('Serialized'); |
e9100ff7 |
60 | my $inflated; |
61 | |
8b621a87 |
62 | #======= testing hashref serialization |
e81a6241 |
63 | |
8b621a87 |
64 | my $object = $rs->create( { |
8b621a87 |
65 | serialized => '', |
66 | } ); |
67 | ok($object->update( { serialized => $struct_hash } ), 'hashref deflation'); |
68 | ok($inflated = $object->serialized, 'hashref inflation'); |
69 | is_deeply($inflated, $struct_hash, 'inflated hash matches original'); |
70 | |
71 | $object = $rs->create( { |
8b621a87 |
72 | serialized => '', |
73 | } ); |
31c3800e |
74 | $object->set_inflated_column('serialized', $struct_hash); |
8b621a87 |
75 | is_deeply($object->serialized, $struct_hash, 'inflated hash matches original'); |
76 | |
31c3800e |
77 | $object = $rs->new({}); |
78 | $object->serialized ($struct_hash); |
79 | $object->insert; |
80 | is_deeply ( |
81 | $rs->find ({id => $object->id})->serialized, |
82 | $struct_hash, |
83 | 'new/insert works', |
84 | ); |
e81a6241 |
85 | |
8b621a87 |
86 | #====== testing arrayref serialization |
e9100ff7 |
87 | |
8b621a87 |
88 | ok($object->update( { serialized => $struct_array } ), 'arrayref deflation'); |
89 | ok($inflated = $object->serialized, 'arrayref inflation'); |
90 | is_deeply($inflated, $struct_array, 'inflated array matches original'); |
497d874a |
91 | |
31c3800e |
92 | $object = $rs->new({}); |
93 | $object->serialized ($struct_array); |
94 | $object->insert; |
95 | is_deeply ( |
96 | $rs->find ({id => $object->id})->serialized, |
97 | $struct_array, |
98 | 'new/insert works', |
99 | ); |
497d874a |
100 | |
31c3800e |
101 | #===== make sure make_column_dirty interacts reasonably with inflation |
497d874a |
102 | $object = $rs->first; |
103 | $object->update ({serialized => { x => 'y'}}); |
104 | |
105 | $object->serialized->{x} = 'z'; # change state without notifying $object |
106 | ok (!$object->get_dirty_columns, 'no dirty columns yet'); |
107 | is_deeply ($object->serialized, { x => 'z' }, 'object data correct'); |
108 | |
109 | $object->make_column_dirty('serialized'); |
110 | $object->update; |
111 | |
112 | is_deeply ($rs->first->serialized, { x => 'z' }, 'changes made it to the db' ); |
c9372242 |
113 | |
114 | done_testing; |