Lots of Pg Producer tests, some other fixes
[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::SQL::Translator;
18 use SQL::Translator;
19 use SQL::Translator::Schema::Constants;
20
21 # Testing 1,2,3,..
22 #=============================================================================
23
24 BEGIN {
25     maybe_plan(335,
26         'SQL::Translator::Parser::XML::XMI::SQLFairy',
27         'SQL::Translator::Producer::MySQL');
28 }
29
30 my $testschema = "$Bin/data/xmi/OrderDB.sqlfairy.poseidon2.xmi";
31 die "Can't find test schema $testschema" unless -e $testschema;
32
33 my $obj;
34 $obj = SQL::Translator->new(
35     filename => $testschema,
36     from     => 'XML-XMI-SQLFairy',
37     to       => 'MySQL',
38     debug          => DEBUG,
39     show_warnings  => 1,
40 );
41 my $sql = $obj->translate;
42 ok( $sql, "Got some SQL");
43 print $sql if DEBUG;
44 print "Translator:",Dumper($obj) if DEBUG;
45
46
47 #
48 # Test the schema
49 #
50 my $scma = $obj->schema;
51 is( $scma->is_valid, 1, 'Schema is valid' );
52 my @tblnames = map {$_->name} $scma->get_tables;
53 is(scalar(@{$scma->get_tables}), scalar(@tblnames), "Right number of tables");
54 is_deeply( \@tblnames, 
55     [qw/Order OrderLine Customer ContactDetails ContactDetails_Customer/]
56 ,"tables");
57
58 table_ok( $scma->get_table("Customer"), {
59     name => "Customer",
60     extra => {
61         mysql_table_type => 'InnoDB'
62     },
63     fields => [
64     {
65         name => "name",
66         data_type => "VARCHAR",
67         size => 255,
68         default_value => undef,
69         is_nullable => 0,
70         is_primary_key => 0,
71     },
72     {
73         name => "email",
74         data_type => "VARCHAR",
75         size => 255,
76         default_value => undef,
77         is_nullable => 1,
78         is_primary_key => 0,
79     },
80     {
81         name => "CustomerID",
82         data_type => "INT",
83         size => 10,
84         default_value => undef,
85         is_nullable => 0,
86         is_primary_key => 1,
87         is_auto_increment => 1,
88     },
89     ],
90     constraints => [
91         {
92             type => "PRIMARY KEY",
93             fields => ["CustomerID"],
94         },
95         #{
96         #    name => "UniqueEmail",
97         #    type => "UNIQUE",
98         #    fields => ["email"],
99         #},
100     ],
101 });
102
103 table_ok( $scma->get_table("ContactDetails_Customer"), {
104     name => "ContactDetails_Customer",
105     extra => {
106         mysql_table_type => 'InnoDB'
107     },
108     fields => [
109     {
110         name => "ContactDetailsID",
111         data_type => "INT",
112         size => 10,
113         default_value => undef,
114         is_nullable => 0,
115         is_primary_key => 1,
116         is_auto_increment => 0,
117         is_foreign_key => 1,
118     },
119     {
120         name => "CustomerID",
121         data_type => "INT",
122         size => 10,
123         default_value => undef,
124         is_nullable => 0,
125         is_primary_key => 1,
126         is_auto_increment => 0,
127         is_foreign_key => 1,
128     },
129     ],
130     constraints => [
131         {
132             type => "FOREIGN KEY",
133             fields => ["ContactDetailsID"],
134             reference_table => "ContactDetails",
135             reference_fields => ["ContactDetailsID"],
136         },
137         {
138             type => "FOREIGN KEY",
139             fields => ["CustomerID"],
140             reference_table => "Customer",
141             reference_fields => ["CustomerID"],
142         },
143         {
144             type => "PRIMARY KEY",
145             fields => ["ContactDetailsID","CustomerID"],
146         },
147     ],
148 });
149
150 table_ok( $scma->get_table("ContactDetails"), {
151     name => "ContactDetails",
152     extra => {
153         mysql_table_type => 'InnoDB'
154     },
155     fields => [
156     {
157         name => "address",
158         data_type => "VARCHAR",
159         size => "255",
160         default_value => undef,
161         is_nullable => 1,
162         is_primary_key => 0,
163     },
164     {
165         name => "telephone",
166         data_type => "VARCHAR",
167         size => "255",
168         default_value => undef,
169         is_nullable => 1,
170         is_primary_key => 0,
171     },
172     {
173         name => "ContactDetailsID",
174         data_type => "INT",
175         size => 10,
176         default_value => undef,
177         is_nullable => 0,
178         is_primary_key => 1,
179         is_auto_increment => 1,
180     },
181     ],
182     constraints => [
183         {
184             type => "PRIMARY KEY",
185             fields => ["ContactDetailsID"],
186         },
187     ],
188 });
189
190 table_ok( $scma->get_table("Order"), {
191     name => "Order",
192     extra => {
193         mysql_table_type => 'InnoDB'
194     },
195     fields => [
196     {
197         name => "invoiceNumber",
198         data_type => "INT",
199         size => 10,
200         default_value => undef,
201         is_nullable => 0,
202         is_primary_key => 1,
203         is_auto_increment => 1,
204     },
205     {
206         name => "orderDate",
207         data_type => "DATE",
208         default_value => undef,
209         is_nullable => 0,
210         is_primary_key => 0,
211     },
212     {
213         name => "CustomerID",
214         data_type => "INT",
215         size => 10,
216         default_value => undef,
217         is_nullable => 0,
218         is_primary_key => 0,
219         is_foreign_key => 1,
220     },
221     ],
222     constraints => [
223         {
224             type => "PRIMARY KEY",
225             fields => ["invoiceNumber"],
226         },
227         {
228             type => "FOREIGN KEY",
229             fields => ["CustomerID"],
230             reference_table => "Customer",
231             reference_fields => ["CustomerID"],
232         },
233     ],
234     # TODO
235     #indexes => [
236     #    {
237     #        name => "idxOrderDate",
238     #        type => "INDEX",
239     #        fields => ["orderDate"],
240     #    },
241     #],
242 });
243
244
245 table_ok( $scma->get_table("OrderLine"), {
246     name => "OrderLine",
247     extra => {
248         mysql_table_type => 'InnoDB'
249     },
250     fields => [
251     {
252         name => "lineNumber",
253         data_type => "INT",
254         size => 255,
255         default_value => 1,
256         is_nullable => 0,
257         is_primary_key => 0,
258     },
259     {
260         name => "quantity",
261         data_type => "INT",
262         size => 255,
263         default_value => 1,
264         is_nullable => 0,
265         is_primary_key => 0,
266     },
267     {
268         name => "OrderLineID",
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         name => "invoiceNumber",
278         data_type => "INT",
279         size => 10,
280         default_value => undef,
281         is_nullable => 1,
282         is_primary_key => 1,
283     },
284     ],
285     constraints => [
286         {
287             type => "PRIMARY KEY",
288             fields => ["OrderLineID","invoiceNumber"],
289         },
290         {
291             type => "FOREIGN KEY",
292             fields => ["invoiceNumber"],
293             reference_table => "Order",
294             reference_fields => ["invoiceNumber"],
295         },
296     ],
297 });