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