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