File: serialize.t

package info (click to toggle)
libdbix-class-perl 0.082844-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,320 kB
  • sloc: perl: 27,215; sql: 322; sh: 29; makefile: 16
file content (112 lines) | stat: -rw-r--r-- 2,773 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
use strict;
use warnings;

use Test::More;
use lib qw(t/lib);
use DBICTest;

my $schema = DBICTest->init_schema();

my @serializers = (
    { module => 'YAML.pm',
      inflater => sub { YAML::Load (shift) },
      deflater => sub { die "Expecting a reference" unless (ref $_[0]); YAML::Dump (shift) },
    },
    { module => 'Storable.pm',
      inflater => sub { Storable::thaw (shift) },
      deflater => sub { die "Expecting a reference" unless (ref $_[0]); Storable::nfreeze (shift) },
    },
);


my $selected;
foreach my $serializer (@serializers) {
    eval { require $serializer->{module} };
    unless ($@) {
      $selected = $serializer;
      last;
    }
}

plan (skip_all => "No suitable serializer found") unless $selected;

DBICTest::Schema::Serialized->inflate_column( 'serialized',
    { inflate => $selected->{inflater},
      deflate => $selected->{deflater},
    },
);
Class::C3->reinitialize if DBIx::Class::_ENV_::OLD_MRO;

my $struct_hash = {
    a => 1,
    b => [
        { c => 2 },
    ],
    d => 3,
};

my $struct_array = [
    'a',
    {
      b => 1,
      c => 2,
    },
    'd',
];

my $rs = $schema->resultset('Serialized');
my $inflated;

#======= testing hashref serialization

my $object = $rs->create( {
    serialized => '',
} );
ok($object->update( { serialized => $struct_hash } ), 'hashref deflation');
ok($inflated = $object->serialized, 'hashref inflation');
is_deeply($inflated, $struct_hash, 'inflated hash matches original');

$object = $rs->create( {
    serialized => '',
} );
$object->set_inflated_column('serialized', $struct_hash);
is_deeply($object->serialized, $struct_hash, 'inflated hash matches original');

$object = $rs->new({});
$object->serialized ($struct_hash);
$object->insert;
is_deeply (
  $rs->find ({id => $object->id})->serialized,
  $struct_hash,
  'new/insert works',
);

#====== testing arrayref serialization

ok($object->update( { serialized => $struct_array } ), 'arrayref deflation');
ok($inflated = $object->serialized, 'arrayref inflation');
is_deeply($inflated, $struct_array, 'inflated array matches original');

$object = $rs->new({});
$object->serialized ($struct_array);
$object->insert;
is_deeply (
  $rs->find ({id => $object->id})->serialized,
  $struct_array,
  'new/insert works',
);

#===== make sure make_column_dirty interacts reasonably with inflation
$object = $rs->first;
$object->update ({serialized => { x => 'y'}});

$object->serialized->{x} = 'z'; # change state without notifying $object
ok (!$object->get_dirty_columns, 'no dirty columns yet');
is_deeply ($object->serialized, { x => 'z' }, 'object data correct');

$object->make_column_dirty('serialized');
$object->update;

is_deeply ($rs->first->serialized, { x => 'z' }, 'changes made it to the db' );

done_testing;