066fbfa8097fe7902b030da13a40eb80fbd86265
[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++; $_[1] * 2 },
15   filter_to_storage   => sub { $to_storage_ran++; $_[1] / 2 },
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
123 # test in-memory operations
124 for my $artist_maker (
125   sub { $schema->resultset('Artist')->new({ charfield => 42 }) },
126   sub { my $art = $schema->resultset('Artist')->new({}); $art->charfield(42); $art },
127 ) {
128
129   my $expected_from = $from_storage_ran;
130   my $expected_to   = $to_storage_ran;
131
132   my $artist = $artist_maker->();
133
134   is $from_storage_ran, $expected_from, 'from has not run yet';
135   is $to_storage_ran, $expected_to, 'to has not run yet';
136
137   ok( ! $artist->has_column_loaded('artistid'), 'pk not loaded' );
138   ok( $artist->has_column_loaded('charfield'), 'Filtered column marked as loaded under new' );
139   is( $artist->charfield, 42, 'Proper unfiltered value' );
140   is( $artist->get_column('charfield'), 21, 'Proper filtered value' );
141 }
142
143 # test literals
144 for my $v ( \ '16', \[ '?', '16' ] ) {
145   my $rs = $schema->resultset('Artist');
146   $rs->delete;
147
148   my $art = $rs->new({ charfield => 10 });
149   $art->charfield($v);
150
151   is_deeply( $art->charfield, $v);
152   is_deeply( $art->get_filtered_column("charfield"), $v);
153   is_deeply( $art->get_column("charfield"), $v);
154
155   $art->insert;
156   $art->discard_changes;
157
158   is ($art->get_column("charfield"), 16, "Literal inserted into database properly");
159   is ($art->charfield, 32, "filtering still works");
160
161   $art->update({ 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->discard_changes;
168
169   is ($art->get_column("charfield"), 16, "Literal inserted into database properly");
170   is ($art->charfield, 32, "filtering still works");
171 }
172
173 IC_DIE: {
174   throws_ok {
175      DBICTest::Schema::Artist->inflate_column(charfield =>
176         { inflate => sub {}, deflate => sub {} }
177      );
178   } qr/InflateColumn can not be used on a column with a declared FilterColumn filter/, q(Can't inflate column after filter column);
179
180   DBICTest::Schema::Artist->inflate_column(name =>
181      { inflate => sub {}, deflate => sub {} }
182   );
183
184   throws_ok {
185      DBICTest::Schema::Artist->filter_column(name => {
186         filter_to_storage => sub {},
187         filter_from_storage => sub {}
188      });
189   } qr/FilterColumn can not be used on a column with a declared InflateColumn inflator/, q(Can't filter column after inflate column);
190 }
191
192 # test when we do not set both filter_from_storage/filter_to_storage
193 DBICTest::Schema::Artist->filter_column(charfield => {
194   filter_to_storage => sub { $to_storage_ran++; $_[1] },
195 });
196 Class::C3->reinitialize() if DBIx::Class::_ENV_::OLD_MRO;
197
198 ASYMMETRIC_TO_TEST: {
199   # initialise value
200   $artist->charfield(20);
201   $artist->update;
202
203   my $expected_from = $from_storage_ran;
204   my $expected_to   = $to_storage_ran;
205
206   $artist->charfield(10);
207   ok ($artist->is_column_changed ('charfield'), 'Column marked as dirty on accessor-set value');
208   is ($artist->charfield, '10', 'Column set properly');
209   is $from_storage_ran, $expected_from, 'from did not run';
210   is $to_storage_ran, ++$expected_to,  'to did run';
211
212   $artist->discard_changes;
213
214   is ($artist->charfield, '20', 'Column set properly');
215   is $from_storage_ran, $expected_from, 'from did not run';
216   is $to_storage_ran, $expected_to,  'to did not run';
217 }
218
219 DBICTest::Schema::Artist->filter_column(charfield => {
220   filter_from_storage => sub { $from_storage_ran++; $_[1] },
221 });
222 Class::C3->reinitialize() if DBIx::Class::_ENV_::OLD_MRO;
223
224 ASYMMETRIC_FROM_TEST: {
225   # initialise value
226   $artist->charfield(23);
227   $artist->update;
228
229   my $expected_from = $from_storage_ran;
230   my $expected_to   = $to_storage_ran;
231
232   $artist->charfield(13);
233   ok ($artist->is_column_changed ('charfield'), 'Column marked as dirty on accessor-set value');
234   is ($artist->charfield, '13', 'Column set properly');
235   is $from_storage_ran, $expected_from, 'from did not run';
236   is $to_storage_ran, $expected_to,  'to did not run';
237
238   $artist->discard_changes;
239
240   is ($artist->charfield, '23', 'Column set properly');
241   is $from_storage_ran, ++$expected_from, 'from did run';
242   is $to_storage_ran, $expected_to,  'to did not run';
243 }
244
245 throws_ok { DBICTest::Schema::Artist->filter_column( charfield => {} ) }
246   qr/\QAn invocation of filter_column() must specify either a filter_from_storage or filter_to_storage/,
247   'Correctly throws exception for empty attributes'
248 ;
249
250 done_testing;