Play nicer with lower-level methods
[dbsrgits/DBIx-Class-Historic.git] / t / row / filter_column.t
CommitLineData
956f4141 1use strict;
2use warnings;
3
4use Test::More;
956f4141 5use lib qw(t/lib);
6use DBICTest;
cc2d2ead 7
85439e0c 8my $from_storage_ran = 0;
9my $to_storage_ran = 0;
cc2d2ead 10my $schema = DBICTest->init_schema();
11DBICTest::Schema::Artist->load_components('FilterColumn');
12DBICTest::Schema::Artist->filter_column(rank => {
85439e0c 13 filter_from_storage => sub { $from_storage_ran++; $_[1] * 2 },
14 filter_to_storage => sub { $to_storage_ran++; $_[1] / 2 },
cc2d2ead 15});
16Class::C3->reinitialize();
17
18my $artist = $schema->resultset('Artist')->create( { rank => 20 } );
19
20# this should be using the cursor directly, no inflation/processing of any sort
21my ($raw_db_rank) = $schema->resultset('Artist')
22 ->search ($artist->ident_condition)
23 ->get_column('rank')
24 ->_resultset
25 ->cursor
26 ->next;
27
28is ($raw_db_rank, 10, 'INSERT: correctly unfiltered on insertion');
29
30for my $reloaded (0, 1) {
31 my $test = $reloaded ? 'reloaded' : 'stored';
32 $artist->discard_changes if $reloaded;
33
34 is( $artist->rank , 20, "got $test filtered rank" );
35}
36
37$artist->update;
38$artist->discard_changes;
39is( $artist->rank , 20, "got filtered rank" );
40
41$artist->update ({ rank => 40 });
42($raw_db_rank) = $schema->resultset('Artist')
43 ->search ($artist->ident_condition)
44 ->get_column('rank')
45 ->_resultset
46 ->cursor
47 ->next;
48is ($raw_db_rank, 20, 'UPDATE: correctly unflitered on update');
49
50$artist->discard_changes;
51$artist->rank(40);
52ok( !$artist->is_column_changed('rank'), 'column is not dirty after setting the same value' );
53
8cfbd51c 54MC: {
55 my $cd = $schema->resultset('CD')->create({
56 artist => { rank => 20 },
57 title => 'fun time city!',
58 year => 'forevertime',
59 });
0f914ece 60 ($raw_db_rank) = $schema->resultset('Artist')
61 ->search ($cd->artist->ident_condition)
62 ->get_column('rank')
63 ->_resultset
64 ->cursor
65 ->next;
66
67 is $raw_db_rank, 10, 'artist rank gets correctly unfiltered w/ MC';
68 is $cd->artist->rank, 20, 'artist rank gets correctly filtered w/ MC';
8cfbd51c 69}
70
cde96798 71my $expected_from = $from_storage_ran;
72my $expected_to = $to_storage_ran;
85439e0c 73
74# ensure we are creating a fresh obj
75$artist = $schema->resultset('Artist')->single($artist->ident_condition);
76
cde96798 77is $from_storage_ran, $expected_from, 'from has not run yet';
78is $to_storage_ran, $expected_to, 'to has not run yet';
85439e0c 79
80$artist->rank;
cde96798 81cmp_ok (
82 $artist->get_filtered_column('rank'),
83 '!=',
84 $artist->get_column('rank'),
85 'filter/unfilter differ'
86);
87is $from_storage_ran, ++$expected_from, 'from ran once, therefor caches';
88is $to_storage_ran, $expected_to, 'to did not run';
85439e0c 89
cde96798 90$artist->rank(6);
91is $from_storage_ran, $expected_from, 'from did not run';
92is $to_storage_ran, ++$expected_to, 'to ran once';
85439e0c 93
cde96798 94ok ($artist->is_column_changed ('rank'), 'Column marked as dirty');
85439e0c 95
96$artist->rank;
cde96798 97is $from_storage_ran, ++$expected_from, 'from ran once';
98is $to_storage_ran, $expected_to, 'to did not run';
85439e0c 99
100$artist->rank;
cde96798 101is $from_storage_ran, $expected_from, 'from did not run';
102is $to_storage_ran, $expected_to, 'to did not run';
85439e0c 103
cde96798 104$artist->update;
85439e0c 105
cde96798 106$artist->set_column(rank => 3);
107ok (! $artist->is_column_changed ('rank'), 'Column not marked as dirty on same set_column value');
108is ($artist->rank, '6', 'Column set properly (cache blown)');
109is $from_storage_ran, ++$expected_from, 'from ran once (set_column blew cache)';
110is $to_storage_ran, $expected_to, 'to did not run';
111
112$artist->rank(6);
113ok (! $artist->is_column_changed ('rank'), 'Column not marked as dirty on same accessor-set value');
114is ($artist->rank, '6', 'Column set properly');
115is $from_storage_ran, $expected_from, 'from did not run';
116is $to_storage_ran, $expected_to, 'to did not run';
117
118$artist->store_column(rank => 4);
119ok (! $artist->is_column_changed ('rank'), 'Column not marked as dirty on differing store_column value');
120is ($artist->rank, '6', 'Filtered column still contains old value (cache not blown)');
121is $from_storage_ran, $expected_from, 'from did not run';
122is $to_storage_ran, $expected_to, 'to did not run';
123
124$artist->set_column(rank => 4);
125TODO: {
126 local $TODO = 'There seems to be no way around that much wizardry... which is ok';
127 ok ($artist->is_column_changed ('rank'), 'Column marked as dirty on out-of-sync set_column value');
128}
129is ($artist->rank, '8', 'Column set properly (cache blown)');
130is $from_storage_ran, ++$expected_from, 'from ran once (set_column blew cache)';
131is $to_storage_ran, $expected_to, 'to did not run';
85439e0c 132
cc2d2ead 133done_testing;