order_by tests
[dbsrgits/DBIx-Class.git] / t / order / with_bind.t
CommitLineData
0c033974 1use strict;
2use warnings;
3
4use Test::More;
5use lib qw(t/lib);
6use DBICTest;
7use DBIC::SqlMakerTest;
8
9my $schema = DBICTest->init_schema;
10
11my $rs = $schema->resultset('FourKeys');
12
13sub test_order {
14 my $args = shift;
15
16 my $req_order =
17 $args->{order_req}
18 ? "ORDER BY $args->{order_req}"
19 : '';
20
21 is_same_sql_bind(
22 $rs->search(
23 { foo => 'bar' },
24 {
25 order_by => $args->{order_by},
26 having =>
27 [ { read_count => { '>' => 5 } }, \[ 'read_count < ?', 8 ] ]
28 }
29 )->as_query,
30 "(
31 SELECT me.foo, me.bar, me.hello, me.goodbye, me.sensors, me.read_count
32 FROM fourkeys me
33 WHERE ( foo = ? )
34 HAVING read_count > ? OR read_count < ?
35 $req_order
36 )",
37 [
38 [qw(foo bar)], [qw(read_count 5)],
39 8, $args->{bind} ? @{ $args->{bind} } : ()
40 ],
41 );
42}
43
44my @tests = (
45 {
46 order_by => \'foo DESC',
47 order_req => 'foo DESC',
48 bind => [],
49 },
50 {
51 order_by => { -asc => 'foo' },
52 order_req => 'foo ASC',
53 bind => [],
54 },
55 {
56 order_by => { -desc => \[ 'colA LIKE ?', 'test' ] },
57 order_req => 'colA LIKE ? DESC',
58 bind => [qw(test)],
59 },
60 {
61 order_by => \[ 'colA LIKE ? DESC', 'test' ],
62 order_req => 'colA LIKE ? DESC',
63 bind => [qw(test)],
64 },
65 {
66 order_by => [
67 { -asc => \['colA'] },
68 { -desc => \[ 'colB LIKE ?', 'test' ] },
69 { -asc => \[ 'colC LIKE ?', 'tost' ] }
70 ],
71 order_req => 'colA ASC, colB LIKE ? DESC, colC LIKE ? ASC',
72 bind => [qw(test tost)],
73 },
74 { # this would be really really nice!
75 order_by => [
76 { -asc => 'colA' },
77 { -desc => { colB => { 'LIKE' => 'test' } } },
78 { -asc => { colC => { 'LIKE' => 'tost' } } }
79 ],
80 order_req => 'colA ASC, colB LIKE ? DESC, colC LIKE ? ASC',
81 bind => [ [ colB => 'test' ], [ colC => 'tost' ] ], # ???
82 },
83 {
84 order_by => { -desc => { colA => { LIKE => 'test' } } },
85 order_req => 'colA LIKE ? DESC',
86 bind => [qw(test)],
87 },
88);
89
90plan( tests => scalar @tests );
91
92test_order($_) for @tests;
93