More test hackage, some cleanup in ResultSet
[dbsrgits/DBIx-Class.git] / t / run / 08inflate_has_a.tl
1 sub run_tests {
2 my $schema = shift;
3
4 eval { require DateTime };
5 plan skip_all => "Need DateTime for inflation tests" if $@;
6
7 plan tests => 6;
8
9 DBICTest::Schema::CD->load_components(qw/CDBICompat::HasA/);
10
11 DBICTest::Schema::CD->has_a( 'year', 'DateTime',
12       inflate => sub { DateTime->new( year => shift ) },
13       deflate => sub { shift->year }
14 );
15 Class::C3->reinitialize;
16
17 # inflation test
18 my $cd = $schema->resultset("CD")->find(3);
19
20 is( ref($cd->year), 'DateTime', 'year is a DateTime, ok' );
21
22 is( $cd->year->month, 1, 'inflated month ok' );
23
24 # deflate test
25 my $now = DateTime->now;
26 $cd->year( $now );
27 $cd->update;
28
29 ($cd) = $schema->resultset("CD")->search( year => $now->year );
30 is( $cd->year->year, $now->year, 'deflate ok' );
31
32 # re-test using alternate deflate syntax
33 $schema->class("CD")->has_a( 'year', 'DateTime',
34       inflate => sub { DateTime->new( year => shift ) },
35       deflate => 'year'
36 );
37
38 # inflation test
39 $cd = $schema->resultset("CD")->find(3);
40
41 is( ref($cd->year), 'DateTime', 'year is a DateTime, ok' );
42
43 is( $cd->year->month, 1, 'inflated month ok' );
44
45 # deflate test
46 $now = DateTime->now;
47 $cd->year( $now );
48 $cd->update;
49
50 ($cd) = $schema->resultset("CD")->search( year => $now->year );
51 is( $cd->year->year, $now->year, 'deflate ok' );
52
53 }
54
55 1;