Deduplicate (and stabilize) the result of _collapse_cond
[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 ':DiffSQL';
9
10 sub test_order {
11     my $rs = shift;
12     my $args = shift;
13
14     local $TODO = "Not implemented" if $args->{todo};
15
16     lives_ok {
17       is_same_sql_bind(
18         $rs->search(
19             { foo => 'bar' },
20             {
21                 order_by => $args->{order_by},
22                 having =>
23                   [ { read_count => { '>' => 5 } }, \[ 'read_count < ?', [ read_count => 8  ] ] ]
24             }
25           )->as_query,
26         "(
27           SELECT me.foo, me.bar, me.hello, me.goodbye, me.sensors, me.read_count
28           FROM fourkeys me
29           WHERE ( foo = ? )
30           HAVING read_count > ? OR read_count < ?
31           ORDER BY $args->{order_req}
32         )",
33         [
34             [ { sqlt_datatype => 'integer', dbic_colname => 'foo' }
35                 => 'bar' ],
36             [ { sqlt_datatype => 'int', dbic_colname => 'read_count' }
37                 => 5 ],
38             [ { sqlt_datatype => 'int', dbic_colname => 'read_count' }
39                 => 8 ],
40             $args->{bind}
41               ? map { [ { dbic_colname => $_->[0] } => $_->[1] ] } @{ $args->{bind} }
42               : ()
43         ],
44       ) || diag Dumper $args->{order_by};
45     };
46 }
47
48 my @tests = (
49     {
50         order_by  => \'foo DESC',
51         order_req => 'foo DESC',
52         bind      => [],
53     },
54     {
55         order_by  => { -asc => 'foo' },
56         order_req => 'foo ASC',
57         bind      => [],
58     },
59     {
60         order_by  => { -desc => \[ 'colA LIKE ?', [ colA => 'test' ] ] },
61         order_req => 'colA LIKE ? DESC',
62         bind      => [ [ colA => 'test' ] ],
63     },
64     {
65         order_by  => \[ 'colA LIKE ? DESC', [ colA => 'test' ] ],
66         order_req => 'colA LIKE ? DESC',
67         bind      => [ [ colA => 'test' ] ],
68     },
69     {
70         order_by => [
71             { -asc  => \['colA'] },
72             { -desc => \[ 'colB LIKE ?', [ colB => 'test' ] ] },
73             { -asc  => \[ 'colC LIKE ?', [ colC => 'tost' ] ] },
74         ],
75         order_req => 'colA ASC, colB LIKE ? DESC, colC LIKE ? ASC',
76         bind      => [ [ colB => 'test' ], [ colC => 'tost' ] ],
77     },
78     {
79         todo => 1,
80         order_by => [
81             { -asc  => 'colA' },
82             { -desc => { colB => { 'LIKE' => 'test' } } },
83             { -asc  => { colC => { 'LIKE' => 'tost' } } }
84         ],
85         order_req => 'colA ASC, colB LIKE ? DESC, colC LIKE ? ASC',
86         bind      => [ [ colB => 'test' ], [ colC => 'tost' ] ],
87     },
88     {
89         todo => 1,
90         order_by  => { -desc => { colA  => { LIKE  => 'test' } } },
91         order_req => 'colA LIKE ? DESC',
92         bind      => [ [ colA => 'test' ] ],
93     },
94 );
95
96 my $rs = DBICTest->init_schema->resultset('FourKeys');
97 test_order($rs, $_) for @tests;
98
99 done_testing;