Added list of constants exported to the docs.
[dbsrgits/SQL-Translator.git] / t / 28xml-xmi-parser-sqlfairy.t
1 #!/usr/bin/perl -w
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}'" );
33
34         is( $f1->data_type, $test->{data_type}, "    Type is '$test->{data_type}'" )
35         if exists $test->{data_type};
36
37         is( $f1->size, $test->{size}, "    Size is '$test->{size}'" )
38         if exists $test->{size};
39
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};
43
44         is( $f1->is_nullable, $test->{is_nullable},
45                 "    ".($test->{is_nullable} ? 'can' : 'cannot').' be null' )
46         if exists $test->{is_nullable};
47
48         is( $f1->is_unique, $test->{is_unique},
49                 "    ".($test->{is_unique} ? 'can' : 'cannot').' be unique' )
50         if exists $test->{is_unique};
51
52         is( $f1->is_primary_key, $test->{is_primary_key},
53                 "    is ".($test->{is_primary_key} ? '' : 'not').' a primary_key' )
54         if exists $test->{is_primary_key};
55
56         is( $f1->is_foreign_key, $test->{is_foreign_key},
57                 "    is ".($test->{is_foreign_key} ? '' : 'not').' a foreign_key' )
58         if exists $test->{is_foreign_key};
59
60         is( $f1->is_auto_increment, $test->{is_auto_increment},
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         }
75
76         is( $con->type, $test->{type}, "    type is '$test->{type}'" )
77         if exists $test->{type};
78
79         is( $con->table->name, $test->{table}, "    table is '$test->{table}'" )
80         if exists $test->{table};
81
82         is( join(",",$con->fields), $test->{fields},
83         "    fields is '$test->{fields}'" )
84         if exists $test->{fields};
85
86         is( $con->reference_table, $test->{reference_table},
87         "    reference_table is '$test->{reference_table}'" )
88         if exists $test->{reference_table};
89
90         is( join(",",$con->reference_fields), $test->{reference_fields},
91         "    reference_fields is '$test->{reference_fields}'" )
92         if exists $test->{reference_fields};
93
94         is( $con->match_type, $test->{match_type},
95         "    match_type is '$test->{match_type}'" )
96         if exists $test->{match_type};
97
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};
101
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.";
112
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     }
121
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
136 plan tests => 151;
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, 
162     [qw/Order OrderLine Customer ContactDetails ContactDetails_Customer/]
163 ,"tables");
164
165 test_table( $scma->get_table("Customer"),
166     name => "Customer",
167     fields => [
168     {
169         name => "name",
170         data_type => "VARCHAR",
171                 size => 255,
172         default_value => undef,
173         is_nullable => 0,
174         is_primary_key => 0,
175     },
176     {
177         name => "email",
178         data_type => "VARCHAR",
179                 size => 255,
180         default_value => undef,
181         is_nullable => 1,
182         is_primary_key => 0,
183     },
184     {
185         name => "CustomerID",
186         data_type => "INT",
187                 size => 10,
188         default_value => undef,
189         is_nullable => 0,
190         is_primary_key => 1,
191     },
192     ],
193         constraints => [
194                 {
195                         type => "PRIMARY KEY",
196                         fields => "CustomerID",
197                 },
198         #{
199                 #       name => "UniqueEmail",
200                 #       type => "UNIQUE",
201                 #       fields => "email",
202                 #},
203         ],
204 );
205
206 test_table( $scma->get_table("ContactDetails_Customer"),
207     name => "ContactDetails_Customer",
208     fields => [
209     {
210         name => "ContactDetailsID",
211         data_type => "INT",
212                 size => 10,
213         default_value => undef,
214         is_nullable => 0,
215         is_primary_key => 1,
216         is_auto_increment => 0,
217     },
218     {
219         name => "CustomerID",
220         data_type => "INT",
221                 size => 10,
222         default_value => undef,
223         is_nullable => 0,
224         is_primary_key => 1,
225         is_auto_increment => 0,
226     },
227     ],
228         constraints => [
229                 {
230                         type => "FOREIGN KEY",
231                         fields => "ContactDetailsID",
232                         reference_table => "ContactDetails",
233                         reference_fields => "ContactDetailsID",
234                 },
235                 {
236                         type => "FOREIGN KEY",
237                         fields => "CustomerID",
238                         reference_table => "Customer",
239                         reference_fields => "CustomerID",
240                 },
241                 {
242                         type => "PRIMARY KEY",
243                         fields => "ContactDetailsID,CustomerID",
244                 },
245         ],
246 );
247
248 test_table( $scma->get_table("ContactDetails"),
249     name => "ContactDetails",
250     fields => [
251     {
252         name => "address",
253         data_type => "VARCHAR",
254         size => "255",
255         default_value => undef,
256         is_nullable => 1,
257         is_primary_key => 0,
258     },
259     {
260         name => "telephone",
261         data_type => "VARCHAR",
262         size => "255",
263         default_value => undef,
264         is_nullable => 1,
265         is_primary_key => 0,
266     },
267     {
268         name => "ContactDetailsID",
269         data_type => "INT",
270                 size => 10,
271         default_value => undef,
272         is_nullable => 0,
273         is_primary_key => 1,
274         is_auto_increment => 1,
275     },
276     ],
277         constraints => [
278                 {
279                         type => "PRIMARY KEY",
280                         fields => "ContactDetailsID",
281                 },
282         ],
283 );
284
285 test_table( $scma->get_table("Order"),
286     name => "Order",
287     fields => [
288     {
289         name => "invoiceNumber",
290         data_type => "INT",
291                 size => 10,
292         default_value => undef,
293         is_nullable => 0,
294         is_primary_key => 1,
295     },
296     {
297         name => "orderDate",
298         data_type => "DATE",
299         default_value => undef,
300         is_nullable => 0,
301         is_primary_key => 0,
302     },
303     {
304         name => "CustomerID",
305         data_type => "INT",
306                 size => 10,
307         default_value => undef,
308         is_nullable => 0,
309         is_primary_key => 0,
310         is_foreign_key => 1,
311     },
312     ],
313         constraints => [
314                 {
315                         type => "PRIMARY KEY",
316                         fields => "invoiceNumber",
317                 },
318                 {
319                         type => "FOREIGN KEY",
320                         fields => "CustomerID",
321                         reference_table => "Customer",
322                         reference_fields => "CustomerID",
323                 },
324         ],
325         # TODO
326         #indexes => [
327         #       {
328         #               name => "idxOrderDate",
329         #               type => "INDEX",
330         #               fields => "orderDate",
331         #       },
332         #],
333 );
334
335
336 test_table( $scma->get_table("OrderLine"),
337     name => "OrderLine",
338     fields => [
339     {
340         name => "lineNumber",
341         data_type => "INT",
342                 size => 255,
343         default_value => 1,
344         is_nullable => 0,
345         is_primary_key => 0,
346     },
347     {
348         name => "quantity",
349         data_type => "INT",
350                 size => 255,
351         default_value => 1,
352         is_nullable => 0,
353         is_primary_key => 0,
354     },
355     {
356         name => "OrderLineID",
357         data_type => "INT",
358                 size => 10,
359         default_value => undef,
360         is_nullable => 0,
361         is_primary_key => 1,
362     },
363     {
364         name => "invoiceNumber",
365         data_type => "INT",
366                 size => 10,
367         default_value => undef,
368         is_nullable => 1,
369         is_primary_key => 1,
370     },
371     ],
372         constraints => [
373                 {
374                         type => "PRIMARY KEY",
375                         fields => "OrderLineID,invoiceNumber",
376                 },
377                 {
378                         type => "FOREIGN KEY",
379                         fields => "invoiceNumber",
380                         reference_table => "Order",
381                         reference_fields => "invoiceNumber",
382                 },
383         ],
384 );