cleanup svn attrs
[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
61fd5dfc 11plan tests => 7;
a04af85f 12
13my $schema = DBICTest->init_schema();
51a296b4 14
15
a04af85f 16my $no_prefetch = $schema->resultset('Artist')->search(
25cac750 17 undef,
18 { rows => 3 }
a04af85f 19);
20
21my $use_prefetch = $schema->resultset('Artist')->search(
51a296b4 22 [ # search deliberately contrived
23 { 'artwork.cd_id' => undef },
24 { 'tracks.title' => { '!=' => 'blah-blah-1234568' }}
25 ],
25cac750 26 {
27 prefetch => 'cds',
51a296b4 28 join => { cds => [qw/artwork tracks/] },
29 rows => 3,
30 order_by => { -desc => 'name' },
25cac750 31 }
a04af85f 32);
33
a04af85f 34is($no_prefetch->count, $use_prefetch->count, '$no_prefetch->count == $use_prefetch->count');
5624ba1f 35is(
9117ccfb 36 scalar ($no_prefetch->all),
37 scalar ($use_prefetch->all),
38 "Amount of returned rows is right"
5624ba1f 39);
40
9117ccfb 41my $artist_many_cds = $schema->resultset('Artist')->search ( {}, {
42 join => 'cds',
43 group_by => 'me.artistid',
44 having => \ 'count(cds.cdid) > 1',
45})->first;
46
47
5624ba1f 48$no_prefetch = $schema->resultset('Artist')->search(
9117ccfb 49 { artistid => $artist_many_cds->id },
5624ba1f 50 { rows => 1 }
51);
52
61fd5dfc 53$use_prefetch = $no_prefetch->search ({}, { prefetch => 'cds' });
5624ba1f 54
61fd5dfc 55my $normal_artist = $no_prefetch->single;
56my $prefetch_artist = $use_prefetch->find({ name => $artist_many_cds->name });
57my $prefetch2_artist = $use_prefetch->first;
5624ba1f 58
59is(
60 $prefetch_artist->cds->count,
61 $normal_artist->cds->count,
61fd5dfc 62 "Count of child rel with prefetch + rows => 1 is right (find)"
63);
64is(
65 $prefetch2_artist->cds->count,
66 $normal_artist->cds->count,
67 "Count of child rel with prefetch + rows => 1 is right (first)"
5624ba1f 68);
61fd5dfc 69
9117ccfb 70is (
71 scalar ($prefetch_artist->cds->all),
72 scalar ($normal_artist->cds->all),
61fd5dfc 73 "Amount of child rel rows with prefetch + rows => 1 is right (find)"
74);
75is (
76 scalar ($prefetch2_artist->cds->all),
77 scalar ($normal_artist->cds->all),
78 "Amount of child rel rows with prefetch + rows => 1 is right (first)"
79);
80
81throws_ok (
82 sub { $use_prefetch->single },
83 qr/resultsets prefetching has_many/,
84 'single() with multiprefetch is illegal',
9117ccfb 85);