Renamed DBIx::Class::PK's retrieve() as find()
[dbsrgits/DBIx-Class.git] / t / 08inflate_has_a.t
CommitLineData
4a07648a 1use Test::More;
8bc482e9 2
3eval { require DateTime };
4plan skip_all => "Need DateTime for inflation tests" if $@;
4a07648a 5
759cfa82 6plan tests => 7;
4a07648a 7
8use lib qw(t/lib);
9
10use_ok('DBICTest');
11
12use DBIx::Class::CDBICompat::HasA;
13
14unshift(@DBICTest::ISA, 'DBIx::Class::CDBICompat::HasA');
15
16DBICTest::CD->has_a( 'year', 'DateTime',
17 inflate => sub { DateTime->new( year => shift ) },
18 deflate => sub { shift->year }
19);
20
21# inflation test
656796f2 22my $cd = DBICTest::CD->find(3);
4a07648a 23
24is( ref($cd->year), 'DateTime', 'year is a DateTime, ok' );
25
26is( $cd->year->month, 1, 'inflated month ok' );
27
28# deflate test
29my $now = DateTime->now;
30$cd->year( $now );
31$cd->update;
32
33($cd) = DBICTest::CD->search( year => $now->year );
34is( $cd->year->year, $now->year, 'deflate ok' );
759cfa82 35
36# re-test using alternate deflate syntax
37DBICTest::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
45is( ref($cd->year), 'DateTime', 'year is a DateTime, ok' );
46
47is( $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 );
55is( $cd->year->year, $now->year, 'deflate ok' );
56