Some stylistic test changes in preparation for next commits
[dbsrgits/DBIx-Class.git] / t / inflate / serialize.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use lib qw(t/lib);
6 use DBICTest;
7
8 my $schema = DBICTest->init_schema();
9
10 my @serializers = (
11     { module => 'YAML.pm',
12       inflater => sub { YAML::Load (shift) },
13       deflater => sub { die "Expecting a reference" unless (ref $_[0]); YAML::Dump (shift) },
14     },
15     { module => 'Storable.pm',
16       inflater => sub { Storable::thaw (shift) },
17       deflater => sub { die "Expecting a reference" unless (ref $_[0]); Storable::nfreeze (shift) },
18     },
19 );
20
21
22 my $selected;
23 foreach my $serializer (@serializers) {
24     eval { require $serializer->{module} };
25     unless ($@) {
26       $selected = $serializer;
27       last;
28     }
29 }
30
31 DBICTest::Schema::Serialized->inflate_column( 'serialized',
32     { inflate => $selected->{inflater},
33       deflate => $selected->{deflater},
34     },
35 );
36 Class::C3->reinitialize if DBIx::Class::_ENV_::OLD_MRO;
37
38 my $struct_hash = {
39     a => 1,
40     b => [
41         { c => 2 },
42     ],
43     d => 3,
44 };
45
46 my $struct_array = [
47     'a',
48     {
49       b => 1,
50       c => 2,
51     },
52     'd',
53 ];
54
55 my $rs = $schema->resultset('Serialized');
56 my $inflated;
57
58 #======= testing hashref serialization
59
60 my $object = $rs->create( {
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
67 $object = $rs->create( {
68     serialized => '',
69 } );
70 $object->set_inflated_column('serialized', $struct_hash);
71 is_deeply($object->serialized, $struct_hash, 'inflated hash matches original');
72
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 );
81
82 #====== testing arrayref serialization
83
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');
87
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 );
96
97 #===== make sure make_column_dirty interacts reasonably with inflation
98 $object = $rs->search({}, { rows => 1 })->next;
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' );
109
110 done_testing;