Commit | Line | Data |
f992cfaa |
1 | #!/usr/bin/perl -w |
55e462b4 |
2 | # vim:filetype=perl |
3 | |
4 | # Before `make install' is performed this script should be runnable with |
5 | # `make test'. After `make install' it should work as `perl test.pl' |
6 | |
7 | use strict; |
8 | use FindBin qw/$Bin/; |
9 | use Data::Dumper; |
10 | |
11 | # run with -d for debug |
12 | my %opt; |
13 | BEGIN { map { $opt{$_}=1 if s/^-// } @ARGV; } |
14 | use constant DEBUG => (exists $opt{d} ? 1 : 0); |
15 | |
16 | use Test::More; |
17 | use Test::Exception; |
18 | use SQL::Translator; |
19 | use SQL::Translator::Schema::Constants; |
20 | |
21 | # Usefull test subs for the schema objs |
22 | #============================================================================= |
23 | |
24 | sub test_field { |
25 | my ($f1,$test) = @_; |
26 | unless ($f1) { |
27 | fail " Field '$test->{name}' doesn't exist!"; |
28 | # TODO Do a skip on the following tests |
29 | return; |
30 | } |
31 | |
32 | is( $f1->name, $test->{name}, " Field name '$test->{name}'" ); |
f992cfaa |
33 | |
55e462b4 |
34 | is( $f1->data_type, $test->{data_type}, " Type is '$test->{data_type}'" ) |
35 | if exists $test->{data_type}; |
f992cfaa |
36 | |
55e462b4 |
37 | is( $f1->size, $test->{size}, " Size is '$test->{size}'" ) |
38 | if exists $test->{size}; |
f992cfaa |
39 | |
55e462b4 |
40 | is( $f1->default_value, $test->{default_value}, |
41 | " Default value is ".(defined($test->{default_value}) ? "'$test->{default_value}'" : "UNDEF" ) ) |
42 | if exists $test->{default_value}; |
f992cfaa |
43 | |
44 | is( $f1->is_nullable, $test->{is_nullable}, |
55e462b4 |
45 | " ".($test->{is_nullable} ? 'can' : 'cannot').' be null' ) |
46 | if exists $test->{is_nullable}; |
f992cfaa |
47 | |
48 | is( $f1->is_unique, $test->{is_unique}, |
55e462b4 |
49 | " ".($test->{is_unique} ? 'can' : 'cannot').' be unique' ) |
50 | if exists $test->{is_unique}; |
f992cfaa |
51 | |
52 | is( $f1->is_primary_key, $test->{is_primary_key}, |
55e462b4 |
53 | " is ".($test->{is_primary_key} ? '' : 'not').' a primary_key' ) |
54 | if exists $test->{is_primary_key}; |
f992cfaa |
55 | |
56 | is( $f1->is_foreign_key, $test->{is_foreign_key}, |
55e462b4 |
57 | " is ".($test->{is_foreign_key} ? '' : 'not').' a foreign_key' ) |
58 | if exists $test->{is_foreign_key}; |
f992cfaa |
59 | |
60 | is( $f1->is_auto_increment, $test->{is_auto_increment}, |
55e462b4 |
61 | " is ".($test->{is_auto_increment} ? '' : 'not').' an auto_increment' ) |
62 | if exists $test->{is_auto_increment}; |
63 | } |
64 | |
65 | sub constraint_ok { |
66 | my ($con,$test) = @_; |
67 | #$test->{name} ||= "<anon>"; |
68 | |
69 | if ( exists $test->{name} ) { |
70 | is( $con->name, $test->{name}, " Constraint '$test->{name}'" ); |
71 | } |
72 | else { |
73 | ok( $con, " Constraint" ); |
74 | } |
f992cfaa |
75 | |
55e462b4 |
76 | is( $con->type, $test->{type}, " type is '$test->{type}'" ) |
77 | if exists $test->{type}; |
f992cfaa |
78 | |
55e462b4 |
79 | is( $con->table->name, $test->{table}, " table is '$test->{table}'" ) |
80 | if exists $test->{table}; |
f992cfaa |
81 | |
55e462b4 |
82 | is( join(",",$con->fields), $test->{fields}, |
83 | " fields is '$test->{fields}'" ) |
84 | if exists $test->{fields}; |
f992cfaa |
85 | |
55e462b4 |
86 | is( $con->reference_table, $test->{reference_table}, |
87 | " reference_table is '$test->{reference_table}'" ) |
88 | if exists $test->{reference_table}; |
f992cfaa |
89 | |
55e462b4 |
90 | is( join(",",$con->reference_fields), $test->{reference_fields}, |
91 | " reference_fields is '$test->{reference_fields}'" ) |
92 | if exists $test->{reference_fields}; |
f992cfaa |
93 | |
55e462b4 |
94 | is( $con->match_type, $test->{match_type}, |
95 | " match_type is '$test->{match_type}'" ) |
96 | if exists $test->{match_type}; |
f992cfaa |
97 | |
55e462b4 |
98 | is( $con->on_delete_do, $test->{on_delete_do}, |
99 | " on_delete_do is '$test->{on_delete_do}'" ) |
100 | if exists $test->{on_delete_do}; |
f992cfaa |
101 | |
55e462b4 |
102 | is( $con->on_update_do, $test->{on_update_do}, |
103 | " on_update_do is '$test->{on_update_do}'" ) |
104 | if exists $test->{on_update_do}; |
105 | } |
106 | |
107 | sub test_table { |
108 | my $tbl = shift; |
109 | my %arg = @_; |
110 | $arg{constraints} ||= []; |
111 | my $name = $arg{name} || die "Need a table name to test."; |
f992cfaa |
112 | |
55e462b4 |
113 | my @fldnames = map { $_->{name} } @{$arg{fields}}; |
114 | is_deeply( [ map {$_->name} $tbl->get_fields ], |
115 | [ map {$_->{name}} @{$arg{fields}} ], |
116 | "Table $name\'s fields" ); |
117 | foreach ( @{$arg{fields}} ) { |
118 | my $name = $_->{name} || die "Need a field name to test."; |
119 | test_field( $tbl->get_field($name), $_ ); |
120 | } |
f992cfaa |
121 | |
55e462b4 |
122 | if ( my @tcons = @{$arg{constraints}} ) { |
123 | my @cons = $tbl->get_constraints; |
124 | is(scalar(@cons), scalar(@tcons), |
125 | "Table $name has ".scalar(@tcons)." Constraints"); |
126 | foreach ( @cons ) { |
127 | my $ans = { table => $tbl->name, %{shift @tcons}}; |
128 | constraint_ok( $_, $ans ); |
129 | } |
130 | } |
131 | } |
132 | |
133 | # Testing 1,2,3,.. |
134 | #============================================================================= |
135 | |
f992cfaa |
136 | plan tests => 94; |
55e462b4 |
137 | |
138 | my $testschema = "$Bin/data/xmi/OrderDB.sqlfairy.poseidon2.xmi"; |
139 | die "Can't find test schema $testschema" unless -e $testschema; |
140 | |
141 | my $obj; |
142 | $obj = SQL::Translator->new( |
143 | filename => $testschema, |
144 | from => 'XML-XMI-SQLFairy', |
145 | to => 'MySQL', |
146 | debug => DEBUG, |
147 | show_warnings => 1, |
148 | ); |
149 | my $sql = $obj->translate; |
150 | ok( $sql, "Got some SQL"); |
151 | print $sql if DEBUG; |
152 | |
153 | |
154 | # |
155 | # Test the schema |
156 | # |
157 | my $scma = $obj->schema; |
158 | is( $scma->is_valid, 1, 'Schema is valid' ); |
159 | my @tblnames = map {$_->name} $scma->get_tables; |
160 | is(scalar(@{$scma->get_tables}), scalar(@tblnames), "Right number of tables"); |
161 | is_deeply( \@tblnames, [qw/Order OrderLine Customer/] |
162 | ,"tables"); |
163 | |
164 | test_table( $scma->get_table("Customer"), |
165 | name => "Customer", |
166 | fields => [ |
167 | { |
55e462b4 |
168 | name => "name", |
169 | data_type => "VARCHAR", |
170 | size => 255, |
171 | default_value => undef, |
172 | is_nullable => 0, |
173 | is_primary_key => 0, |
174 | }, |
175 | { |
176 | name => "email", |
177 | data_type => "VARCHAR", |
178 | size => 255, |
179 | default_value => undef, |
180 | is_nullable => 1, |
181 | is_primary_key => 0, |
182 | }, |
f992cfaa |
183 | { |
184 | name => "CustomerID", |
185 | data_type => "INT", |
186 | size => 10, |
187 | default_value => undef, |
188 | is_nullable => 0, |
189 | is_primary_key => 1, |
190 | }, |
55e462b4 |
191 | ], |
192 | constraints => [ |
193 | { |
194 | type => "PRIMARY KEY", |
f992cfaa |
195 | fields => "CustomerID", |
55e462b4 |
196 | }, |
197 | #{ |
198 | # name => "UniqueEmail", |
199 | # type => "UNIQUE", |
200 | # fields => "email", |
201 | #}, |
202 | ], |
203 | ); |
204 | |
205 | test_table( $scma->get_table("Order"), |
206 | name => "Order", |
207 | fields => [ |
208 | { |
f992cfaa |
209 | name => "invoiceNumber", |
210 | data_type => "INT", |
211 | size => 10, |
55e462b4 |
212 | default_value => undef, |
213 | is_nullable => 0, |
f992cfaa |
214 | is_primary_key => 1, |
55e462b4 |
215 | }, |
216 | { |
f992cfaa |
217 | name => "orderDate", |
218 | data_type => "DATE", |
55e462b4 |
219 | default_value => undef, |
220 | is_nullable => 0, |
f992cfaa |
221 | is_primary_key => 0, |
55e462b4 |
222 | }, |
223 | { |
f992cfaa |
224 | name => "CustomerID", |
55e462b4 |
225 | data_type => "INT", |
f992cfaa |
226 | size => 10, |
55e462b4 |
227 | default_value => undef, |
228 | is_nullable => 0, |
229 | is_primary_key => 0, |
230 | is_foreign_key => 1, |
231 | }, |
232 | ], |
233 | constraints => [ |
234 | { |
235 | type => "PRIMARY KEY", |
f992cfaa |
236 | fields => "invoiceNumber", |
55e462b4 |
237 | }, |
238 | { |
239 | type => "FOREIGN KEY", |
f992cfaa |
240 | fields => "CustomerID", |
55e462b4 |
241 | reference_table => "Customer", |
f992cfaa |
242 | reference_fields => "CustomerID", |
55e462b4 |
243 | }, |
244 | ], |
245 | # TODO |
246 | #indexes => [ |
247 | # { |
248 | # name => "idxOrderDate", |
249 | # type => "INDEX", |
250 | # fields => "orderDate", |
251 | # }, |
252 | #], |
253 | ); |
254 | |
255 | |
256 | test_table( $scma->get_table("OrderLine"), |
257 | name => "OrderLine", |
258 | fields => [ |
259 | { |
260 | name => "lineNumber", |
261 | data_type => "INT", |
262 | size => 255, |
263 | default_value => 1, |
264 | is_nullable => 0, |
f992cfaa |
265 | is_primary_key => 0, |
55e462b4 |
266 | }, |
267 | { |
268 | name => "quantity", |
269 | data_type => "INT", |
270 | size => 255, |
271 | default_value => 1, |
272 | is_nullable => 0, |
273 | is_primary_key => 0, |
274 | }, |
275 | { |
f992cfaa |
276 | name => "OrderLineID", |
277 | data_type => "INT", |
278 | size => 10, |
279 | default_value => undef, |
280 | is_nullable => 0, |
281 | is_primary_key => 1, |
282 | }, |
283 | { |
284 | name => "invoiceNumber", |
55e462b4 |
285 | data_type => "INT", |
286 | size => 10, |
287 | default_value => undef, |
288 | is_nullable => 1, |
f992cfaa |
289 | is_primary_key => 1, |
55e462b4 |
290 | }, |
291 | ], |
292 | constraints => [ |
293 | { |
294 | type => "PRIMARY KEY", |
f992cfaa |
295 | fields => "OrderLineID,invoiceNumber", |
55e462b4 |
296 | }, |
297 | { |
298 | type => "FOREIGN KEY", |
f992cfaa |
299 | fields => "invoiceNumber", |
55e462b4 |
300 | reference_table => "Order", |
f992cfaa |
301 | reference_fields => "invoiceNumber", |
55e462b4 |
302 | }, |
303 | ], |
304 | ); |