Reorganize CDBICompat tests - centralize prereq checks in one place
[dbsrgits/DBIx-Class.git] / t / cdbi / 68-inflate_has_a.t
1 use strict;
2 use warnings;
3 use Test::More;
4
5 use lib qw(t/cdbi/testlib);
6 use DBIC::Test::SQLite (); # this will issue the necessary SKIPs on missing reqs
7
8 BEGIN {
9   eval { require DateTime; DateTime->VERSION(0.55) }
10     or plan skip_all => 'DateTime 0.55 required for this test';
11 }
12
13 my $schema = DBICTest->init_schema();
14
15 DBICTest::Schema::CD->load_components(qw/CDBICompat::Relationships/);
16
17 DBICTest::Schema::CD->has_a( 'year', 'DateTime',
18       inflate => sub { DateTime->new( year => shift ) },
19       deflate => sub { shift->year }
20 );
21 Class::C3->reinitialize;
22
23 # inflation test
24 my $cd = $schema->resultset("CD")->find(3);
25
26 is( ref($cd->year), 'DateTime', 'year is a DateTime, ok' );
27
28 is( $cd->year->month, 1, 'inflated month ok' );
29
30 # deflate test
31 my $now = DateTime->now;
32 $cd->year( $now );
33 $cd->update;
34
35 ($cd) = $schema->resultset("CD")->search({ year => $now->year });
36 is( $cd->year->year, $now->year, 'deflate ok' );
37
38 # re-test using alternate deflate syntax
39 $schema->class("CD")->has_a( 'year', 'DateTime',
40       inflate => sub { DateTime->new( year => shift ) },
41       deflate => 'year'
42 );
43
44 # inflation test
45 $cd = $schema->resultset("CD")->find(3);
46
47 is( ref($cd->year), 'DateTime', 'year is a DateTime, ok' );
48
49 is( $cd->year->month, 1, 'inflated month ok' );
50
51 # deflate test
52 $now = DateTime->now;
53 $cd->year( $now );
54 $cd->update;
55
56 ($cd) = $schema->resultset("CD")->search({ year => $now->year });
57 is( $cd->year->year, $now->year, 'deflate ok' );
58
59 done_testing;