fix and regression test for RT #62642
[dbsrgits/DBIx-Class.git] / t / prefetch / one_to_many_to_one.t
CommitLineData
13d06949 1use strict;
2use warnings;
3
4use Test::More;
5use Test::Exception;
6
7use lib qw(t/lib);
8use DBICTest;
9
10my $schema = DBICTest->init_schema();
11
12my $artist = $schema->resultset ('Artist')->find ({artistid => 1});
13is ($artist->cds->count, 3, 'Correct number of CDs');
14is ($artist->cds->search_related ('genre')->count, 1, 'Only one of the cds has a genre');
15
16my $queries = 0;
17my $orig_cb = $schema->storage->debugcb;
18$schema->storage->debugcb(sub { $queries++ });
19$schema->storage->debug(1);
20
21
22my $pref = $schema->resultset ('Artist')
23 ->search ({ 'me.artistid' => $artist->id }, { prefetch => { cds => 'genre' } })
24 ->next;
25
26is ($pref->cds->count, 3, 'Correct number of CDs prefetched');
27is ($pref->cds->search_related ('genre')->count, 1, 'Only one of the prefetched cds has a prefetched genre');
28
29
30is ($queries, 1, 'All happened within one query only');
31$schema->storage->debugcb($orig_cb);
32$schema->storage->debug(0);
33
34
35done_testing;