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