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