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