Some stylistic test changes in preparation for next commits
[dbsrgits/DBIx-Class.git] / t / inflate / serialize.t
CommitLineData
70350518 1use strict;
68de9438 2use warnings;
70350518 3
4use Test::More;
5use lib qw(t/lib);
6use DBICTest;
7
a47e1233 8my $schema = DBICTest->init_schema();
e9100ff7 9
e9100ff7 10my @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
22my $selected;
23foreach my $serializer (@serializers) {
24 eval { require $serializer->{module} };
25 unless ($@) {
c9372242 26 $selected = $serializer;
27 last;
e9100ff7 28 }
29}
30
e9100ff7 31DBICTest::Schema::Serialized->inflate_column( 'serialized',
32 { inflate => $selected->{inflater},
33 deflate => $selected->{deflater},
34 },
35);
cc8ffd7b 36Class::C3->reinitialize if DBIx::Class::_ENV_::OLD_MRO;
e9100ff7 37
8b621a87 38my $struct_hash = {
39 a => 1,
c9372242 40 b => [
8b621a87 41 { c => 2 },
42 ],
43 d => 3,
e9100ff7 44};
45
8b621a87 46my $struct_array = [
c9372242 47 'a',
48 {
49 b => 1,
50 c => 2,
8b621a87 51 },
52 'd',
53];
e9100ff7 54
55my $rs = $schema->resultset('Serialized');
e9100ff7 56my $inflated;
57
8b621a87 58#======= testing hashref serialization
e81a6241 59
8273e845 60my $object = $rs->create( {
8b621a87 61 serialized => '',
62} );
63ok($object->update( { serialized => $struct_hash } ), 'hashref deflation');
64ok($inflated = $object->serialized, 'hashref inflation');
65is_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 71is_deeply($object->serialized, $struct_hash, 'inflated hash matches original');
72
31c3800e 73$object = $rs->new({});
74$object->serialized ($struct_hash);
75$object->insert;
76is_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 84ok($object->update( { serialized => $struct_array } ), 'arrayref deflation');
85ok($inflated = $object->serialized, 'arrayref inflation');
86is_deeply($inflated, $struct_array, 'inflated array matches original');
497d874a 87
31c3800e 88$object = $rs->new({});
89$object->serialized ($struct_array);
90$object->insert;
91is_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
b74b15b0 98$object = $rs->search({}, { rows => 1 })->next;
497d874a 99$object->update ({serialized => { x => 'y'}});
100
101$object->serialized->{x} = 'z'; # change state without notifying $object
102ok (!$object->get_dirty_columns, 'no dirty columns yet');
103is_deeply ($object->serialized, { x => 'z' }, 'object data correct');
104
105$object->make_column_dirty('serialized');
106$object->update;
107
108is_deeply ($rs->first->serialized, { x => 'z' }, 'changes made it to the db' );
c9372242 109
110done_testing;