First part of changes for better unexpected NULL reporting
[dbsrgits/DBIx-Class.git] / t / prefetch / lazy_cursor.t
1 BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7 use Test::Warn;
8 use Test::Exception;
9
10 use DBICTest;
11
12 my $schema = DBICTest->init_schema();
13
14 my $rs = $schema->resultset('Artist')->search({}, {
15   select => 'artistid',
16   prefetch => { cds => 'tracks' },
17 });
18
19 my $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
38 for (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
64 my @objs = $rs->all;
65 is (@objs, $initial_artists_cnt + 3, '->all resets everything correctly');
66 is ( ($rs->cursor->next)[0], 1, 'Cursor auto-rewound after all()');
67 ok (! @{ $rs->{_stashed_rows} || [] }, 'Nothing else left in $rs stash');
68
69 my $unordered_rs = $rs->search({}, { order_by => 'cds.title' });
70
71 warnings_exist {
72   ok ($unordered_rs->next, 'got row 1');
73 } qr/performed an eager cursor slurp underneath/, 'Warned on auto-eager cursor';
74
75 is_deeply ([$unordered_rs->cursor->next], [], 'Nothing left on cursor, eager slurp');
76 ok ($unordered_rs->next, "got row $_")  for (2 .. $initial_artists_cnt + 3);
77 is ($unordered_rs->next, undef, 'End of RS reached');
78 is ($unordered_rs->next, undef, 'End of RS not lost');
79
80 {
81   my $non_uniquely_ordered_constrained = $schema->resultset('CD')->search(
82     { artist => 1 },
83     { order_by => [qw( me.genreid me.title me.year )], prefetch => 'tracks' },
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
91 done_testing;