Merge 'file_column' into 'trunk'
[dbsrgits/DBIx-Class.git] / t / 68inflate_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 = (
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
24my $selected;
25foreach my $serializer (@serializers) {
26 eval { require $serializer->{module} };
27 unless ($@) {
28 $selected = $serializer;
29 last;
30 }
31}
32
33plan (skip_all => "No suitable serializer found") unless $selected;
34
e81a6241 35plan (tests => 8);
e9100ff7 36DBICTest::Schema::Serialized->inflate_column( 'serialized',
37 { inflate => $selected->{inflater},
38 deflate => $selected->{deflater},
39 },
40);
41Class::C3->reinitialize;
42
8b621a87 43my $struct_hash = {
44 a => 1,
45 b => [
46 { c => 2 },
47 ],
48 d => 3,
e9100ff7 49};
50
8b621a87 51my $struct_array = [
52 'a',
53 {
54 b => 1,
55 c => 2
56 },
57 'd',
58];
e9100ff7 59
60my $rs = $schema->resultset('Serialized');
e9100ff7 61my $inflated;
62
8b621a87 63#======= testing hashref serialization
e81a6241 64
8b621a87 65my $object = $rs->create( {
66 id => 1,
67 serialized => '',
68} );
69ok($object->update( { serialized => $struct_hash } ), 'hashref deflation');
70ok($inflated = $object->serialized, 'hashref inflation');
71is_deeply($inflated, $struct_hash, 'inflated hash matches original');
72
73$object = $rs->create( {
74 id => 2,
75 serialized => '',
76} );
77eval { $object->set_inflated_column('serialized', $struct_hash) };
e81a6241 78ok(!$@, 'set_inflated_column to a hashref');
8b621a87 79is_deeply($object->serialized, $struct_hash, 'inflated hash matches original');
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');