Fix an obscure regression when inserting an object with a serialize-deflating column set
[dbsrgits/DBIx-Class.git] / t / inflate / serialize.t
CommitLineData
70350518 1use strict;
2use warnings;
3
4use Test::More;
5use lib qw(t/lib);
6use DBICTest;
7
a47e1233 8my $schema = DBICTest->init_schema();
e9100ff7 9
10use Data::Dumper;
11
12my @serializers = (
c9372242 13 { module => 'YAML.pm',
14 inflater => sub { YAML::Load (shift) },
15 deflater => sub { die "Expecting a reference" unless (ref $_[0]); YAML::Dump (shift) },
e9100ff7 16 },
c9372242 17 { module => 'Storable.pm',
18 inflater => sub { Storable::thaw (shift) },
19 deflater => sub { die "Expecting a reference" unless (ref $_[0]); Storable::nfreeze (shift) },
e9100ff7 20 },
21);
22
23
24my $selected;
25foreach my $serializer (@serializers) {
26 eval { require $serializer->{module} };
27 unless ($@) {
c9372242 28 $selected = $serializer;
29 last;
e9100ff7 30 }
31}
32
33plan (skip_all => "No suitable serializer found") unless $selected;
34
e9100ff7 35DBICTest::Schema::Serialized->inflate_column( 'serialized',
36 { inflate => $selected->{inflater},
37 deflate => $selected->{deflater},
38 },
39);
40Class::C3->reinitialize;
41
8b621a87 42my $struct_hash = {
43 a => 1,
c9372242 44 b => [
8b621a87 45 { c => 2 },
46 ],
47 d => 3,
e9100ff7 48};
49
8b621a87 50my $struct_array = [
c9372242 51 'a',
52 {
53 b => 1,
54 c => 2,
8b621a87 55 },
56 'd',
57];
e9100ff7 58
59my $rs = $schema->resultset('Serialized');
e9100ff7 60my $inflated;
61
8b621a87 62#======= testing hashref serialization
e81a6241 63
8b621a87 64my $object = $rs->create( {
8b621a87 65 serialized => '',
66} );
67ok($object->update( { serialized => $struct_hash } ), 'hashref deflation');
68ok($inflated = $object->serialized, 'hashref inflation');
69is_deeply($inflated, $struct_hash, 'inflated hash matches original');
70
71$object = $rs->create( {
8b621a87 72 serialized => '',
73} );
31c3800e 74$object->set_inflated_column('serialized', $struct_hash);
8b621a87 75is_deeply($object->serialized, $struct_hash, 'inflated hash matches original');
76
31c3800e 77$object = $rs->new({});
78$object->serialized ($struct_hash);
79$object->insert;
80is_deeply (
81 $rs->find ({id => $object->id})->serialized,
82 $struct_hash,
83 'new/insert works',
84);
e81a6241 85
8b621a87 86#====== testing arrayref serialization
e9100ff7 87
8b621a87 88ok($object->update( { serialized => $struct_array } ), 'arrayref deflation');
89ok($inflated = $object->serialized, 'arrayref inflation');
90is_deeply($inflated, $struct_array, 'inflated array matches original');
497d874a 91
31c3800e 92$object = $rs->new({});
93$object->serialized ($struct_array);
94$object->insert;
95is_deeply (
96 $rs->find ({id => $object->id})->serialized,
97 $struct_array,
98 'new/insert works',
99);
497d874a 100
31c3800e 101#===== make sure make_column_dirty interacts reasonably with inflation
497d874a 102$object = $rs->first;
103$object->update ({serialized => { x => 'y'}});
104
105$object->serialized->{x} = 'z'; # change state without notifying $object
106ok (!$object->get_dirty_columns, 'no dirty columns yet');
107is_deeply ($object->serialized, { x => 'z' }, 'object data correct');
108
109$object->make_column_dirty('serialized');
110$object->update;
111
112is_deeply ($rs->first->serialized, { x => 'z' }, 'changes made it to the db' );
c9372242 113
114done_testing;