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 = ( |
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 | |
e81a6241 |
35 | plan (tests => 8); |
e9100ff7 |
36 | DBICTest::Schema::Serialized->inflate_column( 'serialized', |
37 | { inflate => $selected->{inflater}, |
38 | deflate => $selected->{deflater}, |
39 | }, |
40 | ); |
41 | Class::C3->reinitialize; |
42 | |
8b621a87 |
43 | my $struct_hash = { |
44 | a => 1, |
45 | b => [ |
46 | { c => 2 }, |
47 | ], |
48 | d => 3, |
e9100ff7 |
49 | }; |
50 | |
8b621a87 |
51 | my $struct_array = [ |
52 | 'a', |
53 | { |
54 | b => 1, |
55 | c => 2 |
56 | }, |
57 | 'd', |
58 | ]; |
e9100ff7 |
59 | |
60 | my $rs = $schema->resultset('Serialized'); |
e9100ff7 |
61 | my $inflated; |
62 | |
8b621a87 |
63 | #======= testing hashref serialization |
e81a6241 |
64 | |
8b621a87 |
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) }; |
e81a6241 |
78 | ok(!$@, 'set_inflated_column to a hashref'); |
8b621a87 |
79 | is_deeply($object->serialized, $struct_hash, 'inflated hash matches original'); |
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'); |