Lose yet another dep (Data::Dumper::Concise)
[dbsrgits/DBIx-Class.git] / t / sqlmaker / order_by_bindtransport.t
1 BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7 use Test::Exception;
8
9 use DBIx::Class::_Util 'dump_value';
10 use DBICTest ':DiffSQL';
11
12 sub test_order {
13     my $rs = shift;
14     my $args = shift;
15
16     local $TODO = "Not implemented" if $args->{todo};
17
18     lives_ok {
19       is_same_sql_bind(
20         $rs->search(
21             { foo => 'bar' },
22             {
23                 order_by => $args->{order_by},
24                 having =>
25                   [ { read_count => { '>' => 5 } }, \[ 'read_count < ?', [ read_count => 8  ] ] ]
26             }
27           )->as_query,
28         "(
29           SELECT me.foo, me.bar, me.hello, me.goodbye, me.sensors, me.read_count
30           FROM fourkeys me
31           WHERE ( foo = ? )
32           HAVING read_count > ? OR read_count < ?
33           ORDER BY $args->{order_req}
34         )",
35         [
36             [ { sqlt_datatype => 'integer', dbic_colname => 'foo' }
37                 => 'bar' ],
38             [ { sqlt_datatype => 'int', dbic_colname => 'read_count' }
39                 => 5 ],
40             [ { sqlt_datatype => 'int', dbic_colname => 'read_count' }
41                 => 8 ],
42             $args->{bind}
43               ? map { [ { dbic_colname => $_->[0] } => $_->[1] ] } @{ $args->{bind} }
44               : ()
45         ],
46       ) || diag dump_value $args->{order_by};
47     };
48 }
49
50 my @tests = (
51     {
52         order_by  => \'foo DESC',
53         order_req => 'foo DESC',
54         bind      => [],
55     },
56     {
57         order_by  => { -asc => 'foo' },
58         order_req => 'foo ASC',
59         bind      => [],
60     },
61     {
62         order_by  => { -desc => \[ 'colA LIKE ?', [ colA => 'test' ] ] },
63         order_req => 'colA LIKE ? DESC',
64         bind      => [ [ colA => 'test' ] ],
65     },
66     {
67         order_by  => \[ 'colA LIKE ? DESC', [ colA => 'test' ] ],
68         order_req => 'colA LIKE ? DESC',
69         bind      => [ [ colA => 'test' ] ],
70     },
71     {
72         order_by => [
73             { -asc  => \['colA'] },
74             { -desc => \[ 'colB LIKE ?', [ colB => 'test' ] ] },
75             { -asc  => \[ 'colC LIKE ?', [ colC => 'tost' ] ] },
76         ],
77         order_req => 'colA ASC, colB LIKE ? DESC, colC LIKE ? ASC',
78         bind      => [ [ colB => 'test' ], [ colC => 'tost' ] ],
79     },
80     {
81         todo => 1,
82         order_by => [
83             { -asc  => 'colA' },
84             { -desc => { colB => { 'LIKE' => 'test' } } },
85             { -asc  => { colC => { 'LIKE' => 'tost' } } }
86         ],
87         order_req => 'colA ASC, colB LIKE ? DESC, colC LIKE ? ASC',
88         bind      => [ [ colB => 'test' ], [ colC => 'tost' ] ],
89     },
90     {
91         todo => 1,
92         order_by  => { -desc => { colA  => { LIKE  => 'test' } } },
93         order_req => 'colA LIKE ? DESC',
94         bind      => [ [ colA => 'test' ] ],
95     },
96 );
97
98 my $rs = DBICTest->init_schema->resultset('FourKeys');
99 test_order($rs, $_) for @tests;
100
101 done_testing;