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 | |
e9100ff7 |
10 | my @serializers = ( |
c9372242 |
11 | { module => 'YAML.pm', |
12 | inflater => sub { YAML::Load (shift) }, |
13 | deflater => sub { die "Expecting a reference" unless (ref $_[0]); YAML::Dump (shift) }, |
e9100ff7 |
14 | }, |
c9372242 |
15 | { module => 'Storable.pm', |
16 | inflater => sub { Storable::thaw (shift) }, |
17 | deflater => sub { die "Expecting a reference" unless (ref $_[0]); Storable::nfreeze (shift) }, |
e9100ff7 |
18 | }, |
19 | ); |
20 | |
21 | |
22 | my $selected; |
23 | foreach my $serializer (@serializers) { |
24 | eval { require $serializer->{module} }; |
25 | unless ($@) { |
c9372242 |
26 | $selected = $serializer; |
27 | last; |
e9100ff7 |
28 | } |
29 | } |
30 | |
31 | plan (skip_all => "No suitable serializer found") unless $selected; |
32 | |
e9100ff7 |
33 | DBICTest::Schema::Serialized->inflate_column( 'serialized', |
34 | { inflate => $selected->{inflater}, |
35 | deflate => $selected->{deflater}, |
36 | }, |
37 | ); |
38 | Class::C3->reinitialize; |
39 | |
8b621a87 |
40 | my $struct_hash = { |
41 | a => 1, |
c9372242 |
42 | b => [ |
8b621a87 |
43 | { c => 2 }, |
44 | ], |
45 | d => 3, |
e9100ff7 |
46 | }; |
47 | |
8b621a87 |
48 | my $struct_array = [ |
c9372242 |
49 | 'a', |
50 | { |
51 | b => 1, |
52 | c => 2, |
8b621a87 |
53 | }, |
54 | 'd', |
55 | ]; |
e9100ff7 |
56 | |
57 | my $rs = $schema->resultset('Serialized'); |
e9100ff7 |
58 | my $inflated; |
59 | |
8b621a87 |
60 | #======= testing hashref serialization |
e81a6241 |
61 | |
8b621a87 |
62 | my $object = $rs->create( { |
8b621a87 |
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( { |
8b621a87 |
70 | serialized => '', |
71 | } ); |
31c3800e |
72 | $object->set_inflated_column('serialized', $struct_hash); |
8b621a87 |
73 | is_deeply($object->serialized, $struct_hash, 'inflated hash matches original'); |
74 | |
31c3800e |
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 | ); |
e81a6241 |
83 | |
8b621a87 |
84 | #====== testing arrayref serialization |
e9100ff7 |
85 | |
8b621a87 |
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'); |
497d874a |
89 | |
31c3800e |
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 | ); |
497d874a |
98 | |
31c3800e |
99 | #===== make sure make_column_dirty interacts reasonably with inflation |
497d874a |
100 | $object = $rs->first; |
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' ); |
c9372242 |
111 | |
112 | done_testing; |