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