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 },
17 Class::C3->reinitialize() if DBIx::Class::_ENV_::OLD_MRO;
19 my $artist = $schema->resultset('Artist')->create( { charfield => 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')
29 is ($raw_db_charfield, 10, 'INSERT: correctly unfiltered on insertion');
31 for my $reloaded (0, 1) {
32 my $test = $reloaded ? 'reloaded' : 'stored';
33 $artist->discard_changes if $reloaded;
35 is( $artist->charfield , 20, "got $test filtered charfield" );
39 $artist->discard_changes;
40 is( $artist->charfield , 20, "got filtered charfield" );
42 $artist->update ({ charfield => 40 });
43 ($raw_db_charfield) = $schema->resultset('Artist')
44 ->search ($artist->ident_condition)
45 ->get_column('charfield')
49 is ($raw_db_charfield, 20, 'UPDATE: correctly unflitered on update');
51 $artist->discard_changes;
52 $artist->charfield(40);
53 ok( !$artist->is_column_changed('charfield'), 'column is not dirty after setting the same value' );
56 my $cd = $schema->resultset('CD')->create({
57 artist => { charfield => 20 },
58 title => 'fun time city!',
59 year => 'forevertime',
61 ($raw_db_charfield) = $schema->resultset('Artist')
62 ->search ($cd->artist->ident_condition)
63 ->get_column('charfield')
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';
73 my $expected_from = $from_storage_ran;
74 my $expected_to = $to_storage_ran;
76 # ensure we are creating a fresh obj
77 $artist = $schema->resultset('Artist')->single($artist->ident_condition);
79 is $from_storage_ran, $expected_from, 'from has not run yet';
80 is $to_storage_ran, $expected_to, 'to has not run yet';
84 $artist->get_filtered_column('charfield'),
86 $artist->get_column('charfield'),
87 'filter/unfilter differ'
89 is $from_storage_ran, ++$expected_from, 'from ran once, therefor caches';
90 is $to_storage_ran, $expected_to, 'to did not run';
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';
96 ok ($artist->is_column_changed ('charfield'), 'Column marked as dirty');
99 is $from_storage_ran, $expected_from, 'from did not run';
100 is $to_storage_ran, $expected_to, 'to did not run';
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';
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)';
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';
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';
126 $artist->discard_changes;
127 is ( $artist->get_column('charfield'), undef, 'Got back null' );
128 is ( $artist->charfield, undef, 'Got back null through filter' );
130 is $from_storage_ran, ++$expected_from, 'from did run';
131 is $to_storage_ran, $expected_to, 'to did not run';
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 },
141 my $expected_from = $from_storage_ran;
142 my $expected_to = $to_storage_ran;
144 my $artist = $artist_maker->();
146 is $from_storage_ran, $expected_from, 'from has not run yet';
147 is $to_storage_ran, $expected_to, 'to has not run yet';
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' );
156 for my $v ( \ '16', \[ '?', '16' ] ) {
157 my $rs = $schema->resultset('Artist');
160 my $art = $rs->new({ charfield => 10 });
163 is_deeply( $art->charfield, $v);
164 is_deeply( $art->get_filtered_column("charfield"), $v);
165 is_deeply( $art->get_column("charfield"), $v);
168 $art->discard_changes;
170 is ($art->get_column("charfield"), 16, "Literal inserted into database properly");
171 is ($art->charfield, 32, "filtering still works");
173 $art->update({ charfield => $v });
175 is_deeply( $art->charfield, $v);
176 is_deeply( $art->get_filtered_column("charfield"), $v);
177 is_deeply( $art->get_column("charfield"), $v);
179 $art->discard_changes;
181 is ($art->get_column("charfield"), 16, "Literal inserted into database properly");
182 is ($art->charfield, 32, "filtering still works");
187 DBICTest::Schema::Artist->inflate_column(charfield =>
188 { inflate => sub {}, deflate => sub {} }
190 } qr/InflateColumn can not be used on a column with a declared FilterColumn filter/, q(Can't inflate column after filter column);
192 DBICTest::Schema::Artist->inflate_column(name =>
193 { inflate => sub {}, deflate => sub {} }
197 DBICTest::Schema::Artist->filter_column(name => {
198 filter_to_storage => sub {},
199 filter_from_storage => sub {}
201 } qr/FilterColumn can not be used on a column with a declared InflateColumn inflator/, q(Can't filter column after inflate column);
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] },
208 Class::C3->reinitialize() if DBIx::Class::_ENV_::OLD_MRO;
210 ASYMMETRIC_TO_TEST: {
212 $artist->charfield(20);
215 my $expected_from = $from_storage_ran;
216 my $expected_to = $to_storage_ran;
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';
224 $artist->discard_changes;
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';
231 DBICTest::Schema::Artist->filter_column(charfield => {
232 filter_from_storage => sub { $from_storage_ran++; $_[1] },
234 Class::C3->reinitialize() if DBIx::Class::_ENV_::OLD_MRO;
236 ASYMMETRIC_FROM_TEST: {
238 $artist->charfield(23);
241 my $expected_from = $from_storage_ran;
242 my $expected_to = $to_storage_ran;
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';
250 $artist->discard_changes;
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';
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'
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...
267 $schema->source('Artist')->column_info('artistid')->{_filter_info} ||= {};
269 for my $key ('', 'primary') {
271 $schema->resultset('Artist')->find_or_create({ artistid => 42 }, { $key ? ( key => $key ) : () });
276 DBICTest::Schema::Artist->filter_column(artistid => {
277 filter_to_storage => sub { $_[1] * 100 },
278 filter_from_storage => sub { $_[1] - 100 },
281 for my $key ('', 'primary') {
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'/;