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