Make sure DBICTest is always loaded first (purely bookkeep)
[dbsrgits/DBIx-Class-Historic.git] / t / search / select_chains_unbalanced.t
CommitLineData
00336453 1use strict;
2use warnings;
3
4use Test::More;
f7f53a89 5use Test::Exception;
00336453 6
7use lib qw(t/lib);
00336453 8use DBICTest;
5e724964 9use DBIC::SqlMakerTest;
00336453 10
11my $schema = DBICTest->init_schema();
12
13my $multicol_rs = $schema->resultset('Artist')->search({ artistid => \'1' }, { columns => [qw/name rank/] });
14
15my @chain = (
16 {
f7f53a89 17 select => 'cdid',
18 as => 'cd_id',
19 columns => [ 'title' ],
00336453 20 } => 'SELECT
f7f53a89 21 me.title,
22 me.cdid
00336453 23 FROM cd me'
f7f53a89 24 => [qw/title cd_id/],
00336453 25
26 {
27 '+select' => \ 'DISTINCT(foo, bar)',
28 '+as' => [qw/foo bar/],
29 } => 'SELECT
00336453 30 me.title,
f7f53a89 31 me.cdid,
00336453 32 DISTINCT(foo, bar)
33 FROM cd me'
f7f53a89 34 => [qw/title cd_id foo bar/],
00336453 35
36 {
36fd7f07 37 '+select' => [ 'genreid', $multicol_rs->as_query ],
38 '+as' => [qw/genreid name rank/],
00336453 39 } => 'SELECT
00336453 40 me.title,
f7f53a89 41 me.cdid,
00336453 42 DISTINCT(foo, bar),
43 me.genreid,
b19b11ef 44 (SELECT me.name, me.rank FROM artist me WHERE ( artistid 1 ))
00336453 45 FROM cd me'
f7f53a89 46 => [qw/title cd_id foo bar genreid name rank/],
00336453 47
48 {
36fd7f07 49 '+select' => { count => 'me.cdid', -as => 'cnt' }, # lack of 'as' infers from '-as'
50 '+columns' => { len => { length => 'me.title' } },
00336453 51 } => 'SELECT
00336453 52 me.title,
53 LENGTH( me.title ),
f7f53a89 54 me.cdid,
55 DISTINCT(foo, bar),
56 me.genreid,
57 (SELECT me.name, me.rank FROM artist me WHERE ( artistid 1 )),
b19b11ef 58 COUNT( me.cdid ) AS cnt
f7f53a89 59 FROM cd me'
60 => [qw/title len cd_id foo bar genreid name rank cnt/],
61 {
62 '+select' => \'unaliased randomness',
63 } => 'SELECT
64 me.title,
65 LENGTH( me.title ),
66 me.cdid,
00336453 67 DISTINCT(foo, bar),
68 me.genreid,
69 (SELECT me.name, me.rank FROM artist me WHERE ( artistid 1 )),
f7f53a89 70 COUNT( me.cdid ) AS cnt,
00336453 71 unaliased randomness
72 FROM cd me'
f7f53a89 73 => [qw/title len cd_id foo bar genreid name rank cnt/],
74 {
75 '+select' => \'MOAR unaliased randomness',
76 } => 'SELECT
77 me.title,
78 LENGTH( me.title ),
79 me.cdid,
80 DISTINCT(foo, bar),
81 me.genreid,
82 (SELECT me.name, me.rank FROM artist me WHERE ( artistid 1 )),
83 COUNT( me.cdid ) AS cnt,
84 unaliased randomness,
85 MOAR unaliased randomness
86 FROM cd me'
87 => [qw/title len cd_id foo bar genreid name rank cnt/],
00336453 88);
89
90my $rs = $schema->resultset('CD');
91
92my $testno = 1;
93while (@chain) {
94 my $attrs = shift @chain;
95 my $sql = shift @chain;
96 my $as = shift @chain;
97
98 $rs = $rs->search ({}, $attrs);
99
100 is_same_sql_bind (
101 $rs->as_query,
102 "($sql)",
103 [],
104 "Test $testno of SELECT assembly ok",
105 );
106
107 is_deeply(
108 $rs->_resolved_attrs->{as},
109 $as,
36fd7f07 110 "Correct dbic-side aliasing for test $testno",
00336453 111 );
112
113 $testno++;
114}
115
f7f53a89 116# make sure proper exceptions are thrown on unbalanced use
117{
118 my $rs = $schema->resultset('CD')->search({}, { select => \'count(me.cdid)'});
119
120 lives_ok(sub {
121 $rs->search({}, { '+select' => 'me.cdid' })->next
122 }, 'Two dark selectors are ok');
123
124 throws_ok(sub {
125 $rs->search({}, { '+select' => 'me.cdid', '+as' => 'cdid' })->next
126 }, qr/resultset contains an unnamed selector/, 'Unnamed followed by named is not');
127
128 throws_ok(sub {
129 $rs->search_rs({}, { prefetch => 'tracks' })->next
130 }, qr/resultset contains an unnamed selector/, 'Throw on unaliased selector followed by prefetch');
131
132 throws_ok(sub {
133 $rs->search_rs({}, { '+select' => 'me.title', '+as' => 'title' })->next
134 }, qr/resultset contains an unnamed selector/, 'Throw on unaliased selector followed by +select/+as');
135}
136
137
00336453 138done_testing;