Commit | Line | Data |
70350518 |
1 | use strict; |
794b796f |
2 | use warnings; |
70350518 |
3 | |
4 | use Test::More; |
794b796f |
5 | use Test::Exception; |
70350518 |
6 | use lib qw(t/lib); |
7 | use DBICTest; |
8 | |
a47e1233 |
9 | my $schema = DBICTest->init_schema(); |
0567538f |
10 | |
11 | eval { require DateTime }; |
12 | plan 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 |
19 | my $rs = $schema->resultset('CD'); |
20 | |
0567538f |
21 | # inflation test |
f92b166e |
22 | my $cd = $rs->find(3); |
0567538f |
23 | |
24 | is( ref($cd->year), 'DateTime', 'year is a DateTime, ok' ); |
25 | |
89279e9d |
26 | is( $cd->year->year, 1997, 'inflated year ok' ); |
27 | |
0567538f |
28 | is( $cd->year->month, 1, 'inflated month ok' ); |
29 | |
e81a6241 |
30 | eval { $cd->year(\'year +1'); }; |
31 | ok(!$@, 'updated year using a scalarref'); |
32 | $cd->update(); |
33 | $cd->discard_changes(); |
34 | |
35 | is( ref($cd->year), 'DateTime', 'year is still a DateTime, ok' ); |
36 | |
37 | is( $cd->year->year, 1998, 'updated year, bypassing inflation' ); |
38 | |
39 | is( $cd->year->month, 1, 'month is still 1' ); |
40 | |
41 | # get_inflated_column test |
42 | |
43 | is( ref($cd->get_inflated_column('year')), 'DateTime', 'get_inflated_column produces a DateTime'); |
44 | |
0567538f |
45 | # deflate test |
46 | my $now = DateTime->now; |
47 | $cd->year( $now ); |
48 | $cd->update; |
49 | |
f92b166e |
50 | $cd = $rs->find(3); |
0567538f |
51 | is( $cd->year->year, $now->year, 'deflate ok' ); |
52 | |
e81a6241 |
53 | # set_inflated_column test |
54 | eval { $cd->set_inflated_column('year', $now) }; |
55 | ok(!$@, 'set_inflated_column with DateTime object'); |
56 | $cd->update; |
57 | |
f92b166e |
58 | $cd = $rs->find(3); |
e81a6241 |
59 | is( $cd->year->year, $now->year, 'deflate ok' ); |
60 | |
f92b166e |
61 | $cd = $rs->find(3); |
ad5d0ee9 |
62 | my $before_year = $cd->year->year; |
e81a6241 |
63 | eval { $cd->set_inflated_column('year', \'year + 1') }; |
e81a6241 |
64 | ok(!$@, 'set_inflated_column to "year + 1"'); |
65 | $cd->update; |
e81a6241 |
66 | |
c6e0ee83 |
67 | TODO: { |
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 |
77 | is( $cd->year->year, $before_year+1, 'deflate ok' ); |
e81a6241 |
78 | |
79 | # store_inflated_column test |
f92b166e |
80 | $cd = $rs->find(3); |
e81a6241 |
81 | eval { $cd->store_inflated_column('year', $now) }; |
82 | ok(!$@, 'store_inflated_column with DateTime object'); |
83 | $cd->update; |
84 | |
85 | is( $cd->year->year, $now->year, 'deflate ok' ); |
86 | |
ad5d0ee9 |
87 | # update tests |
f92b166e |
88 | $cd = $rs->find(3); |
ad5d0ee9 |
89 | eval { $cd->update({'year' => $now}) }; |
90 | ok(!$@, 'update using DateTime object ok'); |
91 | is($cd->year->year, $now->year, 'deflate ok'); |
92 | |
f92b166e |
93 | $cd = $rs->find(3); |
ad5d0ee9 |
94 | $before_year = $cd->year->year; |
95 | eval { $cd->update({'year' => \'year + 1'}) }; |
96 | ok(!$@, 'update using scalarref ok'); |
97 | |
f92b166e |
98 | $cd = $rs->find(3); |
ad5d0ee9 |
99 | is($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 | |
108 | is($cd->year->year, $before_year + 1, 'discard_changes clears the inflated value'); |
07d4fb20 |
109 | |
110 | my $copy = $cd->copy({ year => $now, title => "zemoose" }); |
111 | |
f92b166e |
112 | is( $copy->year->year, $now->year, "copy" ); |
113 | |
114 | |
115 | |
116 | my $artist = $cd->artist; |
117 | my $sval = \ '2012'; |
118 | |
119 | $cd = $rs->create ({ |
120 | artist => $artist, |
121 | year => $sval, |
122 | title => 'create with scalarref', |
123 | }); |
124 | |
125 | is ($cd->year, $sval, 'scalar value retained'); |
126 | my $cd2 = $cd->copy ({ title => 'copy with scalar in coldata' }); |
127 | is ($cd2->year, $sval, 'copied scalar value retained'); |
128 | |
129 | $cd->discard_changes; |
130 | is ($cd->year->year, 2012, 'infation upon reload'); |
131 | |
132 | $cd2->discard_changes; |
133 | is ($cd2->year->year, 2012, 'infation upon reload of copy'); |
134 | |
135 | |
136 | my $precount = $rs->count; |
137 | $cd = $rs->update_or_create ({artist => $artist, title => 'nonexisting update/create test row', year => $sval }); |
138 | is ($rs->count, $precount + 1, 'Row created'); |
139 | |
140 | is ($cd->year, $sval, 'scalar value retained on creating update_or_create'); |
141 | $cd->discard_changes; |
142 | is ($cd->year->year, 2012, 'infation upon reload'); |
143 | |
144 | my $sval2 = \ '2013'; |
145 | |
146 | $cd = $rs->update_or_create ({artist => $artist, title => 'nonexisting update/create test row', year => $sval2 }); |
147 | is ($rs->count, $precount + 1, 'No more rows created'); |
148 | |
149 | is ($cd->year, $sval2, 'scalar value retained on updating update_or_create'); |
150 | $cd->discard_changes; |
151 | is ($cd->year->year, 2013, 'infation upon reload'); |
794b796f |
152 | |
153 | done_testing; |