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