I think we are done here
[dbsrgits/DBIx-Class.git] / t / prefetch / lazy_cursor.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use Test::Exception;
6 use lib qw(t/lib);
7 use DBICTest;
8
9 my $schema = DBICTest->init_schema();
10
11 my $rs = $schema->resultset('Artist')->search({}, {
12   select => 'artistid',
13   prefetch => { cds => 'tracks' },
14 });
15
16 my $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
35 for (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
61 my @objs = $rs->all;
62 is (@objs, $initial_artists_cnt + 3, '->all resets everything correctly');
63 is ( ($rs->cursor->next)[0], 1, 'Cursor auto-rewound after all()');
64 is ($rs->{stashed_rows}, undef, 'Nothing else left in $rs stash');
65
66 my $unordered_rs = $rs->search({}, { order_by => 'cds.title' });
67 ok ($unordered_rs->next, 'got row 1');
68 is_deeply ([$unordered_rs->cursor->next], [], 'Nothing left on cursor, eager slurp');
69 ok ($unordered_rs->next, "got row $_")  for (2 .. $initial_artists_cnt + 3);
70 is ($unordered_rs->next, undef, 'End of RS reached');
71 is ($unordered_rs->next, undef, 'End of RS not lost');
72
73 done_testing;