bb8eb4c48c575688eccf15ac4a305e5e58815776
[dbsrgits/DBIx-Class.git] / t / count / joined.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5
6 use lib qw(t/lib);
7
8 use DBICTest;
9
10 my $schema = DBICTest->init_schema();
11
12 my $cds = $schema->resultset("CD")->search({ cdid => 1 }, { join => { cd_to_producer => 'producer' } });
13 cmp_ok($cds->count, '>', 1, "extra joins explode entity count");
14
15 for my $arg (
16   [ 'prefetch-collapsed has_many' => { prefetch => 'cd_to_producer' } ],
17   [ 'distict-collapsed result' => { distinct => 1 } ],
18   [ 'explicit collapse request' => { collapse => 1 } ],
19 ) {
20   for my $hri (0,1) {
21     my $diag = $arg->[0] . ($hri ? ' with HRI' : '');
22
23     my $rs = $cds->search({}, {
24       %{$arg->[1]},
25       $hri ? ( result_class => 'DBIx::Class::ResultClass::HashRefInflator' ) : (),
26     });
27
28     is
29       $rs->count,
30       1,
31       "Count correct on $diag",
32     ;
33
34     is
35       scalar $rs->all,
36       1,
37       "Amount of constructed objects matches count on $diag",
38     ;
39   }
40 }
41
42 # JOIN and LEFT JOIN issues mean that we've seen problems where counted rows and fetched rows are sometimes 1 higher than they should
43 # be in the related resultset.
44 my $artist=$schema->resultset('Artist')->create({name => 'xxx'});
45 is($artist->related_resultset('cds')->count(), 0, "No CDs found for a shiny new artist");
46 is(scalar($artist->related_resultset('cds')->all()), 0, "No CDs fetched for a shiny new artist");
47
48 my $artist_rs = $schema->resultset('Artist')->search({artistid => $artist->id});
49 is($artist_rs->related_resultset('cds')->count(), 0, "No CDs counted for a shiny new artist using a resultset search");
50 is(scalar($artist_rs->related_resultset('cds')->all), 0, "No CDs fetched for a shiny new artist using a resultset search");
51
52 done_testing;