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