Whitespace
[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} );
74eval { $object->set_inflated_column('serialized', $struct_hash) };
e81a6241 75ok(!$@, 'set_inflated_column to a hashref');
8b621a87 76is_deeply($object->serialized, $struct_hash, 'inflated hash matches original');
77
e81a6241 78
8b621a87 79#====== testing arrayref serialization
e9100ff7 80
8b621a87 81ok($object->update( { serialized => $struct_array } ), 'arrayref deflation');
82ok($inflated = $object->serialized, 'arrayref inflation');
83is_deeply($inflated, $struct_array, 'inflated array matches original');
497d874a 84
85
86#===== make sure make_column_dirty ineracts reasonably with inflation
87$object = $rs->first;
88$object->update ({serialized => { x => 'y'}});
89
90$object->serialized->{x} = 'z'; # change state without notifying $object
91ok (!$object->get_dirty_columns, 'no dirty columns yet');
92is_deeply ($object->serialized, { x => 'z' }, 'object data correct');
93
94$object->make_column_dirty('serialized');
95$object->update;
96
97is_deeply ($rs->first->serialized, { x => 'z' }, 'changes made it to the db' );
c9372242 98
99done_testing;