Fixup update/new ignoring with scalarrefs
[dbsrgits/DBIx-Class.git] / t / 68inflate.t
CommitLineData
70350518 1use strict;
2use warnings;
3
4use Test::More;
5use lib qw(t/lib);
6use DBICTest;
7
ec9c93f1 8DBICTest::Schema::CD->add_column('year');
a47e1233 9my $schema = DBICTest->init_schema();
0567538f 10
11eval { require DateTime };
12plan skip_all => "Need DateTime for inflation tests" if $@;
13
ad5d0ee9 14plan tests => 20;
0567538f 15
2d679367 16DBICTest::Schema::CD->inflate_column( 'year',
0567538f 17 { inflate => sub { DateTime->new( year => shift ) },
18 deflate => sub { shift->year } }
19);
2d679367 20Class::C3->reinitialize;
0567538f 21
22# inflation test
f9db5527 23my $cd = $schema->resultset("CD")->find(3);
0567538f 24
25is( ref($cd->year), 'DateTime', 'year is a DateTime, ok' );
26
89279e9d 27is( $cd->year->year, 1997, 'inflated year ok' );
28
0567538f 29is( $cd->year->month, 1, 'inflated month ok' );
30
e81a6241 31eval { $cd->year(\'year +1'); };
32ok(!$@, 'updated year using a scalarref');
33$cd->update();
34$cd->discard_changes();
35
36is( ref($cd->year), 'DateTime', 'year is still a DateTime, ok' );
37
38is( $cd->year->year, 1998, 'updated year, bypassing inflation' );
39
40is( $cd->year->month, 1, 'month is still 1' );
41
42# get_inflated_column test
43
44is( ref($cd->get_inflated_column('year')), 'DateTime', 'get_inflated_column produces a DateTime');
45
0567538f 46# deflate test
47my $now = DateTime->now;
48$cd->year( $now );
49$cd->update;
50
ad5d0ee9 51$cd = $schema->resultset("CD")->find(3);
0567538f 52is( $cd->year->year, $now->year, 'deflate ok' );
53
e81a6241 54# set_inflated_column test
55eval { $cd->set_inflated_column('year', $now) };
56ok(!$@, 'set_inflated_column with DateTime object');
57$cd->update;
58
ad5d0ee9 59$cd = $schema->resultset("CD")->find(3);
e81a6241 60is( $cd->year->year, $now->year, 'deflate ok' );
61
ad5d0ee9 62$cd = $schema->resultset("CD")->find(3);
63my $before_year = $cd->year->year;
e81a6241 64eval { $cd->set_inflated_column('year', \'year + 1') };
e81a6241 65ok(!$@, 'set_inflated_column to "year + 1"');
66$cd->update;
e81a6241 67
ad5d0ee9 68$cd = $schema->resultset("CD")->find(3);
69is( $cd->year->year, $before_year+1, 'deflate ok' );
e81a6241 70
71# store_inflated_column test
ad5d0ee9 72$cd = $schema->resultset("CD")->find(3);
e81a6241 73eval { $cd->store_inflated_column('year', $now) };
74ok(!$@, 'store_inflated_column with DateTime object');
75$cd->update;
76
77is( $cd->year->year, $now->year, 'deflate ok' );
78
ad5d0ee9 79# update tests
80$cd = $schema->resultset("CD")->find(3);
81eval { $cd->update({'year' => $now}) };
82ok(!$@, 'update using DateTime object ok');
83is($cd->year->year, $now->year, 'deflate ok');
84
85$cd = $schema->resultset("CD")->find(3);
86$before_year = $cd->year->year;
87eval { $cd->update({'year' => \'year + 1'}) };
88ok(!$@, 'update using scalarref ok');
89
90$cd = $schema->resultset("CD")->find(3);
91is($cd->year->year, $before_year + 1, 'deflate ok');
92
93# discard_changes test
94$cd = $schema->resultset("CD")->find(3);
95# inflate the year
96$before_year = $cd->year->year;
97$cd->update({ year => \'year + 1'});
98$cd->discard_changes;
99
100is($cd->year->year, $before_year + 1, 'discard_changes clears the inflated value');
101
e81a6241 102# eval { $cd->store_inflated_column('year', \'year + 1') };
103# print STDERR "ERROR: $@" if($@);
104# ok(!$@, 'store_inflated_column to "year + 1"');
105
106# is_deeply( $cd->year, \'year + 1', 'deflate ok' );
107