4738a96cea04e619c75e3b3435be98f97316d3cc
[dbsrgits/DBIx-Class.git] / t / 84serialize.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use Test::Exception;
6 use lib qw(t/lib);
7 use DBICTest;
8 use Storable qw(dclone freeze nfreeze thaw);
9
10 my $schema = DBICTest->init_schema();
11 my $orig_debug = $schema->storage->debug;
12
13 my %stores = (
14     dclone_method           => sub { return $schema->dclone($_[0]) },
15     dclone_func             => sub { return dclone($_[0]) },
16     "freeze/thaw_method"    => sub {
17         my $ice = $schema->freeze($_[0]);
18         return $schema->thaw($ice);
19     },
20     "freeze/thaw_func"      => sub {
21         thaw(freeze($_[0]));
22     },
23     "nfreeze/thaw_func"      => sub {
24         thaw(nfreeze($_[0]));
25     },
26 );
27
28 plan tests => (17 * keys %stores);
29
30 for my $name (keys %stores) {
31     my $store = $stores{$name};
32     my $copy;
33
34     my $artist = $schema->resultset('Artist')->find(1);
35
36     # Test that the procedural versions will work if there's a registered
37     # schema as with CDBICompat objects and that the methods work
38     # without.
39     if( $name =~ /func/ ) {
40         $artist->result_source_instance->schema($schema);
41         DBICTest::CD->result_source_instance->schema($schema);
42     }
43     else {
44         $artist->result_source_instance->schema(undef);
45         DBICTest::CD->result_source_instance->schema(undef);
46     }
47
48     lives_ok { $copy = $store->($artist) } "serialize row object lives: $name";
49     is_deeply($copy, $artist, "serialize row object works: $name");
50
51     my $cd_rs = $artist->search_related("cds");
52
53     # test that a live result source can be serialized as well
54     is( $cd_rs->count, 3, '3 CDs in database');
55     ok( $cd_rs->next, 'Advance cursor' );
56
57     lives_ok {
58       $copy = $store->($cd_rs);
59       is_deeply (
60         [ $copy->all ],
61         [ $cd_rs->all ],
62         "serialize resultset works: $name",
63       );
64     } "serialize resultset lives: $name";
65
66     # Test that an object with a related_resultset can be serialized.
67     ok $artist->{related_resultsets}, 'has key: related_resultsets';
68
69     lives_ok { $copy = $store->($artist) } "serialize row object with related_resultset lives: $name";
70     for my $key (keys %$artist) {
71         next if $key eq 'related_resultsets';
72         next if $key eq '_inflated_column';
73         is_deeply($copy->{$key}, $artist->{$key},
74                   qq[serialize with related_resultset "$key"]);
75     }
76
77     lives_ok(
78       sub { $copy->discard_changes }, "Discard changes works: $name"
79     ) or diag $@;
80     is($copy->id, $artist->id, "IDs still match ");
81
82
83     # Test resultsource with cached rows
84     my $query_count;
85     $cd_rs = $cd_rs->search ({}, { cache => 1 });
86
87     $schema->storage->debug(1);
88     $schema->storage->debugcb(sub { $query_count++ } );
89
90     # this will hit the database once and prime the cache
91     my @cds = $cd_rs->all;
92
93     lives_ok {
94       $copy = $store->($cd_rs);
95       is_deeply (
96         [ $copy->all ],
97         [ $cd_rs->all ],
98         "serialize cached resultset works: $name",
99       );
100
101       is ($copy->count, $cd_rs->count, 'Cached count identical');
102     } "serialize cached resultset lives: $name";
103
104     is ($query_count, 1, 'Only one db query fired');
105
106     $schema->storage->debug($orig_debug);
107     $schema->storage->debugcb(undef);
108 }