futz changes, fix populate. I'm a retard.
[dbsrgits/DBIx-Class.git] / t / helperrels / 26sqlt.t
1 use Test::More;
2 use lib qw(t/lib);
3 use DBICTest;
4 use DBICTest::HelperRels;
5
6 eval "use SQL::Translator";
7 plan skip_all => 'SQL::Translator required' if $@;
8
9 # do not taunt happy dave ball
10
11 my $schema = DBICTest::Schema;
12
13 plan tests => 31;
14
15 my $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
26 my $output = $translator->translate();
27
28 my @fk_constraints = 
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'],
37    'needed' => 1, on_delete => 'CASCADE', on_update => 'CASCADE'},
38   {'display' => 'cd_to_producer->cd',
39    'selftable' => 'cd_to_producer', 'foreigntable' => 'cd', 
40    'selfcols'  => ['cd'], 'foreigncols' => ['cdid'],
41    'needed' => 1, on_delete => 'CASCADE', on_update => 'CASCADE'},
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'],
49    'needed' => 1, on_delete => 'CASCADE', on_update => 'CASCADE'},
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'],
57    'needed' => 1, on_delete => 'CASCADE', on_update => 'CASCADE'},
58   {'display' => 'artist_undirected_map -> artist for id1',
59    'selftable' => 'artist_undirected_map', 'foreigntable' => 'artist', 
60    'selfcols'  => ['id1'], 'foreigncols' => ['artistid'],
61    'needed' => 1, on_delete => 'CASCADE', on_update => ''},
62   {'display' => 'artist_undirected_map -> artist for id2',
63    'selftable' => 'artist_undirected_map', 'foreigntable' => 'artist', 
64    'selfcols'  => ['id2'], 'foreigncols' => ['artistid'],
65    'needed' => 1, on_delete => 'CASCADE', on_update => ''},
66   {'display' => 'track->cd',
67    'selftable' => 'track', 'foreigntable' => 'cd', 
68    'selfcols'  => ['cd'], 'foreigncols' => ['cdid'],
69    'needed' => 2, on_delete => 'CASCADE', on_update => 'CASCADE'},
70   {'display' => 'treelike -> treelike for parent',
71    'selftable' => 'treelike', 'foreigntable' => 'treelike', 
72    'selfcols'  => ['parent'], 'foreigncols' => ['id'],
73    'needed' => 1, on_delete => '', on_update => ''},
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
82   {'display' => 'tags -> cd',
83    'selftable' => 'tags', 'foreigntable' => 'cd', 
84    'selfcols'  => ['cd'], 'foreigncols' => ['cdid'],
85    'needed' => 1, on_delete => 'CASCADE', on_update => 'CASCADE'},
86   {'display' => 'bookmark -> link',
87    'selftable' => 'bookmark', 'foreigntable' => 'link', 
88    'selfcols'  => ['link'], 'foreigncols' => ['id'],
89    'needed' => 1, on_delete => '', on_update => ''},
90  );
91
92 my @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},
99 #  {'display' => 'employee position and group_id unique',
100 #   'table' => 'employee', cols => ['position', 'group_id'],
101 #   'needed' => 1},
102 );
103
104 my $tschema = $translator->schema();
105 for my $table ($tschema->get_tables) {
106     my $table_name = $table->name;
107     for my $c ( $table->get_constraints ) {
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         }
117     }
118 }
119
120 # Make sure all the foreign keys are done.
121 my $i;
122 for ($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.
126 for ($i = 0; $i <= $#unique_constraints; ++$i) {
127  ok(!$unique_constraints[$i]->{'needed'}, "Constraint $unique_constraints[$i]->{display}");
128 }
129
130 sub check_fk {
131  my ($selftable, $selfcol, $foreigntable, $foreigncol, $ondel, $onupd) = @_;
132
133  $ondel = '' if (!defined($ondel));
134  $onupd = '' if (!defined($onupd));
135
136  my $i;
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})) {
142          # check columns
143
144          my $found = 0;
145          for (my $j = 0; $j <= $#$selfcol; ++$j) {
146              $found = 0;
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
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 }