Commit | Line | Data |
4a07648a |
1 | use Test::More; |
8bc482e9 |
2 | |
3 | eval { require DateTime }; |
4 | plan skip_all => "Need DateTime for inflation tests" if $@; |
4a07648a |
5 | |
759cfa82 |
6 | plan tests => 7; |
4a07648a |
7 | |
8 | use lib qw(t/lib); |
9 | |
10 | use_ok('DBICTest'); |
11 | |
12 | use DBIx::Class::CDBICompat::HasA; |
13 | |
14 | unshift(@DBICTest::ISA, 'DBIx::Class::CDBICompat::HasA'); |
15 | |
16 | DBICTest::CD->has_a( 'year', 'DateTime', |
17 | inflate => sub { DateTime->new( year => shift ) }, |
18 | deflate => sub { shift->year } |
19 | ); |
20 | |
21 | # inflation test |
656796f2 |
22 | my $cd = DBICTest::CD->find(3); |
4a07648a |
23 | |
24 | is( ref($cd->year), 'DateTime', 'year is a DateTime, ok' ); |
25 | |
26 | is( $cd->year->month, 1, 'inflated month ok' ); |
27 | |
28 | # deflate test |
29 | my $now = DateTime->now; |
30 | $cd->year( $now ); |
31 | $cd->update; |
32 | |
33 | ($cd) = DBICTest::CD->search( year => $now->year ); |
34 | is( $cd->year->year, $now->year, 'deflate ok' ); |
759cfa82 |
35 | |
36 | # re-test using alternate deflate syntax |
37 | DBICTest::CD->has_a( 'year', 'DateTime', |
38 | inflate => sub { DateTime->new( year => shift ) }, |
39 | deflate => 'year' |
40 | ); |
41 | |
42 | # inflation test |
656796f2 |
43 | $cd = DBICTest::CD->find(3); |
759cfa82 |
44 | |
45 | is( ref($cd->year), 'DateTime', 'year is a DateTime, ok' ); |
46 | |
47 | is( $cd->year->month, 1, 'inflated month ok' ); |
48 | |
49 | # deflate test |
50 | $now = DateTime->now; |
51 | $cd->year( $now ); |
52 | $cd->update; |
53 | |
54 | ($cd) = DBICTest::CD->search( year => $now->year ); |
55 | is( $cd->year->year, $now->year, 'deflate ok' ); |
56 | |