Add tests using select/as to sqlahacks
[dbsrgits/DBIx-Class.git] / t / sqlahacks / limit_dialects / toplimit.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 # We could test all of this via $sq->$op directly,
13 # but some conditions need a $rsrc
14 delete $schema->storage->_sql_maker->{_cached_syntax};
15 $schema->storage->_sql_maker->limit_dialect ('Top');
16
17 my $rs = $schema->resultset ('BooksInLibrary')->search ({}, { prefetch => 'owner', rows => 1, offset => 3 });
18
19 sub default_test_order {
20    my $order_by = shift;
21    is_same_sql_bind(
22       $rs->search ({}, {order_by => $order_by})->as_query,
23       "(SELECT
24         TOP 1 me__id, source, owner, title, price, owner__id, name FROM
25          (SELECT
26            TOP 4 me.id AS me__id, me.source, me.owner, me.title, me.price, owner.id AS owner__id, owner.name
27            FROM books me
28            JOIN owners owner ON
29            owner.id = me.owner
30            WHERE ( source = ? )
31            ORDER BY me__id ASC
32          ) me ORDER BY me__id DESC
33        )",
34     [ [ source => 'Library' ] ],
35   );
36 }
37
38 sub test_order {
39   my $args = shift;
40
41   my $req_order = $args->{order_req}
42     ? "ORDER BY $args->{order_req}"
43     : ''
44   ;
45
46   is_same_sql_bind(
47     $rs->search ({}, {order_by => $args->{order_by}})->as_query,
48     "(SELECT
49       me__id, source, owner, title, price, owner__id, name FROM
50       (SELECT
51         TOP 1 me__id, source, owner, title, price, owner__id, name FROM
52          (SELECT
53            TOP 4 me.id AS me__id, me.source, me.owner, me.title, me.price, owner.id AS owner__id, owner.name FROM
54            books me
55            JOIN owners owner ON owner.id = me.owner
56            WHERE ( source = ? )
57            ORDER BY $args->{order_inner}
58          ) me ORDER BY $args->{order_outer}
59       ) me $req_order
60     )",
61     [ [ source => 'Library' ] ],
62   );
63 }
64
65 my @tests = (
66   {
67     order_by => \'foo DESC',
68     order_req => 'foo DESC',
69     order_inner => 'foo DESC',
70     order_outer => 'foo ASC'
71   },
72   {
73     order_by => { -asc => 'foo'  },
74     order_req => 'foo ASC',
75     order_inner => 'foo ASC',
76     order_outer => 'foo DESC',
77   },
78   {
79     order_by => 'foo',
80     order_req => 'foo',
81     order_inner => 'foo ASC',
82     order_outer => 'foo DESC',
83   },
84   {
85     order_by => [ qw{ foo bar}   ],
86     order_req => 'foo, bar',
87     order_inner => 'foo ASC, bar ASC',
88     order_outer => 'foo DESC, bar DESC',
89   },
90   {
91     order_by => { -desc => 'foo' },
92     order_req => 'foo DESC',
93     order_inner => 'foo DESC',
94     order_outer => 'foo ASC',
95   },
96   {
97     order_by => ['foo', { -desc => 'bar' } ],
98     order_req => 'foo, bar DESC',
99     order_inner => 'foo ASC, bar DESC',
100     order_outer => 'foo DESC, bar ASC',
101   },
102   {
103     order_by => { -asc => [qw{ foo bar }] },
104     order_req => 'foo ASC, bar ASC',
105     order_inner => 'foo ASC, bar ASC',
106     order_outer => 'foo DESC, bar DESC',
107   },
108   {
109     order_by => [
110       { -asc => 'foo' },
111       { -desc => [qw{bar}] },
112       { -asc  => [qw{hello sensors}]},
113     ],
114     order_req => 'foo ASC, bar DESC, hello ASC, sensors ASC',
115     order_inner => 'foo ASC, bar DESC, hello ASC, sensors ASC',
116     order_outer => 'foo DESC, bar ASC, hello DESC, sensors DESC',
117   },
118 );
119
120 my @default_tests = ( undef, '', {}, [] );
121
122 # plan (tests => scalar @tests + scalar @default_tests + 1);
123
124 test_order ($_) for @tests;
125 default_test_order ($_) for @default_tests;
126
127
128 is_same_sql_bind (
129   $rs->search ({}, { group_by => 'title', order_by => 'title' })->as_query,
130 '(SELECT
131 me.id, me.source, me.owner, me.title, me.price, owner.id, owner.name FROM
132    ( SELECT
133       id, source, owner, title, price FROM
134       ( SELECT
135          TOP 1 id, source, owner, title, price FROM
136          ( SELECT
137             TOP 4 me.id, me.source, me.owner, me.title, me.price FROM
138             books me  JOIN
139             owners owner ON owner.id = me.owner
140             WHERE ( source = ? )
141             GROUP BY title
142             ORDER BY title ASC
143          ) me
144          ORDER BY title DESC
145       ) me
146       ORDER BY title
147    ) me  JOIN
148    owners owner ON owner.id = me.owner WHERE
149    ( source = ? )
150    ORDER BY title)' ,
151   [ [ source => 'Library' ], [ source => 'Library' ] ],
152 );
153
154 my $rs_selectas_top = $schema->resultset ('BooksInLibrary')->search ({}, { '+select' => ['owner.name'], '+as' => ['owner_name'], join => 'owner', rows => 1 });
155
156 is_same_sql_bind( $rs_selectas_top->search({})->as_query,
157                   "(SELECT 
158                       TOP 1 me.id, me.source, me.owner, me.title, me.price, 
159                       owner.name 
160                     FROM books me 
161                     JOIN owners owner ON owner.id = me.owner 
162                     WHERE ( source = ? )  
163                     ORDER BY me.id ASC
164                    )",
165                    [ [ 'source', 'Library' ] ],
166                 );
167
168 done_testing;