DBIx-Class

 view release on metacpan or  search on metacpan

t/prefetch/standard.t  view on Meta::CPAN

use strict;
use warnings;

use Test::More;
use Test::Exception;
use lib qw(t/lib);
use DBICTest;

my $schema = DBICTest->init_schema();

my $rs;
$schema->is_executed_querycount( sub {
  my $search = { 'artist.name' => 'Caterwauler McCrae' };
  my $attr = { prefetch => [ qw/artist liner_notes/ ],
             order_by => 'me.cdid' };

  $rs = $schema->resultset("CD")->search($search, $attr);
  my @cd = $rs->all;

  is($cd[0]->title, 'Spoonful of bees', 'First record returned ok');

  ok(!defined $cd[0]->liner_notes, 'No prefetch for NULL LEFT join');

  is($cd[1]->liner_notes->notes, 'Buy Whiskey!', 'Prefetch for present LEFT JOIN');

  is(ref $cd[1]->liner_notes, 'DBICTest::LinerNotes', 'Prefetch returns correct class');

  is($cd[2]->artist->name, 'Caterwauler McCrae', 'Prefetch on parent object ok');
}, 1, 'prefetch ran only 1 select statement');

# test for partial prefetch via columns attr
my $cd;
$schema->is_executed_querycount( sub {
  $cd = $schema->resultset('CD')->find(1,
    {
      columns => [qw/title artist artist.name/],
      join => { 'artist' => {} }
    }
  );
  is( $cd->artist->name, 'Caterwauler McCrae', 'single related column prefetched');
}, 1, 'manual prefetch ran only 1 select statement');

# start test for nested prefetch SELECT count
my $tag;
$schema->is_executed_querycount( sub {
  $rs = $schema->resultset('Tag')->search(
    { 'me.tagid' => 1 },
    {
      prefetch => { cd => 'artist' }
    }
  );

  $tag = $rs->first;

  is( $tag->cd->title, 'Spoonful of bees', 'step 1 ok for nested prefetch' );

  is( $tag->cd->artist->name, 'Caterwauler McCrae', 'step 2 ok for nested prefetch');
}, 1, 'nested prefetch ran exactly 1 select statement');


$schema->is_executed_querycount( sub {
  is($tag->search_related('cd')->search_related('artist')->first->name,
   'Caterwauler McCrae',
   'chained belongs_to->belongs_to search_related ok');
}, 0, 'chained search_related after belongs_to->belongs_to prefetch ran no queries');


$schema->is_executed_querycount( sub {
  $cd = $schema->resultset('CD')->find(1, { prefetch => 'artist' });

  is($cd->artist->name, 'Caterwauler McCrae', 'artist prefetched correctly on find');
}, 1, 'find with prefetch ran exactly 1 select statement');

$schema->is_executed_querycount( sub {
  $cd = $schema->resultset('CD')->find(1, { prefetch => { cd_to_producer => 'producer' }, order_by => 'producer.producerid' });

  is($cd->producers->first->name, 'Matt S Trout', 'many_to_many accessor ok');
}, 1, 'many_to_many accessor with nested prefetch ran exactly 1 query');

$schema->is_executed_querycount( sub {
  my $producers = $cd->search_related('cd_to_producer')->search_related('producer');

  is($producers->first->name, 'Matt S Trout', 'chained many_to_many search_related ok');
}, 0, 'chained search_related after many_to_many prefetch ran no queries');

$rs = $schema->resultset('Tag')->search(
  {},
  {
    join => { cd => 'artist' },
    prefetch => { cd => 'artist' }
  }
);

cmp_ok( $rs->count, '>=', 0, 'nested prefetch does not duplicate joins' );

my ($artist) = $schema->resultset("Artist")->search({ 'cds.year' => 2001 },
                 { order_by => 'artistid DESC', join => 'cds' });

is($artist->name, 'Random Boy Band', "Join search by object ok");

my @cds = $schema->resultset("CD")->search({ 'liner_notes.notes' => 'Buy Merch!' },
                               { join => 'liner_notes' });

cmp_ok(scalar @cds, '==', 1, "Single CD retrieved via might_have");

is($cds[0]->title, "Generic Manufactured Singles", "Correct CD retrieved");



( run in 0.672 second using v1.01-cache-2.11-cpan-5735350b133 )