First part of changes for better unexpected NULL reporting
[dbsrgits/DBIx-Class.git] / t / prefetch / lazy_cursor.t
CommitLineData
c0329273 1BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2
4e9fc3f3 3use strict;
4use warnings;
5
6use Test::More;
69e99ee6 7use Test::Warn;
4e9fc3f3 8use Test::Exception;
c0329273 9
4e9fc3f3 10use DBICTest;
11
12my $schema = DBICTest->init_schema();
13
14my $rs = $schema->resultset('Artist')->search({}, {
15 select => 'artistid',
16 prefetch => { cds => 'tracks' },
17});
18
19my $initial_artists_cnt = $rs->count;
20
21# create one extra artist with just one cd with just one track
22# and then an artist with nothing at all
23# the implicit order by me.artistid will get them back in correct order
24$rs->create({
25 name => 'foo',
26 cds => [{
27 year => 2012,
28 title => 'foocd',
29 tracks => [{
30 title => 'footrack',
31 }]
32 }],
33});
34$rs->create({ name => 'bar' });
35$rs->create({ name => 'baz' });
36
37# make sure we are reentrant, and also check with explicit order_by
38for (undef, undef, 'me.artistid') {
39 $rs = $rs->search({}, { order_by => $_ }) if $_;
40
41 for (1 .. $initial_artists_cnt) {
42 is ($rs->next->artistid, $_, 'Default fixture artists in order') || exit;
43 }
44
45 my $foo_artist = $rs->next;
46 is ($foo_artist->cds->next->tracks->next->title, 'footrack', 'Right track');
47
48 is (
49 [$rs->cursor->next]->[0],
50 $initial_artists_cnt + 3,
51 'Very last artist still on the cursor'
52 );
53
54 is_deeply ([$rs->cursor->next], [], 'Nothing else left');
55
56 is ($rs->next->artistid, $initial_artists_cnt + 2, 'Row stashed in resultset still accessible');
57 is ($rs->next, undef, 'Nothing left in resultset either');
58
59 $rs->reset;
60}
61
62$rs->next;
63
64my @objs = $rs->all;
65is (@objs, $initial_artists_cnt + 3, '->all resets everything correctly');
66is ( ($rs->cursor->next)[0], 1, 'Cursor auto-rewound after all()');
5ff6d603 67ok (! @{ $rs->{_stashed_rows} || [] }, 'Nothing else left in $rs stash');
4e9fc3f3 68
69my $unordered_rs = $rs->search({}, { order_by => 'cds.title' });
69e99ee6 70
71warnings_exist {
72 ok ($unordered_rs->next, 'got row 1');
73} qr/performed an eager cursor slurp underneath/, 'Warned on auto-eager cursor';
74
4e9fc3f3 75is_deeply ([$unordered_rs->cursor->next], [], 'Nothing left on cursor, eager slurp');
76ok ($unordered_rs->next, "got row $_") for (2 .. $initial_artists_cnt + 3);
77is ($unordered_rs->next, undef, 'End of RS reached');
78is ($unordered_rs->next, undef, 'End of RS not lost');
79
0e81e691 80{
81 my $non_uniquely_ordered_constrained = $schema->resultset('CD')->search(
82 { artist => 1 },
318e3d94 83 { order_by => [qw( me.genreid me.title me.year )], prefetch => 'tracks' },
0e81e691 84 );
85
86 isa_ok ($non_uniquely_ordered_constrained->next, 'DBICTest::CD' );
87
88 ok( defined $non_uniquely_ordered_constrained->cursor->next, 'Cursor not exhausted' );
89}
90
4e9fc3f3 91done_testing;