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