Restore ability to handle underdefined root (t/prefetch/incomplete.t)
[dbsrgits/DBIx-Class.git] / t / prefetch / lazy_cursor.t
CommitLineData
4e9fc3f3 1use strict;
2use warnings;
3
4use Test::More;
5use Test::Exception;
6use lib qw(t/lib);
7use DBICTest;
8
9my $schema = DBICTest->init_schema();
10
11my $rs = $schema->resultset('Artist')->search({}, {
12 select => 'artistid',
13 prefetch => { cds => 'tracks' },
14});
15
16my $initial_artists_cnt = $rs->count;
17
18# create one extra artist with just one cd with just one track
19# and then an artist with nothing at all
20# the implicit order by me.artistid will get them back in correct order
21$rs->create({
22 name => 'foo',
23 cds => [{
24 year => 2012,
25 title => 'foocd',
26 tracks => [{
27 title => 'footrack',
28 }]
29 }],
30});
31$rs->create({ name => 'bar' });
32$rs->create({ name => 'baz' });
33
34# make sure we are reentrant, and also check with explicit order_by
35for (undef, undef, 'me.artistid') {
36 $rs = $rs->search({}, { order_by => $_ }) if $_;
37
38 for (1 .. $initial_artists_cnt) {
39 is ($rs->next->artistid, $_, 'Default fixture artists in order') || exit;
40 }
41
42 my $foo_artist = $rs->next;
43 is ($foo_artist->cds->next->tracks->next->title, 'footrack', 'Right track');
44
45 is (
46 [$rs->cursor->next]->[0],
47 $initial_artists_cnt + 3,
48 'Very last artist still on the cursor'
49 );
50
51 is_deeply ([$rs->cursor->next], [], 'Nothing else left');
52
53 is ($rs->next->artistid, $initial_artists_cnt + 2, 'Row stashed in resultset still accessible');
54 is ($rs->next, undef, 'Nothing left in resultset either');
55
56 $rs->reset;
57}
58
59$rs->next;
60
61my @objs = $rs->all;
62is (@objs, $initial_artists_cnt + 3, '->all resets everything correctly');
63is ( ($rs->cursor->next)[0], 1, 'Cursor auto-rewound after all()');
64is ($rs->{stashed_rows}, undef, 'Nothing else left in $rs stash');
65
66my $unordered_rs = $rs->search({}, { order_by => 'cds.title' });
67ok ($unordered_rs->next, 'got row 1');
68is_deeply ([$unordered_rs->cursor->next], [], 'Nothing left on cursor, eager slurp');
69ok ($unordered_rs->next, "got row $_") for (2 .. $initial_artists_cnt + 3);
70is ($unordered_rs->next, undef, 'End of RS reached');
71is ($unordered_rs->next, undef, 'End of RS not lost');
72
73done_testing;