Restore ability to handle underdefined root (t/prefetch/incomplete.t)
[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#
8273e845 34# What we try is all possible permutations of join/prefetch
4ca037d2 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' => [],
8bc47467 41 'no cd empty' => [ '' ],
42 'no cd undef' => [ undef ],
43 'no cd href' => [ {} ],
44 'no cd aoh' => [ [{}] ],
45 'no cd complex' => [ [ [ undef ] ] ],
4ca037d2 46 'cd' => ['cd'],
47 'cd->artist1' => [{'cd' => 'artist'}]
48};
49my $a2a_paths = {
50 'no a2a' => [],
8bc47467 51 'no a2a empty ' => [ '' ],
52 'no a2a undef' => [ undef ],
53 'no a2a href' => [ {} ],
54 'no a2a aoh' => [ [{}] ],
55 'no a2a complex' => [ [ '' ] ],
4ca037d2 56 'a2a' => ['artwork_to_artist'],
57 'a2a->artist2' => [{'artwork_to_artist' => 'artist'}]
58};
59
60my %tests;
61
62foreach my $cd_path (keys %$cd_paths) {
63
64 foreach my $a2a_path (keys %$a2a_paths) {
65
66
67 $tests{sprintf "join %s, %s", $cd_path, $a2a_path} = $ars->search({}, {
68 'join' => [
69 @{ $cd_paths->{$cd_path} },
70 @{ $a2a_paths->{$a2a_path} },
71 ],
72 'prefetch' => [
73 ],
74 });
75
76
77 $tests{sprintf "prefetch %s, %s", $cd_path, $a2a_path} = $ars->search({}, {
78 'join' => [
79 ],
80 'prefetch' => [
81 @{ $cd_paths->{$cd_path} },
82 @{ $a2a_paths->{$a2a_path} },
83 ],
84 });
85
86
87 $tests{sprintf "join %s, prefetch %s", $cd_path, $a2a_path} = $ars->search({}, {
88 'join' => [
89 @{ $cd_paths->{$cd_path} },
90 ],
91 'prefetch' => [
92 @{ $a2a_paths->{$a2a_path} },
93 ],
94 });
95
96
97 $tests{sprintf "join %s, prefetch %s", $a2a_path, $cd_path} = $ars->search({}, {
98 'join' => [
99 @{ $a2a_paths->{$a2a_path} },
100 ],
101 'prefetch' => [
102 @{ $cd_paths->{$cd_path} },
103 ],
104 });
105
106 }
107}
108
4ca037d2 109foreach my $name (keys %tests) {
110 foreach my $artwork ($tests{$name}->all()) {
111 is($artwork->id, 1, $name . ', correct artwork');
112 is($artwork->cd->artist->artistid, 1, $name . ', correct artist_id over cd');
113 is($artwork->artwork_to_artist->first->artist->artistid, 2, $name . ', correct artist_id over A2A');
114 }
c9d29bb2 115}
116
117done_testing;