Introduce GOVERNANCE document and empty RESOLUTIONS file.
[dbsrgits/DBIx-Class.git] / t / inflate / serialize.t
CommitLineData
c0329273 1BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2
70350518 3use strict;
68de9438 4use warnings;
70350518 5
6use Test::More;
c0329273 7
70350518 8use DBICTest;
9
a47e1233 10my $schema = DBICTest->init_schema();
e9100ff7 11
e9100ff7 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
e9100ff7 33DBICTest::Schema::Serialized->inflate_column( 'serialized',
34 { inflate => $selected->{inflater},
35 deflate => $selected->{deflater},
36 },
37);
cc8ffd7b 38Class::C3->reinitialize if DBIx::Class::_ENV_::OLD_MRO;
e9100ff7 39
8b621a87 40my $struct_hash = {
41 a => 1,
c9372242 42 b => [
8b621a87 43 { c => 2 },
44 ],
45 d => 3,
e9100ff7 46};
47
8b621a87 48my $struct_array = [
c9372242 49 'a',
50 {
51 b => 1,
52 c => 2,
8b621a87 53 },
54 'd',
55];
e9100ff7 56
57my $rs = $schema->resultset('Serialized');
e9100ff7 58my $inflated;
59
8b621a87 60#======= testing hashref serialization
e81a6241 61
8273e845 62my $object = $rs->create( {
8b621a87 63 serialized => '',
64} );
65ok($object->update( { serialized => $struct_hash } ), 'hashref deflation');
66ok($inflated = $object->serialized, 'hashref inflation');
67is_deeply($inflated, $struct_hash, 'inflated hash matches original');
68
8273e845 69$object = $rs->create( {
8b621a87 70 serialized => '',
71} );
31c3800e 72$object->set_inflated_column('serialized', $struct_hash);
8b621a87 73is_deeply($object->serialized, $struct_hash, 'inflated hash matches original');
74
31c3800e 75$object = $rs->new({});
76$object->serialized ($struct_hash);
77$object->insert;
78is_deeply (
79 $rs->find ({id => $object->id})->serialized,
80 $struct_hash,
81 'new/insert works',
82);
e81a6241 83
8b621a87 84#====== testing arrayref serialization
e9100ff7 85
8b621a87 86ok($object->update( { serialized => $struct_array } ), 'arrayref deflation');
87ok($inflated = $object->serialized, 'arrayref inflation');
88is_deeply($inflated, $struct_array, 'inflated array matches original');
497d874a 89
31c3800e 90$object = $rs->new({});
91$object->serialized ($struct_array);
92$object->insert;
93is_deeply (
94 $rs->find ({id => $object->id})->serialized,
95 $struct_array,
96 'new/insert works',
97);
497d874a 98
31c3800e 99#===== make sure make_column_dirty interacts reasonably with inflation
b74b15b0 100$object = $rs->search({}, { rows => 1 })->next;
497d874a 101$object->update ({serialized => { x => 'y'}});
102
103$object->serialized->{x} = 'z'; # change state without notifying $object
104ok (!$object->get_dirty_columns, 'no dirty columns yet');
105is_deeply ($object->serialized, { x => 'z' }, 'object data correct');
106
107$object->make_column_dirty('serialized');
108$object->update;
109
110is_deeply ($rs->first->serialized, { x => 'z' }, 'changes made it to the db' );
c9372242 111
112done_testing;