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 },
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';
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 },
129 my $expected_from = $from_storage_ran;
130 my $expected_to = $to_storage_ran;
132 my $artist = $artist_maker->();
134 is $from_storage_ran, $expected_from, 'from has not run yet';
135 is $to_storage_ran, $expected_to, 'to has not run yet';
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' );
144 for my $v ( \ '16', \[ '?', '16' ] ) {
145 my $rs = $schema->resultset('Artist');
148 my $art = $rs->new({ charfield => 10 });
151 is_deeply( $art->charfield, $v);
152 is_deeply( $art->get_filtered_column("charfield"), $v);
153 is_deeply( $art->get_column("charfield"), $v);
156 $art->discard_changes;
158 is ($art->get_column("charfield"), 16, "Literal inserted into database properly");
159 is ($art->charfield, 32, "filtering still works");
161 $art->update({ charfield => $v });
163 is_deeply( $art->charfield, $v);
164 is_deeply( $art->get_filtered_column("charfield"), $v);
165 is_deeply( $art->get_column("charfield"), $v);
167 $art->discard_changes;
169 is ($art->get_column("charfield"), 16, "Literal inserted into database properly");
170 is ($art->charfield, 32, "filtering still works");
175 DBICTest::Schema::Artist->inflate_column(charfield =>
176 { inflate => sub {}, deflate => sub {} }
178 } qr/InflateColumn can not be used on a column with a declared FilterColumn filter/, q(Can't inflate column after filter column);
180 DBICTest::Schema::Artist->inflate_column(name =>
181 { inflate => sub {}, deflate => sub {} }
185 DBICTest::Schema::Artist->filter_column(name => {
186 filter_to_storage => sub {},
187 filter_from_storage => sub {}
189 } qr/FilterColumn can not be used on a column with a declared InflateColumn inflator/, q(Can't filter column after inflate column);
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] },
196 Class::C3->reinitialize() if DBIx::Class::_ENV_::OLD_MRO;
198 ASYMMETRIC_TO_TEST: {
200 $artist->charfield(20);
203 my $expected_from = $from_storage_ran;
204 my $expected_to = $to_storage_ran;
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';
212 $artist->discard_changes;
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';
219 DBICTest::Schema::Artist->filter_column(charfield => {
220 filter_from_storage => sub { $from_storage_ran++; $_[1] },
222 Class::C3->reinitialize() if DBIx::Class::_ENV_::OLD_MRO;
224 ASYMMETRIC_FROM_TEST: {
226 $artist->charfield(23);
229 my $expected_from = $from_storage_ran;
230 my $expected_to = $to_storage_ran;
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';
238 $artist->discard_changes;
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';
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'