8 my $schema = DBICTest->init_schema();
10 eval { require DateTime };
11 plan skip_all => "Need DateTime for inflation tests" if $@;
17 ->inflate_column( 'year',
18 { inflate => sub { DateTime->new( year => shift ) },
19 deflate => sub { shift->year } }
21 Class::C3->reinitialize;
24 my $cd = $schema->resultset("CD")->find(3);
26 is( ref($cd->year), 'DateTime', 'year is a DateTime, ok' );
28 is( $cd->year->year, 1997, 'inflated year ok' );
30 is( $cd->year->month, 1, 'inflated month ok' );
32 eval { $cd->year(\'year +1'); };
33 ok(!$@, 'updated year using a scalarref');
35 $cd->discard_changes();
37 is( ref($cd->year), 'DateTime', 'year is still a DateTime, ok' );
39 is( $cd->year->year, 1998, 'updated year, bypassing inflation' );
41 is( $cd->year->month, 1, 'month is still 1' );
43 # get_inflated_column test
45 is( ref($cd->get_inflated_column('year')), 'DateTime', 'get_inflated_column produces a DateTime');
48 my $now = DateTime->now;
52 $cd = $schema->resultset("CD")->find(3);
53 is( $cd->year->year, $now->year, 'deflate ok' );
55 # set_inflated_column test
56 eval { $cd->set_inflated_column('year', $now) };
57 ok(!$@, 'set_inflated_column with DateTime object');
60 $cd = $schema->resultset("CD")->find(3);
61 is( $cd->year->year, $now->year, 'deflate ok' );
63 $cd = $schema->resultset("CD")->find(3);
64 my $before_year = $cd->year->year;
65 eval { $cd->set_inflated_column('year', \'year + 1') };
66 ok(!$@, 'set_inflated_column to "year + 1"');
69 $cd = $schema->resultset("CD")->find(3);
70 is( $cd->year->year, $before_year+1, 'deflate ok' );
72 # store_inflated_column test
73 $cd = $schema->resultset("CD")->find(3);
74 eval { $cd->store_inflated_column('year', $now) };
75 ok(!$@, 'store_inflated_column with DateTime object');
78 is( $cd->year->year, $now->year, 'deflate ok' );
81 $cd = $schema->resultset("CD")->find(3);
82 eval { $cd->update({'year' => $now}) };
83 ok(!$@, 'update using DateTime object ok');
84 is($cd->year->year, $now->year, 'deflate ok');
86 $cd = $schema->resultset("CD")->find(3);
87 $before_year = $cd->year->year;
88 eval { $cd->update({'year' => \'year + 1'}) };
89 ok(!$@, 'update using scalarref ok');
91 $cd = $schema->resultset("CD")->find(3);
92 is($cd->year->year, $before_year + 1, 'deflate ok');
94 # discard_changes test
95 $cd = $schema->resultset("CD")->find(3);
97 $before_year = $cd->year->year;
98 $cd->update({ year => \'year + 1'});
101 is($cd->year->year, $before_year + 1, 'discard_changes clears the inflated value');
103 # eval { $cd->store_inflated_column('year', \'year + 1') };
104 # print STDERR "ERROR: $@" if($@);
105 # ok(!$@, 'store_inflated_column to "year + 1"');
107 # is_deeply( $cd->year, \'year + 1', 'deflate ok' );