use strict; use warnings; use Test::More; use lib qw(t/lib); use DBICTest; use Data::Dumper; my $schema = DBICTest->init_schema(); my $orig_debug = $schema->storage->debug; BEGIN { eval "use DBD::SQLite"; plan $@ ? ( skip_all => 'needs DBD::SQLite for testing' ) : ( tests => 10 ); } my $artist = $schema->resultset("Artist")->first; $artist->cds->delete; $artist->onekeys->delete; $artist->create_related( "cds", { title => "cd1", year => "1984" } ); my $cd = $artist->create_related( "cds", { title => "cd2", year => "1984" } ); $artist->create_related( "cds", { title => "cd3", year => "1984" } ); $cd->create_related( "tracks", { title => "foo1", position => 1 } ); $cd->create_related( "tracks", { title => "foo2", position => 2 } ); $artist->create_related( "onekeys", { cd => 1 } ); $artist->create_related( "onekeys", { cd => 2 } ); is( $artist->onekeys->count, 2, "count without prefetch is fine" ); is( $artist->cds->count, 3 ); my $search = {artistid => $artist->artistid}; my $attr = { prefetch => [{cds => "tracks"} ,"onekeys"], distinct => 1 }; $artist = $schema->resultset("Artist")->search( $search, $attr )->first; is($artist->cds->count, 3); is($artist->onekeys->count, 2); my $count; map { $count += $_->tracks->count } $artist->cds->all; is($count, 2); # reverse order of prefetch $attr = { prefetch => ["onekeys", {cds => "tracks"} ], distinct => 1 }; $artist = $schema->resultset("Artist")->search( $search, $attr )->first; is($artist->cds->count, 3); is($artist->onekeys->count, 2); my $count; map { $count += $_->tracks->count } $artist->cds->all; is($count, 2);