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