Switch CDBICompat and its tests to OptDeps
[dbsrgits/DBIx-Class.git] / t / cdbi / 68-inflate_has_a.t
1 use DBIx::Class::Optional::Dependencies -skip_all_without => qw( icdt cdbicompat );
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7 use lib 't/lib';
8 use DBICTest;
9
10 my $schema = DBICTest->init_schema();
11
12 DBICTest::Schema::CD->load_components(qw/CDBICompat::Relationships/);
13
14 DBICTest::Schema::CD->has_a( 'year', 'DateTime',
15       inflate => sub { DateTime->new( year => shift ) },
16       deflate => sub { shift->year }
17 );
18 Class::C3->reinitialize if DBIx::Class::_ENV_::OLD_MRO;
19
20 # inflation test
21 my $cd = $schema->resultset("CD")->find(3);
22
23 is( ref($cd->year), 'DateTime', 'year is a DateTime, ok' );
24
25 is( $cd->year->month, 1, 'inflated month ok' );
26
27 # deflate test
28 my $now = DateTime->now;
29 $cd->year( $now );
30 $cd->update;
31
32 ($cd) = $schema->resultset("CD")->search({ year => $now->year });
33 is( $cd->year->year, $now->year, 'deflate ok' );
34
35 # re-test using alternate deflate syntax
36 $schema->class("CD")->has_a( 'year', 'DateTime',
37       inflate => sub { DateTime->new( year => shift ) },
38       deflate => 'year'
39 );
40
41 # inflation test
42 $cd = $schema->resultset("CD")->find(3);
43
44 is( ref($cd->year), 'DateTime', 'year is a DateTime, ok' );
45
46 is( $cd->year->month, 1, 'inflated month ok' );
47
48 # deflate test
49 $now = DateTime->now;
50 $cd->year( $now );
51 $cd->update;
52
53 ($cd) = $schema->resultset("CD")->search({ year => $now->year });
54 is( $cd->year->year, $now->year, 'deflate ok' );
55
56 done_testing;