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 | |
2e251255 |
9 | my $schema = DBICTest->init_schema(); |
a2287768 |
10 | my $sdebug = $schema->storage->debug; |
e9bd1473 |
11 | |
27ffa6c0 |
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/], } ); |
e9bd1473 |
15 | |
27ffa6c0 |
16 | my $tracks_rs = $cd_rs->first->tracks; |
17 | my $tracks_count = $tracks_rs->count; |
e9bd1473 |
18 | |
27ffa6c0 |
19 | my ( $pr_tracks_rs, $pr_tracks_count ); |
e9bd1473 |
20 | |
27ffa6c0 |
21 | my $queries = 0; |
22 | $schema->storage->debugcb( sub { $queries++ } ); |
23 | $schema->storage->debug(1); |
e9bd1473 |
24 | |
27ffa6c0 |
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; |
e9bd1473 |
31 | |
27ffa6c0 |
32 | ok( !$o_mm_warn, |
33 | 'no warning on attempt to prefetch several same level has_many\'s (1 -> M + M)' |
34 | ); |
e9bd1473 |
35 | |
27ffa6c0 |
36 | is( $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 |
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 | ); |
e9bd1473 |
46 | |
27ffa6c0 |
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/] }, } ); |
e9bd1473 |
52 | |
27ffa6c0 |
53 | my $tags_rs = $note_rs->first->cd->tags; |
54 | my $tags_count = $tags_rs->count; |
e9bd1473 |
55 | |
27ffa6c0 |
56 | my ( $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 |
62 | my $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 |
69 | ok( !$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 |
73 | is( $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 |
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)' |
28fddc39 |
82 | ); |
83 | |
27ffa6c0 |
84 | done_testing; |