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