Merge 'trunk' into 'DBIx-Class-current'
[dbsrgits/DBIx-Class.git] / t / helperrels / 26sqlt.t
CommitLineData
637ca936 1use Test::More;
2use lib qw(t/lib);
3use DBICTest;
4use DBICTest::HelperRels;
5
6eval "use SQL::Translator";
7plan skip_all => 'SQL::Translator required' if $@;
8
9my $schema = DBICTest::Schema;
10
11plan tests => 27;
12
13my $translator = SQL::Translator->new(
14 parser_args => {
15 'DBIx::Schema' => $schema,
16 },
17 producer_args => {
18 },
19);
20
21$translator->parser('SQL::Translator::Parser::DBIx::Class');
22$translator->producer('SQLite');
23
24my $output = $translator->translate();
25
26my @constraints =
27 (
28 {'display' => 'twokeys->cd',
29 'selftable' => 'twokeys', 'foreigntable' => 'cd',
30 'selfcols' => ['cd'], 'foreigncols' => ['cdid'],
31 'needed' => 1, on_delete => '', on_update => ''},
32 {'display' => 'twokeys->artist',
33 'selftable' => 'twokeys', 'foreigntable' => 'artist',
34 'selfcols' => ['artist'], 'foreigncols' => ['artistid'],
35 'needed' => 1, on_delete => '', on_update => ''},
36 {'display' => 'cd_to_producer->cd',
37 'selftable' => 'cd_to_producer', 'foreigntable' => 'cd',
38 'selfcols' => ['cd'], 'foreigncols' => ['cdid'],
39 'needed' => 1, on_delete => '', on_update => ''},
40 {'display' => 'cd_to_producer->producer',
41 'selftable' => 'cd_to_producer', 'foreigntable' => 'producer',
42 'selfcols' => ['producer'], 'foreigncols' => ['producerid'],
43 'needed' => 1, on_delete => '', on_update => ''},
44 {'display' => 'self_ref_alias -> self_ref for self_ref',
45 'selftable' => 'self_ref_alias', 'foreigntable' => 'self_ref',
46 'selfcols' => ['self_ref'], 'foreigncols' => ['id'],
47 'needed' => 1, on_delete => '', on_update => ''},
48 {'display' => 'self_ref_alias -> self_ref for alias',
49 'selftable' => 'self_ref_alias', 'foreigntable' => 'self_ref',
50 'selfcols' => ['alias'], 'foreigncols' => ['id'],
51 'needed' => 1, on_delete => '', on_update => ''},
52 {'display' => 'cd -> artist',
53 'selftable' => 'cd', 'foreigntable' => 'artist',
54 'selfcols' => ['artist'], 'foreigncols' => ['artistid'],
55 'needed' => 1, on_delete => '', on_update => ''},
56 {'display' => 'artist_undirected_map -> artist for id1',
57 'selftable' => 'artist_undirected_map', 'foreigntable' => 'artist',
58 'selfcols' => ['id1'], 'foreigncols' => ['artistid'],
59 'needed' => 1, on_delete => '', on_update => ''},
60 {'display' => 'artist_undirected_map -> artist for id2',
61 'selftable' => 'artist_undirected_map', 'foreigntable' => 'artist',
62 'selfcols' => ['id2'], 'foreigncols' => ['artistid'],
63 'needed' => 1, on_delete => '', on_update => ''},
64 {'display' => 'track->cd',
65 'selftable' => 'track', 'foreigntable' => 'cd',
66 'selfcols' => ['cd'], 'foreigncols' => ['cdid'],
67 'needed' => 2, on_delete => '', on_update => ''},
68 {'display' => 'treelike -> treelike for parent',
69 'selftable' => 'treelike', 'foreigntable' => 'treelike',
70 'selfcols' => ['parent'], 'foreigncols' => ['id'],
71 'needed' => 1, on_delete => '', on_update => ''},
72 {'display' => 'twokeytreelike -> twokeytreelike for parent1,parent2',
73 'selftable' => 'twokeytreelike', 'foreigntable' => 'twokeytreelike',
74 'selfcols' => ['parent1', 'parent2'], 'foreigncols' => ['id1','id2'],
75 'needed' => 1, on_delete => '', on_update => ''},
76 {'display' => 'tags -> cd',
77 'selftable' => 'tags', 'foreigntable' => 'cd',
78 'selfcols' => ['cd'], 'foreigncols' => ['cdid'],
79 'needed' => 1, on_delete => '', on_update => ''},
80 );
81
82my $tschema = $translator->schema();
83for my $table ($tschema->get_tables) {
84 my $table_name = $table->name;
85 for my $c ( $table->get_constraints ) {
86 next unless $c->type eq 'FOREIGN KEY';
87
88 ok(check($table_name, scalar $c->fields,
89 $c->reference_table, scalar $c->reference_fields,
90 $c->on_delete, $c->on_update), "Constraint on $table_name matches an expected constraint");
91 }
92}
93
94my $i;
95for ($i = 0; $i <= $#constraints; ++$i) {
96 ok(!$constraints[$i]->{'needed'}, "Constraint $constraints[$i]->{display}");
97}
98
99sub check {
100 my ($selftable, $selfcol, $foreigntable, $foreigncol, $ondel, $onupd) = @_;
101
102 $ondel = '' if (!defined($ondel));
103 $onupd = '' if (!defined($onupd));
104
105 my $i;
106 for ($i = 0; $i <= $#constraints; ++$i) {
107 if ($selftable eq $constraints[$i]->{'selftable'} &&
108 $foreigntable eq $constraints[$i]->{'foreigntable'} &&
109 ($ondel eq $constraints[$i]->{on_delete}) &&
110 ($onupd eq $constraints[$i]->{on_update})) {
111 # check columns
112
113 my $found = 0;
114 for (my $j = 0; $j <= $#$selfcol; ++$j) {
115 $found = 0;
116 for (my $k = 0; $k <= $#{$constraints[$i]->{'selfcols'}}; ++$k) {
117 if ($selfcol->[$j] eq $constraints[$i]->{'selfcols'}->[$k] &&
118 $foreigncol->[$j] eq $constraints[$i]->{'foreigncols'}->[$k]) {
119 $found = 1;
120 last;
121 }
122 }
123 last unless $found;
124 }
125
126 if ($found) {
127 for (my $j = 0; $j <= $#{$constraints[$i]->{'selfcols'}}; ++$j) {
128 $found = 0;
129 for (my $k = 0; $k <= $#$selfcol; ++$k) {
130 if ($selfcol->[$k] eq $constraints[$i]->{'selfcols'}->[$j] &&
131 $foreigncol->[$k] eq $constraints[$i]->{'foreigncols'}->[$j]) {
132 $found = 1;
133 last;
134 }
135 }
136 last unless $found;
137 }
138 }
139
140 if ($found) {
141 --$constraints[$i]->{needed};
142 return 1;
143 }
144 }
145 }
146 return 0;
147}