Revert some test changes to bring things back in line with master
[dbsrgits/DBIx-Class.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);
42168332 9use Scalar::Util qw/refaddr/;
13ab0b9f 10use Carp;
11
42168332 12sub ref_ne {
13ab0b9f 13 my ($refa, $refb) = map { refaddr $_ or croak "$_ is not a reference!" } @_[0,1];
42168332 14 cmp_ok (
15 $refa,
16 '!=',
17 $refb,
18 sprintf ('%s (0x%07x != 0x%07x)',
19 $_[2],
20 $refa,
21 $refb,
22 ),
23 );
24}
69ac22ee 25
42168332 26my $schema = DBICTest->init_schema;
69ac22ee 27
e60dc79f 28my %stores = (
3a81f59b 29 dclone_method => sub { return $schema->dclone($_[0]) },
42168332 30 dclone_func => sub {
31 local $DBIx::Class::ResultSourceHandle::thaw_schema = $schema;
32 return dclone($_[0])
7244b45f 33 },
42168332 34 "freeze/thaw_method" => sub {
35 my $ice = $schema->freeze($_[0]);
36 return $schema->thaw($ice);
3a81f59b 37 },
26148d36 38 "nfreeze/thaw_func" => sub {
42168332 39 my $ice = freeze($_[0]);
40 local $DBIx::Class::ResultSourceHandle::thaw_schema = $schema;
41 return thaw($ice);
26148d36 42 },
69ac22ee 43
42168332 44 "freeze/thaw_func (cdbi legacy)" => sub {
45 # this one is special-cased to leak the $schema all over
46 # the same way as cdbi-compat does
47 DBICTest::Artist->result_source_instance->schema($schema);
48 DBICTest::CD->result_source_instance->schema($schema);
49
50 my $fire = thaw(freeze($_[0]));
51
52 # clean up the mess
53 $_->result_source_instance->schema(undef)
54 for map { $schema->class ($_) } $schema->sources;
55
56 return $fire;
57 },
58
42168332 59);
69ac22ee 60
49b3a264 61if ($ENV{DBICTEST_MEMCACHED}) {
62 if (DBIx::Class::Optional::Dependencies->req_ok_for ('test_memcached')) {
63 my $memcached = Cache::Memcached->new(
64 { servers => [ $ENV{DBICTEST_MEMCACHED} ] }
65 );
66
67 my $key = 'tmp_dbic_84serialize_memcached_test';
68
69 $stores{memcached} = sub {
70 $memcached->set( $key, $_[0], 60 );
71 local $DBIx::Class::ResultSourceHandle::thaw_schema = $schema;
72 return $memcached->get($key);
73 };
74 }
75 else {
76 SKIP: {
77 skip 'Memcached tests need ' . DBIx::Class::Optional::Dependencies->req_missing_for ('test_memcached'), 1;
78 }
79 }
80}
81else {
82 SKIP: {
83 skip 'Set $ENV{DBICTEST_MEMCACHED} to run the memcached serialization tests', 1;
84 }
85}
86
87
88
e60dc79f 89for my $name (keys %stores) {
42168332 90
e60dc79f 91 my $store = $stores{$name};
7d7d6975 92 my $copy;
e60dc79f 93
94 my $artist = $schema->resultset('Artist')->find(1);
26148d36 95
7d7d6975 96 lives_ok { $copy = $store->($artist) } "serialize row object lives: $name";
42168332 97 ref_ne($copy, $artist, 'Simple row cloned');
e60dc79f 98 is_deeply($copy, $artist, "serialize row object works: $name");
99
7d7d6975 100 my $cd_rs = $artist->search_related("cds");
101
0b66414b 102 # test that a live result source can be serialized as well
103 is( $cd_rs->count, 3, '3 CDs in database');
104 ok( $cd_rs->next, 'Advance cursor' );
3a81f59b 105
7d7d6975 106 lives_ok {
107 $copy = $store->($cd_rs);
42168332 108
109 ref_ne($copy, $artist, 'Simple row cloned');
110
7d7d6975 111 is_deeply (
112 [ $copy->all ],
113 [ $cd_rs->all ],
114 "serialize resultset works: $name",
115 );
116 } "serialize resultset lives: $name";
117
118 # Test that an object with a related_resultset can be serialized.
e60dc79f 119 ok $artist->{related_resultsets}, 'has key: related_resultsets';
120
7d7d6975 121 lives_ok { $copy = $store->($artist) } "serialize row object with related_resultset lives: $name";
e60dc79f 122 for my $key (keys %$artist) {
123 next if $key eq 'related_resultsets';
124 next if $key eq '_inflated_column';
42168332 125
126 ref_ne($copy->{$key}, $artist->{$key}, "Simple row internals cloned '$key'")
127 if ref $artist->{$key};
128
e60dc79f 129 is_deeply($copy->{$key}, $artist->{$key},
42168332 130 qq[serialize with related_resultset '$key']);
e60dc79f 131 }
7d7d6975 132
0b66414b 133 lives_ok(
134 sub { $copy->discard_changes }, "Discard changes works: $name"
135 ) or diag $@;
c65da661 136 is($copy->id, $artist->id, "IDs still match ");
0b66414b 137
138
139 # Test resultsource with cached rows
140 my $query_count;
141 $cd_rs = $cd_rs->search ({}, { cache => 1 });
142
42168332 143 my $orig_debug = $schema->storage->debug;
0b66414b 144 $schema->storage->debug(1);
145 $schema->storage->debugcb(sub { $query_count++ } );
146
147 # this will hit the database once and prime the cache
148 my @cds = $cd_rs->all;
149
150 lives_ok {
151 $copy = $store->($cd_rs);
42168332 152 ref_ne($copy, $cd_rs, 'Cached resultset cloned');
0b66414b 153 is_deeply (
154 [ $copy->all ],
155 [ $cd_rs->all ],
156 "serialize cached resultset works: $name",
157 );
158
159 is ($copy->count, $cd_rs->count, 'Cached count identical');
160 } "serialize cached resultset lives: $name";
161
162 is ($query_count, 1, 'Only one db query fired');
163
164 $schema->storage->debug($orig_debug);
165 $schema->storage->debugcb(undef);
e60dc79f 166}
42168332 167
4376a157 168# test schema-less detached thaw
169{
170 my $artist = $schema->resultset('Artist')->find(1);
171
172 $artist = dclone $artist;
173
174 is( $artist->name, 'Caterwauler McCrae', 'getting column works' );
175
176 ok( $artist->update, 'Non-dirty update noop' );
177
178 ok( $artist->name( 'Beeeeeeees' ), 'setting works' );
179
180 ok( $artist->is_column_changed( 'name' ), 'Column dirtyness works' );
181 ok( $artist->is_changed, 'object dirtyness works' );
182
183 my $rs = $artist->result_source->resultset;
184 $rs->set_cache([ $artist ]);
185
186 is( $rs->count, 1, 'Synthetic resultset count works' );
187
188 my $exc = qr/Unable to perform storage-dependent operations with a detached result source.+use \$schema->thaw/;
189
190 throws_ok { $artist->update }
191 $exc,
192 'Correct exception on row op'
193 ;
194
195 throws_ok { $artist->discard_changes }
196 $exc,
197 'Correct exception on row op'
198 ;
199
200 throws_ok { $rs->find(1) }
201 $exc,
202 'Correct exception on rs op'
203 ;
204}
205
42168332 206done_testing;