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