This seems to be the prefetch+limit fix - ugly as hell but appears to work
[dbsrgits/DBIx-Class.git] / t / prefetch / rows_bug.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 lib qw(t/lib);
8 use DBICTest;
9
10 plan tests => 4;
11
12 my $schema = DBICTest->init_schema();
13
14
15 my $no_prefetch = $schema->resultset('Artist')->search(
16   undef,
17   { rows => 3 }
18 );
19
20 my $use_prefetch = $schema->resultset('Artist')->search(
21   [   # search deliberately contrived
22     { 'artwork.cd_id' => undef },
23     { 'tracks.title' => { '!=' => 'blah-blah-1234568' }}
24   ],
25   {
26     prefetch => 'cds',
27     join => { cds => [qw/artwork tracks/] },
28     rows     => 3,
29     order_by => { -desc => 'name' },
30   }
31 );
32
33 is($no_prefetch->count, $use_prefetch->count, '$no_prefetch->count == $use_prefetch->count');
34 is(
35   scalar ($no_prefetch->all),
36   scalar ($use_prefetch->all),
37   "Amount of returned rows is right"
38 );
39
40 my $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
47 $no_prefetch = $schema->resultset('Artist')->search(
48   { artistid => $artist_many_cds->id },
49   { rows => 1 }
50 );
51
52 $use_prefetch = $schema->resultset('Artist')->search(
53   { artistid => $artist_many_cds->id },
54   {
55     prefetch => 'cds',
56     rows     => 1
57   }
58 );
59
60 my $prefetch_artist = $use_prefetch->first;
61 my $normal_artist = $no_prefetch->first;
62
63 is(
64   $prefetch_artist->cds->count,
65   $normal_artist->cds->count,
66   "Count of child rel with prefetch + rows => 1 is right"
67 );
68 is (
69   scalar ($prefetch_artist->cds->all),
70   scalar ($normal_artist->cds->all),
71   "Amount of child rel rows with prefetch + rows => 1 is right"
72 );