Added two tests and marked one todo_skip
[dbsrgits/DBIx-Class.git] / t / count / prefetch.t
1 use strict;
2 use warnings;
3
4 use lib qw(t/lib);
5
6 use Test::More;
7 use DBICTest;
8 use DBIC::SqlMakerTest;
9 use DBIC::DebugObj;
10
11 plan tests => 17;
12
13 my $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,
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',
36     [ qw/'1' '2'/ ],
37   );
38 }
39
40 # Added test by mo per http://scsys.co.uk:8001/31870
41 TODO: {
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
66 # collapsing prefetch with distinct
67 TODO: {
68   todo_skip "This is busted", 3;
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',
88     [ qw/'1' '2'/ ],
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,
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 = ? ) )',
113     [ qw/'1' '2'/ ],
114   );
115 }
116
117 # Added test by mo per http://scsys.co.uk:8001/31873
118 TODO: {
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 }