Sketches of tests that should ultimately pass
[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
a04af85f 11my $schema = DBICTest->init_schema();
51a296b4 12
13
a04af85f 14my $no_prefetch = $schema->resultset('Artist')->search(
51a296b4 15 [ # search deliberately contrived
16 { 'artwork.cd_id' => undef },
17 { 'tracks.title' => { '!=' => 'blah-blah-1234568' }}
18 ],
96faafb4 19 { rows => 3, join => { cds => [qw/artwork tracks/] },
20 }
21);
22
23my $use_prefetch = $no_prefetch->search(
24 {},
25cac750 25 {
9f6555d3 26 select => ['me.artistid', 'me.name'],
27 as => ['artistid', 'name'],
25cac750 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");
9f6555d3 92
93done_testing;