Institute a central "load this first in testing" package
[dbsrgits/DBIx-Class.git] / t / search / distinct.t
CommitLineData
c0329273 1BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2
d955e938 3use strict;
4use warnings;
5
6use Test::More;
d955e938 7
c0329273 8
a5a7bb73 9use DBICTest ':DiffSQL';
d955e938 10
11my $schema = DBICTest->init_schema();
12
13# make sure order + distinct do not double-inject group criteria
eb58c082 14my $rs = $schema->resultset ('CD')->search ({}, {
d955e938 15 distinct => 1,
eb58c082 16 columns => 'title',
d955e938 17});
18
eb58c082 19# title + cdid == unique constraint
20my $unique_rs = $rs->search ({}, {
21 '+columns' => 'cdid',
22});
23
24is_same_sql_bind (
25 $rs->search({}, { order_by => 'title' })->as_query,
26 '(
27 SELECT me.title
28 FROM cd me
29 GROUP BY me.title
30 ORDER BY title
31 )',
32 [],
33 'Correct GROUP BY on selection+order_by on same column',
34);
35
36is_same_sql_bind (
37 $rs->search({}, { order_by => 'year' })->as_query,
38 '(
39 SELECT me.title
40 FROM cd me
41 GROUP BY me.title
42 ORDER BY MIN(year)
43 )',
44 [],
45 'Correct GROUP BY on non-unique selection and order by a different column',
46);
47
d955e938 48is_same_sql_bind (
eb58c082 49 $unique_rs->search({}, { order_by => 'year' })->as_query,
d955e938 50 '(
eb58c082 51 SELECT me.title, me.cdid
d955e938 52 FROM cd me
eb58c082 53 GROUP BY me.title, me.cdid, me.year
d955e938 54 ORDER BY year
55 )',
56 [],
eb58c082 57 'Correct GROUP BY on unique selection and order by a different column',
58);
59
60is_same_sql_bind (
61 $rs->search({}, { order_by => 'artist.name', join => 'artist' })->as_query,
62 '(
63 SELECT me.title
64 FROM cd me
65 JOIN artist artist
66 ON artist.artistid = me.artist
67 GROUP BY me.title
68 ORDER BY MIN(artist.name)
69 )',
70 [],
71 'Correct GROUP BY on non-unique selection and external single order_by',
72);
73
74is_same_sql_bind (
75 $unique_rs->search({}, { order_by => 'artist.name', join => 'artist' })->as_query,
76 '(
77 SELECT me.title, me.cdid
78 FROM cd me
79 JOIN artist artist
80 ON artist.artistid = me.artist
81 GROUP BY me.title, me.cdid, artist.name
82 ORDER BY artist.name
83 )',
84 [],
85 'Correct GROUP BY on unique selection and external single order_by',
86);
87
88is_same_sql_bind (
89 $rs->search({}, { order_by => 'tracks.title', join => 'tracks' })->as_query,
90 '(
91 SELECT me.title
92 FROM cd me
93 LEFT JOIN track tracks
94 ON tracks.cd = me.cdid
95 GROUP BY me.title
96 ORDER BY MIN(tracks.title)
97 )',
98 [],
99 'Correct GROUP BY on non-unique selection and external multi order_by',
100);
101
102is_same_sql_bind (
103 $unique_rs->search({}, { order_by => 'tracks.title', join => 'tracks' })->as_query,
104 '(
105 SELECT me.title, me.cdid
106 FROM cd me
107 LEFT JOIN track tracks
108 ON tracks.cd = me.cdid
109 GROUP BY me.title, me.cdid
110 ORDER BY MIN(tracks.title)
111 )',
112 [],
113 'Correct GROUP BY on unique selection and external multi order_by',
d955e938 114);
115
116done_testing;