Commit | Line | Data |
70350518 |
1 | use strict; |
68de9438 |
2 | use warnings; |
70350518 |
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 | |
e9100ff7 |
31 | DBICTest::Schema::Serialized->inflate_column( 'serialized', |
32 | { inflate => $selected->{inflater}, |
33 | deflate => $selected->{deflater}, |
34 | }, |
35 | ); |
cc8ffd7b |
36 | Class::C3->reinitialize if DBIx::Class::_ENV_::OLD_MRO; |
e9100ff7 |
37 | |
8b621a87 |
38 | my $struct_hash = { |
39 | a => 1, |
c9372242 |
40 | b => [ |
8b621a87 |
41 | { c => 2 }, |
42 | ], |
43 | d => 3, |
e9100ff7 |
44 | }; |
45 | |
8b621a87 |
46 | my $struct_array = [ |
c9372242 |
47 | 'a', |
48 | { |
49 | b => 1, |
50 | c => 2, |
8b621a87 |
51 | }, |
52 | 'd', |
53 | ]; |
e9100ff7 |
54 | |
55 | my $rs = $schema->resultset('Serialized'); |
e9100ff7 |
56 | my $inflated; |
57 | |
8b621a87 |
58 | #======= testing hashref serialization |
e81a6241 |
59 | |
8273e845 |
60 | my $object = $rs->create( { |
8b621a87 |
61 | serialized => '', |
62 | } ); |
63 | ok($object->update( { serialized => $struct_hash } ), 'hashref deflation'); |
64 | ok($inflated = $object->serialized, 'hashref inflation'); |
65 | is_deeply($inflated, $struct_hash, 'inflated hash matches original'); |
66 | |
8273e845 |
67 | $object = $rs->create( { |
8b621a87 |
68 | serialized => '', |
69 | } ); |
31c3800e |
70 | $object->set_inflated_column('serialized', $struct_hash); |
8b621a87 |
71 | is_deeply($object->serialized, $struct_hash, 'inflated hash matches original'); |
72 | |
31c3800e |
73 | $object = $rs->new({}); |
74 | $object->serialized ($struct_hash); |
75 | $object->insert; |
76 | is_deeply ( |
77 | $rs->find ({id => $object->id})->serialized, |
78 | $struct_hash, |
79 | 'new/insert works', |
80 | ); |
e81a6241 |
81 | |
8b621a87 |
82 | #====== testing arrayref serialization |
e9100ff7 |
83 | |
8b621a87 |
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'); |
497d874a |
87 | |
31c3800e |
88 | $object = $rs->new({}); |
89 | $object->serialized ($struct_array); |
90 | $object->insert; |
91 | is_deeply ( |
92 | $rs->find ({id => $object->id})->serialized, |
93 | $struct_array, |
94 | 'new/insert works', |
95 | ); |
497d874a |
96 | |
31c3800e |
97 | #===== make sure make_column_dirty interacts reasonably with inflation |
497d874a |
98 | $object = $rs->first; |
99 | $object->update ({serialized => { x => 'y'}}); |
100 | |
101 | $object->serialized->{x} = 'z'; # change state without notifying $object |
102 | ok (!$object->get_dirty_columns, 'no dirty columns yet'); |
103 | is_deeply ($object->serialized, { x => 'z' }, 'object data correct'); |
104 | |
105 | $object->make_column_dirty('serialized'); |
106 | $object->update; |
107 | |
108 | is_deeply ($rs->first->serialized, { x => 'z' }, 'changes made it to the db' ); |
c9372242 |
109 | |
110 | done_testing; |