Some stylistic test changes in preparation for next commits
[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);
0567538f 72
b74b15b0 73{
74 my $page = $it->page(2);
0567538f 75
b74b15b0 76 is( $page->count, 2, "standard resultset paged rs count ok" );
0567538f 77
b74b15b0 78 is( $page->next->title, "Generic Manufactured Singles", "second page of standard resultset ok" );
79}
65245220 80
0567538f 81# test software-based limit paging
65245220 82$it = $rs->search(
0567538f 83 {},
84 { order_by => 'title',
85 rows => 3,
86 page => 2,
87 software_limit => 1 }
88);
89is( $it->pager->entries_on_this_page, 2, "software entries_on_this_page ok" );
90
91is( $it->pager->previous_page, 1, "software previous_page ok" );
92
93is( $it->count, 2, "software count on paged rs ok" );
94
95is( $it->next->title, "Generic Manufactured Singles", "software iterator->next ok" );
96
4d993a62 97# test paging with chained searches
65245220 98$it = $rs->search(
4d993a62 99 {},
100 { rows => 2,
101 page => 2 }
102)->search( undef, { order_by => 'title' } );
103
104is( $it->count, 2, "chained searches paging ok" );
e6c747fd 105
5b1c7d7f 106# test page with offset
65245220 107$it = $rs->search({}, {
5b1c7d7f 108 rows => 2,
109 page => 2,
110 offset => 1,
111 order_by => 'cdid'
112});
113
65245220 114my $row = $rs->search({}, {
8273e845 115 order_by => 'cdid',
5b1c7d7f 116 offset => 3,
117 rows => 1
118})->single;
119
120is($row->cdid, $it->first->cdid, 'page with offset');
89bddb49 121
65245220 122
123# test pager on non-title page behavior
124$qcnt = 0;
125$it = $rs->search({}, { rows => 3 })->page (2);
126ok ($it->pager);
127is ($qcnt, 0, 'No count on past-first-page pager instantiation');
128
129is ($it->pager->current_page, 2, 'Page set properby by $rs');
130is( $it->pager->total_entries, 5, 'total_entries correct' );
131
132$rs->create ({ artist => 1, title => 'MOAR!', year => 2010 });
133is( $it->count, 3, 'Dynamic count on filling up page' );
134$rs->create ({ artist => 1, title => 'MOAR!!!', year => 2011 });
135is( $it->count, 3, 'Count still correct (does not overflow' );
136
137$qcnt = 0;
138is( $it->pager->total_entries, 5, 'total_entries properly cached at old value' );
139is ($qcnt, 0, 'No queries');
140
141# test fresh pager with explicit total count assignment
142$qcnt = 0;
143$pager = $rs->search({}, { rows => 4 })->page (2)->pager;
144$pager->total_entries (13);
145
146is ($pager->current_page, 2, 'Correct start page');
147is ($pager->next_page, 3, 'One more page');
148is ($pager->last_page, 4, 'And one more page');
149is ($pager->previous_page, 1, 'One page in front');
150
151is ($qcnt, 0, 'No queries with explicitly sey total count');
152
153# test cached resultsets
154my $init_cnt = $rs->count;
155
74719352 156$it = $rs->search({}, { rows => 3, cache => 1 })->page(2);
157is ($it->count, 3, '3 rows');
158is (scalar $it->all, 3, '3 objects');
159
160isa_ok($it->pager,'Data::Page','Get a pager back ok');
161is($it->pager->total_entries,7);
162is($it->pager->current_page,2);
163is($it->pager->entries_on_this_page,3);
164
165$it = $it->page(3);
65245220 166is ($it->count, 1, 'One row');
167is (scalar $it->all, 1, 'One object');
168
74719352 169isa_ok($it->pager,'Data::Page','Get a pager back ok');
170is($it->pager->total_entries,7);
171is($it->pager->current_page,3);
172is($it->pager->entries_on_this_page,1);
173
174
65245220 175$it->delete;
176is ($rs->count, $init_cnt - 1, 'One row deleted as expected');
177
178is ($it->count, 1, 'One row (cached)');
179is (scalar $it->all, 1, 'One object (cached)');
180
65245220 181# test fresh rs creation with modified defaults
182my $p = sub { $schema->resultset('CD')->page(1)->pager->entries_per_page; };
183
184is($p->(), 10, 'default rows is 10');
185
186$schema->default_resultset_attributes({ rows => 5 });
187
188is($p->(), 5, 'default rows is 5');
189
cd122820 190# does serialization work (preserve laziness, while preserving state if exits)
191$qcnt = 0;
192$it = $rs->search(
193 {},
194 { order_by => 'title',
195 rows => 5,
196 page => 2 }
197);
198$pager = $it->pager;
199is ($qcnt, 0, 'No queries on rs/pager creation');
200
1c30a2e4 201# test *requires* it to be Storable
202$it = do {
203 local $DBIx::Class::ResultSourceHandle::thaw_schema = $schema;
204 Storable::dclone ($it);
205};
cd122820 206is ($qcnt, 0, 'No queries on rs/pager freeze/thaw');
207
208is( $it->pager->entries_on_this_page, 1, "entries_on_this_page ok for page 2" );
209
210is ($qcnt, 1, 'Count fired to get pager page entries');
211
212$rs->create({ title => 'bah', artist => 1, year => 2011 });
213
214$qcnt = 0;
1c30a2e4 215# test *requires* it to be Storable
216$it = do {
217 local $DBIx::Class::ResultSourceHandle::thaw_schema = $schema;
218 Storable::dclone ($it);
219};
cd122820 220is ($qcnt, 0, 'No queries on rs/pager freeze/thaw');
221
222is( $it->pager->entries_on_this_page, 1, "entries_on_this_page ok for page 2, even though underlying count changed" );
223
224is ($qcnt, 0, 'No count fired on pre-existing total count');
65245220 225
89bddb49 226done_testing;