Add extra test for prefetch+has_many
[dbsrgits/DBIx-Class.git] / t / prefetch / with_limit.t
1 # Test to ensure we get a consistent result set wether or not we use the
2 # prefetch option in combination rows (LIMIT).
3 use strict;
4 use warnings;
5
6 use Test::More;
7 use Test::Exception;
8 use lib qw(t/lib);
9 use DBICTest;
10
11 plan tests => 7;
12
13 my $schema = DBICTest->init_schema();
14
15
16 my $no_prefetch = $schema->resultset('Artist')->search(
17   undef,
18   { rows => 3 }
19 );
20
21 my $use_prefetch = $schema->resultset('Artist')->search(
22   [   # search deliberately contrived
23     { 'artwork.cd_id' => undef },
24     { 'tracks.title' => { '!=' => 'blah-blah-1234568' }}
25   ],
26   {
27     prefetch => 'cds',
28     join => { cds => [qw/artwork tracks/] },
29     rows     => 3,
30     order_by => { -desc => 'name' },
31   }
32 );
33
34 is($no_prefetch->count, $use_prefetch->count, '$no_prefetch->count == $use_prefetch->count');
35 is(
36   scalar ($no_prefetch->all),
37   scalar ($use_prefetch->all),
38   "Amount of returned rows is right"
39 );
40
41 my $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
48 $no_prefetch = $schema->resultset('Artist')->search(
49   { artistid => $artist_many_cds->id },
50   { rows => 1 }
51 );
52
53 $use_prefetch = $no_prefetch->search ({}, { prefetch => 'cds' });
54
55 my $normal_artist = $no_prefetch->single;
56 my $prefetch_artist = $use_prefetch->find({ name => $artist_many_cds->name });
57 my $prefetch2_artist = $use_prefetch->first;
58
59 is(
60   $prefetch_artist->cds->count,
61   $normal_artist->cds->count,
62   "Count of child rel with prefetch + rows => 1 is right (find)"
63 );
64 is(
65   $prefetch2_artist->cds->count,
66   $normal_artist->cds->count,
67   "Count of child rel with prefetch + rows => 1 is right (first)"
68 );
69
70 is (
71   scalar ($prefetch_artist->cds->all),
72   scalar ($normal_artist->cds->all),
73   "Amount of child rel rows with prefetch + rows => 1 is right (find)"
74 );
75 is (
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
81 throws_ok (
82   sub { $use_prefetch->single },
83   qr/resultsets prefetching has_many/,
84   'single() with multiprefetch is illegal',
85 );
86
87 my $artist = $use_prefetch->search({'cds.title' => $artist_many_cds->cds->first->title })->slice->next;
88
89 is($artist->cds->count, 1, "count on search limiting prefetched has_many");
90