Commit | Line | Data |
0567538f |
1 | sub run_tests { |
1edaf6fe |
2 | my $schema = shift; |
0567538f |
3 | |
4 | eval { require DateTime }; |
5 | plan skip_all => "Need DateTime for inflation tests" if $@; |
6 | |
7 | plan tests => 6; |
8 | |
2d679367 |
9 | DBICTest::Schema::CD->load_components(qw/CDBICompat::HasA/); |
0567538f |
10 | |
2d679367 |
11 | DBICTest::Schema::CD->has_a( 'year', 'DateTime', |
0567538f |
12 | inflate => sub { DateTime->new( year => shift ) }, |
13 | deflate => sub { shift->year } |
14 | ); |
2d679367 |
15 | Class::C3->reinitialize; |
0567538f |
16 | |
17 | # inflation test |
f9db5527 |
18 | my $cd = $schema->resultset("CD")->find(3); |
0567538f |
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 | |
f9db5527 |
29 | ($cd) = $schema->resultset("CD")->search( year => $now->year ); |
0567538f |
30 | is( $cd->year->year, $now->year, 'deflate ok' ); |
31 | |
32 | # re-test using alternate deflate syntax |
1edaf6fe |
33 | $schema->class("CD")->has_a( 'year', 'DateTime', |
0567538f |
34 | inflate => sub { DateTime->new( year => shift ) }, |
35 | deflate => 'year' |
36 | ); |
37 | |
38 | # inflation test |
f9db5527 |
39 | $cd = $schema->resultset("CD")->find(3); |
0567538f |
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 | |
f9db5527 |
50 | ($cd) = $schema->resultset("CD")->search( year => $now->year ); |
0567538f |
51 | is( $cd->year->year, $now->year, 'deflate ok' ); |
52 | |
53 | } |
54 | |
55 | 1; |