s/indices/indexes/ to keep Jess happy.
[dbsrgits/DBIx-Class.git] / t / 86sqlt.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use lib qw(t/lib);
6 use DBICTest;
7
8 eval "use SQL::Translator";
9 plan skip_all => 'SQL::Translator required' if $@;
10
11 my $schema = DBICTest->init_schema;
12
13 plan tests => 55;
14
15 my $translator = SQL::Translator->new( 
16   parser_args => {
17     'DBIx::Schema' => $schema,
18   },
19   producer_args => {},
20 );
21
22 $translator->parser('SQL::Translator::Parser::DBIx::Class');
23 $translator->producer('SQLite');
24
25 my $output = $translator->translate();
26
27
28 ok($output, "SQLT produced someoutput")
29   or diag($translator->error);
30
31
32 # Note that the constraints listed here are the only ones that are tested -- if
33 # more exist in the Schema than are listed here and all listed constraints are
34 # correct, the test will still pass. If you add a class with UNIQUE or FOREIGN
35 # KEY constraints to DBICTest::Schema, add tests here if you think the existing
36 # test coverage is not sufficient
37
38 my %fk_constraints = (
39
40   # TwoKeys
41   twokeys => [
42     {
43       'display' => 'twokeys->cd',
44       'selftable' => 'twokeys', 'foreigntable' => 'cd', 
45       'selfcols'  => ['cd'], 'foreigncols' => ['cdid'], 
46       on_delete => '', on_update => '',
47     },
48     {
49       'display' => 'twokeys->artist',
50       'selftable' => 'twokeys', 'foreigntable' => 'artist', 
51       'selfcols'  => ['artist'], 'foreigncols' => ['artistid'],
52       on_delete => 'CASCADE', on_update => 'CASCADE',
53     },
54   ],
55
56   # FourKeys_to_TwoKeys
57   fourkeys_to_twokeys => [
58     {
59       'display' => 'fourkeys_to_twokeys->twokeys',
60       'selftable' => 'fourkeys_to_twokeys', 'foreigntable' => 'twokeys', 
61       'selfcols'  => ['t_artist', 't_cd'], 'foreigncols' => ['artist', 'cd'], 
62       on_delete => 'CASCADE', on_update => 'CASCADE',
63     },
64     {
65       'display' => 'fourkeys_to_twokeys->fourkeys',
66       'selftable' => 'fourkeys_to_twokeys', 'foreigntable' => 'fourkeys', 
67       'selfcols'  => [qw(f_foo f_bar f_hello f_goodbye)],
68       'foreigncols' => [qw(foo bar hello goodbye)], 
69       on_delete => 'CASCADE', on_update => 'CASCADE',
70     },
71   ],
72
73   # CD_to_Producer
74   cd_to_producer => [
75     {
76       'display' => 'cd_to_producer->cd',
77       'selftable' => 'cd_to_producer', 'foreigntable' => 'cd', 
78       'selfcols'  => ['cd'], 'foreigncols' => ['cdid'],
79       on_delete => 'CASCADE', on_update => 'CASCADE',
80     },
81     {
82       'display' => 'cd_to_producer->producer',
83       'selftable' => 'cd_to_producer', 'foreigntable' => 'producer', 
84       'selfcols'  => ['producer'], 'foreigncols' => ['producerid'],
85       on_delete => '', on_update => '',
86     },
87   ],
88
89   # Self_ref_alias
90   self_ref_alias => [
91     {
92       'display' => 'self_ref_alias->self_ref for self_ref',
93       'selftable' => 'self_ref_alias', 'foreigntable' => 'self_ref', 
94       'selfcols'  => ['self_ref'], 'foreigncols' => ['id'],
95       on_delete => 'CASCADE', on_update => 'CASCADE',
96     },
97     {
98       'display' => 'self_ref_alias->self_ref for alias',
99       'selftable' => 'self_ref_alias', 'foreigntable' => 'self_ref', 
100       'selfcols'  => ['alias'], 'foreigncols' => ['id'],
101       on_delete => '', on_update => '',
102     },
103   ],
104
105   # CD
106   cd => [
107     {
108       'display' => 'cd->artist',
109       'selftable' => 'cd', 'foreigntable' => 'artist', 
110       'selfcols'  => ['artist'], 'foreigncols' => ['artistid'],
111       on_delete => 'CASCADE', on_update => 'CASCADE',
112     },
113   ],
114
115   # Artist_undirected_map
116   artist_undirected_map => [
117     {
118       'display' => 'artist_undirected_map->artist for id1',
119       'selftable' => 'artist_undirected_map', 'foreigntable' => 'artist', 
120       'selfcols'  => ['id1'], 'foreigncols' => ['artistid'],
121       on_delete => 'CASCADE', on_update => '',
122     },
123     {
124       'display' => 'artist_undirected_map->artist for id2',
125       'selftable' => 'artist_undirected_map', 'foreigntable' => 'artist', 
126       'selfcols'  => ['id2'], 'foreigncols' => ['artistid'],
127       on_delete => 'CASCADE', on_update => '',
128     },
129   ],
130
131   # Track
132   track => [
133     {
134       'display' => 'track->cd',
135       'selftable' => 'track', 'foreigntable' => 'cd', 
136       'selfcols'  => ['cd'], 'foreigncols' => ['cdid'],
137       on_delete => 'CASCADE', on_update => 'CASCADE',
138     },
139   ],
140
141   # TreeLike
142   treelike => [
143     {
144       'display' => 'treelike->treelike for parent',
145       'selftable' => 'treelike', 'foreigntable' => 'treelike', 
146       'selfcols'  => ['parent'], 'foreigncols' => ['id'],
147       on_delete => 'CASCADE', on_update => 'CASCADE',
148     },
149   ],
150
151   # TwoKeyTreeLike
152   twokeytreelike => [
153     {
154       'display' => 'twokeytreelike->twokeytreelike for parent1,parent2',
155       'selftable' => 'twokeytreelike', 'foreigntable' => 'twokeytreelike', 
156       'selfcols'  => ['parent1', 'parent2'], 'foreigncols' => ['id1','id2'],
157       on_delete => '', on_update => '',
158     },
159   ],
160
161   # Tags
162   tags => [
163     {
164       'display' => 'tags->cd',
165       'selftable' => 'tags', 'foreigntable' => 'cd', 
166       'selfcols'  => ['cd'], 'foreigncols' => ['cdid'],
167       on_delete => 'CASCADE', on_update => 'CASCADE',
168     },
169   ],
170
171   # Bookmark
172   bookmark => [
173     {
174       'display' => 'bookmark->link',
175       'selftable' => 'bookmark', 'foreigntable' => 'link', 
176       'selfcols'  => ['link'], 'foreigncols' => ['id'],
177       on_delete => '', on_update => '',
178     },
179   ],
180 );
181
182 my %unique_constraints = (
183   # CD
184   cd => [
185     {
186       'display' => 'cd artist and title unique',
187       'table' => 'cd', 'cols' => ['artist', 'title'],
188     },
189   ],
190
191   # Producer
192   producer => [
193     {
194       'display' => 'producer name unique',
195       'table' => 'producer', 'cols' => ['name'],
196     },
197   ],
198
199   # TwoKeyTreeLike
200   twokeytreelike => [
201     {
202       'display' => 'twokeytreelike name unique',
203       'table' => 'twokeytreelike', 'cols'  => ['name'],
204     },
205   ],
206
207   # Employee
208 # Constraint is commented out in DBICTest/Schema/Employee.pm
209 #  employee => [
210 #    {
211 #      'display' => 'employee position and group_id unique',
212 #      'table' => 'employee', cols => ['position', 'group_id'],
213 #    },
214 #  ],
215 );
216
217 my %indexes = (
218   artist => [
219     {
220       'fields' => ['name']
221     },
222   ]
223 );
224
225 my $tschema = $translator->schema();
226
227 # Test that nonexistent constraints are not found
228 my $constraint = get_constraint('FOREIGN KEY', 'cd', ['title'], 'cd', ['year']);
229 ok( !defined($constraint), 'nonexistent FOREIGN KEY constraint not found' );
230 $constraint = get_constraint('UNIQUE', 'cd', ['artist']);
231 ok( !defined($constraint), 'nonexistent UNIQUE constraint not found' );
232
233 for my $expected_constraints (keys %fk_constraints) {
234   for my $expected_constraint (@{ $fk_constraints{$expected_constraints} }) {
235     my $desc = $expected_constraint->{display};
236     my $constraint = get_constraint(
237       'FOREIGN KEY',
238       $expected_constraint->{selftable}, $expected_constraint->{selfcols},
239       $expected_constraint->{foreigntable}, $expected_constraint->{foreigncols},
240     );
241     ok( defined($constraint), "FOREIGN KEY constraint matching `$desc' found" );
242     test_fk($expected_constraint, $constraint);
243   }
244 }
245
246 for my $expected_constraints (keys %unique_constraints) {
247   for my $expected_constraint (@{ $unique_constraints{$expected_constraints} }) {
248     my $desc = $expected_constraint->{display};
249     my $constraint = get_constraint(
250       'UNIQUE', $expected_constraint->{table}, $expected_constraint->{cols},
251     );
252     ok( defined($constraint), "UNIQUE constraint matching `$desc' found" );
253   }
254 }
255
256 for my $table_index (keys %indexes) {
257   for my $expected_index ( @{ $indexes{$table_index} } ) {
258
259     ok ( get_index($table_index, $expected_index), "Got a matching index on $table_index table");
260   }
261 }
262
263 # Returns the Constraint object for the specified constraint type, table and
264 # columns from the SQL::Translator schema, or undef if no matching constraint
265 # is found.
266 #
267 # NB: $type is either 'FOREIGN KEY' or 'UNIQUE'. In UNIQUE constraints the last
268 # two parameters are not used.
269 sub get_constraint {
270   my ($type, $table_name, $cols, $f_table, $f_cols) = @_;
271   $f_table ||= ''; # For UNIQUE constraints, reference_table is ''
272   $f_cols ||= [];
273
274   my $table = $tschema->get_table($table_name);
275
276   my %fields = map { $_ => 1 } @$cols;
277   my %f_fields = map { $_ => 1 } @$f_cols;
278
279  CONSTRAINT:
280   for my $constraint ( $table->get_constraints ) {
281     next unless $constraint->type eq $type;
282     next unless $constraint->reference_table eq $f_table;
283
284     my %rev_fields = map { $_ => 1 } $constraint->fields;
285     my %rev_f_fields = map { $_ => 1 } $constraint->reference_fields;
286
287     # Check that the given fields are a subset of the constraint's fields
288     for my $field ($constraint->fields) {
289       next CONSTRAINT unless $fields{$field};
290     }
291     if ($type eq 'FOREIGN KEY') {
292       for my $f_field ($constraint->reference_fields) {
293         next CONSTRAINT unless $f_fields{$f_field};
294       }
295     }
296
297     # Check that the constraint's fields are a subset of the given fields
298     for my $field (@$cols) {
299       next CONSTRAINT unless $rev_fields{$field};
300     }
301     if ($type eq 'FOREIGN KEY') {
302       for my $f_field (@$f_cols) {
303         next CONSTRAINT unless $rev_f_fields{$f_field};
304       }
305     }
306
307     return $constraint; # everything passes, found the constraint
308   }
309   return undef; # didn't find a matching constraint
310 }
311
312 sub get_index {
313   my ($table_name, $index) = @_;
314
315   my $table = $tschema->get_table($table_name);
316
317  CAND_INDEX:
318   for my $cand_index ( $table->get_indices ) {
319    
320     next CAND_INDEX if $index->{name} && $cand_index->name ne $index->{name}
321                     || $index->{type} && $cand_index->type ne $index->{type};
322
323     my %idx_fields = map { $_ => 1 } $cand_index->fields;
324
325     for my $field ( @{ $index->{fields} } ) {
326       next CAND_INDEX unless $idx_fields{$field};
327     }
328
329     %idx_fields = map { $_ => 1 } @{$index->{fields}};
330     for my $field ( $cand_index->fields) {
331       next CAND_INDEX unless $idx_fields{$field};
332     }
333
334     return $cand_index;
335   }
336
337   return undef; # No matching idx
338 }
339
340 # Test parameters in a FOREIGN KEY constraint other than columns
341 sub test_fk {
342   my ($expected, $got) = @_;
343   my $desc = $expected->{display};
344   is( $got->on_delete, $expected->{on_delete},
345       "on_delete parameter correct for `$desc'" );
346   is( $got->on_update, $expected->{on_update},
347       "on_update parameter correct for `$desc'" );
348 }