Fixed caveats
[dbsrgits/DBIx-Class.git] / t / count / prefetch.t
CommitLineData
639cf8f9 1use strict;
2use warnings;
3
4use lib qw(t/lib);
5
6use Test::More;
7use DBICTest;
8use DBIC::SqlMakerTest;
9use DBIC::DebugObj;
10
11plan tests => 6;
12
13my $schema = DBICTest->init_schema();
14
15# collapsing prefetch
16{
17 my $rs = $schema->resultset("Artist")
18 ->search_related('cds',
19 { 'tracks.position' => [1,2] },
20 { prefetch => [qw/tracks artist/] },
21 );
22 is ($rs->all, 5, 'Correct number of objects');
23
24
25 my ($sql, @bind);
26 $schema->storage->debugobj(DBIC::DebugObj->new(\$sql, \@bind));
27 $schema->storage->debug(1);
28
29
30 is ($rs->count, 5, 'Correct count');
31
32 is_same_sql_bind (
33 $sql,
34 \@bind,
c98169a7 35 'SELECT COUNT( * ) FROM (SELECT cds.cdid FROM artist me JOIN cd cds ON cds.artist = me.artistid LEFT JOIN track tracks ON tracks.cd = cds.cdid JOIN artist artist ON artist.artistid = cds.artist WHERE tracks.position = ? OR tracks.position = ? GROUP BY cds.cdid) count_subq',
af6aac2d 36 [ qw/'1' '2'/ ],
639cf8f9 37 );
38}
39
40# non-collapsing prefetch (no multi prefetches)
41{
42 my $rs = $schema->resultset("CD")
43 ->search_related('tracks',
44 { position => [1,2] },
45 { prefetch => [qw/disc lyrics/] },
46 );
47 is ($rs->all, 10, 'Correct number of objects');
48
49
50 my ($sql, @bind);
51 $schema->storage->debugobj(DBIC::DebugObj->new(\$sql, \@bind));
52 $schema->storage->debug(1);
53
54
55 is ($rs->count, 10, 'Correct count');
56
57 is_same_sql_bind (
58 $sql,
59 \@bind,
c98169a7 60 'SELECT COUNT( * ) FROM cd me JOIN track tracks ON tracks.cd = me.cdid JOIN cd disc ON disc.cdid = tracks.cd LEFT JOIN lyrics lyrics ON lyrics.track_id = tracks.trackid WHERE ( ( position = ? OR position = ? ) )',
af6aac2d 61 [ qw/'1' '2'/ ],
639cf8f9 62 );
63}