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