added the default attrs to solve the failing test recently commited
[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;
639cf8f9 9
639cf8f9 10my $schema = DBICTest->init_schema();
11
12# collapsing prefetch
13{
14 my $rs = $schema->resultset("Artist")
15 ->search_related('cds',
16 { 'tracks.position' => [1,2] },
17 { prefetch => [qw/tracks artist/] },
18 );
19 is ($rs->all, 5, 'Correct number of objects');
639cf8f9 20 is ($rs->count, 5, 'Correct count');
21
22 is_same_sql_bind (
f785d72e 23 $rs->count_rs->as_query,
24 '(
25 SELECT COUNT( * )
26 FROM (
27 SELECT cds.cdid
28 FROM artist me
29 JOIN cd cds ON cds.artist = me.artistid
30 LEFT JOIN track tracks ON tracks.cd = cds.cdid
31 JOIN artist artist ON artist.artistid = cds.artist
32 WHERE tracks.position = ? OR tracks.position = ?
33 GROUP BY cds.cdid
34 ) count_subq
35 )',
36 [ map { [ 'tracks.position' => $_ ] } (1, 2) ],
cfcc66e9 37 );
38}
39
6a05068a 40# collapsing prefetch with distinct
f785d72e 41{
42 my $first_cd = $schema->resultset('Artist')->first->cds->first;
43 $first_cd->update ({
44 genreid => $first_cd->create_related (
45 genre => ({ name => 'vague genre' })
46 )->id
47 });
48
6a05068a 49 my $rs = $schema->resultset("Artist")->search(undef, {distinct => 1})
50 ->search_related('cds')->search_related('genre',
f785d72e 51 { 'genre.name' => { '!=', 'foo' } },
6a05068a 52 { prefetch => q(cds) },
53 );
f785d72e 54 is ($rs->all, 1, 'Correct number of objects');
55 is ($rs->count, 1, 'Correct count');
6a05068a 56
57 is_same_sql_bind (
f785d72e 58 $rs->count_rs->as_query,
59 '(
60 SELECT COUNT( * )
61 FROM (
62 SELECT genre.genreid
63 FROM artist me
64 JOIN cd cds ON cds.artist = me.artistid
65 JOIN genre genre ON genre.genreid = cds.genreid
66 LEFT JOIN cd cds_2 ON cds_2.genreid = genre.genreid
67 WHERE ( genre.name != ? )
68 GROUP BY genre.genreid
69 ) count_subq
70 )',
71 [ [ 'genre.name' => 'foo' ] ],
639cf8f9 72 );
73}
74
75# non-collapsing prefetch (no multi prefetches)
76{
77 my $rs = $schema->resultset("CD")
78 ->search_related('tracks',
79 { position => [1,2] },
80 { prefetch => [qw/disc lyrics/] },
81 );
82 is ($rs->all, 10, 'Correct number of objects');
83
84
639cf8f9 85 is ($rs->count, 10, 'Correct count');
86
87 is_same_sql_bind (
f785d72e 88 $rs->count_rs->as_query,
89 '(
90 SELECT COUNT( * )
91 FROM cd me
92 JOIN track tracks ON tracks.cd = me.cdid
93 JOIN cd disc ON disc.cdid = tracks.cd
94 LEFT JOIN lyrics lyrics ON lyrics.track_id = tracks.trackid
95 WHERE position = ? OR position = ?
96 )',
97 [ map { [ position => $_ ] } (1, 2) ],
639cf8f9 98 );
99}
cfcc66e9 100
6841b059 101done_testing;