Switch most remaining debug-hooks to $dbictest_schema->is_executed_querycount()
[dbsrgits/DBIx-Class.git] / t / prefetch / multiple_hasmany.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use Test::Warn;
6 use lib qw(t/lib);
7 use DBICTest;
8
9 my $schema = DBICTest->init_schema();
10
11 #( 1 -> M + M )
12 my $cd_rs = $schema->resultset('CD')->search( { 'me.title' => 'Forkful of bees' } );
13 my $pr_cd_rs = $cd_rs->search( {}, { prefetch => [qw/tracks tags/], } );
14
15 my $tracks_rs    = $cd_rs->first->tracks;
16 my $tracks_count = $tracks_rs->count;
17
18 $schema->is_executed_querycount( sub {
19   my $pcr = $pr_cd_rs;
20   my $pr_tracks_rs;
21
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)' ;
25
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   );
29
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   );
33
34 }, 1, 'prefetch one->(has_many,has_many) ran exactly 1 query' );
35
36
37 #( M -> 1 -> M + M )
38 my $note_rs =
39   $schema->resultset('LinerNotes')->search( { notes => 'Buy Whiskey!' } );
40 my $pr_note_rs =
41   $note_rs->search( {}, { prefetch => { cd => [qw/tracks tags/] }, } );
42
43 my $tags_rs    = $note_rs->first->cd->tags;
44 my $tags_count = $tags_rs->count;
45
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
63
64 done_testing;