Switch most remaining debug-hooks to $dbictest_schema->is_executed_querycount()
[dbsrgits/DBIx-Class.git] / t / prefetch / multiple_hasmany.t
CommitLineData
e9bd1473 1use strict;
9188c1ed 2use warnings;
e9bd1473 3
4use Test::More;
49eeb48d 5use Test::Warn;
e9bd1473 6use lib qw(t/lib);
7use DBICTest;
e9bd1473 8
2e251255 9my $schema = DBICTest->init_schema();
e9bd1473 10
27ffa6c0 11#( 1 -> M + M )
12my $cd_rs = $schema->resultset('CD')->search( { 'me.title' => 'Forkful of bees' } );
13my $pr_cd_rs = $cd_rs->search( {}, { prefetch => [qw/tracks tags/], } );
e9bd1473 14
27ffa6c0 15my $tracks_rs = $cd_rs->first->tracks;
16my $tracks_count = $tracks_rs->count;
e9bd1473 17
49eeb48d 18$schema->is_executed_querycount( sub {
19 my $pcr = $pr_cd_rs;
20 my $pr_tracks_rs;
e9bd1473 21
49eeb48d 22 warnings_exist {
23 $pr_tracks_rs = $pcr->first->tracks;
24 } [], 'no warning on attempt to prefetch several same level has_many\'s (1 -> M + M)' ;
e9bd1473 25
49eeb48d 26 is( $pr_tracks_rs->count, $tracks_count,
27 'equal count of prefetched relations over several same level has_many\'s (1 -> M + M)'
28 );
e9bd1473 29
49eeb48d 30 is( $pr_tracks_rs->all, $tracks_count,
31 'equal amount of objects returned with and without prefetch over several same level has_many\'s (1 -> M + M)'
32 );
e9bd1473 33
49eeb48d 34}, 1, 'prefetch one->(has_many,has_many) ran exactly 1 query' );
e9bd1473 35
e9bd1473 36
27ffa6c0 37#( M -> 1 -> M + M )
38my $note_rs =
39 $schema->resultset('LinerNotes')->search( { notes => 'Buy Whiskey!' } );
40my $pr_note_rs =
41 $note_rs->search( {}, { prefetch => { cd => [qw/tracks tags/] }, } );
e9bd1473 42
27ffa6c0 43my $tags_rs = $note_rs->first->cd->tags;
44my $tags_count = $tags_rs->count;
e9bd1473 45
49eeb48d 46$schema->is_executed_querycount( sub {
47 my $pnr = $pr_note_rs;
48 my $pr_tags_rs;
49
50 warnings_exist {
51 $pr_tags_rs = $pnr->first->cd->tags;
52 } [], 'no warning on attempt to prefetch several same level has_many\'s (M -> 1 -> M + M)';
53
54 is( $pr_tags_rs->count, $tags_count,
55 'equal count of prefetched relations over several same level has_many\'s (M -> 1 -> M + M)'
56 );
57 is( $pr_tags_rs->all, $tags_count,
58 'equal amount of objects with and without prefetch over several same level has_many\'s (M -> 1 -> M + M)'
59 );
60
61}, 1, 'prefetch one->(has_many,has_many) ran exactly 1 query' );
62
28fddc39 63
27ffa6c0 64done_testing;