bf478df45d9082c5e39a45caea3789eac795886d
[dbsrgits/SQL-Abstract.git] / t / 06order_by.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5 use Test::More;
6 use Test::Exception;
7
8 use SQL::Abstract;
9
10 use SQL::Abstract::Test import => ['is_same_sql_bind'];
11 my @cases =
12   (
13    {
14     given => \'colA DESC',
15     expects => ' ORDER BY colA DESC',
16     expects_quoted => ' ORDER BY colA DESC',
17    },
18    {
19     given => 'colA',
20     expects => ' ORDER BY colA',
21     expects_quoted => ' ORDER BY `colA`',
22    },
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    },
28    {
29     given => [qw/colA colB/],
30     expects => ' ORDER BY colA, colB',
31     expects_quoted => ' ORDER BY `colA`, `colB`',
32    },
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    },
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    },
53    {
54     given => ['colA', {-desc => 'colB'}],
55     expects => ' ORDER BY colA, colB DESC',
56     expects_quoted => ' ORDER BY `colA`, `colB` DESC',
57    },
58    {
59     given => undef,
60     expects => '',
61     expects_quoted => '',
62    },
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    },
84    {
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',
88    },
89    {
90     given => { -desc => \['colA LIKE ?', 'test'] },
91     expects => ' ORDER BY colA LIKE ? DESC',
92     expects_quoted => ' ORDER BY colA LIKE ? DESC',
93     bind => ['test'],
94    },
95    {
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    {
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/],
106    },
107   );
108
109 my $sql  = SQL::Abstract->new;
110 my $sqlq = SQL::Abstract->new({quote_char => '`'});
111
112 for my $case( @cases) {
113   my ($stat, @bind);
114
115   ($stat, @bind) = $sql->_order_by($case->{given});
116   is_same_sql_bind (
117     $stat,
118     \@bind,
119     $case->{expects},
120     $case->{bind} || [],
121   );
122
123   ($stat, @bind) = $sqlq->_order_by($case->{given});
124   is_same_sql_bind (
125     $stat,
126     \@bind,
127     $case->{expects_quoted},
128     $case->{bind} || [],
129   );
130 }
131
132 throws_ok (
133   sub { $sql->_order_by({-desc => 'colA', -asc => 'colB' }) },
134   qr/hash passed .+ must have exactly one key/,
135   'Undeterministic order exception',
136 );
137
138 throws_ok (
139   sub { $sql->_order_by({-desc => [ qw/colA colB/ ], -asc => [ qw/colC colD/ ] }) },
140   qr/hash passed .+ must have exactly one key/,
141   'Undeterministic order exception',
142 );
143
144 done_testing;