implemented _collapse_result and _merge_result
[dbsrgits/DBIx-Class.git] / t / prefetch / multiple_hasmany.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use Test::Exception;
6 use lib qw(t/lib);
7 use DBICTest;
8
9 my $schema = DBICTest->init_schema();
10 my $sdebug = $schema->storage->debug;
11
12 #( 1 -> M + M )
13 my $cd_rs = $schema->resultset('CD')->search( { 'me.title' => 'Forkful of bees' } );
14 my $pr_cd_rs = $cd_rs->search( {}, { prefetch => [qw/tracks tags/], } );
15
16 my $tracks_rs    = $cd_rs->first->tracks;
17 my $tracks_count = $tracks_rs->count;
18
19 my ( $pr_tracks_rs, $pr_tracks_count );
20
21 my $queries = 0;
22 $schema->storage->debugcb( sub { $queries++ } );
23 $schema->storage->debug(1);
24
25 my $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;
31
32 ok( !$o_mm_warn,
33 'no warning on attempt to prefetch several same level has_many\'s (1 -> M + M)'
34 );
35
36 is( $queries, 1, 'prefetch one->(has_many,has_many) ran exactly 1 query' );
37 $schema->storage->debugcb(undef);
38 $schema->storage->debug($sdebug);
39
40 is( $pr_tracks_count, $tracks_count,
41 'equal count of prefetched relations over several same level has_many\'s (1 -> M + M)'
42 );
43 is( $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 );
46
47 #( M -> 1 -> M + M )
48 my $note_rs =
49   $schema->resultset('LinerNotes')->search( { notes => 'Buy Whiskey!' } );
50 my $pr_note_rs =
51   $note_rs->search( {}, { prefetch => { cd => [qw/tracks tags/] }, } );
52
53 my $tags_rs    = $note_rs->first->cd->tags;
54 my $tags_count = $tags_rs->count;
55
56 my ( $pr_tags_rs, $pr_tags_count );
57
58 $queries = 0;
59 $schema->storage->debugcb( sub { $queries++ } );
60 $schema->storage->debug(1);
61
62 my $m_o_mm_warn;
63 {
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;
68
69 ok( !$m_o_mm_warn,
70 'no warning on attempt to prefetch several same level has_many\'s (M -> 1 -> M + M)'
71 );
72
73 is( $queries, 1, 'prefetch one->(has_many,has_many) ran exactly 1 query' );
74 $schema->storage->debugcb(undef);
75 $schema->storage->debug($sdebug);
76
77 is( $pr_tags_count, $tags_count,
78 'equal count of prefetched relations over several same level has_many\'s (M -> 1 -> M + M)'
79 );
80 is( $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)'
82 );
83
84 done_testing;