7 use DBIC::SqlMakerTest;
9 my $schema = DBICTest->init_schema;
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');
17 my $rs = $schema->resultset ('BooksInLibrary')->search ({}, { prefetch => 'owner', rows => 1, offset => 3 });
19 sub default_test_order {
22 $rs->search ({}, {order_by => $order_by})->as_query,
24 TOP 1 me__id, source, owner, title, price, owner__id, name FROM
26 TOP 4 me.id AS me__id, me.source, me.owner, me.title, me.price, owner.id AS owner__id, owner.name
32 ) me ORDER BY me__id DESC
34 [ [ source => 'Library' ] ],
41 my $req_order = $args->{order_req}
42 ? "ORDER BY $args->{order_req}"
47 $rs->search ({}, {order_by => $args->{order_by}})->as_query,
49 me__id, source, owner, title, price, owner__id, name FROM
51 TOP 1 me__id, source, owner, title, price, owner__id, name FROM
53 TOP 4 me.id AS me__id, me.source, me.owner, me.title, me.price, owner.id AS owner__id, owner.name FROM
55 JOIN owners owner ON owner.id = me.owner
57 ORDER BY $args->{order_inner}
58 ) me ORDER BY $args->{order_outer}
61 [ [ source => 'Library' ] ],
67 order_by => \'foo DESC',
68 order_req => 'foo DESC',
69 order_inner => 'foo DESC',
70 order_outer => 'foo ASC'
73 order_by => { -asc => 'foo' },
74 order_req => 'foo ASC',
75 order_inner => 'foo ASC',
76 order_outer => 'foo DESC',
81 order_inner => 'foo ASC',
82 order_outer => 'foo DESC',
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',
91 order_by => { -desc => 'foo' },
92 order_req => 'foo DESC',
93 order_inner => 'foo DESC',
94 order_outer => 'foo ASC',
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',
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',
111 { -desc => [qw{bar}] },
112 { -asc => [qw{hello sensors}]},
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',
120 my @default_tests = ( undef, '', {}, [] );
122 plan (tests => scalar @tests + scalar @default_tests + 1);
124 test_order ($_) for @tests;
125 default_test_order ($_) for @default_tests;
129 $rs->search ({}, { group_by => 'title', order_by => 'title' })->as_query,
131 me.id, me.source, me.owner, me.title, me.price, owner.id, owner.name FROM
133 id, source, owner, title, price FROM
135 TOP 1 id, source, owner, title, price FROM
137 TOP 4 me.id, me.source, me.owner, me.title, me.price FROM
139 owners owner ON owner.id = me.owner
148 owners owner ON owner.id = me.owner WHERE
151 [ [ source => 'Library' ], [ source => 'Library' ] ],