65a2c398630c7129e79023b61f1911a26edc5771
[dbsrgits/DBIx-Class.git] / t / prefetch / o2m_o2m_order_by_with_limit.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5
6 use lib qw(t/lib);
7 use DBICTest ':DiffSQL';
8 use DBIx::Class::SQLMaker::LimitDialects;
9
10 my ($ROWS, $OFFSET) = (
11    DBIx::Class::SQLMaker::LimitDialects->__rows_bindtype,
12    DBIx::Class::SQLMaker::LimitDialects->__offset_bindtype,
13 );
14
15 my $schema = DBICTest->init_schema(quote_names => 1);
16
17 my $artist_rs = $schema->resultset('Artist');
18
19 my $filtered_cd_rs = $artist_rs->search_related('cds_unordered',
20   { "me.rank" => 13 },
21   {
22     prefetch => 'tracks',
23     join => 'genre',
24     order_by => [ { -desc => 'genre.name' }, { -desc => \ 'tracks.title' }, { -asc => "me.name" }, { -desc => [qw(year cds_unordered.title)] } ], # me. is the artist, *NOT* the cd
25   },
26 );
27
28 my $hri_contents = [
29   {
30     artist => 1, cdid => 1, genreid => 1, single_track => undef, title => "Spoonful of bees", year => 1999, tracks => [
31       { cd => 1, last_updated_at => undef, last_updated_on => undef, position => 1, title => "The Bees Knees", trackid => 16 },
32       { cd => 1, last_updated_at => undef, last_updated_on => undef, position => 3, title => "Beehind You", trackid => 18 },
33       { cd => 1, last_updated_at => undef, last_updated_on => undef, position => 2, title => "Apiary", trackid => 17 },
34     ],
35   },
36   {
37     artist => 1, cdid => 3, genreid => undef, single_track => undef, title => "Caterwaulin' Blues", year => 1997, tracks => [
38       { cd => 3, last_updated_at => undef, last_updated_on => undef, position => 1, title => "Yowlin", trackid => 7 },
39       { cd => 3, last_updated_at => undef, last_updated_on => undef, position => 2, title => "Howlin", trackid => 8 },
40       { cd => 3, last_updated_at => undef, last_updated_on => undef, position => 3, title => "Fowlin", trackid => 9 },
41     ],
42   },
43   {
44     artist => 3, cdid => 5, genreid => undef, single_track => undef, title => "Come Be Depressed With Us", year => 1998, tracks => [
45       { cd => 5, last_updated_at => undef, last_updated_on => undef, position => 2, title => "Under The Weather", trackid => 14 },
46       { cd => 5, last_updated_at => undef, last_updated_on => undef, position => 3, title => "Suicidal", trackid => 15 },
47       { cd => 5, last_updated_at => undef, last_updated_on => undef, position => 1, title => "Sad", trackid => 13 },
48     ],
49   },
50   {
51     artist => 1, cdid => 2, genreid => undef, single_track => undef, title => "Forkful of bees", year => 2001, tracks => [
52       { cd => 2, last_updated_at => undef, last_updated_on => undef, position => 1, title => "Stung with Success", trackid => 4 },
53       { cd => 2, last_updated_at => undef, last_updated_on => undef, position => 2, title => "Stripy", trackid => 5 },
54       { cd => 2, last_updated_at => undef, last_updated_on => undef, position => 3, title => "Sticky Honey", trackid => 6 },
55     ],
56   },
57   {
58     artist => 2, cdid => 4, genreid => undef, single_track => undef, title => "Generic Manufactured Singles", year => 2001, tracks => [
59       { cd => 4, last_updated_at => undef, last_updated_on => undef, position => 3, title => "No More Ideas", trackid => 12 },
60       { cd => 4, last_updated_at => undef, last_updated_on => undef, position => 2, title => "Boring Song", trackid => 11 },
61       { cd => 4, last_updated_at => undef, last_updated_on => undef, position => 1, title => "Boring Name", trackid => 10},
62     ],
63   },
64 ];
65
66 is_deeply(
67   $filtered_cd_rs->all_hri,
68   $hri_contents,
69   'Expected ordered unlimited contents',
70 );
71
72 for (
73   [ 0, 1 ],
74   [ 2, 0 ],
75   [ 20, 2 ],
76   [ 1, 3 ],
77   [ 2, 4 ],
78 ) {
79   my ($limit, $offset) = @$_;
80
81   my $rs = $filtered_cd_rs->search({}, { $limit ? (rows => $limit) : (), offset => $offset });
82
83   my $used_limit = $limit || DBIx::Class::SQLMaker->__max_int;
84   my $offset_str = $offset ? 'OFFSET ?' : '';
85
86   is_same_sql_bind(
87     $rs->as_query,
88     qq{(
89       SELECT  "cds_unordered"."cdid", "cds_unordered"."artist", "cds_unordered"."title", "cds_unordered"."year", "cds_unordered"."genreid", "cds_unordered"."single_track",
90               "tracks"."trackid", "tracks"."cd", "tracks"."position", "tracks"."title", "tracks"."last_updated_on", "tracks"."last_updated_at"
91         FROM "artist" "me"
92         JOIN (
93           SELECT "cds_unordered"."cdid", "cds_unordered"."artist", "cds_unordered"."title", "cds_unordered"."year", "cds_unordered"."genreid", "cds_unordered"."single_track"
94             FROM "artist" "me"
95             JOIN cd "cds_unordered"
96               ON "cds_unordered"."artist" = "me"."artistid"
97             LEFT JOIN "genre" "genre"
98               ON "genre"."genreid" = "cds_unordered"."genreid"
99             LEFT JOIN "track" "tracks"
100               ON "tracks"."cd" = "cds_unordered"."cdid"
101           WHERE "me"."rank" = ?
102           GROUP BY "cds_unordered"."cdid", "cds_unordered"."artist", "cds_unordered"."title", "cds_unordered"."year", "cds_unordered"."genreid", "cds_unordered"."single_track", "me"."name"
103           ORDER BY  MAX("genre"."name") DESC,
104                     MAX( tracks.title ) DESC,
105                     "me"."name" ASC,
106                     "year" DESC,
107                     "cds_unordered"."title" DESC
108           LIMIT ?
109           $offset_str
110         ) "cds_unordered"
111           ON "cds_unordered"."artist" = "me"."artistid"
112         LEFT JOIN "genre" "genre"
113           ON "genre"."genreid" = "cds_unordered"."genreid"
114         LEFT JOIN "track" "tracks"
115           ON "tracks"."cd" = "cds_unordered"."cdid"
116       WHERE "me"."rank" = ?
117       ORDER BY  "genre"."name" DESC,
118                 tracks.title DESC,
119                 "me"."name" ASC,
120                 "year" DESC,
121                 "cds_unordered"."title" DESC
122     )},
123     [
124       [ { sqlt_datatype => 'integer', dbic_colname => 'me.rank' } => 13 ],
125       [ $ROWS => $used_limit ],
126       $offset ? [ $OFFSET => $offset ] : (),
127       [ { sqlt_datatype => 'integer', dbic_colname => 'me.rank' } => 13 ],
128     ],
129     "correct SQL on prefetch over search_related ordered by external joins with limit '$limit', offset '$offset'",
130   );
131
132   is_deeply(
133     $rs->all_hri,
134     [ @{$hri_contents}[$offset .. List::Util::min( $used_limit+$offset-1, $#$hri_contents)] ],
135     "Correct slice of the resultset returned with limit '$limit', offset '$offset'",
136   );
137 }
138
139 done_testing;