Commit | Line | Data |
70350518 |
1 | use strict; |
f4086911 |
2 | use warnings; |
70350518 |
3 | use Test::More; |
70350518 |
4 | |
f4086911 |
5 | BEGIN { |
68de9438 |
6 | eval "use DBIx::Class::CDBICompat; use DateTime 0.55; use Clone;"; |
7 | plan skip_all => "Clone, DateTime 0.55, Class::Trigger and DBIx::ContextualFetch required" |
f4086911 |
8 | if $@; |
f4086911 |
9 | } |
ae0419e2 |
10 | |
0567538f |
11 | plan tests => 6; |
12 | |
f4086911 |
13 | use lib qw(t/lib); |
14 | use DBICTest; |
15 | |
16 | my $schema = DBICTest->init_schema(); |
17 | |
658b87f6 |
18 | DBICTest::Schema::CD->load_components(qw/CDBICompat::Relationships/); |
0567538f |
19 | |
2d679367 |
20 | DBICTest::Schema::CD->has_a( 'year', 'DateTime', |
0567538f |
21 | inflate => sub { DateTime->new( year => shift ) }, |
22 | deflate => sub { shift->year } |
23 | ); |
2d679367 |
24 | Class::C3->reinitialize; |
0567538f |
25 | |
26 | # inflation test |
f9db5527 |
27 | my $cd = $schema->resultset("CD")->find(3); |
0567538f |
28 | |
29 | is( ref($cd->year), 'DateTime', 'year is a DateTime, ok' ); |
30 | |
31 | is( $cd->year->month, 1, 'inflated month ok' ); |
32 | |
33 | # deflate test |
34 | my $now = DateTime->now; |
35 | $cd->year( $now ); |
36 | $cd->update; |
37 | |
f9db5527 |
38 | ($cd) = $schema->resultset("CD")->search( year => $now->year ); |
0567538f |
39 | is( $cd->year->year, $now->year, 'deflate ok' ); |
40 | |
41 | # re-test using alternate deflate syntax |
1edaf6fe |
42 | $schema->class("CD")->has_a( 'year', 'DateTime', |
0567538f |
43 | inflate => sub { DateTime->new( year => shift ) }, |
44 | deflate => 'year' |
45 | ); |
46 | |
47 | # inflation test |
f9db5527 |
48 | $cd = $schema->resultset("CD")->find(3); |
0567538f |
49 | |
50 | is( ref($cd->year), 'DateTime', 'year is a DateTime, ok' ); |
51 | |
52 | is( $cd->year->month, 1, 'inflated month ok' ); |
53 | |
54 | # deflate test |
55 | $now = DateTime->now; |
56 | $cd->year( $now ); |
57 | $cd->update; |
58 | |
f9db5527 |
59 | ($cd) = $schema->resultset("CD")->search( year => $now->year ); |
0567538f |
60 | is( $cd->year->year, $now->year, 'deflate ok' ); |
61 | |