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