f88473969599710404acbfbb6a3c877605252be6
[dbsrgits/DBIx-Class.git] / t / sqlmaker / order_by_bindtransport.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use Test::Exception;
6 use Data::Dumper::Concise;
7 use lib qw(t/lib);
8 use DBICTest;
9 use DBIC::SqlMakerTest;
10
11 my $schema = DBICTest->init_schema;
12
13 my $rs = $schema->resultset('FourKeys');
14
15 sub test_order {
16
17   TODO: {
18     my $args = shift;
19
20     local $TODO = "Not implemented" if $args->{todo};
21
22     lives_ok {
23       is_same_sql_bind(
24         $rs->search(
25             { foo => 'bar' },
26             {
27                 order_by => $args->{order_by},
28                 having =>
29                   [ { read_count => { '>' => 5 } }, \[ 'read_count < ?', [ read_count => 8  ] ] ]
30             }
31           )->as_query,
32         "(
33           SELECT me.foo, me.bar, me.hello, me.goodbye, me.sensors, me.read_count 
34           FROM fourkeys me 
35           WHERE ( foo = ? ) 
36           HAVING read_count > ? OR read_count < ?
37           ORDER BY $args->{order_req}
38         )",
39         [
40             [qw(foo bar)],
41             [qw(read_count 5)],
42             [qw(read_count 8)],
43             $args->{bind}
44               ? @{ $args->{bind} }
45               : ()
46         ],
47       ) || diag Dumper $args->{order_by};
48     };
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 ?', [ colA => 'test' ] ] },
65         order_req => 'colA LIKE ? DESC',
66         bind      => [ [ colA => 'test' ] ],
67     },
68     {
69         order_by  => \[ 'colA LIKE ? DESC', [ colA => 'test' ] ],
70         order_req => 'colA LIKE ? DESC',
71         bind      => [ [ colA => 'test' ] ],
72     },
73     {
74         order_by => [
75             { -asc  => \['colA'] },
76             { -desc => \[ 'colB LIKE ?', [ colB => 'test' ] ] },
77             { -asc  => \[ 'colC LIKE ?', [ colC => 'tost' ] ] },
78         ],
79         order_req => 'colA ASC, colB LIKE ? DESC, colC LIKE ? ASC',
80         bind      => [ [ colB => 'test' ], [ colC => 'tost' ] ],
81     },
82     {
83         todo => 1,
84         order_by => [
85             { -asc  => 'colA' },
86             { -desc => { colB => { 'LIKE' => 'test' } } },
87             { -asc  => { colC => { 'LIKE' => 'tost' } } }
88         ],
89         order_req => 'colA ASC, colB LIKE ? DESC, colC LIKE ? ASC',
90         bind      => [ [ colB => 'test' ], [ colC => 'tost' ] ],
91     },
92     {
93         todo => 1,
94         order_by  => { -desc => { colA  => { LIKE  => 'test' } } },
95         order_req => 'colA LIKE ? DESC',
96         bind      => [ [ colA => 'test' ] ],
97     },
98 );
99
100 test_order($_) for @tests;
101
102 done_testing;