8 my $schema = DBICTest->init_schema();
10 eval { require DateTime };
11 plan skip_all => "Need DateTime for inflation tests" if $@;
15 $schema->class('CD') ->inflate_column( 'year',
16 { inflate => sub { DateTime->new( year => shift ) },
17 deflate => sub { shift->year } }
21 my $cd = $schema->resultset("CD")->find(3);
23 is( ref($cd->year), 'DateTime', 'year is a DateTime, ok' );
25 is( $cd->year->year, 1997, 'inflated year ok' );
27 is( $cd->year->month, 1, 'inflated month ok' );
29 eval { $cd->year(\'year +1'); };
30 ok(!$@, 'updated year using a scalarref');
32 $cd->discard_changes();
34 is( ref($cd->year), 'DateTime', 'year is still a DateTime, ok' );
36 is( $cd->year->year, 1998, 'updated year, bypassing inflation' );
38 is( $cd->year->month, 1, 'month is still 1' );
40 # get_inflated_column test
42 is( ref($cd->get_inflated_column('year')), 'DateTime', 'get_inflated_column produces a DateTime');
45 my $now = DateTime->now;
49 $cd = $schema->resultset("CD")->find(3);
50 is( $cd->year->year, $now->year, 'deflate ok' );
52 # set_inflated_column test
53 eval { $cd->set_inflated_column('year', $now) };
54 ok(!$@, 'set_inflated_column with DateTime object');
57 $cd = $schema->resultset("CD")->find(3);
58 is( $cd->year->year, $now->year, 'deflate ok' );
60 $cd = $schema->resultset("CD")->find(3);
61 my $before_year = $cd->year->year;
62 eval { $cd->set_inflated_column('year', \'year + 1') };
63 ok(!$@, 'set_inflated_column to "year + 1"');
67 local $TODO = 'this was left in without a TODO - should it work?';
70 $cd->store_inflated_column('year', \'year + 1');
71 is_deeply( $cd->year, \'year + 1', 'deflate ok' );
73 ok(!$@, 'store_inflated_column to "year + 1"');
76 $cd = $schema->resultset("CD")->find(3);
77 is( $cd->year->year, $before_year+1, 'deflate ok' );
79 # store_inflated_column test
80 $cd = $schema->resultset("CD")->find(3);
81 eval { $cd->store_inflated_column('year', $now) };
82 ok(!$@, 'store_inflated_column with DateTime object');
85 is( $cd->year->year, $now->year, 'deflate ok' );
88 $cd = $schema->resultset("CD")->find(3);
89 eval { $cd->update({'year' => $now}) };
90 ok(!$@, 'update using DateTime object ok');
91 is($cd->year->year, $now->year, 'deflate ok');
93 $cd = $schema->resultset("CD")->find(3);
94 $before_year = $cd->year->year;
95 eval { $cd->update({'year' => \'year + 1'}) };
96 ok(!$@, 'update using scalarref ok');
98 $cd = $schema->resultset("CD")->find(3);
99 is($cd->year->year, $before_year + 1, 'deflate ok');
101 # discard_changes test
102 $cd = $schema->resultset("CD")->find(3);
104 $before_year = $cd->year->year;
105 $cd->update({ year => \'year + 1'});
106 $cd->discard_changes;
108 is($cd->year->year, $before_year + 1, 'discard_changes clears the inflated value');
110 my $copy = $cd->copy({ year => $now, title => "zemoose" });
112 isnt( $copy->year->year, $before_year, "copy" );