Massive rewrite of bind handling, and overall simplification of ::Storage::DBI
[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             [ { sqlt_datatype => 'integer', dbic_colname => 'foo' }
41                 => 'bar' ],
42             [ { sqlt_datatype => 'int', dbic_colname => 'read_count' }
43                 => 5 ],
44             [ { sqlt_datatype => 'int', dbic_colname => 'read_count' }
45                 => 8 ],
46             $args->{bind}
47               ? map { [ { dbic_colname => $_->[0] } => $_->[1] ] } @{ $args->{bind} }
48               : ()
49         ],
50       ) || diag Dumper $args->{order_by};
51     };
52   }
53 }
54
55 my @tests = (
56     {
57         order_by  => \'foo DESC',
58         order_req => 'foo DESC',
59         bind      => [],
60     },
61     {
62         order_by  => { -asc => 'foo' },
63         order_req => 'foo ASC',
64         bind      => [],
65     },
66     {
67         order_by  => { -desc => \[ 'colA LIKE ?', [ colA => 'test' ] ] },
68         order_req => 'colA LIKE ? DESC',
69         bind      => [ [ colA => 'test' ] ],
70     },
71     {
72         order_by  => \[ 'colA LIKE ? DESC', [ colA => 'test' ] ],
73         order_req => 'colA LIKE ? DESC',
74         bind      => [ [ colA => 'test' ] ],
75     },
76     {
77         order_by => [
78             { -asc  => \['colA'] },
79             { -desc => \[ 'colB LIKE ?', [ colB => 'test' ] ] },
80             { -asc  => \[ 'colC LIKE ?', [ colC => 'tost' ] ] },
81         ],
82         order_req => 'colA ASC, colB LIKE ? DESC, colC LIKE ? ASC',
83         bind      => [ [ colB => 'test' ], [ colC => 'tost' ] ],
84     },
85     {
86         todo => 1,
87         order_by => [
88             { -asc  => 'colA' },
89             { -desc => { colB => { 'LIKE' => 'test' } } },
90             { -asc  => { colC => { 'LIKE' => 'tost' } } }
91         ],
92         order_req => 'colA ASC, colB LIKE ? DESC, colC LIKE ? ASC',
93         bind      => [ [ colB => 'test' ], [ colC => 'tost' ] ],
94     },
95     {
96         todo => 1,
97         order_by  => { -desc => { colA  => { LIKE  => 'test' } } },
98         order_req => 'colA LIKE ? DESC',
99         bind      => [ [ colA => 'test' ] ],
100     },
101 );
102
103 test_order($_) for @tests;
104
105 done_testing;