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