Test and fix scalarref in an inflatable slot corner-case
[dbsrgits/DBIx-Class.git] / t / inflate / core.t
CommitLineData
70350518 1use strict;
794b796f 2use warnings;
70350518 3
4use Test::More;
794b796f 5use Test::Exception;
70350518 6use lib qw(t/lib);
7use DBICTest;
8
a47e1233 9my $schema = DBICTest->init_schema();
0567538f 10
11eval { require DateTime };
12plan skip_all => "Need DateTime for inflation tests" if $@;
13
c6e0ee83 14$schema->class('CD') ->inflate_column( 'year',
0567538f 15 { inflate => sub { DateTime->new( year => shift ) },
16 deflate => sub { shift->year } }
17);
18
f92b166e 19my $rs = $schema->resultset('CD');
20
0567538f 21# inflation test
f92b166e 22my $cd = $rs->find(3);
0567538f 23
24is( ref($cd->year), 'DateTime', 'year is a DateTime, ok' );
25
89279e9d 26is( $cd->year->year, 1997, 'inflated year ok' );
27
0567538f 28is( $cd->year->month, 1, 'inflated month ok' );
29
e81a6241 30eval { $cd->year(\'year +1'); };
31ok(!$@, 'updated year using a scalarref');
32$cd->update();
33$cd->discard_changes();
34
35is( ref($cd->year), 'DateTime', 'year is still a DateTime, ok' );
36
37is( $cd->year->year, 1998, 'updated year, bypassing inflation' );
38
39is( $cd->year->month, 1, 'month is still 1' );
40
41# get_inflated_column test
42
43is( ref($cd->get_inflated_column('year')), 'DateTime', 'get_inflated_column produces a DateTime');
44
0567538f 45# deflate test
46my $now = DateTime->now;
47$cd->year( $now );
48$cd->update;
49
f92b166e 50$cd = $rs->find(3);
0567538f 51is( $cd->year->year, $now->year, 'deflate ok' );
52
e81a6241 53# set_inflated_column test
54eval { $cd->set_inflated_column('year', $now) };
55ok(!$@, 'set_inflated_column with DateTime object');
56$cd->update;
57
f92b166e 58$cd = $rs->find(3);
e81a6241 59is( $cd->year->year, $now->year, 'deflate ok' );
60
f92b166e 61$cd = $rs->find(3);
ad5d0ee9 62my $before_year = $cd->year->year;
e81a6241 63eval { $cd->set_inflated_column('year', \'year + 1') };
e81a6241 64ok(!$@, 'set_inflated_column to "year + 1"');
65$cd->update;
e81a6241 66
c6e0ee83 67TODO: {
68 local $TODO = 'this was left in without a TODO - should it work?';
69
794b796f 70 lives_ok (sub {
c6e0ee83 71 $cd->store_inflated_column('year', \'year + 1');
72 is_deeply( $cd->year, \'year + 1', 'deflate ok' );
794b796f 73 }, 'store_inflated_column to "year + 1"');
c6e0ee83 74}
75
f92b166e 76$cd = $rs->find(3);
ad5d0ee9 77is( $cd->year->year, $before_year+1, 'deflate ok' );
e81a6241 78
79# store_inflated_column test
f92b166e 80$cd = $rs->find(3);
e81a6241 81eval { $cd->store_inflated_column('year', $now) };
82ok(!$@, 'store_inflated_column with DateTime object');
83$cd->update;
84
85is( $cd->year->year, $now->year, 'deflate ok' );
86
ad5d0ee9 87# update tests
f92b166e 88$cd = $rs->find(3);
ad5d0ee9 89eval { $cd->update({'year' => $now}) };
90ok(!$@, 'update using DateTime object ok');
91is($cd->year->year, $now->year, 'deflate ok');
92
f92b166e 93$cd = $rs->find(3);
ad5d0ee9 94$before_year = $cd->year->year;
95eval { $cd->update({'year' => \'year + 1'}) };
96ok(!$@, 'update using scalarref ok');
97
f92b166e 98$cd = $rs->find(3);
ad5d0ee9 99is($cd->year->year, $before_year + 1, 'deflate ok');
100
101# discard_changes test
f92b166e 102$cd = $rs->find(3);
ad5d0ee9 103# inflate the year
104$before_year = $cd->year->year;
105$cd->update({ year => \'year + 1'});
106$cd->discard_changes;
107
108is($cd->year->year, $before_year + 1, 'discard_changes clears the inflated value');
07d4fb20 109
110my $copy = $cd->copy({ year => $now, title => "zemoose" });
111
f92b166e 112is( $copy->year->year, $now->year, "copy" );
113
114
115
116my $artist = $cd->artist;
117my $sval = \ '2012';
118
119$cd = $rs->create ({
120 artist => $artist,
121 year => $sval,
122 title => 'create with scalarref',
123});
124
125is ($cd->year, $sval, 'scalar value retained');
126my $cd2 = $cd->copy ({ title => 'copy with scalar in coldata' });
127is ($cd2->year, $sval, 'copied scalar value retained');
128
129$cd->discard_changes;
130is ($cd->year->year, 2012, 'infation upon reload');
131
132$cd2->discard_changes;
133is ($cd2->year->year, 2012, 'infation upon reload of copy');
134
135
136my $precount = $rs->count;
137$cd = $rs->update_or_create ({artist => $artist, title => 'nonexisting update/create test row', year => $sval });
138is ($rs->count, $precount + 1, 'Row created');
139
140is ($cd->year, $sval, 'scalar value retained on creating update_or_create');
141$cd->discard_changes;
142is ($cd->year->year, 2012, 'infation upon reload');
143
144my $sval2 = \ '2013';
145
146$cd = $rs->update_or_create ({artist => $artist, title => 'nonexisting update/create test row', year => $sval2 });
147is ($rs->count, $precount + 1, 'No more rows created');
148
149is ($cd->year, $sval2, 'scalar value retained on updating update_or_create');
150$cd->discard_changes;
151is ($cd->year->year, 2013, 'infation upon reload');
794b796f 152
153done_testing;