order_by tests
[dbsrgits/DBIx-Class.git] / t / order / with_bind.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use lib qw(t/lib);
6 use DBICTest;
7 use DBIC::SqlMakerTest;
8
9 my $schema = DBICTest->init_schema;
10
11 my $rs = $schema->resultset('FourKeys');
12
13 sub test_order {
14     my $args = shift;
15
16     my $req_order =
17       $args->{order_req}
18       ? "ORDER BY $args->{order_req}"
19       : '';
20
21     is_same_sql_bind(
22         $rs->search(
23             { foo => 'bar' },
24             {
25                 order_by => $args->{order_by},
26                 having =>
27                   [ { read_count => { '>' => 5 } }, \[ 'read_count < ?', 8 ] ]
28             }
29           )->as_query,
30         "(
31           SELECT me.foo, me.bar, me.hello, me.goodbye, me.sensors, me.read_count 
32           FROM fourkeys me 
33           WHERE ( foo = ? ) 
34           HAVING read_count > ? OR read_count < ?
35           $req_order
36         )",
37         [
38             [qw(foo bar)], [qw(read_count 5)],
39             8, $args->{bind} ? @{ $args->{bind} } : ()
40         ],
41     );
42 }
43
44 my @tests = (
45     {
46         order_by  => \'foo DESC',
47         order_req => 'foo DESC',
48         bind      => [],
49     },
50     {
51         order_by  => { -asc => 'foo' },
52         order_req => 'foo ASC',
53         bind      => [],
54     },
55     {
56         order_by  => { -desc => \[ 'colA LIKE ?', 'test' ] },
57         order_req => 'colA LIKE ? DESC',
58         bind      => [qw(test)],
59     },
60     {
61         order_by  => \[ 'colA LIKE ? DESC', 'test' ],
62         order_req => 'colA LIKE ? DESC',
63         bind      => [qw(test)],
64     },
65     {
66         order_by => [
67             { -asc  => \['colA'] },
68             { -desc => \[ 'colB LIKE ?', 'test' ] },
69             { -asc  => \[ 'colC LIKE ?', 'tost' ] }
70         ],
71         order_req => 'colA ASC, colB LIKE ? DESC, colC LIKE ? ASC',
72         bind      => [qw(test tost)],
73     },
74     {    # this would be really really nice!
75         order_by => [
76             { -asc  => 'colA' },
77             { -desc => { colB => { 'LIKE' => 'test' } } },
78             { -asc  => { colC => { 'LIKE' => 'tost' } } }
79         ],
80         order_req => 'colA ASC, colB LIKE ? DESC, colC LIKE ? ASC',
81         bind      => [ [ colB => 'test' ], [ colC => 'tost' ] ],      # ???
82     },
83     {
84         order_by  => { -desc => { colA  => { LIKE  => 'test' } } },
85         order_req => 'colA LIKE ? DESC',
86         bind      => [qw(test)],
87     },
88 );
89
90 plan( tests => scalar @tests );
91
92 test_order($_) for @tests;
93