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