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