8 my $schema = DBICTest->init_schema();
10 is ($schema->resultset("CD")->count, 5, 'Initial count sanity check');
13 $schema->storage->debugcb(sub { $qcnt++ });
14 $schema->storage->debug (1);
16 my $rs = $schema->resultset("CD");
22 { order_by => 'title',
26 my $pager = $it->pager;
27 is ($qcnt, 0, 'No queries on rs/pager creation');
29 is ($pager->entries_per_page, 3, 'Pager created with correct entries_per_page');
30 ok ($pager->current_page(-1), 'Set nonexistent page');
31 is ($pager->current_page, 1, 'Page set behaves correctly');
32 ok ($pager->current_page(2), 'Set 2nd page');
34 is ($qcnt, 0, 'No queries on total_count-independent methods');
36 is( $it->pager->entries_on_this_page, 2, "entries_on_this_page ok for page 2" );
38 is ($qcnt, 1, 'Count fired to get pager page entries');
41 is ($pager->previous_page, 1, 'Correct previous_page');
42 is ($pager->next_page, undef, 'No more pages');
43 is ($qcnt, 0, 'No more counts - amount of entries cached in pager');
45 is( $it->count, 3, "count on paged rs ok" );
46 is ($qcnt, 1, 'An $rs->count still fires properly');
48 is( $it->next->title, "Caterwaulin' Blues", "iterator->next ok" );
53 is( $it->next, undef, "next past end of page ok" );
56 # second page, testing with array
57 my @page2 = $rs->search(
59 { order_by => 'title',
64 is( $page2[0]->title, "Generic Manufactured Singles", "second page first title ok" );
66 # page a standard resultset
69 { order_by => 'title',
72 my $page = $it->page(2);
74 is( $page->count, 2, "standard resultset paged rs count ok" );
76 is( $page->next->title, "Generic Manufactured Singles", "second page of standard resultset ok" );
79 # test software-based limit paging
82 { order_by => 'title',
87 is( $it->pager->entries_on_this_page, 2, "software entries_on_this_page ok" );
89 is( $it->pager->previous_page, 1, "software previous_page ok" );
91 is( $it->count, 2, "software count on paged rs ok" );
93 is( $it->next->title, "Generic Manufactured Singles", "software iterator->next ok" );
95 # test paging with chained searches
100 )->search( undef, { order_by => 'title' } );
102 is( $it->count, 2, "chained searches paging ok" );
104 # test page with offset
105 $it = $rs->search({}, {
112 my $row = $rs->search({}, {
118 is($row->cdid, $it->first->cdid, 'page with offset');
121 # test pager on non-title page behavior
123 $it = $rs->search({}, { rows => 3 })->page (2);
125 is ($qcnt, 0, 'No count on past-first-page pager instantiation');
127 is ($it->pager->current_page, 2, 'Page set properby by $rs');
128 is( $it->pager->total_entries, 5, 'total_entries correct' );
130 $rs->create ({ artist => 1, title => 'MOAR!', year => 2010 });
131 is( $it->count, 3, 'Dynamic count on filling up page' );
132 $rs->create ({ artist => 1, title => 'MOAR!!!', year => 2011 });
133 is( $it->count, 3, 'Count still correct (does not overflow' );
136 is( $it->pager->total_entries, 5, 'total_entries properly cached at old value' );
137 is ($qcnt, 0, 'No queries');
139 # test fresh pager with explicit total count assignment
141 $pager = $rs->search({}, { rows => 4 })->page (2)->pager;
142 $pager->total_entries (13);
144 is ($pager->current_page, 2, 'Correct start page');
145 is ($pager->next_page, 3, 'One more page');
146 is ($pager->last_page, 4, 'And one more page');
147 is ($pager->previous_page, 1, 'One page in front');
149 is ($qcnt, 0, 'No queries with explicitly sey total count');
151 # test cached resultsets
152 my $init_cnt = $rs->count;
154 $it = $rs->search({}, { rows => 3, cache => 1 })->page(2);
155 is ($it->count, 3, '3 rows');
156 is (scalar $it->all, 3, '3 objects');
158 isa_ok($it->pager,'Data::Page','Get a pager back ok');
159 is($it->pager->total_entries,7);
160 is($it->pager->current_page,2);
161 is($it->pager->entries_on_this_page,3);
164 is ($it->count, 1, 'One row');
165 is (scalar $it->all, 1, 'One object');
167 isa_ok($it->pager,'Data::Page','Get a pager back ok');
168 is($it->pager->total_entries,7);
169 is($it->pager->current_page,3);
170 is($it->pager->entries_on_this_page,1);
174 is ($rs->count, $init_cnt - 1, 'One row deleted as expected');
176 is ($it->count, 1, 'One row (cached)');
177 is (scalar $it->all, 1, 'One object (cached)');
179 # test fresh rs creation with modified defaults
180 my $p = sub { $schema->resultset('CD')->page(1)->pager->entries_per_page; };
182 is($p->(), 10, 'default rows is 10');
184 $schema->default_resultset_attributes({ rows => 5 });
186 is($p->(), 5, 'default rows is 5');
188 # does serialization work (preserve laziness, while preserving state if exits)
192 { order_by => 'title',
197 is ($qcnt, 0, 'No queries on rs/pager creation');
199 # test *requires* it to be Storable
201 local $DBIx::Class::ResultSourceHandle::thaw_schema = $schema;
202 Storable::dclone ($it);
204 is ($qcnt, 0, 'No queries on rs/pager freeze/thaw');
206 is( $it->pager->entries_on_this_page, 1, "entries_on_this_page ok for page 2" );
208 is ($qcnt, 1, 'Count fired to get pager page entries');
210 $rs->create({ title => 'bah', artist => 1, year => 2011 });
213 # test *requires* it to be Storable
215 local $DBIx::Class::ResultSourceHandle::thaw_schema = $schema;
216 Storable::dclone ($it);
218 is ($qcnt, 0, 'No queries on rs/pager freeze/thaw');
220 is( $it->pager->entries_on_this_page, 1, "entries_on_this_page ok for page 2, even though underlying count changed" );
222 is ($qcnt, 0, 'No count fired on pre-existing total count');