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