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