Commit | Line | Data |
0c033974 |
1 | use strict; |
2 | use warnings; |
3 | |
4 | use Test::More; |
a2368d30 |
5 | use Test::Exception; |
a17640f1 |
6 | use Data::Dumper::Concise; |
0c033974 |
7 | use lib qw(t/lib); |
a5a7bb73 |
8 | use DBICTest ':DiffSQL'; |
0c033974 |
9 | |
0c033974 |
10 | sub test_order { |
65d35121 |
11 | my $rs = shift; |
0c033974 |
12 | my $args = shift; |
13 | |
a2368d30 |
14 | local $TODO = "Not implemented" if $args->{todo}; |
0c033974 |
15 | |
a2368d30 |
16 | lives_ok { |
17 | is_same_sql_bind( |
0c033974 |
18 | $rs->search( |
19 | { foo => 'bar' }, |
20 | { |
21 | order_by => $args->{order_by}, |
22 | having => |
a17640f1 |
23 | [ { read_count => { '>' => 5 } }, \[ 'read_count < ?', [ read_count => 8 ] ] ] |
0c033974 |
24 | } |
25 | )->as_query, |
26 | "( |
8273e845 |
27 | SELECT me.foo, me.bar, me.hello, me.goodbye, me.sensors, me.read_count |
28 | FROM fourkeys me |
29 | WHERE ( foo = ? ) |
0c033974 |
30 | HAVING read_count > ? OR read_count < ? |
a2368d30 |
31 | ORDER BY $args->{order_req} |
0c033974 |
32 | )", |
33 | [ |
0e773352 |
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 ], |
a2368d30 |
40 | $args->{bind} |
0e773352 |
41 | ? map { [ { dbic_colname => $_->[0] } => $_->[1] ] } @{ $args->{bind} } |
a2368d30 |
42 | : () |
0c033974 |
43 | ], |
a17640f1 |
44 | ) || diag Dumper $args->{order_by}; |
a2368d30 |
45 | }; |
0c033974 |
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 | { |
a17640f1 |
60 | order_by => { -desc => \[ 'colA LIKE ?', [ colA => 'test' ] ] }, |
0c033974 |
61 | order_req => 'colA LIKE ? DESC', |
a17640f1 |
62 | bind => [ [ colA => 'test' ] ], |
0c033974 |
63 | }, |
64 | { |
a17640f1 |
65 | order_by => \[ 'colA LIKE ? DESC', [ colA => 'test' ] ], |
0c033974 |
66 | order_req => 'colA LIKE ? DESC', |
a17640f1 |
67 | bind => [ [ colA => 'test' ] ], |
0c033974 |
68 | }, |
69 | { |
70 | order_by => [ |
71 | { -asc => \['colA'] }, |
a17640f1 |
72 | { -desc => \[ 'colB LIKE ?', [ colB => 'test' ] ] }, |
73 | { -asc => \[ 'colC LIKE ?', [ colC => 'tost' ] ] }, |
0c033974 |
74 | ], |
75 | order_req => 'colA ASC, colB LIKE ? DESC, colC LIKE ? ASC', |
a17640f1 |
76 | bind => [ [ colB => 'test' ], [ colC => 'tost' ] ], |
0c033974 |
77 | }, |
a2368d30 |
78 | { |
a17640f1 |
79 | todo => 1, |
0c033974 |
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', |
a17640f1 |
86 | bind => [ [ colB => 'test' ], [ colC => 'tost' ] ], |
0c033974 |
87 | }, |
88 | { |
a17640f1 |
89 | todo => 1, |
0c033974 |
90 | order_by => { -desc => { colA => { LIKE => 'test' } } }, |
91 | order_req => 'colA LIKE ? DESC', |
a17640f1 |
92 | bind => [ [ colA => 'test' ] ], |
0c033974 |
93 | }, |
94 | ); |
95 | |
65d35121 |
96 | my $rs = DBICTest->init_schema->resultset('FourKeys'); |
97 | test_order($rs, $_) for @tests; |
22912b45 |
98 | |
a17640f1 |
99 | done_testing; |