Institute a central "load this first in testing" package
[dbsrgits/DBIx-Class.git] / t / cdbi / 68-inflate_has_a.t
1 BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2 use DBIx::Class::Optional::Dependencies -skip_all_without => qw( ic_dt cdbicompat );
3
4 use strict;
5 use warnings;
6
7 use Test::More;
8
9 use DBICTest;
10
11 my $schema = DBICTest->init_schema();
12
13 DBICTest::Schema::CD->load_components(qw/CDBICompat::Relationships/);
14
15 DBICTest::Schema::CD->has_a( 'year', 'DateTime',
16       inflate => sub { DateTime->new( year => shift ) },
17       deflate => sub { shift->year }
18 );
19 Class::C3->reinitialize if DBIx::Class::_ENV_::OLD_MRO;
20
21 # inflation test
22 my $cd = $schema->resultset("CD")->find(3);
23
24 is( ref($cd->year), 'DateTime', 'year is a DateTime, ok' );
25
26 is( $cd->year->month, 1, 'inflated month ok' );
27
28 # deflate test
29 my $now = DateTime->now;
30 $cd->year( $now );
31 $cd->update;
32
33 ($cd) = $schema->resultset("CD")->search({ year => $now->year });
34 is( $cd->year->year, $now->year, 'deflate ok' );
35
36 # re-test using alternate deflate syntax
37 $schema->class("CD")->has_a( 'year', 'DateTime',
38       inflate => sub { DateTime->new( year => shift ) },
39       deflate => 'year'
40 );
41
42 # inflation test
43 $cd = $schema->resultset("CD")->find(3);
44
45 is( ref($cd->year), 'DateTime', 'year is a DateTime, ok' );
46
47 is( $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) = $schema->resultset("CD")->search({ year => $now->year });
55 is( $cd->year->year, $now->year, 'deflate ok' );
56
57 done_testing;