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