9 my $schema = DBICTest->init_schema();
11 eval { require DateTime };
12 plan skip_all => "Need DateTime for inflation tests" if $@;
14 $schema->class('CD') ->inflate_column( 'year',
15 { inflate => sub { DateTime->new( year => shift ) },
16 deflate => sub { shift->year } }
20 my $cd = $schema->resultset("CD")->find(3);
22 is( ref($cd->year), 'DateTime', 'year is a DateTime, ok' );
24 is( $cd->year->year, 1997, 'inflated year ok' );
26 is( $cd->year->month, 1, 'inflated month ok' );
28 eval { $cd->year(\'year +1'); };
29 ok(!$@, 'updated year using a scalarref');
31 $cd->discard_changes();
33 is( ref($cd->year), 'DateTime', 'year is still a DateTime, ok' );
35 is( $cd->year->year, 1998, 'updated year, bypassing inflation' );
37 is( $cd->year->month, 1, 'month is still 1' );
39 # get_inflated_column test
41 is( ref($cd->get_inflated_column('year')), 'DateTime', 'get_inflated_column produces a DateTime');
44 my $now = DateTime->now;
48 $cd = $schema->resultset("CD")->find(3);
49 is( $cd->year->year, $now->year, 'deflate ok' );
51 # set_inflated_column test
52 eval { $cd->set_inflated_column('year', $now) };
53 ok(!$@, 'set_inflated_column with DateTime object');
56 $cd = $schema->resultset("CD")->find(3);
57 is( $cd->year->year, $now->year, 'deflate ok' );
59 $cd = $schema->resultset("CD")->find(3);
60 my $before_year = $cd->year->year;
61 eval { $cd->set_inflated_column('year', \'year + 1') };
62 ok(!$@, 'set_inflated_column to "year + 1"');
66 local $TODO = 'this was left in without a TODO - should it work?';
69 $cd->store_inflated_column('year', \'year + 1');
70 is_deeply( $cd->year, \'year + 1', 'deflate ok' );
71 }, 'store_inflated_column to "year + 1"');
74 $cd = $schema->resultset("CD")->find(3);
75 is( $cd->year->year, $before_year+1, 'deflate ok' );
77 # store_inflated_column test
78 $cd = $schema->resultset("CD")->find(3);
79 eval { $cd->store_inflated_column('year', $now) };
80 ok(!$@, 'store_inflated_column with DateTime object');
83 is( $cd->year->year, $now->year, 'deflate ok' );
86 $cd = $schema->resultset("CD")->find(3);
87 eval { $cd->update({'year' => $now}) };
88 ok(!$@, 'update using DateTime object ok');
89 is($cd->year->year, $now->year, 'deflate ok');
91 $cd = $schema->resultset("CD")->find(3);
92 $before_year = $cd->year->year;
93 eval { $cd->update({'year' => \'year + 1'}) };
94 ok(!$@, 'update using scalarref ok');
96 $cd = $schema->resultset("CD")->find(3);
97 is($cd->year->year, $before_year + 1, 'deflate ok');
99 # discard_changes test
100 $cd = $schema->resultset("CD")->find(3);
102 $before_year = $cd->year->year;
103 $cd->update({ year => \'year + 1'});
104 $cd->discard_changes;
106 is($cd->year->year, $before_year + 1, 'discard_changes clears the inflated value');
108 my $copy = $cd->copy({ year => $now, title => "zemoose" });
110 isnt( $copy->year->year, $before_year, "copy" );