Commit | Line | Data |
e9bd1473 |
1 | use strict; |
9188c1ed |
2 | use warnings; |
e9bd1473 |
3 | |
4 | use Test::More; |
5 | use Test::Exception; |
6 | use lib qw(t/lib); |
7 | use DBICTest; |
e9bd1473 |
8 | use IO::File; |
9 | |
2e251255 |
10 | my $schema = DBICTest->init_schema(); |
a2287768 |
11 | my $sdebug = $schema->storage->debug; |
e9bd1473 |
12 | |
27ffa6c0 |
13 | #( 1 -> M + M ) |
14 | my $cd_rs = $schema->resultset('CD')->search( { 'me.title' => 'Forkful of bees' } ); |
15 | my $pr_cd_rs = $cd_rs->search( {}, { prefetch => [qw/tracks tags/], } ); |
e9bd1473 |
16 | |
27ffa6c0 |
17 | my $tracks_rs = $cd_rs->first->tracks; |
18 | my $tracks_count = $tracks_rs->count; |
e9bd1473 |
19 | |
27ffa6c0 |
20 | my ( $pr_tracks_rs, $pr_tracks_count ); |
e9bd1473 |
21 | |
27ffa6c0 |
22 | my $queries = 0; |
23 | $schema->storage->debugcb( sub { $queries++ } ); |
24 | $schema->storage->debug(1); |
e9bd1473 |
25 | |
27ffa6c0 |
26 | my $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 |
33 | ok( !$o_mm_warn, |
34 | 'no warning on attempt to prefetch several same level has_many\'s (1 -> M + M)' |
35 | ); |
e9bd1473 |
36 | |
27ffa6c0 |
37 | is( $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 |
41 | is( $pr_tracks_count, $tracks_count, |
42 | 'equal count of prefetched relations over several same level has_many\'s (1 -> M + M)' |
43 | ); |
44 | is( $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 ) |
49 | my $note_rs = |
50 | $schema->resultset('LinerNotes')->search( { notes => 'Buy Whiskey!' } ); |
51 | my $pr_note_rs = |
52 | $note_rs->search( {}, { prefetch => { cd => [qw/tracks tags/] }, } ); |
e9bd1473 |
53 | |
27ffa6c0 |
54 | my $tags_rs = $note_rs->first->cd->tags; |
55 | my $tags_count = $tags_rs->count; |
e9bd1473 |
56 | |
27ffa6c0 |
57 | my ( $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 |
63 | my $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 |
70 | ok( !$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 |
74 | is( $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 |
78 | is( $pr_tags_count, $tags_count, |
79 | 'equal count of prefetched relations over several same level has_many\'s (M -> 1 -> M + M)' |
80 | ); |
81 | is( $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 |
85 | done_testing; |