first shot at moving stuff around
[dbsrgits/DBIx-Class.git] / t / sqlahacks / 42rno.t
CommitLineData
73b15388 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$schema->storage_type('::DBI::MSSQL');
11
12# Trick the sqlite DB to use Top limit emulation
13# We could test all of this via $sq->$op directly,
14# but some conditions need a $rsrc
15delete $schema->storage->_sql_maker->{_cached_syntax};
16$schema->storage->_sql_maker->limit_dialect ('RowNumberOver');
17
18my $rs = $schema->resultset ('BooksInLibrary')->search ({}, { prefetch => 'owner', rows => 1, offset => 3 });
19
20sub default_test_order {
21 my $order_by = shift;
22 is_same_sql_bind(
23 $rs->search ({}, {order_by => $order_by})->as_query,
24 "(SELECT me__id, me__source, me__owner, me__title, me__price, owner__id, owner__name FROM
25 (
26 SELECT Q1.*, ROW_NUMBER() OVER( ) AS ROW_NUM FROM (
27 SELECT me.id, me.source, me.owner, me.title, me.price, owner.id, owner.name
28 FROM books me JOIN owners owner ON owner.id = me.owner WHERE ( source = ? )
29 ) Q1
30 ) Q2
31 WHERE ROW_NUM BETWEEN 4 and 4
32 )",
33 [ [ source => 'Library' ] ],
34 );
35}
36
37sub test_order {
38 my $args = shift;
39
40 my $req_order = $args->{order_req}
41 ? "ORDER BY $args->{order_req}"
42 : ''
43 ;
44
45 is_same_sql_bind(
46 $rs->search ({}, {order_by => $args->{order_by}})->as_query,
47 "(SELECT me__id, me__source, me__owner, me__title, me__price, owner__id, owner__name FROM
48 (
49 SELECT Q1.*, ROW_NUMBER() OVER( ) AS ROW_NUM FROM (
50 SELECT me.id, me.source, me.owner, me.title, me.price, owner.id, owner.name
51 FROM books me JOIN owners owner ON owner.id = me.owner WHERE ( source = ? )
52 $req_order
53 ) Q1
54 ) Q2
55 WHERE ROW_NUM BETWEEN 4 and 4
56 )",
57 [ [ source => 'Library' ] ],
58 );
59}
60
61my @tests = (
62 {
63 order_by => \'foo DESC',
64 order_req => 'foo DESC',
65 order_inner => 'foo DESC',
66 order_outer => 'foo ASC'
67 },
68 {
69 order_by => { -asc => 'foo' },
70 order_req => 'foo ASC',
71 order_inner => 'foo ASC',
72 order_outer => 'foo DESC',
73 },
74 {
75 order_by => 'foo',
76 order_req => 'foo',
77 order_inner => 'foo ASC',
78 order_outer => 'foo DESC',
79 },
80 {
81 order_by => [ qw{ foo bar} ],
82 order_req => 'foo, bar',
83 order_inner => 'foo ASC,bar ASC',
84 order_outer => 'foo DESC, bar DESC',
85 },
86 {
87 order_by => { -desc => 'foo' },
88 order_req => 'foo DESC',
89 order_inner => 'foo DESC',
90 order_outer => 'foo ASC',
91 },
92 {
93 order_by => ['foo', { -desc => 'bar' } ],
94 order_req => 'foo, bar DESC',
95 order_inner => 'foo ASC, bar DESC',
96 order_outer => 'foo DESC, bar ASC',
97 },
98 {
99 order_by => { -asc => [qw{ foo bar }] },
100 order_req => 'foo ASC, bar ASC',
101 order_inner => 'foo ASC, bar ASC',
102 order_outer => 'foo DESC, bar DESC',
103 },
104 {
105 order_by => [
106 { -asc => 'foo' },
107 { -desc => [qw{bar}] },
108 { -asc => [qw{hello sensors}]},
109 ],
110 order_req => 'foo ASC, bar DESC, hello ASC, sensors ASC',
111 order_inner => 'foo ASC, bar DESC, hello ASC, sensors ASC',
112 order_outer => 'foo DESC, bar ASC, hello DESC, sensors DESC',
113 },
114);
115
116my @default_tests = ( undef, '', {}, [] );
117
118plan (tests => scalar @tests + scalar @default_tests + 1);
119
120test_order ($_) for @tests;
121default_test_order ($_) for @default_tests;
122
123
124is_same_sql_bind (
125 $rs->search ({}, { group_by => 'title', order_by => 'title' })->as_query,
126'(SELECT
127me.id, me.source, me.owner, me.title, me.price, owner.id, owner.name FROM
128 ( SELECT
129 id, source, owner, title, price FROM
130 ( SELECT
131 TOP 1 id, source, owner, title, price FROM
132 ( SELECT
133 TOP 4 me.id, me.source, me.owner, me.title, me.price FROM
134 books me JOIN
135 owners owner ON owner.id = me.owner
136 WHERE ( source = ? )
137 GROUP BY title
138 ORDER BY title ASC
139 ) me
140 ORDER BY title DESC
141 ) me
142 ORDER BY title
143 ) me JOIN
144 owners owner ON owner.id = me.owner WHERE
145 ( source = ? )
146 ORDER BY title)' ,
147 [ [ source => 'Library' ], [ source => 'Library' ] ],
148);