0819d8c599dad94ff66001be80171d5a797d3c5c
[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     "freeze/thaw_method"   => sub {
14         my $ice = $schema->freeze($_[0]);
15         return $schema->thaw($ice);
16     },
17 );
18
19 plan tests => (7 * keys %stores);
20
21 for my $name (keys %stores) {
22     my $store = $stores{$name};
23
24     my $artist = $schema->resultset('Artist')->find(1);
25     my $copy = eval { $store->($artist) };
26     is_deeply($copy, $artist, "serialize row object works: $name");
27
28     # Test that an object with a related_resultset can be serialized.
29     my @cds = $artist->related_resultset("cds");
30     ok $artist->{related_resultsets}, 'has key: related_resultsets';
31
32     $copy = eval { $store->($artist) };
33     for my $key (keys %$artist) {
34         next if $key eq 'related_resultsets';
35         next if $key eq '_inflated_column';
36         is_deeply($copy->{$key}, $artist->{$key},
37                   qq[serialize with related_resultset "$key"]);
38     }
39   
40     ok eval { $copy->discard_changes; 1 } or diag $@;
41     is($copy->id, $artist->id, "IDs still match ");
42 }