clauses test (xt/ because extra deps)
[scpubgit/Q-Branch.git] / xt / clauses.t
1 use strict;
2 use warnings;
3 use Test::More;
4 use SQL::Abstract::Test import => [ qw(is_same_sql_bind) ];
5 use SQL::Abstract::ExtraClauses;
6
7 my $sqlac = SQL::Abstract::ExtraClauses->new;
8
9 my ($sql, @bind) = $sqlac->select({
10   select => [ qw(artist.id artist.name), { -func => [ json_agg => 'cd' ] } ],
11   from => [
12     artist => -join => [ cd => on => { 'cd.artist_id' => 'artist.id' } ],
13   ],
14   where => { 'artist.genres', => { '@>', { -value => [ 'Rock' ] } } },
15   order_by => 'artist.name',
16   group_by => 'artist.id',
17   having => { '>' => [ { -func => [ count => 'cd.id' ] }, 3 ] }
18 });
19
20 is_same_sql_bind(
21   $sql, \@bind,
22   q{
23     SELECT artist.id, artist.name, JSON_AGG(cd)
24     FROM artist JOIN cd ON cd.artist_id = artist.id
25     WHERE artist.genres @> ?
26     ORDER BY artist.name
27     GROUP BY artist.id
28     HAVING COUNT(cd.id) > ?
29   },
30   [ [ 'Rock' ], 3 ]
31 );
32
33 done_testing;