Fix for gut-reaching travesty as reported by gbjk
[dbsrgits/DBIx-Class.git] / t / row / filter_column.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 $from_storage_ran = 0;
10 my $to_storage_ran = 0;
11 my $schema = DBICTest->init_schema( no_populate => 1 );
12 DBICTest::Schema::Artist->load_components(qw(FilterColumn InflateColumn));
13 DBICTest::Schema::Artist->filter_column(charfield => {
14   filter_from_storage => sub { $from_storage_ran++; defined $_[1] ? $_[1] * 2 : undef },
15   filter_to_storage   => sub { $to_storage_ran++; defined $_[1] ? $_[1] / 2 : undef },
16 });
17 Class::C3->reinitialize() if DBIx::Class::_ENV_::OLD_MRO;
18
19 my $artist = $schema->resultset('Artist')->create( { charfield => 20 } );
20
21 # this should be using the cursor directly, no inflation/processing of any sort
22 my ($raw_db_charfield) = $schema->resultset('Artist')
23                              ->search ($artist->ident_condition)
24                                ->get_column('charfield')
25                                 ->_resultset
26                                  ->cursor
27                                   ->next;
28
29 is ($raw_db_charfield, 10, 'INSERT: correctly unfiltered on insertion');
30
31 for my $reloaded (0, 1) {
32   my $test = $reloaded ? 'reloaded' : 'stored';
33   $artist->discard_changes if $reloaded;
34
35   is( $artist->charfield , 20, "got $test filtered charfield" );
36 }
37
38 $artist->update;
39 $artist->discard_changes;
40 is( $artist->charfield , 20, "got filtered charfield" );
41
42 $artist->update ({ charfield => 40 });
43 ($raw_db_charfield) = $schema->resultset('Artist')
44                              ->search ($artist->ident_condition)
45                                ->get_column('charfield')
46                                 ->_resultset
47                                  ->cursor
48                                   ->next;
49 is ($raw_db_charfield, 20, 'UPDATE: correctly unflitered on update');
50
51 $artist->discard_changes;
52 $artist->charfield(40);
53 ok( !$artist->is_column_changed('charfield'), 'column is not dirty after setting the same value' );
54
55 MC: {
56    my $cd = $schema->resultset('CD')->create({
57       artist => { charfield => 20 },
58       title => 'fun time city!',
59       year => 'forevertime',
60    });
61    ($raw_db_charfield) = $schema->resultset('Artist')
62                                 ->search ($cd->artist->ident_condition)
63                                   ->get_column('charfield')
64                                    ->_resultset
65                                     ->cursor
66                                      ->next;
67
68    is $raw_db_charfield, 10, 'artist charfield gets correctly unfiltered w/ MC';
69    is $cd->artist->charfield, 20, 'artist charfield gets correctly filtered w/ MC';
70 }
71
72 CACHE_TEST: {
73   my $expected_from = $from_storage_ran;
74   my $expected_to   = $to_storage_ran;
75
76   # ensure we are creating a fresh obj
77   $artist = $schema->resultset('Artist')->single($artist->ident_condition);
78
79   is $from_storage_ran, $expected_from, 'from has not run yet';
80   is $to_storage_ran, $expected_to, 'to has not run yet';
81
82   $artist->charfield;
83   cmp_ok (
84     $artist->get_filtered_column('charfield'),
85       '!=',
86     $artist->get_column('charfield'),
87     'filter/unfilter differ'
88   );
89   is $from_storage_ran, ++$expected_from, 'from ran once, therefor caches';
90   is $to_storage_ran, $expected_to,  'to did not run';
91
92   $artist->charfield(6);
93   is $from_storage_ran, $expected_from, 'from did not run';
94   is $to_storage_ran, ++$expected_to,  'to ran once';
95
96   ok ($artist->is_column_changed ('charfield'), 'Column marked as dirty');
97
98   $artist->charfield;
99   is $from_storage_ran, $expected_from, 'from did not run';
100   is $to_storage_ran, $expected_to,  'to did not run';
101
102   $artist->update;
103
104   $artist->set_column(charfield => 3);
105   ok (! $artist->is_column_changed ('charfield'), 'Column not marked as dirty on same set_column value');
106   is ($artist->charfield, '6', 'Column set properly (cache blown)');
107   is $from_storage_ran, ++$expected_from, 'from ran once (set_column blew cache)';
108   is $to_storage_ran, $expected_to,  'to did not run';
109
110   $artist->charfield(6);
111   ok (! $artist->is_column_changed ('charfield'), 'Column not marked as dirty on same accessor-set value');
112   is ($artist->charfield, '6', 'Column set properly');
113   is $from_storage_ran, $expected_from, 'from did not run';
114   is $to_storage_ran, ++$expected_to,  'to did run once (call in to set_column)';
115
116   $artist->store_column(charfield => 4);
117   ok (! $artist->is_column_changed ('charfield'), 'Column not marked as dirty on differing store_column value');
118   is ($artist->charfield, '8', 'Cache properly blown');
119   is $from_storage_ran, ++$expected_from, 'from did not run';
120   is $to_storage_ran, $expected_to,  'to did not run';
121
122   $artist->update({ charfield => undef });
123   is $from_storage_ran, $expected_from, 'from did not run';
124   is $to_storage_ran, ++$expected_to,  'to did run';
125
126   $artist->discard_changes;
127   is ( $artist->get_column('charfield'), undef, 'Got back null' );
128   is ( $artist->charfield, undef, 'Got back null through filter' );
129
130   is $from_storage_ran, ++$expected_from, 'from did run';
131   is $to_storage_ran, $expected_to,  'to did not run';
132
133 }
134
135 # test in-memory operations
136 for my $artist_maker (
137   sub { $schema->resultset('Artist')->new({ charfield => 42 }) },
138   sub { my $art = $schema->resultset('Artist')->new({}); $art->charfield(42); $art },
139 ) {
140
141   my $expected_from = $from_storage_ran;
142   my $expected_to   = $to_storage_ran;
143
144   my $artist = $artist_maker->();
145
146   is $from_storage_ran, $expected_from, 'from has not run yet';
147   is $to_storage_ran, $expected_to, 'to has not run yet';
148
149   ok( ! $artist->has_column_loaded('artistid'), 'pk not loaded' );
150   ok( $artist->has_column_loaded('charfield'), 'Filtered column marked as loaded under new' );
151   is( $artist->charfield, 42, 'Proper unfiltered value' );
152   is( $artist->get_column('charfield'), 21, 'Proper filtered value' );
153 }
154
155 # test literals
156 for my $v ( \ '16', \[ '?', '16' ] ) {
157   my $rs = $schema->resultset('Artist');
158   $rs->delete;
159
160   my $art = $rs->new({ charfield => 10 });
161   $art->charfield($v);
162
163   is_deeply( $art->charfield, $v);
164   is_deeply( $art->get_filtered_column("charfield"), $v);
165   is_deeply( $art->get_column("charfield"), $v);
166
167   $art->insert;
168   $art->discard_changes;
169
170   is ($art->get_column("charfield"), 16, "Literal inserted into database properly");
171   is ($art->charfield, 32, "filtering still works");
172
173   $art->update({ charfield => $v });
174
175   is_deeply( $art->charfield, $v);
176   is_deeply( $art->get_filtered_column("charfield"), $v);
177   is_deeply( $art->get_column("charfield"), $v);
178
179   $art->discard_changes;
180
181   is ($art->get_column("charfield"), 16, "Literal inserted into database properly");
182   is ($art->charfield, 32, "filtering still works");
183 }
184
185 IC_DIE: {
186   throws_ok {
187      DBICTest::Schema::Artist->inflate_column(charfield =>
188         { inflate => sub {}, deflate => sub {} }
189      );
190   } qr/InflateColumn can not be used on a column with a declared FilterColumn filter/, q(Can't inflate column after filter column);
191
192   DBICTest::Schema::Artist->inflate_column(name =>
193      { inflate => sub {}, deflate => sub {} }
194   );
195
196   throws_ok {
197      DBICTest::Schema::Artist->filter_column(name => {
198         filter_to_storage => sub {},
199         filter_from_storage => sub {}
200      });
201   } qr/FilterColumn can not be used on a column with a declared InflateColumn inflator/, q(Can't filter column after inflate column);
202 }
203
204 # test when we do not set both filter_from_storage/filter_to_storage
205 DBICTest::Schema::Artist->filter_column(charfield => {
206   filter_to_storage => sub { $to_storage_ran++; $_[1] },
207 });
208 Class::C3->reinitialize() if DBIx::Class::_ENV_::OLD_MRO;
209
210 ASYMMETRIC_TO_TEST: {
211   # initialise value
212   $artist->charfield(20);
213   $artist->update;
214
215   my $expected_from = $from_storage_ran;
216   my $expected_to   = $to_storage_ran;
217
218   $artist->charfield(10);
219   ok ($artist->is_column_changed ('charfield'), 'Column marked as dirty on accessor-set value');
220   is ($artist->charfield, '10', 'Column set properly');
221   is $from_storage_ran, $expected_from, 'from did not run';
222   is $to_storage_ran, ++$expected_to,  'to did run';
223
224   $artist->discard_changes;
225
226   is ($artist->charfield, '20', 'Column set properly');
227   is $from_storage_ran, $expected_from, 'from did not run';
228   is $to_storage_ran, $expected_to,  'to did not run';
229 }
230
231 DBICTest::Schema::Artist->filter_column(charfield => {
232   filter_from_storage => sub { $from_storage_ran++; $_[1] },
233 });
234 Class::C3->reinitialize() if DBIx::Class::_ENV_::OLD_MRO;
235
236 ASYMMETRIC_FROM_TEST: {
237   # initialise value
238   $artist->charfield(23);
239   $artist->update;
240
241   my $expected_from = $from_storage_ran;
242   my $expected_to   = $to_storage_ran;
243
244   $artist->charfield(13);
245   ok ($artist->is_column_changed ('charfield'), 'Column marked as dirty on accessor-set value');
246   is ($artist->charfield, '13', 'Column set properly');
247   is $from_storage_ran, $expected_from, 'from did not run';
248   is $to_storage_ran, $expected_to,  'to did not run';
249
250   $artist->discard_changes;
251
252   is ($artist->charfield, '23', 'Column set properly');
253   is $from_storage_ran, ++$expected_from, 'from did run';
254   is $to_storage_ran, $expected_to,  'to did not run';
255 }
256
257 throws_ok { DBICTest::Schema::Artist->filter_column( charfield => {} ) }
258   qr/\QAn invocation of filter_column() must specify either a filter_from_storage or filter_to_storage/,
259   'Correctly throws exception for empty attributes'
260 ;
261
262 FC_ON_PK_TEST: {
263   # there are cases in the wild that autovivify stuff deep in the
264   # colinfo guts. While this is insane, there is no alternative
265   # so at leats make sure it keeps working...
266
267   $schema->source('Artist')->column_info('artistid')->{_filter_info} ||= {};
268
269   for my $key ('', 'primary') {
270     lives_ok {
271       $schema->resultset('Artist')->find_or_create({ artistid => 42 }, { $key ? ( key => $key ) : () });
272     };
273   }
274
275
276   DBICTest::Schema::Artist->filter_column(artistid => {
277     filter_to_storage => sub { $_[1] * 100 },
278     filter_from_storage => sub { $_[1] - 100 },
279   });
280
281   for my $key ('', 'primary') {
282     throws_ok {
283       $schema->resultset('Artist')->find_or_create({ artistid => 42 }, { $key ? ( key => $key ) : () });
284     } qr/\QUnable to satisfy requested constraint 'primary', FilterColumn values not usable for column(s): 'artistid'/;
285   }
286 }
287
288 done_testing;