Parameterize pagination
[dbsrgits/DBIx-Class.git] / t / search / related_strip_prefetch.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use Test::Exception;
6
7 use lib qw(t/lib);
8 use DBIC::SqlMakerTest;
9 use DBICTest;
10 use DBIx::Class::SQLMaker::LimitDialects;
11
12 my $ROWS = DBIx::Class::SQLMaker::LimitDialects->__rows_bindtype;
13
14 my $schema = DBICTest->init_schema();
15
16 my $rs = $schema->resultset('CD')->search (
17   { 'tracks.trackid' => { '!=', 666 }},
18   { join => 'artist', prefetch => 'tracks', rows => 2 }
19 );
20
21 my $rel_rs = $rs->search_related ('tags', { 'tags.tag' => { '!=', undef }}, { distinct => 1});
22
23 is_same_sql_bind (
24   $rel_rs->as_query,
25   '(
26     SELECT tags.tagid, tags.cd, tags.tag
27       FROM (
28         SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track
29           FROM cd me
30           JOIN artist artist ON artist.artistid = me.artist
31           LEFT JOIN track tracks ON tracks.cd = me.cdid
32         WHERE ( tracks.trackid != ? )
33         LIMIT ?
34       ) me
35       JOIN artist artist ON artist.artistid = me.artist
36       JOIN tags tags ON tags.cd = me.cdid
37     WHERE ( tags.tag IS NOT NULL )
38     GROUP BY tags.tagid, tags.cd, tags.tag
39   )',
40
41   [
42     [ { sqlt_datatype => 'integer', dbic_colname => 'tracks.trackid' } => 666 ],
43     [ $ROWS => 2 ]
44   ],
45   'Prefetch spec successfully stripped on search_related'
46 );
47
48 done_testing;