d2a4e83ed896854aa538dc2a2f2a421eea1b0c02
[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 sub test_order {
12
13   TODO: {
14     my $rs = shift;
15     my $args = shift;
16
17     local $TODO = "Not implemented" if $args->{todo};
18
19     lives_ok {
20       is_same_sql_bind(
21         $rs->search(
22             { foo => 'bar' },
23             {
24                 order_by => $args->{order_by},
25                 having =>
26                   [ { read_count => { '>' => 5 } }, \[ 'read_count < ?', [ read_count => 8  ] ] ]
27             }
28           )->as_query,
29         "(
30           SELECT me.foo, me.bar, me.hello, me.goodbye, me.sensors, me.read_count
31           FROM fourkeys me
32           WHERE ( foo = ? )
33           HAVING read_count > ? OR read_count < ?
34           ORDER BY $args->{order_req}
35         )",
36         [
37             [ { sqlt_datatype => 'integer', dbic_colname => 'foo' }
38                 => 'bar' ],
39             [ { sqlt_datatype => 'int', dbic_colname => 'read_count' }
40                 => 5 ],
41             [ { sqlt_datatype => 'int', dbic_colname => 'read_count' }
42                 => 8 ],
43             $args->{bind}
44               ? map { [ { dbic_colname => $_->[0] } => $_->[1] ] } @{ $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 my $rs = DBICTest->init_schema->resultset('FourKeys');
101 test_order($rs, $_) for @tests;
102
103 done_testing;