8 DBICTest::Schema::CD->add_column('year');
9 my $schema = DBICTest->init_schema();
11 eval { require DateTime };
12 plan skip_all => "Need DateTime for inflation tests" if $@;
16 DBICTest::Schema::CD->inflate_column( 'year',
17 { inflate => sub { DateTime->new( year => shift ) },
18 deflate => sub { shift->year } }
20 Class::C3->reinitialize;
23 my $cd = $schema->resultset("CD")->find(3);
25 is( ref($cd->year), 'DateTime', 'year is a DateTime, ok' );
27 is( $cd->year->year, 1997, 'inflated year ok' );
29 is( $cd->year->month, 1, 'inflated month ok' );
31 eval { $cd->year(\'year +1'); };
32 ok(!$@, 'updated year using a scalarref');
34 $cd->discard_changes();
36 is( ref($cd->year), 'DateTime', 'year is still a DateTime, ok' );
38 is( $cd->year->year, 1998, 'updated year, bypassing inflation' );
40 is( $cd->year->month, 1, 'month is still 1' );
42 # get_inflated_column test
44 is( ref($cd->get_inflated_column('year')), 'DateTime', 'get_inflated_column produces a DateTime');
47 my $now = DateTime->now;
51 ($cd) = $schema->resultset("CD")->search( year => $now->year );
52 is( $cd->year->year, $now->year, 'deflate ok' );
54 # set_inflated_column test
55 eval { $cd->set_inflated_column('year', $now) };
56 ok(!$@, 'set_inflated_column with DateTime object');
59 ($cd) = $schema->resultset("CD")->search( year => $now->year );
60 is( $cd->year->year, $now->year, 'deflate ok' );
62 eval { $cd->set_inflated_column('year', \'year + 1') };
63 print STDERR "ERROR: $@" if($@);
64 ok(!$@, 'set_inflated_column to "year + 1"');
66 $cd->discard_changes();
68 ($cd) = $schema->resultset("CD")->search( year => $now->year + 1 );
69 is( $cd->year->year, $now->year+1, 'deflate ok' );
71 # store_inflated_column test
72 eval { $cd->store_inflated_column('year', $now) };
73 ok(!$@, 'store_inflated_column with DateTime object');
76 is( $cd->year->year, $now->year, 'deflate ok' );
78 # eval { $cd->store_inflated_column('year', \'year + 1') };
79 # print STDERR "ERROR: $@" if($@);
80 # ok(!$@, 'store_inflated_column to "year + 1"');
82 # is_deeply( $cd->year, \'year + 1', 'deflate ok' );