Massive rewrite of bind handling, and overall simplification of ::Storage::DBI
[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         ORDER BY me.name ASC, me.artistid DESC
38         LIMIT 3
39         OFFSET 3
40       ) cds_unordered
41         ON cds_unordered.artist = me.artistid
42       LEFT JOIN track tracks
43         ON tracks.cd = cds_unordered.cdid
44     WHERE ( me.rank = ? )
45     ORDER BY me.name ASC, me.artistid DESC, tracks.cd
46   )},
47   [ map { [ { sqlt_datatype => 'integer', dbic_colname => 'me.rank' }
48             => 13 ] } (1,2)
49   ],
50   'correct SQL on limited prefetch over search_related ordered by root',
51 );
52
53 # note: we only requested "get all cds of all artists with rank 13 then order
54 # by the artist name and give me the fourth, fifth and sixth", consequently the
55 # cds that belong to the same artist are unordered; fortunately we know that
56 # the first artist have 3 cds and the second and third artist both have only
57 # one, so the first 3 cds belong to the first artist and the fourth and fifth
58 # cds belong to the second and third artist, respectively, and there's no sixth
59 # row
60 is_deeply (
61   [ $filtered_cd_rs->hri_dump ],
62   [
63     {
64       'artist' => '2',
65       'cdid' => '4',
66       'genreid' => undef,
67       'single_track' => undef,
68       'title' => 'Generic Manufactured Singles',
69       'tracks' => [
70         {
71           'cd' => '4',
72           'last_updated_at' => undef,
73           'last_updated_on' => undef,
74           'position' => '1',
75           'title' => 'Boring Name',
76           'trackid' => '10'
77         },
78         {
79           'cd' => '4',
80           'last_updated_at' => undef,
81           'last_updated_on' => undef,
82           'position' => '2',
83           'title' => 'Boring Song',
84           'trackid' => '11'
85         },
86         {
87           'cd' => '4',
88           'last_updated_at' => undef,
89           'last_updated_on' => undef,
90           'position' => '3',
91           'title' => 'No More Ideas',
92           'trackid' => '12'
93         }
94       ],
95       'year' => '2001'
96     },
97     {
98       'artist' => '3',
99       'cdid' => '5',
100       'genreid' => undef,
101       'single_track' => undef,
102       'title' => 'Come Be Depressed With Us',
103       'tracks' => [
104         {
105           'cd' => '5',
106           'last_updated_at' => undef,
107           'last_updated_on' => undef,
108           'position' => '1',
109           'title' => 'Sad',
110           'trackid' => '13'
111         },
112         {
113           'cd' => '5',
114           'last_updated_at' => undef,
115           'last_updated_on' => undef,
116           'position' => '3',
117           'title' => 'Suicidal',
118           'trackid' => '15'
119         },
120         {
121           'cd' => '5',
122           'last_updated_at' => undef,
123           'last_updated_on' => undef,
124           'position' => '2',
125           'title' => 'Under The Weather',
126           'trackid' => '14'
127         }
128       ],
129       'year' => '1998'
130     }
131   ],
132   'Correctly ordered result',
133 );
134
135 done_testing;