Fix an obscure regression when inserting an object with a serialize-deflating column set
[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 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
35 DBICTest::Schema::Serialized->inflate_column( 'serialized',
36     { inflate => $selected->{inflater},
37       deflate => $selected->{deflater},
38     },
39 );
40 Class::C3->reinitialize;
41
42 my $struct_hash = {
43     a => 1,
44     b => [
45         { c => 2 },
46     ],
47     d => 3,
48 };
49
50 my $struct_array = [
51     'a',
52     {
53       b => 1,
54       c => 2,
55     },
56     'd',
57 ];
58
59 my $rs = $schema->resultset('Serialized');
60 my $inflated;
61
62 #======= testing hashref serialization
63
64 my $object = $rs->create( { 
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( { 
72     serialized => '',
73 } );
74 $object->set_inflated_column('serialized', $struct_hash);
75 is_deeply($object->serialized, $struct_hash, 'inflated hash matches original');
76
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 );
85
86 #====== testing arrayref serialization
87
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');
91
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 );
100
101 #===== make sure make_column_dirty interacts reasonably with inflation
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' );
113
114 done_testing;