1 # Test if prefetch and join in diamond relationship fetching the correct rows
9 my $schema = DBICTest->init_schema();
11 $schema->populate('Artwork', [
16 $schema->populate('Artwork_to_Artist', [
17 [ qw/artwork_cd_id artist_id/ ],
21 my $ars = $schema->resultset ('Artwork');
23 # The relationship diagram here is:
25 # $ars --> artwork_to_artist
31 # The current artwork belongs to a cd by artist1
32 # but the artwork itself is painted by artist2
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.
42 'cd->artist1' => [{'cd' => 'artist'}]
46 'a2a' => ['artwork_to_artist'],
47 'a2a->artist2' => [{'artwork_to_artist' => 'artist'}]
52 foreach my $cd_path (keys %$cd_paths) {
54 foreach my $a2a_path (keys %$a2a_paths) {
57 $tests{sprintf "join %s, %s", $cd_path, $a2a_path} = $ars->search({}, {
59 @{ $cd_paths->{$cd_path} },
60 @{ $a2a_paths->{$a2a_path} },
67 $tests{sprintf "prefetch %s, %s", $cd_path, $a2a_path} = $ars->search({}, {
71 @{ $cd_paths->{$cd_path} },
72 @{ $a2a_paths->{$a2a_path} },
77 $tests{sprintf "join %s, prefetch %s", $cd_path, $a2a_path} = $ars->search({}, {
79 @{ $cd_paths->{$cd_path} },
82 @{ $a2a_paths->{$a2a_path} },
87 $tests{sprintf "join %s, prefetch %s", $a2a_path, $cd_path} = $ars->search({}, {
89 @{ $a2a_paths->{$a2a_path} },
92 @{ $cd_paths->{$cd_path} },
99 plan tests => (scalar (keys %tests) * 3);
101 foreach my $name (keys %tests) {
102 foreach my $artwork ($tests{$name}->all()) {
103 is($artwork->id, 1, $name . ', correct artwork');
104 is($artwork->cd->artist->artistid, 1, $name . ', correct artist_id over cd');
105 is($artwork->artwork_to_artist->first->artist->artistid, 2, $name . ', correct artist_id over A2A');