PKeys automatically generated for Classes that don't set them explicitly with
[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 => 94;
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     {
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     },
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     },
191     ],
192         constraints => [
193                 {
194                         type => "PRIMARY KEY",
195                         fields => "CustomerID",
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     {
209         name => "invoiceNumber",
210         data_type => "INT",
211                 size => 10,
212         default_value => undef,
213         is_nullable => 0,
214         is_primary_key => 1,
215     },
216     {
217         name => "orderDate",
218         data_type => "DATE",
219         default_value => undef,
220         is_nullable => 0,
221         is_primary_key => 0,
222     },
223     {
224         name => "CustomerID",
225         data_type => "INT",
226                 size => 10,
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",
236                         fields => "invoiceNumber",
237                 },
238                 {
239                         type => "FOREIGN KEY",
240                         fields => "CustomerID",
241                         reference_table => "Customer",
242                         reference_fields => "CustomerID",
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,
265         is_primary_key => 0,
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     {
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",
285         data_type => "INT",
286                 size => 10,
287         default_value => undef,
288         is_nullable => 1,
289         is_primary_key => 1,
290     },
291     ],
292         constraints => [
293                 {
294                         type => "PRIMARY KEY",
295                         fields => "OrderLineID,invoiceNumber",
296                 },
297                 {
298                         type => "FOREIGN KEY",
299                         fields => "invoiceNumber",
300                         reference_table => "Order",
301                         reference_fields => "invoiceNumber",
302                 },
303         ],
304 );