File: diamond.t

package info (click to toggle)
libdbix-class-perl 0.08123-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 3,520 kB
  • ctags: 1,695
  • sloc: perl: 19,821; sql: 353; makefile: 10
file content (107 lines) | stat: -rw-r--r-- 2,402 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# Test if prefetch and join in diamond relationship fetching the correct rows
use strict;
use warnings;

use Test::More;
use lib qw(t/lib);
use DBICTest;

my $schema = DBICTest->init_schema();

$schema->populate('Artwork', [
    [ qw/cd_id/ ],
    [ 1 ],
]);

$schema->populate('Artwork_to_Artist', [
    [ qw/artwork_cd_id artist_id/ ],
    [ 1, 2 ],
]);

my $ars = $schema->resultset ('Artwork');

# The relationship diagram here is:
#
#  $ars --> artwork_to_artist
#   |              |
#   |              |
#   V              V
#   cd  ------>  artist
#
# The current artwork belongs to a cd by artist1
# but the artwork itself is painted by artist2
#
# What we try is all possible permutations of join/prefetch 
# combinations in both directions, while always expecting to
# arrive at the specific artist at the end of each path.


my $cd_paths = {
  'no cd' => [],
  'cd' => ['cd'],
  'cd->artist1' => [{'cd' => 'artist'}]
};
my $a2a_paths = {
  'no a2a' => [],
  'a2a' => ['artwork_to_artist'],
  'a2a->artist2' => [{'artwork_to_artist' => 'artist'}]
};

my %tests;

foreach my $cd_path (keys %$cd_paths) {

  foreach my $a2a_path (keys %$a2a_paths) {


    $tests{sprintf "join %s, %s", $cd_path, $a2a_path} = $ars->search({}, {
      'join' => [
        @{ $cd_paths->{$cd_path} },
        @{ $a2a_paths->{$a2a_path} },
      ],
      'prefetch' => [
      ],
    });


    $tests{sprintf "prefetch %s, %s", $cd_path, $a2a_path} = $ars->search({}, {
      'join' => [
      ],
      'prefetch' => [
        @{ $cd_paths->{$cd_path} },
        @{ $a2a_paths->{$a2a_path} },
      ],
    });


    $tests{sprintf "join %s, prefetch %s", $cd_path, $a2a_path} = $ars->search({}, {
      'join' => [
        @{ $cd_paths->{$cd_path} },
      ],
      'prefetch' => [
        @{ $a2a_paths->{$a2a_path} },
      ],
    });


    $tests{sprintf "join %s, prefetch %s", $a2a_path, $cd_path} = $ars->search({}, {
      'join' => [
        @{ $a2a_paths->{$a2a_path} },
      ],
      'prefetch' => [
        @{ $cd_paths->{$cd_path} },
      ],
    });

  }
}

foreach my $name (keys %tests) {
  foreach my $artwork ($tests{$name}->all()) {
    is($artwork->id, 1, $name . ', correct artwork');
    is($artwork->cd->artist->artistid, 1, $name . ', correct artist_id over cd');
    is($artwork->artwork_to_artist->first->artist->artistid, 2, $name . ', correct artist_id over A2A');
  }
}

done_testing;