Add Storable freeze/thaw hooks to ResultSet to detach active cursors
[dbsrgits/DBIx-Class-Historic.git] / t / 84serialize.t
CommitLineData
70350518 1use strict;
26148d36 2use warnings;
70350518 3
4use Test::More;
7d7d6975 5use Test::Exception;
70350518 6use lib qw(t/lib);
7use DBICTest;
26148d36 8use Storable qw(dclone freeze nfreeze thaw);
69ac22ee 9
a47e1233 10my $schema = DBICTest->init_schema();
0b66414b 11my $orig_debug = $schema->storage->debug;
69ac22ee 12
e60dc79f 13my %stores = (
3a81f59b 14 dclone_method => sub { return $schema->dclone($_[0]) },
15 dclone_func => sub { return dclone($_[0]) },
16 "freeze/thaw_method" => sub {
7244b45f 17 my $ice = $schema->freeze($_[0]);
18 return $schema->thaw($ice);
19 },
3a81f59b 20 "freeze/thaw_func" => sub {
21 thaw(freeze($_[0]));
22 },
26148d36 23 "nfreeze/thaw_func" => sub {
24 thaw(nfreeze($_[0]));
25 },
e60dc79f 26);
69ac22ee 27
0b66414b 28plan tests => (17 * keys %stores);
69ac22ee 29
e60dc79f 30for my $name (keys %stores) {
31 my $store = $stores{$name};
7d7d6975 32 my $copy;
e60dc79f 33
34 my $artist = $schema->resultset('Artist')->find(1);
26148d36 35
3a81f59b 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
7d7d6975 48 lives_ok { $copy = $store->($artist) } "serialize row object lives: $name";
e60dc79f 49 is_deeply($copy, $artist, "serialize row object works: $name");
50
7d7d6975 51 my $cd_rs = $artist->search_related("cds");
52
0b66414b 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' );
3a81f59b 56
7d7d6975 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.
e60dc79f 67 ok $artist->{related_resultsets}, 'has key: related_resultsets';
68
7d7d6975 69 lives_ok { $copy = $store->($artist) } "serialize row object with related_resultset lives: $name";
e60dc79f 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 }
7d7d6975 76
0b66414b 77 lives_ok(
78 sub { $copy->discard_changes }, "Discard changes works: $name"
79 ) or diag $@;
c65da661 80 is($copy->id, $artist->id, "IDs still match ");
0b66414b 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);
e60dc79f 108}