bump CAG dep
[dbsrgits/DBIx-Class.git] / t / prefetch / with_limit.t
CommitLineData
a04af85f 1# Test to ensure we get a consistent result set wether or not we use the
2# prefetch option in combination rows (LIMIT).
3use strict;
4use warnings;
5
6use Test::More;
61fd5dfc 7use Test::Exception;
a04af85f 8use lib qw(t/lib);
9use DBICTest;
10
a3683eae 11plan tests => 9;
a04af85f 12
13my $schema = DBICTest->init_schema();
51a296b4 14
15
a04af85f 16my $no_prefetch = $schema->resultset('Artist')->search(
51a296b4 17 [ # search deliberately contrived
18 { 'artwork.cd_id' => undef },
19 { 'tracks.title' => { '!=' => 'blah-blah-1234568' }}
20 ],
96faafb4 21 { rows => 3, join => { cds => [qw/artwork tracks/] },
22 }
23);
24
25my $use_prefetch = $no_prefetch->search(
26 {},
25cac750 27 {
28 prefetch => 'cds',
51a296b4 29 order_by => { -desc => 'name' },
25cac750 30 }
a04af85f 31);
32
a04af85f 33is($no_prefetch->count, $use_prefetch->count, '$no_prefetch->count == $use_prefetch->count');
5624ba1f 34is(
9117ccfb 35 scalar ($no_prefetch->all),
36 scalar ($use_prefetch->all),
37 "Amount of returned rows is right"
5624ba1f 38);
39
9117ccfb 40my $artist_many_cds = $schema->resultset('Artist')->search ( {}, {
41 join => 'cds',
42 group_by => 'me.artistid',
43 having => \ 'count(cds.cdid) > 1',
44})->first;
45
46
5624ba1f 47$no_prefetch = $schema->resultset('Artist')->search(
9117ccfb 48 { artistid => $artist_many_cds->id },
5624ba1f 49 { rows => 1 }
50);
51
61fd5dfc 52$use_prefetch = $no_prefetch->search ({}, { prefetch => 'cds' });
5624ba1f 53
61fd5dfc 54my $normal_artist = $no_prefetch->single;
55my $prefetch_artist = $use_prefetch->find({ name => $artist_many_cds->name });
56my $prefetch2_artist = $use_prefetch->first;
5624ba1f 57
58is(
59 $prefetch_artist->cds->count,
60 $normal_artist->cds->count,
61fd5dfc 61 "Count of child rel with prefetch + rows => 1 is right (find)"
62);
63is(
64 $prefetch2_artist->cds->count,
65 $normal_artist->cds->count,
66 "Count of child rel with prefetch + rows => 1 is right (first)"
5624ba1f 67);
61fd5dfc 68
9117ccfb 69is (
70 scalar ($prefetch_artist->cds->all),
71 scalar ($normal_artist->cds->all),
61fd5dfc 72 "Amount of child rel rows with prefetch + rows => 1 is right (find)"
73);
74is (
75 scalar ($prefetch2_artist->cds->all),
76 scalar ($normal_artist->cds->all),
77 "Amount of child rel rows with prefetch + rows => 1 is right (first)"
78);
79
80throws_ok (
81 sub { $use_prefetch->single },
82 qr/resultsets prefetching has_many/,
83 'single() with multiprefetch is illegal',
9117ccfb 84);
01c781fe 85
09707a31 86my $artist = $use_prefetch->search({'cds.title' => $artist_many_cds->cds->first->title })->next;
01c781fe 87is($artist->cds->count, 1, "count on search limiting prefetched has_many");
88
a3683eae 89# try with double limit
90my $artist2 = $use_prefetch->search({'cds.title' => { '!=' => $artist_many_cds->cds->first->title } })->slice (0,0)->next;
91is($artist2->cds->count, 2, "count on search limiting prefetched has_many");
92