added failing test for -desc => \['colA LIKE ?', 'test']
[scpubgit/Q-Branch.git] / t / 06order_by.t
CommitLineData
f5aab26e 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5use Test::More;
952f9e2d 6use Test::Exception;
f5aab26e 7
8use SQL::Abstract;
9
5aad8cf3 10use SQL::Abstract::Test import => ['is_same_sql_bind'];
f5aab26e 11my @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 },
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',
93 },
f5aab26e 94 );
95
f5aab26e 96
952f9e2d 97plan tests => (scalar(@cases) * 2) + 2;
f5aab26e 98
6dcf723c 99my $sql = SQL::Abstract->new;
100my $sqlq = SQL::Abstract->new({quote_char => '`'});
101
f5aab26e 102for my $case( @cases){
103 is($sql->_order_by($case->{given}), $case->{expects});
104 is($sqlq->_order_by($case->{given}), $case->{expects_quoted});
105}
952f9e2d 106
107throws_ok (
108 sub { $sql->_order_by({-desc => 'colA', -asc => 'colB' }) },
109 qr/hash passed .+ must have exactly one key/,
110 'Undeterministic order exception',
111);
112
113throws_ok (
114 sub { $sql->_order_by({-desc => [ qw/colA colB/ ], -asc => [ qw/colC colD/ ] }) },
115 qr/hash passed .+ must have exactly one key/,
116 'Undeterministic order exception',
117);