Commit | Line | Data |
c0329273 |
1 | BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) } |
2 | |
e9bd1473 |
3 | use strict; |
9188c1ed |
4 | use warnings; |
e9bd1473 |
5 | |
6 | use Test::More; |
49eeb48d |
7 | use Test::Warn; |
c0329273 |
8 | |
e9bd1473 |
9 | use DBICTest; |
e9bd1473 |
10 | |
2e251255 |
11 | my $schema = DBICTest->init_schema(); |
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 | |
49eeb48d |
20 | $schema->is_executed_querycount( sub { |
21 | my $pcr = $pr_cd_rs; |
22 | my $pr_tracks_rs; |
e9bd1473 |
23 | |
49eeb48d |
24 | warnings_exist { |
25 | $pr_tracks_rs = $pcr->first->tracks; |
26 | } [], 'no warning on attempt to prefetch several same level has_many\'s (1 -> M + M)' ; |
e9bd1473 |
27 | |
49eeb48d |
28 | is( $pr_tracks_rs->count, $tracks_count, |
29 | 'equal count of prefetched relations over several same level has_many\'s (1 -> M + M)' |
30 | ); |
e9bd1473 |
31 | |
49eeb48d |
32 | is( $pr_tracks_rs->all, $tracks_count, |
33 | 'equal amount of objects returned with and without prefetch over several same level has_many\'s (1 -> M + M)' |
34 | ); |
e9bd1473 |
35 | |
49eeb48d |
36 | }, 1, 'prefetch one->(has_many,has_many) ran exactly 1 query' ); |
e9bd1473 |
37 | |
e9bd1473 |
38 | |
27ffa6c0 |
39 | #( M -> 1 -> M + M ) |
40 | my $note_rs = |
41 | $schema->resultset('LinerNotes')->search( { notes => 'Buy Whiskey!' } ); |
42 | my $pr_note_rs = |
43 | $note_rs->search( {}, { prefetch => { cd => [qw/tracks tags/] }, } ); |
e9bd1473 |
44 | |
27ffa6c0 |
45 | my $tags_rs = $note_rs->first->cd->tags; |
46 | my $tags_count = $tags_rs->count; |
e9bd1473 |
47 | |
49eeb48d |
48 | $schema->is_executed_querycount( sub { |
49 | my $pnr = $pr_note_rs; |
50 | my $pr_tags_rs; |
51 | |
52 | warnings_exist { |
53 | $pr_tags_rs = $pnr->first->cd->tags; |
54 | } [], 'no warning on attempt to prefetch several same level has_many\'s (M -> 1 -> M + M)'; |
55 | |
56 | is( $pr_tags_rs->count, $tags_count, |
57 | 'equal count of prefetched relations over several same level has_many\'s (M -> 1 -> M + M)' |
58 | ); |
59 | is( $pr_tags_rs->all, $tags_count, |
60 | 'equal amount of objects with and without prefetch over several same level has_many\'s (M -> 1 -> M + M)' |
61 | ); |
62 | |
63 | }, 1, 'prefetch one->(has_many,has_many) ran exactly 1 query' ); |
64 | |
28fddc39 |
65 | |
27ffa6c0 |
66 | done_testing; |