60c305d8ba6a3aba8dcc79d87555d86e1d5a0ef5
[dbsrgits/DBIx-Class.git] / t / row / filter_column.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use lib qw(t/lib);
6 use DBICTest;
7
8 my $from_storage_ran = 0;
9 my $to_storage_ran = 0;
10 my $schema = DBICTest->init_schema();
11 DBICTest::Schema::Artist->load_components('FilterColumn');
12 DBICTest::Schema::Artist->filter_column(rank => {
13   filter_from_storage => sub { $from_storage_ran++; $_[1] * 2 },
14   filter_to_storage   => sub { $to_storage_ran++; $_[1] / 2 },
15 });
16 Class::C3->reinitialize();
17
18 my $artist = $schema->resultset('Artist')->create( { rank => 20 } );
19
20 # this should be using the cursor directly, no inflation/processing of any sort
21 my ($raw_db_rank) = $schema->resultset('Artist')
22                              ->search ($artist->ident_condition)
23                                ->get_column('rank')
24                                 ->_resultset
25                                  ->cursor
26                                   ->next;
27
28 is ($raw_db_rank, 10, 'INSERT: correctly unfiltered on insertion');
29
30 for 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;
39 is( $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;
48 is ($raw_db_rank, 20, 'UPDATE: correctly unflitered on update');
49
50 $artist->discard_changes;
51 $artist->rank(40);
52 ok( !$artist->is_column_changed('rank'), 'column is not dirty after setting the same value' );
53
54 MC: {
55    my $cd = $schema->resultset('CD')->create({
56       artist => { rank => 20 },
57       title => 'fun time city!',
58       year => 'forevertime',
59    });
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';
69 }
70
71 my $initial_from = $from_storage_ran;
72 my $initial_to   = $to_storage_ran;
73
74 # ensure we are creating a fresh obj
75 $artist = $schema->resultset('Artist')->single($artist->ident_condition);
76
77 is $initial_from, $from_storage_ran, 'from has not run yet';
78 is $initial_from, $from_storage_ran, 'to has not run yet';
79
80 $artist->rank;
81 $artist->get_filtered_column('rank');
82 $artist->get_column('rank');
83
84 is $from_storage_ran, $initial_from + 1, 'from ran once, therefor caches';
85 is $to_storage_ran, $initial_to,  'to ran none';
86 $initial_from = $from_storage_ran;
87 $initial_to   = $to_storage_ran;
88
89 $artist->rank(1);
90
91 is $from_storage_ran, $initial_from, 'from ran none';
92 is $to_storage_ran, $initial_to + 1,  'to ran once';
93 $initial_from = $from_storage_ran;
94 $initial_to   = $to_storage_ran;
95
96 $artist->rank;
97
98 is $from_storage_ran, $initial_from + 1, 'from ran once';
99 is $to_storage_ran, $initial_to,  'to ran none';
100 $initial_from = $from_storage_ran;
101 $initial_to   = $to_storage_ran;
102
103 $artist->rank;
104
105 is $from_storage_ran, $initial_from, 'from ran none';
106 is $to_storage_ran, $initial_to,  'to ran none';
107 $initial_from = $from_storage_ran;
108 $initial_to   = $to_storage_ran;
109
110 $artist->set_column(rank => 1);
111 $artist->rank;
112
113 is $from_storage_ran, $initial_from + 1, 'from ran once (set column blows cache)';
114 is $to_storage_ran, $initial_to,  'to ran none';
115 $initial_from = $from_storage_ran;
116 $initial_to   = $to_storage_ran;
117
118 done_testing;