c77530afef75a61e772a16aeca4b81b9189442f2
[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 DBIC::SqlMakerTest;
8 use DBICTest;
9
10 my $schema = DBICTest->init_schema();
11
12 my $artist_rs = $schema->resultset('Artist');
13 my $ar = $artist_rs->current_source_alias;
14
15 my $filtered_cd_rs = $artist_rs->search_related('cds_unordered',
16   { "$ar.rank" => 13 },
17   {
18     prefetch => [ 'tracks' ],
19     order_by => [ { -asc => "$ar.name" }, "$ar.artistid DESC" ],
20     offset   => 3,
21     rows     => 3,
22   },
23 );
24
25 is_same_sql_bind(
26   $filtered_cd_rs->as_query,
27   q{(
28     SELECT  cds_unordered.cdid, cds_unordered.artist, cds_unordered.title, cds_unordered.year, cds_unordered.genreid, cds_unordered.single_track,
29             tracks.trackid, tracks.cd, tracks.position, tracks.title, tracks.last_updated_on, tracks.last_updated_at
30       FROM artist me
31       JOIN (
32         SELECT cds_unordered.cdid, cds_unordered.artist, cds_unordered.title, cds_unordered.year, cds_unordered.genreid, cds_unordered.single_track
33           FROM artist me
34           JOIN cd cds_unordered
35             ON cds_unordered.artist = me.artistid
36         WHERE ( me.rank = ? )
37         GROUP BY cds_unordered.cdid, cds_unordered.artist, cds_unordered.title, cds_unordered.year, cds_unordered.genreid, cds_unordered.single_track, me.name, me.artistid
38         ORDER BY me.name ASC, me.artistid DESC
39         LIMIT 3
40         OFFSET 3
41       ) cds_unordered
42         ON cds_unordered.artist = me.artistid
43       LEFT JOIN track tracks
44         ON tracks.cd = cds_unordered.cdid
45     WHERE ( me.rank = ? )
46     ORDER BY me.name ASC, me.artistid DESC, tracks.cd
47   )},
48   [ [ 'me.rank' => 13 ], [ 'me.rank' => 13 ] ],
49   'correct SQL on limited prefetch over search_related ordered by root',
50 );
51
52 # note: we only requested "get all cds of all artists with rank 13 then order
53 # by the artist name and give me the fourth, fifth and sixth", consequently the
54 # cds that belong to the same artist are unordered; fortunately we know that
55 # the first artist have 3 cds and the second and third artist both have only
56 # one, so the first 3 cds belong to the first artist and the fourth and fifth
57 # cds belong to the second and third artist, respectively, and there's no sixth
58 # row
59 is_deeply (
60   [ $filtered_cd_rs->hri_dump ],
61   [
62     {
63       'artist' => '2',
64       'cdid' => '4',
65       'genreid' => undef,
66       'single_track' => undef,
67       'title' => 'Generic Manufactured Singles',
68       'tracks' => [
69         {
70           'cd' => '4',
71           'last_updated_at' => undef,
72           'last_updated_on' => undef,
73           'position' => '1',
74           'title' => 'Boring Name',
75           'trackid' => '10'
76         },
77         {
78           'cd' => '4',
79           'last_updated_at' => undef,
80           'last_updated_on' => undef,
81           'position' => '2',
82           'title' => 'Boring Song',
83           'trackid' => '11'
84         },
85         {
86           'cd' => '4',
87           'last_updated_at' => undef,
88           'last_updated_on' => undef,
89           'position' => '3',
90           'title' => 'No More Ideas',
91           'trackid' => '12'
92         }
93       ],
94       'year' => '2001'
95     },
96     {
97       'artist' => '3',
98       'cdid' => '5',
99       'genreid' => undef,
100       'single_track' => undef,
101       'title' => 'Come Be Depressed With Us',
102       'tracks' => [
103         {
104           'cd' => '5',
105           'last_updated_at' => undef,
106           'last_updated_on' => undef,
107           'position' => '1',
108           'title' => 'Sad',
109           'trackid' => '13'
110         },
111         {
112           'cd' => '5',
113           'last_updated_at' => undef,
114           'last_updated_on' => undef,
115           'position' => '3',
116           'title' => 'Suicidal',
117           'trackid' => '15'
118         },
119         {
120           'cd' => '5',
121           'last_updated_at' => undef,
122           'last_updated_on' => undef,
123           'position' => '2',
124           'title' => 'Under The Weather',
125           'trackid' => '14'
126         }
127       ],
128       'year' => '1998'
129     }
130   ],
131   'Correctly ordered result',
132 );
133
134 done_testing;