renamed replication storage to replicated (since it's not really doing any replicatio...
[dbsrgits/DBIx-Class.git] / t / 84serialize.t
1 use strict;
2 use warnings;  
3
4 use Test::More;
5 use lib qw(t/lib);
6 use DBICTest;
7 use Storable qw(dclone freeze thaw);
8
9 my $schema = DBICTest->init_schema();
10
11 my %stores = (
12     dclone_method           => sub { return $schema->dclone($_[0]) },
13     dclone_func             => sub { return dclone($_[0]) },
14     "freeze/thaw_method"    => sub {
15         my $ice = $schema->freeze($_[0]);
16         return $schema->thaw($ice);
17     },
18     "freeze/thaw_func"      => sub {
19         thaw(freeze($_[0]));
20     },
21 );
22
23 plan tests => (7 * keys %stores);
24
25 for my $name (keys %stores) {
26     my $store = $stores{$name};
27
28     my $artist = $schema->resultset('Artist')->find(1);
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
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");
47
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     }
57   
58     ok eval { $copy->discard_changes; 1 } or diag $@;
59     is($copy->id, $artist->id, "IDs still match ");
60 }