Test and fix scalarref in an inflatable slot corner-case
[dbsrgits/DBIx-Class.git] / t / inflate / core.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use Test::Exception;
6 use lib qw(t/lib);
7 use DBICTest;
8
9 my $schema = DBICTest->init_schema();
10
11 eval { require DateTime };
12 plan skip_all => "Need DateTime for inflation tests" if $@;
13
14 $schema->class('CD') ->inflate_column( 'year',
15     { inflate => sub { DateTime->new( year => shift ) },
16       deflate => sub { shift->year } }
17 );
18
19 my $rs = $schema->resultset('CD');
20
21 # inflation test
22 my $cd = $rs->find(3);
23
24 is( ref($cd->year), 'DateTime', 'year is a DateTime, ok' );
25
26 is( $cd->year->year, 1997, 'inflated year ok' );
27
28 is( $cd->year->month, 1, 'inflated month ok' );
29
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
45 # deflate test
46 my $now = DateTime->now;
47 $cd->year( $now );
48 $cd->update;
49
50 $cd = $rs->find(3);
51 is( $cd->year->year, $now->year, 'deflate ok' );
52
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
58 $cd = $rs->find(3);
59 is( $cd->year->year, $now->year, 'deflate ok' );
60
61 $cd = $rs->find(3);
62 my $before_year = $cd->year->year;
63 eval { $cd->set_inflated_column('year', \'year + 1') };
64 ok(!$@, 'set_inflated_column to "year + 1"');
65 $cd->update;
66
67 TODO: {
68   local $TODO = 'this was left in without a TODO - should it work?';
69
70   lives_ok (sub {
71     $cd->store_inflated_column('year', \'year + 1');
72     is_deeply( $cd->year, \'year + 1', 'deflate ok' );
73   }, 'store_inflated_column to "year + 1"');
74 }
75
76 $cd = $rs->find(3);
77 is( $cd->year->year, $before_year+1, 'deflate ok' );
78
79 # store_inflated_column test
80 $cd = $rs->find(3);
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
87 # update tests
88 $cd = $rs->find(3);
89 eval { $cd->update({'year' => $now}) };
90 ok(!$@, 'update using DateTime object ok');
91 is($cd->year->year, $now->year, 'deflate ok');
92
93 $cd = $rs->find(3);
94 $before_year = $cd->year->year;
95 eval { $cd->update({'year' => \'year + 1'}) };
96 ok(!$@, 'update using scalarref ok');
97
98 $cd = $rs->find(3);
99 is($cd->year->year, $before_year + 1, 'deflate ok');
100
101 # discard_changes test
102 $cd = $rs->find(3);
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');
109
110 my $copy = $cd->copy({ year => $now, title => "zemoose" });
111
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');
152
153 done_testing;