Improve IC::DT timezone checker POD, add note explaining code weirdness
[dbsrgits/DBIx-Class.git] / t / 68inflate_has_a.t
CommitLineData
70350518 1use strict;
2use warnings;
3
4use Test::More;
5use lib qw(t/lib);
6use DBICTest;
7
a47e1233 8my $schema = DBICTest->init_schema();
0567538f 9
10eval { require DateTime };
11plan skip_all => "Need DateTime for inflation tests" if $@;
12
ae0419e2 13eval { require Clone };
14plan skip_all => "Need Clone for CDBICompat inflation tests" if $@;
15
0567538f 16plan tests => 6;
17
658b87f6 18DBICTest::Schema::CD->load_components(qw/CDBICompat::Relationships/);
0567538f 19
2d679367 20DBICTest::Schema::CD->has_a( 'year', 'DateTime',
0567538f 21 inflate => sub { DateTime->new( year => shift ) },
22 deflate => sub { shift->year }
23);
2d679367 24Class::C3->reinitialize;
0567538f 25
26# inflation test
f9db5527 27my $cd = $schema->resultset("CD")->find(3);
0567538f 28
29is( ref($cd->year), 'DateTime', 'year is a DateTime, ok' );
30
31is( $cd->year->month, 1, 'inflated month ok' );
32
33# deflate test
34my $now = DateTime->now;
35$cd->year( $now );
36$cd->update;
37
f9db5527 38($cd) = $schema->resultset("CD")->search( year => $now->year );
0567538f 39is( $cd->year->year, $now->year, 'deflate ok' );
40
41# re-test using alternate deflate syntax
1edaf6fe 42$schema->class("CD")->has_a( 'year', 'DateTime',
0567538f 43 inflate => sub { DateTime->new( year => shift ) },
44 deflate => 'year'
45);
46
47# inflation test
f9db5527 48$cd = $schema->resultset("CD")->find(3);
0567538f 49
50is( ref($cd->year), 'DateTime', 'year is a DateTime, ok' );
51
52is( $cd->year->month, 1, 'inflated month ok' );
53
54# deflate test
55$now = DateTime->now;
56$cd->year( $now );
57$cd->update;
58
f9db5527 59($cd) = $schema->resultset("CD")->search( year => $now->year );
0567538f 60is( $cd->year->year, $now->year, 'deflate ok' );
61