eval "use DBD::SQLite";
plan skip_all => 'needs DBD::SQLite for testing' if $@;
-plan tests => 13;
+plan tests => 16;
-my $in_rs = $schema->resultset('Tag')->search({ tag => [ 'Blue', 'Shiny' ] });
+# The tag Blue is assigned to cds 1 2 3 and 5
+# The tag Cheesy is assigned to cds 2 4 and 5
+#
+# This combination should make some interesting group_by's
+#
my $rs;
+my $in_rs = $schema->resultset('Tag')->search({ tag => [ 'Blue', 'Cheesy' ] });
$rs = $schema->resultset('Tag')->search({ tag => 'Blue' });
is($rs->count, 4, 'Count without DISTINCT');
-$rs = $schema->resultset('Tag')->search({ tag => [ 'Blue', 'Shiny' ] }, { group_by => 'tag' });
+$rs = $schema->resultset('Tag')->search({ tag => [ 'Blue', 'Cheesy' ] }, { group_by => 'tag' });
is($rs->count, 2, 'Count with single column group_by');
+$rs = $schema->resultset('Tag')->search({ tag => [ 'Blue', 'Cheesy' ] }, { group_by => 'cd' });
+is($rs->count, 5, 'Count with another single column group_by');
+
$rs = $schema->resultset('Tag')->search({ tag => 'Blue' }, { group_by => [ qw/tag cd/ ]});
is($rs->count, 4, 'Count with multiple column group_by');
is($rs->count, 4, 'Count with single column distinct');
$rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->get_column('tag')->as_query } });
-#SELECT COUNT( * ) FROM tags me WHERE ( tag IN ( SELECT me.tag FROM tags me WHERE ( ( tag = ? OR tag = ? ) ) ) ): 'Blue', 'Shiny'
-is($rs->count, 6, 'Count with IN subquery');
+is($rs->count, 7, 'Count with IN subquery');
$rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->get_column('tag')->as_query } }, { group_by => 'tag' });
-#SELECT COUNT( * ) FROM (SELECT tag FROM tags me WHERE ( tag IN ( SELECT me.tag FROM tags me WHERE ( ( tag = ? OR tag = ? ) ) ) ) GROUP BY tag) mesub: 'Blue', 'Shiny'
is($rs->count, 2, 'Count with IN subquery with outside group_by');
$rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->get_column('tag')->as_query } }, { distinct => 1 });
-#SELECT COUNT( * ) FROM (SELECT me.tagid, me.cd, me.tag FROM tags me WHERE ( tag IN ( SELECT me.tag FROM tags me WHERE ( ( tag = ? OR tag = ? ) ) ) ) GROUP BY me.tagid, me.cd, me.tag) mesub: 'Blue', 'Shiny'
-is($rs->count, 6, 'Count with IN subquery with outside distinct');
+is($rs->count, 7, 'Count with IN subquery with outside distinct');
$rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->get_column('tag')->as_query } }, { distinct => 1, select => 'tag' }),
-#SELECT COUNT( * ) FROM (SELECT tag FROM tags me WHERE ( tag IN ( SELECT me.tag FROM tags me WHERE ( ( tag = ? OR tag = ? ) ) ) ) GROUP BY tag) mesub: 'Blue', 'Shiny'
is($rs->count, 2, 'Count with IN subquery with outside distinct on a single column');
$rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->search({}, { group_by => 'tag' })->get_column('tag')->as_query } });
-#SELECT COUNT( * ) FROM tags me WHERE ( tag IN ( SELECT me.tag FROM tags me WHERE ( ( tag = ? OR tag = ? ) ) GROUP BY tag ) ): 'Blue', 'Shiny'
-is($rs->count, 6, 'Count with IN subquery with single group_by');
+is($rs->count, 7, 'Count with IN subquery with single group_by');
+
+$rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->search({}, { group_by => 'cd' })->get_column('tag')->as_query } });
+is($rs->count, 7, 'Count with IN subquery with another single group_by');
$rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->search({}, { group_by => [ qw/tag cd/ ] })->get_column('tag')->as_query } });
-#SELECT COUNT( * ) FROM tags me WHERE ( tag IN ( SELECT me.tag FROM tags me WHERE ( ( tag = ? OR tag = ? ) ) GROUP BY tag, cd ) ): 'Blue', 'Shiny'
-is($rs->count, 6, 'Count with IN subquery with multiple group_by');
+is($rs->count, 7, 'Count with IN subquery with multiple group_by');
$rs = $schema->resultset('Tag')->search({ tag => \"= 'Blue'" });
is($rs->count, 4, 'Count without DISTINCT, using literal SQL');
-$rs = $schema->resultset('Tag')->search({ tag => \" IN ('Blue', 'Shiny')" }, { group_by => 'tag' });
+$rs = $schema->resultset('Tag')->search({ tag => \" IN ('Blue', 'Cheesy')" }, { group_by => 'tag' });
is($rs->count, 2, 'Count with literal SQL and single group_by');
-$rs = $schema->resultset('Tag')->search({ tag => \" IN ('Blue', 'Shiny')" }, { group_by => [ qw/tag cd/ ] });
-is($rs->count, 6, 'Count with literal SQL and multiple group_by');
+$rs = $schema->resultset('Tag')->search({ tag => \" IN ('Blue', 'Cheesy')" }, { group_by => 'cd' });
+is($rs->count, 5, 'Count with literal SQL and another single group_by');
+
+$rs = $schema->resultset('Tag')->search({ tag => \" IN ('Blue', 'Cheesy')" }, { group_by => [ qw/tag cd/ ] });
+is($rs->count, 7, 'Count with literal SQL and multiple group_by');