42abaa6e0d352dd8535a5da583381b8b22b5d0be
[dbsrgits/SQL-Abstract.git] / t / 06order_by.t
1 use strict;
2 use warnings;
3 use Test::More;
4 use Test::Exception;
5
6 use SQL::Abstract;
7
8 use SQL::Abstract::Test import => ['is_same_sql_bind'];
9 my @cases =
10   (
11    {
12     given => \'colA DESC',
13     expects => ' ORDER BY colA DESC',
14     expects_quoted => ' ORDER BY colA DESC',
15    },
16    {
17     given => 'colA',
18     expects => ' ORDER BY colA',
19     expects_quoted => ' ORDER BY `colA`',
20    },
21    {  # it may look odd, but this is the desired behaviour (mst)
22     given => 'colA DESC',
23     expects => ' ORDER BY colA DESC',
24     expects_quoted => ' ORDER BY `colA DESC`',
25    },
26    {
27     given => [qw/colA colB/],
28     expects => ' ORDER BY colA, colB',
29     expects_quoted => ' ORDER BY `colA`, `colB`',
30    },
31    {  # it may look odd, but this is the desired behaviour (mst)
32     given => ['colA ASC', 'colB DESC'],
33     expects => ' ORDER BY colA ASC, colB DESC',
34     expects_quoted => ' ORDER BY `colA ASC`, `colB DESC`',
35    },
36    {
37     given => {-asc => 'colA'},
38     expects => ' ORDER BY colA ASC',
39     expects_quoted => ' ORDER BY `colA` ASC',
40    },
41    {
42     given => {-desc => 'colB'},
43     expects => ' ORDER BY colB DESC',
44     expects_quoted => ' ORDER BY `colB` DESC',
45    },
46    {
47     given => [{-asc => 'colA'}, {-desc => 'colB'}],
48     expects => ' ORDER BY colA ASC, colB DESC',
49     expects_quoted => ' ORDER BY `colA` ASC, `colB` DESC',
50    },
51    {
52     given => ['colA', {-desc => 'colB'}],
53     expects => ' ORDER BY colA, colB DESC',
54     expects_quoted => ' ORDER BY `colA`, `colB` DESC',
55    },
56    {
57     given => undef,
58     expects => '',
59     expects_quoted => '',
60    },
61
62    {
63     given => [{-desc => [ qw/colA colB/ ] }],
64     expects => ' ORDER BY colA DESC, colB DESC',
65     expects_quoted => ' ORDER BY `colA` DESC, `colB` DESC',
66    },
67    {
68     given => [{-desc => [ qw/colA colB/ ] }, {-asc => 'colC'}],
69     expects => ' ORDER BY colA DESC, colB DESC, colC ASC',
70     expects_quoted => ' ORDER BY `colA` DESC, `colB` DESC, `colC` ASC',
71    },
72    {
73     given => [{-desc => [ qw/colA colB/ ] }, {-asc => [ qw/colC colD/ ] }],
74     expects => ' ORDER BY colA DESC, colB DESC, colC ASC, colD ASC',
75     expects_quoted => ' ORDER BY `colA` DESC, `colB` DESC, `colC` ASC, `colD` ASC',
76    },
77    {
78     given => [{-desc => [ qw/colA colB/ ] }, {-desc => 'colC' }],
79     expects => ' ORDER BY colA DESC, colB DESC, colC DESC',
80     expects_quoted => ' ORDER BY `colA` DESC, `colB` DESC, `colC` DESC',
81    },
82    {
83     given => [{ -asc => 'colA' }, { -desc => [qw/colB/] }, { -asc => [qw/colC colD/] }],
84     expects => ' ORDER BY colA ASC, colB DESC, colC ASC, colD ASC',
85     expects_quoted => ' ORDER BY `colA` ASC, `colB` DESC, `colC` ASC, `colD` ASC',
86    },
87    {
88     given => { -desc => \['colA LIKE ?', 'test'] },
89     expects => ' ORDER BY colA LIKE ? DESC',
90     expects_quoted => ' ORDER BY colA LIKE ? DESC',
91     bind => ['test'],
92    },
93    {
94     given => \['colA LIKE ? DESC', 'test'],
95     expects => ' ORDER BY colA LIKE ? DESC',
96     expects_quoted => ' ORDER BY colA LIKE ? DESC',
97     bind => ['test'],
98    },
99    {
100     given => [ { -asc => \['colA'] }, { -desc => \['colB LIKE ?', 'test'] }, { -asc => \['colC LIKE ?', 'tost'] }],
101     expects => ' ORDER BY colA ASC, colB LIKE ? DESC, colC LIKE ? ASC',
102     expects_quoted => ' ORDER BY colA ASC, colB LIKE ? DESC, colC LIKE ? ASC',
103     bind => [qw/test tost/],
104    },
105   );
106
107 my $sql  = SQL::Abstract->new;
108 my $sqlq = SQL::Abstract->new({quote_char => '`'});
109
110 for my $case( @cases) {
111   my ($stat, @bind);
112
113   ($stat, @bind) = $sql->where(undef, $case->{given});
114   is_same_sql_bind (
115     $stat,
116     \@bind,
117     $case->{expects},
118     $case->{bind} || [],
119   );
120
121   ($stat, @bind) = $sqlq->where(undef, $case->{given});
122   is_same_sql_bind (
123     $stat,
124     \@bind,
125     $case->{expects_quoted},
126     $case->{bind} || [],
127   );
128 }
129
130 throws_ok (
131   sub { $sql->_order_by({-desc => 'colA', -asc => 'colB' }) },
132   qr/hash passed .+ must have exactly one key/,
133   'Undeterministic order exception',
134 );
135
136 throws_ok (
137   sub { $sql->_order_by({-desc => [ qw/colA colB/ ], -asc => [ qw/colC colD/ ] }) },
138   qr/hash passed .+ must have exactly one key/,
139   'Undeterministic order exception',
140 );
141
142 done_testing;