More test hackage, some cleanup in ResultSet
[dbsrgits/DBIx-Class.git] / t / run / 08inflate_has_a.tl
CommitLineData
0567538f 1sub run_tests {
1edaf6fe 2my $schema = shift;
0567538f 3
4eval { require DateTime };
5plan skip_all => "Need DateTime for inflation tests" if $@;
6
7plan tests => 6;
8
2d679367 9DBICTest::Schema::CD->load_components(qw/CDBICompat::HasA/);
0567538f 10
2d679367 11DBICTest::Schema::CD->has_a( 'year', 'DateTime',
0567538f 12 inflate => sub { DateTime->new( year => shift ) },
13 deflate => sub { shift->year }
14);
2d679367 15Class::C3->reinitialize;
0567538f 16
17# inflation test
f9db5527 18my $cd = $schema->resultset("CD")->find(3);
0567538f 19
20is( ref($cd->year), 'DateTime', 'year is a DateTime, ok' );
21
22is( $cd->year->month, 1, 'inflated month ok' );
23
24# deflate test
25my $now = DateTime->now;
26$cd->year( $now );
27$cd->update;
28
f9db5527 29($cd) = $schema->resultset("CD")->search( year => $now->year );
0567538f 30is( $cd->year->year, $now->year, 'deflate ok' );
31
32# re-test using alternate deflate syntax
1edaf6fe 33$schema->class("CD")->has_a( 'year', 'DateTime',
0567538f 34 inflate => sub { DateTime->new( year => shift ) },
35 deflate => 'year'
36);
37
38# inflation test
f9db5527 39$cd = $schema->resultset("CD")->find(3);
0567538f 40
41is( ref($cd->year), 'DateTime', 'year is a DateTime, ok' );
42
43is( $cd->year->month, 1, 'inflated month ok' );
44
45# deflate test
46$now = DateTime->now;
47$cd->year( $now );
48$cd->update;
49
f9db5527 50($cd) = $schema->resultset("CD")->search( year => $now->year );
0567538f 51is( $cd->year->year, $now->year, 'deflate ok' );
52
53}
54
551;