Remove the transparrent hook lazy-pager-count experiment
[dbsrgits/DBIx-Class.git] / t / 67pager.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use Test::Exception;
6 use lib qw(t/lib);
7 use DBICTest;
8 use Storable qw/dclone/;
9
10 my $schema = DBICTest->init_schema();
11
12 is ($schema->resultset("CD")->count, 5, 'Initial count sanity check');
13
14 my $qcnt;
15 $schema->storage->debugcb(sub { $qcnt++ });
16 $schema->storage->debug (1);
17
18 my $rs = $schema->resultset("CD");
19
20 # first page
21 $qcnt = 0;
22 my $it = $rs->search(
23     {},
24     { order_by => 'title',
25       rows => 3,
26       page => 1 }
27 );
28 my $pager = $it->pager;
29 is ($qcnt, 0, 'No queries on rs/pager creation');
30
31 is ($pager->entries_per_page, 3, 'Pager created with correct entries_per_page');
32 ok ($pager->current_page(-1), 'Set nonexistent page');
33 is ($pager->current_page, 1, 'Page set behaves correctly');
34 ok ($pager->current_page(2), 'Set 2nd page');
35
36 is ($qcnt, 0, 'No queries on total_count-independent methods');
37
38 is( $it->pager->entries_on_this_page, 2, "entries_on_this_page ok for page 2" );
39
40 is ($qcnt, 1, 'Count fired to get pager page entries');
41
42 $qcnt = 0;
43 is ($pager->previous_page, 1, 'Correct previous_page');
44 is ($pager->next_page, undef, 'No more pages');
45 is ($qcnt, 0, 'No more counts - amount of entries cached in pager');
46
47 is( $it->count, 3, "count on paged rs ok" );
48 is ($qcnt, 1, 'An $rs->count still fires properly');
49
50 is( $it->next->title, "Caterwaulin' Blues", "iterator->next ok" );
51
52 $it->next;
53 $it->next;
54
55 is( $it->next, undef, "next past end of page ok" );
56
57
58 # second page, testing with array
59 my @page2 = $rs->search(
60     {},
61     { order_by => 'title',
62       rows => 3,
63       page => 2 }
64 );
65
66 is( $page2[0]->title, "Generic Manufactured Singles", "second page first title ok" );
67
68 # page a standard resultset
69 $it = $rs->search(
70   {},
71   { order_by => 'title',
72     rows => 3 }
73 );
74 my $page = $it->page(2);
75
76 is( $page->count, 2, "standard resultset paged rs count ok" );
77
78 is( $page->next->title, "Generic Manufactured Singles", "second page of standard resultset ok" );
79
80
81 # test software-based limit paging
82 $it = $rs->search(
83   {},
84   { order_by => 'title',
85     rows => 3,
86     page => 2,
87     software_limit => 1 }
88 );
89 is( $it->pager->entries_on_this_page, 2, "software entries_on_this_page ok" );
90
91 is( $it->pager->previous_page, 1, "software previous_page ok" );
92
93 is( $it->count, 2, "software count on paged rs ok" );
94
95 is( $it->next->title, "Generic Manufactured Singles", "software iterator->next ok" );
96
97 # test paging with chained searches
98 $it = $rs->search(
99     {},
100     { rows => 2,
101       page => 2 }
102 )->search( undef, { order_by => 'title' } );
103
104 is( $it->count, 2, "chained searches paging ok" );
105
106 # test page with offset
107 $it = $rs->search({}, {
108     rows => 2,
109     page => 2,
110     offset => 1,
111     order_by => 'cdid'
112 });
113
114 my $row = $rs->search({}, {
115     order_by => 'cdid', 
116     offset => 3,
117     rows => 1
118 })->single;
119
120 is($row->cdid, $it->first->cdid, 'page with offset');
121
122
123 # test pager on non-title page behavior
124 $qcnt = 0;
125 $it = $rs->search({}, { rows => 3 })->page (2);
126 ok ($it->pager);
127 is ($qcnt, 0, 'No count on past-first-page pager instantiation');
128
129 is ($it->pager->current_page, 2, 'Page set properby by $rs');
130 is( $it->pager->total_entries, 5, 'total_entries correct' );
131
132 $rs->create ({ artist => 1, title => 'MOAR!', year => 2010 });
133 is( $it->count, 3, 'Dynamic count on filling up page' );
134 $rs->create ({ artist => 1, title => 'MOAR!!!', year => 2011 });
135 is( $it->count, 3, 'Count still correct (does not overflow' );
136
137 $qcnt = 0;
138 is( $it->pager->total_entries, 5, 'total_entries properly cached at old value' );
139 is ($qcnt, 0, 'No queries');
140
141 # test fresh pager with explicit total count assignment
142 $qcnt = 0;
143 $pager = $rs->search({}, { rows => 4 })->page (2)->pager;
144 $pager->total_entries (13);
145
146 is ($pager->current_page, 2, 'Correct start page');
147 is ($pager->next_page, 3, 'One more page');
148 is ($pager->last_page, 4, 'And one more page');
149 is ($pager->previous_page, 1, 'One page in front');
150
151 is ($qcnt, 0, 'No queries with explicitly sey total count');
152
153 # test cached resultsets
154 my $init_cnt = $rs->count;
155
156 $it = $rs->search({}, { rows => 3, cache => 1 })->page(2);
157 is ($it->count, 3, '3 rows');
158 is (scalar $it->all, 3, '3 objects');
159
160 isa_ok($it->pager,'Data::Page','Get a pager back ok');
161 is($it->pager->total_entries,7);
162 is($it->pager->current_page,2);
163 is($it->pager->entries_on_this_page,3);
164
165 $it = $it->page(3);
166 is ($it->count, 1, 'One row');
167 is (scalar $it->all, 1, 'One object');
168
169 isa_ok($it->pager,'Data::Page','Get a pager back ok');
170 is($it->pager->total_entries,7);
171 is($it->pager->current_page,3);
172 is($it->pager->entries_on_this_page,1);
173
174
175 $it->delete;
176 is ($rs->count, $init_cnt - 1, 'One row deleted as expected');
177
178 is ($it->count, 1, 'One row (cached)');
179 is (scalar $it->all, 1, 'One object (cached)');
180
181 # test fresh rs creation with modified defaults
182 my $p = sub { $schema->resultset('CD')->page(1)->pager->entries_per_page; };
183
184 is($p->(), 10, 'default rows is 10');
185
186 $schema->default_resultset_attributes({ rows => 5 });
187
188 is($p->(), 5, 'default rows is 5');
189
190 # does serialization work (preserve laziness, while preserving state if exits)
191 $qcnt = 0;
192 $it = $rs->search(
193     {},
194     { order_by => 'title',
195       rows => 5,
196       page => 2 }
197 );
198 $pager = $it->pager;
199 is ($qcnt, 0, 'No queries on rs/pager creation');
200
201 $it = do { local $DBIx::Class::ResultSourceHandle::thaw_schema = $schema; dclone ($it) };
202 is ($qcnt, 0, 'No queries on rs/pager freeze/thaw');
203
204 is( $it->pager->entries_on_this_page, 1, "entries_on_this_page ok for page 2" );
205
206 is ($qcnt, 1, 'Count fired to get pager page entries');
207
208 $rs->create({ title => 'bah', artist => 1, year => 2011 });
209
210 $qcnt = 0;
211 $it = do { local $DBIx::Class::ResultSourceHandle::thaw_schema = $schema; dclone ($it) };
212 is ($qcnt, 0, 'No queries on rs/pager freeze/thaw');
213
214 is( $it->pager->entries_on_this_page, 1, "entries_on_this_page ok for page 2, even though underlying count changed" );
215
216 is ($qcnt, 0, 'No count fired on pre-existing total count');
217
218 done_testing;