Restore ability to handle underdefined root (t/prefetch/incomplete.t)
[dbsrgits/DBIx-Class.git] / t / prefetch / multiple_hasmany.t
CommitLineData
e9bd1473 1use strict;
9188c1ed 2use warnings;
e9bd1473 3
4use Test::More;
e9bd1473 5use lib qw(t/lib);
6use DBICTest;
e9bd1473 7
2e251255 8my $schema = DBICTest->init_schema();
a2287768 9my $sdebug = $schema->storage->debug;
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
27ffa6c0 18my ( $pr_tracks_rs, $pr_tracks_count );
e9bd1473 19
27ffa6c0 20my $queries = 0;
21$schema->storage->debugcb( sub { $queries++ } );
22$schema->storage->debug(1);
e9bd1473 23
27ffa6c0 24my $o_mm_warn;
25{
26 local $SIG{__WARN__} = sub { $o_mm_warn = shift };
27 $pr_tracks_rs = $pr_cd_rs->first->tracks;
28};
29$pr_tracks_count = $pr_tracks_rs->count;
e9bd1473 30
27ffa6c0 31ok( !$o_mm_warn,
32'no warning on attempt to prefetch several same level has_many\'s (1 -> M + M)'
33);
e9bd1473 34
27ffa6c0 35is( $queries, 1, 'prefetch one->(has_many,has_many) ran exactly 1 query' );
36$schema->storage->debugcb(undef);
37$schema->storage->debug($sdebug);
e9bd1473 38
27ffa6c0 39is( $pr_tracks_count, $tracks_count,
40'equal count of prefetched relations over several same level has_many\'s (1 -> M + M)'
41);
42is( $pr_tracks_rs->all, $tracks_rs->all,
43'equal amount of objects returned with and without prefetch over several same level has_many\'s (1 -> M + M)'
44);
e9bd1473 45
27ffa6c0 46#( M -> 1 -> M + M )
47my $note_rs =
48 $schema->resultset('LinerNotes')->search( { notes => 'Buy Whiskey!' } );
49my $pr_note_rs =
50 $note_rs->search( {}, { prefetch => { cd => [qw/tracks tags/] }, } );
e9bd1473 51
27ffa6c0 52my $tags_rs = $note_rs->first->cd->tags;
53my $tags_count = $tags_rs->count;
e9bd1473 54
27ffa6c0 55my ( $pr_tags_rs, $pr_tags_count );
e9bd1473 56
27ffa6c0 57$queries = 0;
58$schema->storage->debugcb( sub { $queries++ } );
59$schema->storage->debug(1);
e9bd1473 60
27ffa6c0 61my $m_o_mm_warn;
e9bd1473 62{
27ffa6c0 63 local $SIG{__WARN__} = sub { $m_o_mm_warn = shift };
64 $pr_tags_rs = $pr_note_rs->first->cd->tags;
65};
66$pr_tags_count = $pr_tags_rs->count;
87333f6c 67
27ffa6c0 68ok( !$m_o_mm_warn,
69'no warning on attempt to prefetch several same level has_many\'s (M -> 1 -> M + M)'
70);
28fddc39 71
27ffa6c0 72is( $queries, 1, 'prefetch one->(has_many,has_many) ran exactly 1 query' );
73$schema->storage->debugcb(undef);
74$schema->storage->debug($sdebug);
28fddc39 75
27ffa6c0 76is( $pr_tags_count, $tags_count,
77'equal count of prefetched relations over several same level has_many\'s (M -> 1 -> M + M)'
78);
79is( $pr_tags_rs->all, $tags_rs->all,
80'equal amount of objects with and without prefetch over several same level has_many\'s (M -> 1 -> M + M)'
28fddc39 81);
82
27ffa6c0 83done_testing;