File: diamond.t

package info (click to toggle)
libdbix-class-perl 0.082844-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,320 kB
  • sloc: perl: 27,215; sql: 322; sh: 29; makefile: 16
file content (117 lines) | stat: -rw-r--r-- 2,700 bytes parent folder | download | duplicates (5)
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
108
109
110
111
112
113
114
115
116
117
# 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' => [],
  'no cd empty' => [ '' ],
  'no cd undef' => [ undef ],
  'no cd href' => [ {} ],
  'no cd aoh' => [ [{}] ],
  'no cd complex' => [ [ [ undef ] ] ],
  'cd' => ['cd'],
  'cd->artist1' => [{'cd' => 'artist'}]
};
my $a2a_paths = {
  'no a2a' => [],
  'no a2a empty ' => [ '' ],
  'no a2a undef' => [ undef ],
  'no a2a href' => [ {} ],
  'no a2a aoh' => [ [{}] ],
  'no a2a complex' => [ [ '' ] ],
  '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;