fix from => $rs->as_query
[dbsrgits/DBIx-Class.git] / t / prefetch / diamond.t
CommitLineData
4ca037d2 1# Test if prefetch and join in diamond relationship fetching the correct rows
2use strict;
3use warnings;
4
5use Test::More;
6use lib qw(t/lib);
7use DBICTest;
8
9my $schema = DBICTest->init_schema();
10
11$schema->populate('Artwork', [
12 [ qw/cd_id/ ],
13 [ 1 ],
14]);
15
16$schema->populate('Artwork_to_Artist', [
17 [ qw/artwork_cd_id artist_id/ ],
18 [ 1, 2 ],
19]);
20
21my $ars = $schema->resultset ('Artwork');
22
23# The relationship diagram here is:
24#
25# $ars --> artwork_to_artist
26# | |
27# | |
28# V V
29# cd ------> artist
30#
31# The current artwork belongs to a cd by artist1
32# but the artwork itself is painted by artist2
33#
34# What we try is all possible permutations of join/prefetch
35# combinations in both directions, while always expecting to
36# arrive at the specific artist at the end of each path.
37
38
39my $cd_paths = {
40 'no cd' => [],
41 'cd' => ['cd'],
42 'cd->artist1' => [{'cd' => 'artist'}]
43};
44my $a2a_paths = {
45 'no a2a' => [],
46 'a2a' => ['artwork_to_artist'],
47 'a2a->artist2' => [{'artwork_to_artist' => 'artist'}]
48};
49
50my %tests;
51
52foreach my $cd_path (keys %$cd_paths) {
53
54 foreach my $a2a_path (keys %$a2a_paths) {
55
56
57 $tests{sprintf "join %s, %s", $cd_path, $a2a_path} = $ars->search({}, {
58 'join' => [
59 @{ $cd_paths->{$cd_path} },
60 @{ $a2a_paths->{$a2a_path} },
61 ],
62 'prefetch' => [
63 ],
64 });
65
66
67 $tests{sprintf "prefetch %s, %s", $cd_path, $a2a_path} = $ars->search({}, {
68 'join' => [
69 ],
70 'prefetch' => [
71 @{ $cd_paths->{$cd_path} },
72 @{ $a2a_paths->{$a2a_path} },
73 ],
74 });
75
76
77 $tests{sprintf "join %s, prefetch %s", $cd_path, $a2a_path} = $ars->search({}, {
78 'join' => [
79 @{ $cd_paths->{$cd_path} },
80 ],
81 'prefetch' => [
82 @{ $a2a_paths->{$a2a_path} },
83 ],
84 });
85
86
87 $tests{sprintf "join %s, prefetch %s", $a2a_path, $cd_path} = $ars->search({}, {
88 'join' => [
89 @{ $a2a_paths->{$a2a_path} },
90 ],
91 'prefetch' => [
92 @{ $cd_paths->{$cd_path} },
93 ],
94 });
95
96 }
97}
98
4ca037d2 99foreach my $name (keys %tests) {
100 foreach my $artwork ($tests{$name}->all()) {
101 is($artwork->id, 1, $name . ', correct artwork');
102 is($artwork->cd->artist->artistid, 1, $name . ', correct artist_id over cd');
103 is($artwork->artwork_to_artist->first->artist->artistid, 2, $name . ', correct artist_id over A2A');
104 }
c9d29bb2 105}
106
107done_testing;