Added two tests and marked one todo_skip
[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
cfcc66e9 11plan tests => 17;
639cf8f9 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',
6a05068a 36 [ qw/'1' '2'/ ],
37 );
38}
39
cfcc66e9 40# Added test by mo per http://scsys.co.uk:8001/31870
41TODO: {
42 todo_skip "This breaks stuff", 3;
43 my $rs = $schema->resultset("Artist")->search(undef, {distinct => 1})
44 ->search_related('cds')->search_related('genre',
45 { 'genre.name' => 'foo' },
46 { prefetch => q(cds) },
47 );
48 is ($rs->all, 5, 'Correct number of objects');
49
50
51 my ($sql, @bind);
52 $schema->storage->debugobj(DBIC::DebugObj->new(\$sql, \@bind));
53 $schema->storage->debug(1);
54
55
56 is ($rs->count, 5, 'Correct count');
57
58 is_same_sql_bind (
59 $sql,
60 \@bind,
61 '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',
62 [ qw/'1' '2'/ ],
63 );
64}
65
6a05068a 66# collapsing prefetch with distinct
cfcc66e9 67TODO: {
68 todo_skip "This is busted", 3;
6a05068a 69 my $rs = $schema->resultset("Artist")->search(undef, {distinct => 1})
70 ->search_related('cds')->search_related('genre',
71 { 'genre.name' => 'foo' },
72 { prefetch => q(cds) },
73 );
74 is ($rs->all, 5, 'Correct number of objects');
75
76
77 my ($sql, @bind);
78 $schema->storage->debugobj(DBIC::DebugObj->new(\$sql, \@bind));
79 $schema->storage->debug(1);
80
81
82 is ($rs->count, 5, 'Correct count');
83
84 is_same_sql_bind (
85 $sql,
86 \@bind,
87 '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 88 [ qw/'1' '2'/ ],
639cf8f9 89 );
90}
91
92# non-collapsing prefetch (no multi prefetches)
93{
94 my $rs = $schema->resultset("CD")
95 ->search_related('tracks',
96 { position => [1,2] },
97 { prefetch => [qw/disc lyrics/] },
98 );
99 is ($rs->all, 10, 'Correct number of objects');
100
101
102 my ($sql, @bind);
103 $schema->storage->debugobj(DBIC::DebugObj->new(\$sql, \@bind));
104 $schema->storage->debug(1);
105
106
107 is ($rs->count, 10, 'Correct count');
108
109 is_same_sql_bind (
110 $sql,
111 \@bind,
c98169a7 112 '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 113 [ qw/'1' '2'/ ],
639cf8f9 114 );
115}
cfcc66e9 116
117# Added test by mo per http://scsys.co.uk:8001/31873
118TODO: {
119 todo_skip "This breaks stuff", 5;
120 my $rs = $schema->resultset("Artwork")->search(undef, {distinct => 1})
121 ->search_related('artwork_to_artist')->search_related('artist',
122 undef,
123 { prefetch => q(cds) },
124 );
125 is($rs->all, 0, 'failure without WHERE');
126
127 $rs = $schema->resultset("Artwork")->search(undef, {distinct => 1})
128 ->search_related('artwork_to_artist')->search_related('artist',
129 { 'cds.title' => 'foo' }, # this line has changed
130 { prefetch => q(cds) },
131 );
132 is($rs->all, 0, 'success with WHERE');
133
134 # different case
135
136 $rs = $schema->resultset("Artist")->search(undef)#, {distinct => 1})
137 ->search_related('cds')->search_related('genre',
138 { 'genre.name' => 'foo' },
139 { prefetch => q(cds) },
140 );
141 is($rs->all, 0, 'success without distinct');
142
143 $rs = $schema->resultset("Artist")->search(undef, {distinct => 1})
144 ->search_related('cds')->search_related('genre',
145 { 'genre.name' => 'foo' },
146 #{ prefetch => q(cds) },
147 );
148 is($rs->all, 0, 'success without prefetch');
149
150 $rs = $schema->resultset("Artist")->search(undef, {distinct => 1})
151 ->search_related('cds')->search_related('genre',
152 { 'genre.name' => 'foo' },
153 { prefetch => q(cds) },
154 );
155 is($rs->all, 0, 'failure with distinct');
156}