Commit | Line | Data |
f5aab26e |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | use Test::More; |
952f9e2d |
6 | use Test::Exception; |
f5aab26e |
7 | |
8 | use SQL::Abstract; |
9 | |
5aad8cf3 |
10 | use SQL::Abstract::Test import => ['is_same_sql_bind']; |
10e6c946 |
11 | my @cases = |
f5aab26e |
12 | ( |
13 | { |
14 | given => \'colA DESC', |
15 | expects => ' ORDER BY colA DESC', |
761a495a |
16 | expects_quoted => ' ORDER BY `colA` DESC', |
f5aab26e |
17 | }, |
18 | { |
19 | given => 'colA', |
20 | expects => ' ORDER BY colA', |
21 | expects_quoted => ' ORDER BY `colA`', |
22 | }, |
71519cf6 |
23 | { # it may look odd, but this is the desired behaviour (mst) |
24 | given => 'colA DESC', |
25 | expects => ' ORDER BY colA DESC', |
26 | expects_quoted => ' ORDER BY `colA DESC`', |
27 | }, |
f5aab26e |
28 | { |
29 | given => [qw/colA colB/], |
30 | expects => ' ORDER BY colA, colB', |
31 | expects_quoted => ' ORDER BY `colA`, `colB`', |
32 | }, |
71519cf6 |
33 | { # it may look odd, but this is the desired behaviour (mst) |
34 | given => ['colA ASC', 'colB DESC'], |
35 | expects => ' ORDER BY colA ASC, colB DESC', |
36 | expects_quoted => ' ORDER BY `colA ASC`, `colB DESC`', |
37 | }, |
f5aab26e |
38 | { |
39 | given => {-asc => 'colA'}, |
40 | expects => ' ORDER BY colA ASC', |
41 | expects_quoted => ' ORDER BY `colA` ASC', |
42 | }, |
43 | { |
44 | given => {-desc => 'colB'}, |
45 | expects => ' ORDER BY colB DESC', |
46 | expects_quoted => ' ORDER BY `colB` DESC', |
47 | }, |
48 | { |
49 | given => [{-asc => 'colA'}, {-desc => 'colB'}], |
50 | expects => ' ORDER BY colA ASC, colB DESC', |
51 | expects_quoted => ' ORDER BY `colA` ASC, `colB` DESC', |
52 | }, |
1cfa1db3 |
53 | { |
54 | given => ['colA', {-desc => 'colB'}], |
55 | expects => ' ORDER BY colA, colB DESC', |
56 | expects_quoted => ' ORDER BY `colA`, `colB` DESC', |
57 | }, |
b6475fb1 |
58 | { |
59 | given => undef, |
60 | expects => '', |
61 | expects_quoted => '', |
62 | }, |
994edb77 |
63 | |
64 | { |
65 | given => [{-desc => [ qw/colA colB/ ] }], |
66 | expects => ' ORDER BY colA DESC, colB DESC', |
67 | expects_quoted => ' ORDER BY `colA` DESC, `colB` DESC', |
68 | }, |
69 | { |
70 | given => [{-desc => [ qw/colA colB/ ] }, {-asc => 'colC'}], |
71 | expects => ' ORDER BY colA DESC, colB DESC, colC ASC', |
72 | expects_quoted => ' ORDER BY `colA` DESC, `colB` DESC, `colC` ASC', |
73 | }, |
74 | { |
75 | given => [{-desc => [ qw/colA colB/ ] }, {-asc => [ qw/colC colD/ ] }], |
76 | expects => ' ORDER BY colA DESC, colB DESC, colC ASC, colD ASC', |
77 | expects_quoted => ' ORDER BY `colA` DESC, `colB` DESC, `colC` ASC, `colD` ASC', |
78 | }, |
79 | { |
80 | given => [{-desc => [ qw/colA colB/ ] }, {-desc => 'colC' }], |
81 | expects => ' ORDER BY colA DESC, colB DESC, colC DESC', |
82 | expects_quoted => ' ORDER BY `colA` DESC, `colB` DESC, `colC` DESC', |
83 | }, |
b8b747f7 |
84 | { |
952f9e2d |
85 | given => [{ -asc => 'colA' }, { -desc => [qw/colB/] }, { -asc => [qw/colC colD/] }], |
86 | expects => ' ORDER BY colA ASC, colB DESC, colC ASC, colD ASC', |
87 | expects_quoted => ' ORDER BY `colA` ASC, `colB` DESC, `colC` ASC, `colD` ASC', |
b8b747f7 |
88 | }, |
293149c5 |
89 | { |
90 | given => { -desc => \['colA LIKE ?', 'test'] }, |
91 | expects => ' ORDER BY colA LIKE ? DESC', |
92 | expects_quoted => ' ORDER BY colA LIKE ? DESC', |
f267b646 |
93 | bind => ['test'], |
94 | }, |
95 | { |
16c7f71c |
96 | given => \['colA LIKE ? DESC', 'test'], |
97 | expects => ' ORDER BY colA LIKE ? DESC', |
98 | expects_quoted => ' ORDER BY colA LIKE ? DESC', |
99 | bind => ['test'], |
100 | }, |
101 | { |
f267b646 |
102 | given => [ { -asc => \['colA'] }, { -desc => \['colB LIKE ?', 'test'] }, { -asc => \['colC LIKE ?', 'tost'] }], |
103 | expects => ' ORDER BY colA ASC, colB LIKE ? DESC, colC LIKE ? ASC', |
104 | expects_quoted => ' ORDER BY colA ASC, colB LIKE ? DESC, colC LIKE ? ASC', |
105 | bind => [qw/test tost/], |
293149c5 |
106 | }, |
36e3ea6c |
107 | { |
0f87e4f0 |
108 | given => [ { -ASC => 'colA', -NULLS => 'FIRST' }, { -DESC => 'colB', -NULLS => 'LAST' } ], |
36e3ea6c |
109 | expects => ' ORDER BY colA NULLS FIRST, colB DESC NULLS LAST', |
110 | expects_quoted => ' ORDER BY `colA` NULLS FIRST, `colB` DESC NULLS LAST', |
111 | }, |
112 | { |
113 | given => [ { -asc => 'colA', -nulls => 'first' }, { -desc => 'colB', -nulls => 'last' } ], |
114 | expects => ' ORDER BY colA NULLS FIRST, colB DESC NULLS LAST', |
115 | expects_quoted => ' ORDER BY `colA` NULLS FIRST, `colB` DESC NULLS LAST', |
116 | }, |
117 | { |
118 | given => { -asc => [qw/colA colB/], -nulls => 'first' } , |
119 | expects => ' ORDER BY colA NULLS FIRST, colB NULLS FIRST', |
120 | expects_quoted => ' ORDER BY `colA` NULLS FIRST, `colB` NULLS FIRST', |
121 | }, |
f5aab26e |
122 | ); |
123 | |
6dcf723c |
124 | my $sql = SQL::Abstract->new; |
125 | my $sqlq = SQL::Abstract->new({quote_char => '`'}); |
126 | |
f267b646 |
127 | for my $case( @cases) { |
128 | my ($stat, @bind); |
129 | |
130 | ($stat, @bind) = $sql->_order_by($case->{given}); |
131 | is_same_sql_bind ( |
132 | $stat, |
133 | \@bind, |
134 | $case->{expects}, |
135 | $case->{bind} || [], |
136 | ); |
137 | |
138 | ($stat, @bind) = $sqlq->_order_by($case->{given}); |
139 | is_same_sql_bind ( |
140 | $stat, |
141 | \@bind, |
142 | $case->{expects_quoted}, |
143 | $case->{bind} || [], |
144 | ); |
f5aab26e |
145 | } |
952f9e2d |
146 | |
147 | throws_ok ( |
148 | sub { $sql->_order_by({-desc => 'colA', -asc => 'colB' }) }, |
36e3ea6c |
149 | qr/hash passed .+ must have exactly one of/, |
952f9e2d |
150 | 'Undeterministic order exception', |
151 | ); |
152 | |
153 | throws_ok ( |
154 | sub { $sql->_order_by({-desc => [ qw/colA colB/ ], -asc => [ qw/colC colD/ ] }) }, |
36e3ea6c |
155 | qr/hash passed .+ must have exactly one of/, |
952f9e2d |
156 | 'Undeterministic order exception', |
157 | ); |
36e3ea6c |
158 | |
159 | throws_ok( |
160 | sub { $sql->_order_by({-wibble => "fleem" }) }, |
161 | qr/invalid key in hash/, |
162 | 'Invalid order exception', |
163 | ); |
164 | |
165 | throws_ok( |
166 | sub { $sql->_order_by({-nulls => "fleem" }) }, |
167 | qr/invalid value for -nulls/, |
168 | 'Invalid nulls exception', |
169 | ); |
c771453a |
170 | |
10e6c946 |
171 | done_testing; |