Make freeze/thaw and dclone work as functions on CDBICompat objects.
[dbsrgits/DBIx-Class.git] / t / 84serialize.t
CommitLineData
70350518 1use strict;
2use warnings;
3
4use Test::More;
5use lib qw(t/lib);
6use DBICTest;
e60dc79f 7use Storable qw(dclone freeze thaw);
69ac22ee 8
a47e1233 9my $schema = DBICTest->init_schema();
69ac22ee 10
e60dc79f 11my %stores = (
3a81f59b 12 dclone_method => sub { return $schema->dclone($_[0]) },
13 dclone_func => sub { return dclone($_[0]) },
14 "freeze/thaw_method" => sub {
7244b45f 15 my $ice = $schema->freeze($_[0]);
16 return $schema->thaw($ice);
17 },
3a81f59b 18 "freeze/thaw_func" => sub {
19 thaw(freeze($_[0]));
20 },
e60dc79f 21);
69ac22ee 22
c65da661 23plan tests => (7 * keys %stores);
69ac22ee 24
e60dc79f 25for my $name (keys %stores) {
26 my $store = $stores{$name};
27
28 my $artist = $schema->resultset('Artist')->find(1);
3a81f59b 29
30 # Test that the procedural versions will work if there's a registered
31 # schema as with CDBICompat objects and that the methods work
32 # without.
33 if( $name =~ /func/ ) {
34 $artist->result_source_instance->schema($schema);
35 DBICTest::CD->result_source_instance->schema($schema);
36 }
37 else {
38 $artist->result_source_instance->schema(undef);
39 DBICTest::CD->result_source_instance->schema(undef);
40 }
41
e60dc79f 42 my $copy = eval { $store->($artist) };
43 is_deeply($copy, $artist, "serialize row object works: $name");
44
45 # Test that an object with a related_resultset can be serialized.
46 my @cds = $artist->related_resultset("cds");
3a81f59b 47
e60dc79f 48 ok $artist->{related_resultsets}, 'has key: related_resultsets';
49
50 $copy = eval { $store->($artist) };
51 for my $key (keys %$artist) {
52 next if $key eq 'related_resultsets';
53 next if $key eq '_inflated_column';
54 is_deeply($copy->{$key}, $artist->{$key},
55 qq[serialize with related_resultset "$key"]);
56 }
c65da661 57
7244b45f 58 ok eval { $copy->discard_changes; 1 } or diag $@;
c65da661 59 is($copy->id, $artist->id, "IDs still match ");
e60dc79f 60}