Use a nullable column for the ::FC test
[dbsrgits/DBIx-Class.git] / t / row / filter_column.t
CommitLineData
956f4141 1use strict;
2use warnings;
3
4use Test::More;
c227b295 5use Test::Exception;
956f4141 6use lib qw(t/lib);
7use DBICTest;
cc2d2ead 8
85439e0c 9my $from_storage_ran = 0;
10my $to_storage_ran = 0;
1bc006cf 11my $schema = DBICTest->init_schema( no_populate => 1 );
c227b295 12DBICTest::Schema::Artist->load_components(qw(FilterColumn InflateColumn));
1bc006cf 13DBICTest::Schema::Artist->filter_column(charfield => {
85439e0c 14 filter_from_storage => sub { $from_storage_ran++; $_[1] * 2 },
15 filter_to_storage => sub { $to_storage_ran++; $_[1] / 2 },
cc2d2ead 16});
cc8ffd7b 17Class::C3->reinitialize() if DBIx::Class::_ENV_::OLD_MRO;
cc2d2ead 18
1bc006cf 19my $artist = $schema->resultset('Artist')->create( { charfield => 20 } );
cc2d2ead 20
21# this should be using the cursor directly, no inflation/processing of any sort
1bc006cf 22my ($raw_db_charfield) = $schema->resultset('Artist')
cc2d2ead 23 ->search ($artist->ident_condition)
1bc006cf 24 ->get_column('charfield')
cc2d2ead 25 ->_resultset
26 ->cursor
27 ->next;
28
1bc006cf 29is ($raw_db_charfield, 10, 'INSERT: correctly unfiltered on insertion');
cc2d2ead 30
31for my $reloaded (0, 1) {
32 my $test = $reloaded ? 'reloaded' : 'stored';
33 $artist->discard_changes if $reloaded;
34
1bc006cf 35 is( $artist->charfield , 20, "got $test filtered charfield" );
cc2d2ead 36}
37
38$artist->update;
39$artist->discard_changes;
1bc006cf 40is( $artist->charfield , 20, "got filtered charfield" );
cc2d2ead 41
1bc006cf 42$artist->update ({ charfield => 40 });
43($raw_db_charfield) = $schema->resultset('Artist')
cc2d2ead 44 ->search ($artist->ident_condition)
1bc006cf 45 ->get_column('charfield')
cc2d2ead 46 ->_resultset
47 ->cursor
48 ->next;
1bc006cf 49is ($raw_db_charfield, 20, 'UPDATE: correctly unflitered on update');
cc2d2ead 50
51$artist->discard_changes;
1bc006cf 52$artist->charfield(40);
53ok( !$artist->is_column_changed('charfield'), 'column is not dirty after setting the same value' );
cc2d2ead 54
8cfbd51c 55MC: {
56 my $cd = $schema->resultset('CD')->create({
1bc006cf 57 artist => { charfield => 20 },
8cfbd51c 58 title => 'fun time city!',
59 year => 'forevertime',
60 });
1bc006cf 61 ($raw_db_charfield) = $schema->resultset('Artist')
0f914ece 62 ->search ($cd->artist->ident_condition)
1bc006cf 63 ->get_column('charfield')
0f914ece 64 ->_resultset
65 ->cursor
66 ->next;
67
1bc006cf 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';
8cfbd51c 70}
71
c227b295 72CACHE_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
1bc006cf 82 $artist->charfield;
c227b295 83 cmp_ok (
1bc006cf 84 $artist->get_filtered_column('charfield'),
c227b295 85 '!=',
1bc006cf 86 $artist->get_column('charfield'),
c227b295 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
1bc006cf 92 $artist->charfield(6);
c227b295 93 is $from_storage_ran, $expected_from, 'from did not run';
94 is $to_storage_ran, ++$expected_to, 'to ran once';
95
1bc006cf 96 ok ($artist->is_column_changed ('charfield'), 'Column marked as dirty');
c227b295 97
1bc006cf 98 $artist->charfield;
c227b295 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
1bc006cf 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)');
c227b295 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
1bc006cf 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');
c227b295 113 is $from_storage_ran, $expected_from, 'from did not run';
dc6dadae 114 is $to_storage_ran, ++$expected_to, 'to did run once (call in to set_column)';
c227b295 115
1bc006cf 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');
491c8ff9 119 is $from_storage_ran, ++$expected_from, 'from did not run';
c227b295 120 is $to_storage_ran, $expected_to, 'to did not run';
121}
85439e0c 122
dc6dadae 123# test in-memory operations
124for my $artist_maker (
1bc006cf 125 sub { $schema->resultset('Artist')->new({ charfield => 42 }) },
126 sub { my $art = $schema->resultset('Artist')->new({}); $art->charfield(42); $art },
dc6dadae 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' );
1bc006cf 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' );
dc6dadae 141}
142
a524980e 143# test literals
144for my $v ( \ '16', \[ '?', '16' ] ) {
1bc006cf 145 my $rs = $schema->resultset('Artist');
146 $rs->delete;
a524980e 147
1bc006cf 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);
a524980e 154
155 $art->insert;
156 $art->discard_changes;
157
1bc006cf 158 is ($art->get_column("charfield"), 16, "Literal inserted into database properly");
159 is ($art->charfield, 32, "filtering still works");
a524980e 160
1bc006cf 161 $art->update({ charfield => $v });
a524980e 162
1bc006cf 163 is_deeply( $art->charfield, $v);
164 is_deeply( $art->get_filtered_column("charfield"), $v);
165 is_deeply( $art->get_column("charfield"), $v);
a524980e 166
167 $art->discard_changes;
168
1bc006cf 169 is ($art->get_column("charfield"), 16, "Literal inserted into database properly");
170 is ($art->charfield, 32, "filtering still works");
a524980e 171}
172
c227b295 173IC_DIE: {
85aee309 174 throws_ok {
1bc006cf 175 DBICTest::Schema::Artist->inflate_column(charfield =>
c227b295 176 { inflate => sub {}, deflate => sub {} }
177 );
85aee309 178 } qr/InflateColumn can not be used on a column with a declared FilterColumn filter/, q(Can't inflate column after filter column);
c227b295 179
180 DBICTest::Schema::Artist->inflate_column(name =>
181 { inflate => sub {}, deflate => sub {} }
182 );
183
85aee309 184 throws_ok {
c227b295 185 DBICTest::Schema::Artist->filter_column(name => {
186 filter_to_storage => sub {},
187 filter_from_storage => sub {}
188 });
85aee309 189 } qr/FilterColumn can not be used on a column with a declared InflateColumn inflator/, q(Can't filter column after inflate column);
cde96798 190}
85439e0c 191
eec182b6 192# test when we do not set both filter_from_storage/filter_to_storage
1bc006cf 193DBICTest::Schema::Artist->filter_column(charfield => {
eec182b6 194 filter_to_storage => sub { $to_storage_ran++; $_[1] },
195});
cc8ffd7b 196Class::C3->reinitialize() if DBIx::Class::_ENV_::OLD_MRO;
eec182b6 197
198ASYMMETRIC_TO_TEST: {
199 # initialise value
1bc006cf 200 $artist->charfield(20);
eec182b6 201 $artist->update;
202
203 my $expected_from = $from_storage_ran;
204 my $expected_to = $to_storage_ran;
205
1bc006cf 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');
eec182b6 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
1bc006cf 214 is ($artist->charfield, '20', 'Column set properly');
eec182b6 215 is $from_storage_ran, $expected_from, 'from did not run';
216 is $to_storage_ran, $expected_to, 'to did not run';
217}
218
1bc006cf 219DBICTest::Schema::Artist->filter_column(charfield => {
eec182b6 220 filter_from_storage => sub { $from_storage_ran++; $_[1] },
221});
cc8ffd7b 222Class::C3->reinitialize() if DBIx::Class::_ENV_::OLD_MRO;
eec182b6 223
224ASYMMETRIC_FROM_TEST: {
225 # initialise value
1bc006cf 226 $artist->charfield(23);
eec182b6 227 $artist->update;
228
229 my $expected_from = $from_storage_ran;
230 my $expected_to = $to_storage_ran;
231
1bc006cf 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');
eec182b6 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
1bc006cf 240 is ($artist->charfield, '23', 'Column set properly');
eec182b6 241 is $from_storage_ran, ++$expected_from, 'from did run';
242 is $to_storage_ran, $expected_to, 'to did not run';
243}
244
1bc006cf 245throws_ok { DBICTest::Schema::Artist->filter_column( charfield => {} ) }
eec182b6 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
cc2d2ead 250done_testing;