7 use Storable qw/dclone/;
9 my $schema = DBICTest->init_schema();
11 is ($schema->resultset("CD")->count, 5, 'Initial count sanity check');
14 $schema->storage->debugcb(sub { $qcnt++ });
15 $schema->storage->debug (1);
17 my $rs = $schema->resultset("CD");
23 { order_by => 'title',
27 my $pager = $it->pager;
28 is ($qcnt, 0, 'No queries on rs/pager creation');
30 is ($pager->entries_per_page, 3, 'Pager created with correct entries_per_page');
31 ok ($pager->current_page(-1), 'Set nonexistent page');
32 is ($pager->current_page, 1, 'Page set behaves correctly');
33 ok ($pager->current_page(2), 'Set 2nd page');
35 is ($qcnt, 0, 'No queries on total_count-independent methods');
37 is( $it->pager->entries_on_this_page, 2, "entries_on_this_page ok for page 2" );
39 is ($qcnt, 1, 'Count fired to get pager page entries');
42 is ($pager->previous_page, 1, 'Correct previous_page');
43 is ($pager->next_page, undef, 'No more pages');
44 is ($qcnt, 0, 'No more counts - amount of entries cached in pager');
46 is( $it->count, 3, "count on paged rs ok" );
47 is ($qcnt, 1, 'An $rs->count still fires properly');
49 is( $it->next->title, "Caterwaulin' Blues", "iterator->next ok" );
54 is( $it->next, undef, "next past end of page ok" );
57 # second page, testing with array
58 my @page2 = $rs->search(
60 { order_by => 'title',
65 is( $page2[0]->title, "Generic Manufactured Singles", "second page first title ok" );
67 # page a standard resultset
70 { order_by => 'title',
73 my $page = $it->page(2);
75 is( $page->count, 2, "standard resultset paged rs count ok" );
77 is( $page->next->title, "Generic Manufactured Singles", "second page of standard resultset ok" );
80 # test software-based limit paging
83 { order_by => 'title',
88 is( $it->pager->entries_on_this_page, 2, "software entries_on_this_page ok" );
90 is( $it->pager->previous_page, 1, "software previous_page ok" );
92 is( $it->count, 2, "software count on paged rs ok" );
94 is( $it->next->title, "Generic Manufactured Singles", "software iterator->next ok" );
96 # test paging with chained searches
101 )->search( undef, { order_by => 'title' } );
103 is( $it->count, 2, "chained searches paging ok" );
105 # test page with offset
106 $it = $rs->search({}, {
113 my $row = $rs->search({}, {
119 is($row->cdid, $it->first->cdid, 'page with offset');
122 # test pager on non-title page behavior
124 $it = $rs->search({}, { rows => 3 })->page (2);
126 is ($qcnt, 0, 'No count on past-first-page pager instantiation');
128 is ($it->pager->current_page, 2, 'Page set properby by $rs');
129 is( $it->pager->total_entries, 5, 'total_entries correct' );
131 $rs->create ({ artist => 1, title => 'MOAR!', year => 2010 });
132 is( $it->count, 3, 'Dynamic count on filling up page' );
133 $rs->create ({ artist => 1, title => 'MOAR!!!', year => 2011 });
134 is( $it->count, 3, 'Count still correct (does not overflow' );
137 is( $it->pager->total_entries, 5, 'total_entries properly cached at old value' );
138 is ($qcnt, 0, 'No queries');
140 # test fresh pager with explicit total count assignment
142 $pager = $rs->search({}, { rows => 4 })->page (2)->pager;
143 $pager->total_entries (13);
145 is ($pager->current_page, 2, 'Correct start page');
146 is ($pager->next_page, 3, 'One more page');
147 is ($pager->last_page, 4, 'And one more page');
148 is ($pager->previous_page, 1, 'One page in front');
150 is ($qcnt, 0, 'No queries with explicitly sey total count');
152 # test cached resultsets
153 my $init_cnt = $rs->count;
155 $it = $rs->search({}, { rows => 3, cache => 1 })->page(2);
156 is ($it->count, 3, '3 rows');
157 is (scalar $it->all, 3, '3 objects');
159 isa_ok($it->pager,'Data::Page','Get a pager back ok');
160 is($it->pager->total_entries,7);
161 is($it->pager->current_page,2);
162 is($it->pager->entries_on_this_page,3);
165 is ($it->count, 1, 'One row');
166 is (scalar $it->all, 1, 'One object');
168 isa_ok($it->pager,'Data::Page','Get a pager back ok');
169 is($it->pager->total_entries,7);
170 is($it->pager->current_page,3);
171 is($it->pager->entries_on_this_page,1);
175 is ($rs->count, $init_cnt - 1, 'One row deleted as expected');
177 is ($it->count, 1, 'One row (cached)');
178 is (scalar $it->all, 1, 'One object (cached)');
180 # test fresh rs creation with modified defaults
181 my $p = sub { $schema->resultset('CD')->page(1)->pager->entries_per_page; };
183 is($p->(), 10, 'default rows is 10');
185 $schema->default_resultset_attributes({ rows => 5 });
187 is($p->(), 5, 'default rows is 5');
189 # does serialization work (preserve laziness, while preserving state if exits)
193 { order_by => 'title',
198 is ($qcnt, 0, 'No queries on rs/pager creation');
200 $it = do { local $DBIx::Class::ResultSourceHandle::thaw_schema = $schema; dclone ($it) };
201 is ($qcnt, 0, 'No queries on rs/pager freeze/thaw');
203 is( $it->pager->entries_on_this_page, 1, "entries_on_this_page ok for page 2" );
205 is ($qcnt, 1, 'Count fired to get pager page entries');
207 $rs->create({ title => 'bah', artist => 1, year => 2011 });
210 $it = do { local $DBIx::Class::ResultSourceHandle::thaw_schema = $schema; dclone ($it) };
211 is ($qcnt, 0, 'No queries on rs/pager freeze/thaw');
213 is( $it->pager->entries_on_this_page, 1, "entries_on_this_page ok for page 2, even though underlying count changed" );
215 is ($qcnt, 0, 'No count fired on pre-existing total count');