Make POD more readable, add a (failing) multikey order_by test
[scpubgit/Q-Branch.git] / t / 06order_by.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5 use Test::More;
6
7 use SQL::Abstract;
8
9 use SQL::Abstract::Test import => ['is_same_sql_bind'];
10 my @cases = 
11   (
12    {
13     given => \'colA DESC',
14     expects => ' ORDER BY colA DESC',
15     expects_quoted => ' ORDER BY colA DESC',
16    },
17    {
18     given => 'colA',
19     expects => ' ORDER BY colA',
20     expects_quoted => ' ORDER BY `colA`',
21    },
22    {  # it may look odd, but this is the desired behaviour (mst)
23     given => 'colA DESC',
24     expects => ' ORDER BY colA DESC',
25     expects_quoted => ' ORDER BY `colA DESC`',
26    },
27    {
28     given => [qw/colA colB/],
29     expects => ' ORDER BY colA, colB',
30     expects_quoted => ' ORDER BY `colA`, `colB`',
31    },
32    {  # it may look odd, but this is the desired behaviour (mst)
33     given => ['colA ASC', 'colB DESC'],
34     expects => ' ORDER BY colA ASC, colB DESC',
35     expects_quoted => ' ORDER BY `colA ASC`, `colB DESC`',
36    },
37    {
38     given => {-asc => 'colA'},
39     expects => ' ORDER BY colA ASC',
40     expects_quoted => ' ORDER BY `colA` ASC',
41    },
42    {
43     given => {-desc => 'colB'},
44     expects => ' ORDER BY colB DESC',
45     expects_quoted => ' ORDER BY `colB` DESC',
46    },
47    {
48     given => [{-asc => 'colA'}, {-desc => 'colB'}],
49     expects => ' ORDER BY colA ASC, colB DESC',
50     expects_quoted => ' ORDER BY `colA` ASC, `colB` DESC',
51    },
52    {
53     given => ['colA', {-desc => 'colB'}],
54     expects => ' ORDER BY colA, colB DESC',
55     expects_quoted => ' ORDER BY `colA`, `colB` DESC',
56    },
57    {
58     given => undef,
59     expects => '',
60     expects_quoted => '',
61    },
62
63    {
64     given => [{-desc => [ qw/colA colB/ ] }],
65     expects => ' ORDER BY colA DESC, colB DESC',
66     expects_quoted => ' ORDER BY `colA` DESC, `colB` DESC',
67    },
68    {
69     given => [{-desc => [ qw/colA colB/ ] }, {-asc => 'colC'}],
70     expects => ' ORDER BY colA DESC, colB DESC, colC ASC',
71     expects_quoted => ' ORDER BY `colA` DESC, `colB` DESC, `colC` ASC',
72    },
73    {
74     given => [{-desc => [ qw/colA colB/ ] }, {-asc => [ qw/colC colD/ ] }],
75     expects => ' ORDER BY colA DESC, colB DESC, colC ASC, colD ASC',
76     expects_quoted => ' ORDER BY `colA` DESC, `colB` DESC, `colC` ASC, `colD` ASC',
77    },
78    {
79     given => [{-desc => [ qw/colA colB/ ] }, {-desc => 'colC' }],
80     expects => ' ORDER BY colA DESC, colB DESC, colC DESC',
81     expects_quoted => ' ORDER BY `colA` DESC, `colB` DESC, `colC` DESC',
82    },
83    {
84     given => [{-desc => [ qw/colA colB/ ], -asc => [ qw/colC colD/ ] }],
85     expects => ' ORDER BY colA DESC, colB DESC, colC ASC, colD ASC',
86     expects_quoted => ' ORDER BY `colA` DESC, `colB` DESC, `colC` ASC, `colD` ASC',
87    },
88
89   );
90
91
92 plan tests => (scalar(@cases) * 2);
93
94 my $sql  = SQL::Abstract->new;
95 my $sqlq = SQL::Abstract->new({quote_char => '`'});
96
97 for my $case( @cases){
98   is($sql->_order_by($case->{given}), $case->{expects});
99   is($sqlq->_order_by($case->{given}), $case->{expects_quoted});
100 }