Massive cleanup of DateTime test dependencies, other interim
[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
68de9438 11plan skip_all => 'Inflation tests need ' . DBIx::Class::Optional::Dependencies->req_missing_for ('test_dt')
12 unless DBIx::Class::Optional::Dependencies->req_ok_for ('test_dt');
0567538f 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
68de9438 30lives_ok (
31 sub { $cd->year(\'year +1') },
32 'updated year using a scalarref'
33);
e81a6241 34$cd->update();
35$cd->discard_changes();
36
37is( ref($cd->year), 'DateTime', 'year is still a DateTime, ok' );
38
39is( $cd->year->year, 1998, 'updated year, bypassing inflation' );
40
41is( $cd->year->month, 1, 'month is still 1' );
42
43# get_inflated_column test
44
45is( ref($cd->get_inflated_column('year')), 'DateTime', 'get_inflated_column produces a DateTime');
46
0567538f 47# deflate test
48my $now = DateTime->now;
49$cd->year( $now );
50$cd->update;
51
f92b166e 52$cd = $rs->find(3);
0567538f 53is( $cd->year->year, $now->year, 'deflate ok' );
54
e81a6241 55# set_inflated_column test
68de9438 56lives_ok (
57 sub { $cd->set_inflated_column('year', $now) },
58 'set_inflated_column with DateTime object'
59);
e81a6241 60$cd->update;
61
f92b166e 62$cd = $rs->find(3);
e81a6241 63is( $cd->year->year, $now->year, 'deflate ok' );
64
f92b166e 65$cd = $rs->find(3);
ad5d0ee9 66my $before_year = $cd->year->year;
68de9438 67lives_ok (
68 sub { $cd->set_inflated_column('year', \'year + 1') },
69 'set_inflated_column to "year + 1"',
70);
e81a6241 71$cd->update;
e81a6241 72
7d409be1 73$cd->store_inflated_column('year', \'year + 1');
74is_deeply( $cd->year, \'year + 1', 'scalarref deflate passthrough ok' );
c6e0ee83 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);
68de9438 81lives_ok (
82 sub { $cd->store_inflated_column('year', $now) },
83 'store_inflated_column with DateTime object'
84);
e81a6241 85$cd->update;
86
87is( $cd->year->year, $now->year, 'deflate ok' );
88
ad5d0ee9 89# update tests
f92b166e 90$cd = $rs->find(3);
68de9438 91lives_ok (
92 sub { $cd->update({'year' => $now}) },
93 'update using DateTime object ok'
94);
ad5d0ee9 95is($cd->year->year, $now->year, 'deflate ok');
96
f92b166e 97$cd = $rs->find(3);
ad5d0ee9 98$before_year = $cd->year->year;
68de9438 99lives_ok (
100 sub { $cd->update({'year' => \'year + 1'}) },
101 'update using scalarref ok'
102);
ad5d0ee9 103
f92b166e 104$cd = $rs->find(3);
ad5d0ee9 105is($cd->year->year, $before_year + 1, 'deflate ok');
106
107# discard_changes test
f92b166e 108$cd = $rs->find(3);
ad5d0ee9 109# inflate the year
110$before_year = $cd->year->year;
111$cd->update({ year => \'year + 1'});
112$cd->discard_changes;
113
114is($cd->year->year, $before_year + 1, 'discard_changes clears the inflated value');
07d4fb20 115
116my $copy = $cd->copy({ year => $now, title => "zemoose" });
117
f92b166e 118is( $copy->year->year, $now->year, "copy" );
119
120
121
122my $artist = $cd->artist;
123my $sval = \ '2012';
124
125$cd = $rs->create ({
126 artist => $artist,
127 year => $sval,
128 title => 'create with scalarref',
129});
130
131is ($cd->year, $sval, 'scalar value retained');
132my $cd2 = $cd->copy ({ title => 'copy with scalar in coldata' });
133is ($cd2->year, $sval, 'copied scalar value retained');
134
135$cd->discard_changes;
136is ($cd->year->year, 2012, 'infation upon reload');
137
138$cd2->discard_changes;
139is ($cd2->year->year, 2012, 'infation upon reload of copy');
140
141
142my $precount = $rs->count;
143$cd = $rs->update_or_create ({artist => $artist, title => 'nonexisting update/create test row', year => $sval });
144is ($rs->count, $precount + 1, 'Row created');
145
146is ($cd->year, $sval, 'scalar value retained on creating update_or_create');
147$cd->discard_changes;
148is ($cd->year->year, 2012, 'infation upon reload');
149
150my $sval2 = \ '2013';
151
152$cd = $rs->update_or_create ({artist => $artist, title => 'nonexisting update/create test row', year => $sval2 });
153is ($rs->count, $precount + 1, 'No more rows created');
154
155is ($cd->year, $sval2, 'scalar value retained on updating update_or_create');
156$cd->discard_changes;
157is ($cd->year->year, 2013, 'infation upon reload');
794b796f 158
159done_testing;