futz changes, fix populate. I'm a retard.
[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
8e6df6ae 9# do not taunt happy dave ball
10
637ca936 11my $schema = DBICTest::Schema;
12
cb9fdccb 13plan tests => 31;
637ca936 14
15my $translator = SQL::Translator->new(
16 parser_args => {
17 'DBIx::Schema' => $schema,
18 },
19 producer_args => {
20 },
21);
22
23$translator->parser('SQL::Translator::Parser::DBIx::Class');
24$translator->producer('SQLite');
25
26my $output = $translator->translate();
27
7b90bb13 28my @fk_constraints =
637ca936 29 (
30 {'display' => 'twokeys->cd',
31 'selftable' => 'twokeys', 'foreigntable' => 'cd',
32 'selfcols' => ['cd'], 'foreigncols' => ['cdid'],
33 'needed' => 1, on_delete => '', on_update => ''},
34 {'display' => 'twokeys->artist',
35 'selftable' => 'twokeys', 'foreigntable' => 'artist',
36 'selfcols' => ['artist'], 'foreigncols' => ['artistid'],
de60a93d 37 'needed' => 1, on_delete => 'CASCADE', on_update => 'CASCADE'},
637ca936 38 {'display' => 'cd_to_producer->cd',
39 'selftable' => 'cd_to_producer', 'foreigntable' => 'cd',
40 'selfcols' => ['cd'], 'foreigncols' => ['cdid'],
de60a93d 41 'needed' => 1, on_delete => 'CASCADE', on_update => 'CASCADE'},
637ca936 42 {'display' => 'cd_to_producer->producer',
43 'selftable' => 'cd_to_producer', 'foreigntable' => 'producer',
44 'selfcols' => ['producer'], 'foreigncols' => ['producerid'],
45 'needed' => 1, on_delete => '', on_update => ''},
46 {'display' => 'self_ref_alias -> self_ref for self_ref',
47 'selftable' => 'self_ref_alias', 'foreigntable' => 'self_ref',
48 'selfcols' => ['self_ref'], 'foreigncols' => ['id'],
de60a93d 49 'needed' => 1, on_delete => 'CASCADE', on_update => 'CASCADE'},
637ca936 50 {'display' => 'self_ref_alias -> self_ref for alias',
51 'selftable' => 'self_ref_alias', 'foreigntable' => 'self_ref',
52 'selfcols' => ['alias'], 'foreigncols' => ['id'],
53 'needed' => 1, on_delete => '', on_update => ''},
54 {'display' => 'cd -> artist',
55 'selftable' => 'cd', 'foreigntable' => 'artist',
56 'selfcols' => ['artist'], 'foreigncols' => ['artistid'],
de60a93d 57 'needed' => 1, on_delete => 'CASCADE', on_update => 'CASCADE'},
637ca936 58 {'display' => 'artist_undirected_map -> artist for id1',
59 'selftable' => 'artist_undirected_map', 'foreigntable' => 'artist',
60 'selfcols' => ['id1'], 'foreigncols' => ['artistid'],
de60a93d 61 'needed' => 1, on_delete => 'CASCADE', on_update => ''},
637ca936 62 {'display' => 'artist_undirected_map -> artist for id2',
63 'selftable' => 'artist_undirected_map', 'foreigntable' => 'artist',
64 'selfcols' => ['id2'], 'foreigncols' => ['artistid'],
de60a93d 65 'needed' => 1, on_delete => 'CASCADE', on_update => ''},
637ca936 66 {'display' => 'track->cd',
67 'selftable' => 'track', 'foreigntable' => 'cd',
68 'selfcols' => ['cd'], 'foreigncols' => ['cdid'],
de60a93d 69 'needed' => 2, on_delete => 'CASCADE', on_update => 'CASCADE'},
637ca936 70 {'display' => 'treelike -> treelike for parent',
71 'selftable' => 'treelike', 'foreigntable' => 'treelike',
72 'selfcols' => ['parent'], 'foreigncols' => ['id'],
73 'needed' => 1, on_delete => '', on_update => ''},
cb9fdccb 74
75 # shouldn't this be generated?
76 #
77 #{'display' => 'twokeytreelike -> twokeytreelike for parent1,parent2',
78 # 'selftable' => 'twokeytreelike', 'foreigntable' => 'twokeytreelike',
79 # 'selfcols' => ['parent1', 'parent2'], 'foreigncols' => ['id1','id2'],
80 # 'needed' => 1, on_delete => '', on_update => ''},
81
637ca936 82 {'display' => 'tags -> cd',
83 'selftable' => 'tags', 'foreigntable' => 'cd',
84 'selfcols' => ['cd'], 'foreigncols' => ['cdid'],
de60a93d 85 'needed' => 1, on_delete => 'CASCADE', on_update => 'CASCADE'},
81155549 86 {'display' => 'bookmark -> link',
87 'selftable' => 'bookmark', 'foreigntable' => 'link',
88 'selfcols' => ['link'], 'foreigncols' => ['id'],
89 'needed' => 1, on_delete => '', on_update => ''},
637ca936 90 );
91
7b90bb13 92my @unique_constraints = (
93 {'display' => 'cd artist and title unique',
94 'table' => 'cd', 'cols' => ['artist', 'title'],
95 'needed' => 1},
96 {'display' => 'twokeytreelike name unique',
97 'table' => 'twokeytreelike', 'cols' => ['name'],
98 'needed' => 1},
78145e31 99# {'display' => 'employee position and group_id unique',
100# 'table' => 'employee', cols => ['position', 'group_id'],
101# 'needed' => 1},
7b90bb13 102);
103
637ca936 104my $tschema = $translator->schema();
105for my $table ($tschema->get_tables) {
106 my $table_name = $table->name;
107 for my $c ( $table->get_constraints ) {
7b90bb13 108 if ($c->type eq 'FOREIGN KEY') {
109 ok(check_fk($table_name, scalar $c->fields,
110 $c->reference_table, scalar $c->reference_fields,
111 $c->on_delete, $c->on_update), "Foreign key constraint on $table_name matches an expected constraint");
112 }
113 elsif ($c->type eq 'UNIQUE') {
114 ok(check_unique($table_name, scalar $c->fields),
115 "Unique constraint on $table_name matches an expected constraint");
116 }
637ca936 117 }
118}
119
7b90bb13 120# Make sure all the foreign keys are done.
637ca936 121my $i;
7b90bb13 122for ($i = 0; $i <= $#fk_constraints; ++$i) {
123 ok(!$fk_constraints[$i]->{'needed'}, "Constraint $fk_constraints[$i]->{display}");
124}
125# Make sure all the uniques are done.
126for ($i = 0; $i <= $#unique_constraints; ++$i) {
127 ok(!$unique_constraints[$i]->{'needed'}, "Constraint $unique_constraints[$i]->{display}");
637ca936 128}
129
7b90bb13 130sub check_fk {
637ca936 131 my ($selftable, $selfcol, $foreigntable, $foreigncol, $ondel, $onupd) = @_;
132
133 $ondel = '' if (!defined($ondel));
134 $onupd = '' if (!defined($onupd));
135
136 my $i;
7b90bb13 137 for ($i = 0; $i <= $#fk_constraints; ++$i) {
138 if ($selftable eq $fk_constraints[$i]->{'selftable'} &&
139 $foreigntable eq $fk_constraints[$i]->{'foreigntable'} &&
140 ($ondel eq $fk_constraints[$i]->{on_delete}) &&
141 ($onupd eq $fk_constraints[$i]->{on_update})) {
637ca936 142 # check columns
143
144 my $found = 0;
145 for (my $j = 0; $j <= $#$selfcol; ++$j) {
146 $found = 0;
7b90bb13 147 for (my $k = 0; $k <= $#{$fk_constraints[$i]->{'selfcols'}}; ++$k) {
148 if ($selfcol->[$j] eq $fk_constraints[$i]->{'selfcols'}->[$k] &&
149 $foreigncol->[$j] eq $fk_constraints[$i]->{'foreigncols'}->[$k]) {
150 $found = 1;
151 last;
152 }
153 }
154 last unless $found;
155 }
156
157 if ($found) {
158 for (my $j = 0; $j <= $#{$fk_constraints[$i]->{'selfcols'}}; ++$j) {
159 $found = 0;
160 for (my $k = 0; $k <= $#$selfcol; ++$k) {
161 if ($selfcol->[$k] eq $fk_constraints[$i]->{'selfcols'}->[$j] &&
162 $foreigncol->[$k] eq $fk_constraints[$i]->{'foreigncols'}->[$j]) {
163 $found = 1;
164 last;
165 }
166 }
167 last unless $found;
168 }
169 }
170
171 if ($found) {
172 --$fk_constraints[$i]->{needed};
173 return 1;
174 }
175 }
176 }
177 return 0;
178}
179
180sub check_unique {
181 my ($selftable, $selfcol) = @_;
182
183 $ondel = '' if (!defined($ondel));
184 $onupd = '' if (!defined($onupd));
185
186 my $i;
187 for ($i = 0; $i <= $#unique_constraints; ++$i) {
188 if ($selftable eq $unique_constraints[$i]->{'table'}) {
189
190 my $found = 0;
191 for (my $j = 0; $j <= $#$selfcol; ++$j) {
192 $found = 0;
193 for (my $k = 0; $k <= $#{$unique_constraints[$i]->{'cols'}}; ++$k) {
194 if ($selfcol->[$j] eq $unique_constraints[$i]->{'cols'}->[$k]) {
637ca936 195 $found = 1;
196 last;
197 }
198 }
199 last unless $found;
200 }
201
202 if ($found) {
7b90bb13 203 for (my $j = 0; $j <= $#{$unique_constraints[$i]->{'cols'}}; ++$j) {
637ca936 204 $found = 0;
205 for (my $k = 0; $k <= $#$selfcol; ++$k) {
7b90bb13 206 if ($selfcol->[$k] eq $unique_constraints[$i]->{'cols'}->[$j]) {
637ca936 207 $found = 1;
208 last;
209 }
210 }
211 last unless $found;
212 }
213 }
214
215 if ($found) {
7b90bb13 216 --$unique_constraints[$i]->{needed};
637ca936 217 return 1;
218 }
219 }
220 }
221 return 0;
222}