1 BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
10 my $schema = DBICTest->init_schema();
12 is ($schema->resultset("CD")->count, 5, 'Initial count sanity check');
15 $schema->storage->debugcb(sub { $qcnt++ });
16 $schema->storage->debug (1);
18 my $rs = $schema->resultset("CD");
24 { order_by => 'title',
28 my $pager = $it->pager;
29 is ($qcnt, 0, 'No queries on rs/pager creation');
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');
36 is ($qcnt, 0, 'No queries on total_count-independent methods');
38 is( $it->pager->entries_on_this_page, 2, "entries_on_this_page ok for page 2" );
40 is ($qcnt, 1, 'Count fired to get pager page entries');
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');
47 is( $it->count, 3, "count on paged rs ok" );
48 is ($qcnt, 1, 'An $rs->count still fires properly');
50 is( $it->next->title, "Caterwaulin' Blues", "iterator->next ok" );
55 is( $it->next, undef, "next past end of page ok" );
58 # second page, testing with array
59 my @page2 = $rs->search(
61 { order_by => 'title',
66 is( $page2[0]->title, "Generic Manufactured Singles", "second page first title ok" );
68 # page a standard resultset
71 { order_by => 'title',
76 my $page = $it->page(2);
78 is( $page->count, 2, "standard resultset paged rs count ok" );
80 is( $page->next->title, "Generic Manufactured Singles", "second page of standard resultset ok" );
83 # test software-based limit paging
86 { order_by => 'title',
91 is( $it->pager->entries_on_this_page, 2, "software entries_on_this_page ok" );
93 is( $it->pager->previous_page, 1, "software previous_page ok" );
95 is( $it->count, 2, "software count on paged rs ok" );
97 is( $it->next->title, "Generic Manufactured Singles", "software iterator->next ok" );
99 # test paging with chained searches
104 )->search( undef, { order_by => 'title' } );
106 is( $it->count, 2, "chained searches paging ok" );
108 # test page with offset
109 $it = $rs->search({}, {
116 my $row = $rs->search({}, {
122 is($row->cdid, $it->first->cdid, 'page with offset');
125 # test pager on non-title page behavior
127 $it = $rs->search({}, { rows => 3 })->page (2);
129 is ($qcnt, 0, 'No count on past-first-page pager instantiation');
131 is ($it->pager->current_page, 2, 'Page set properby by $rs');
132 is( $it->pager->total_entries, 5, 'total_entries correct' );
134 $rs->create ({ artist => 1, title => 'MOAR!', year => 2010 });
135 is( $it->count, 3, 'Dynamic count on filling up page' );
136 $rs->create ({ artist => 1, title => 'MOAR!!!', year => 2011 });
137 is( $it->count, 3, 'Count still correct (does not overflow' );
140 is( $it->pager->total_entries, 5, 'total_entries properly cached at old value' );
141 is ($qcnt, 0, 'No queries');
143 # test fresh pager with explicit total count assignment
145 $pager = $rs->search({}, { rows => 4 })->page (2)->pager;
146 $pager->total_entries (13);
148 is ($pager->current_page, 2, 'Correct start page');
149 is ($pager->next_page, 3, 'One more page');
150 is ($pager->last_page, 4, 'And one more page');
151 is ($pager->previous_page, 1, 'One page in front');
153 is ($qcnt, 0, 'No queries with explicitly sey total count');
155 # test cached resultsets
156 my $init_cnt = $rs->count;
158 $it = $rs->search({}, { rows => 3, cache => 1 })->page(2);
159 is ($it->count, 3, '3 rows');
160 is (scalar $it->all, 3, '3 objects');
162 isa_ok($it->pager,'Data::Page','Get a pager back ok');
163 is($it->pager->total_entries,7);
164 is($it->pager->current_page,2);
165 is($it->pager->entries_on_this_page,3);
168 is ($it->count, 1, 'One row');
169 is (scalar $it->all, 1, 'One object');
171 isa_ok($it->pager,'Data::Page','Get a pager back ok');
172 is($it->pager->total_entries,7);
173 is($it->pager->current_page,3);
174 is($it->pager->entries_on_this_page,1);
178 is ($rs->count, $init_cnt - 1, 'One row deleted as expected');
180 is ($it->count, 1, 'One row (cached)');
181 is (scalar $it->all, 1, 'One object (cached)');
183 # test fresh rs creation with modified defaults
184 my $p = sub { $schema->resultset('CD')->page(1)->pager->entries_per_page; };
186 is($p->(), 10, 'default rows is 10');
188 $schema->default_resultset_attributes({ rows => 5 });
190 is($p->(), 5, 'default rows is 5');
192 # does serialization work (preserve laziness, while preserving state if exits)
196 { order_by => 'title',
201 is ($qcnt, 0, 'No queries on rs/pager creation');
203 # test *requires* it to be Storable
205 local $DBIx::Class::ResultSourceHandle::thaw_schema = $schema;
206 Storable::dclone ($it);
208 is ($qcnt, 0, 'No queries on rs/pager freeze/thaw');
210 is( $it->pager->entries_on_this_page, 1, "entries_on_this_page ok for page 2" );
212 is ($qcnt, 1, 'Count fired to get pager page entries');
214 $rs->create({ title => 'bah', artist => 1, year => 2011 });
217 # test *requires* it to be Storable
219 local $DBIx::Class::ResultSourceHandle::thaw_schema = $schema;
220 Storable::dclone ($it);
222 is ($qcnt, 0, 'No queries on rs/pager freeze/thaw');
224 is( $it->pager->entries_on_this_page, 1, "entries_on_this_page ok for page 2, even though underlying count changed" );
226 is ($qcnt, 0, 'No count fired on pre-existing total count');