Define how Top limit emulation should behave
[dbsrgits/DBIx-Class.git] / t / 42toplimit.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use lib qw(t/lib);
6 use DBICTest;
7 use DBIC::SqlMakerTest;
8
9 my $schema = DBICTest->init_schema;
10
11 # Trick the sqlite DB to use Top limit emulation
12 delete $schema->storage->_sql_maker->{_cached_syntax};
13 $schema->storage->_sql_maker->limit_dialect ('Top');
14
15 my $rs = $schema->resultset ('FourKeys')->search ({}, { rows => 1, offset => 3 });
16
17 sub test_order {
18   my $args = shift;
19
20   my $req_order = $args->{order_req}
21     ? "ORDER BY $args->{order_req}"
22     : ''
23   ;
24
25   is_same_sql_bind(
26     $rs->search ({}, {order_by => $args->{order_by}})->as_query,
27     "(
28       SELECT * FROM (
29         SELECT TOP 1 * FROM (
30           SELECT TOP 4 me.foo, me.bar, me.hello, me.goodbye, me.sensors, me.read_count FROM fourkeys me ORDER BY $args->{order_inner}
31         ) foo ORDER BY $args->{order_outer}
32       ) bar
33       $req_order
34     )",
35     [],
36   );
37 }
38
39 my @tests = (
40   {
41     order_by => \ 'foo DESC',
42     order_req => 'foo DESC',
43     order_inner => 'foo DESC',
44     order_outer => 'foo ASC' 
45   },
46   {
47     order_by => { -asc => 'foo'  },
48     order_req => 'foo ASC',
49     order_inner => 'foo ASC',
50     order_outer => 'foo DESC',
51   },
52   {
53     order_by => 'foo',
54     order_req => 'foo',
55     order_inner => 'foo ASC',
56     order_outer => 'foo DESC',
57   },
58   {
59     order_by => [ qw{ foo bar}   ],
60     order_req => 'foo, bar',
61     order_inner => 'foo ASC,bar ASC',
62     order_outer => 'foo DESC, bar DESC',
63   },
64   {
65     order_by => { -desc => 'foo' },
66     order_req => 'foo DESC',
67     order_inner => 'foo DESC',
68     order_outer => 'foo ASC',
69   },
70   {
71     order_by => ['foo', { -desc => 'bar' } ],
72     order_req => 'foo, bar DESC',
73     order_inner => 'foo ASC, bar DESC',
74     order_outer => 'foo DESC, bar ASC',
75   },
76   {
77     order_by => { -asc => [qw{ foo bar }] },
78     order_req => 'foo ASC, bar ASC',
79     order_inner => 'foo ASC, bar ASC',
80     order_outer => 'foo DESC, bar DESC',
81   },
82   {
83     order_by => [
84       { -asc => 'foo' },
85       { -desc => [qw{bar}] },
86       { -asc  => [qw{hello sensors}]},
87     ],
88     order_req => 'foo ASC, bar DESC, hello ASC, sensors ASC',
89     order_inner => 'foo ASC, bar DESC, hello ASC, sensors ASC',
90     order_outer => 'foo DESC, bar ASC, hello DESC, sensors DESC',
91   },
92   {
93     order_by => undef,
94     order_req => undef,
95     order_inner => 'foo ASC, bar ASC, hello ASC, goodbye ASC',
96     order_outer => 'foo DESC, bar DESC, hello DESC, goodbye DESC',
97   },
98   {
99     order_by => '',
100     order_req => undef,
101     order_inner => 'foo ASC, bar ASC, hello ASC, goodbye ASC',
102     order_outer => 'foo DESC, bar DESC, hello DESC, goodbye DESC',
103   },
104   {
105     order_by => {},
106     order_req => undef,
107     order_inner => 'foo ASC, bar ASC, hello ASC, goodbye ASC',
108     order_outer => 'foo DESC, bar DESC, hello DESC, goodbye DESC',
109   },
110   {
111     order_by => [],
112     order_req => undef,
113     order_inner => 'foo ASC, bar ASC, hello ASC, goodbye ASC',
114     order_outer => 'foo DESC, bar DESC, hello DESC, goodbye DESC',
115   },
116 );
117
118 plan (tests => scalar @tests);
119 test_order ($_) for @tests;