All Schema objects now have an extra attribute. Added parsing support (and
[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     fields => [
61     {
62         name => "name",
63         data_type => "VARCHAR",
64         size => 255,
65         default_value => undef,
66         is_nullable => 0,
67         is_primary_key => 0,
68     },
69     {
70         name => "email",
71         data_type => "VARCHAR",
72         size => 255,
73         default_value => undef,
74         is_nullable => 1,
75         is_primary_key => 0,
76     },
77     {
78         name => "CustomerID",
79         data_type => "INT",
80         size => 10,
81         default_value => undef,
82         is_nullable => 0,
83         is_primary_key => 1,
84         is_auto_increment => 1,
85     },
86     ],
87     constraints => [
88         {
89             type => "PRIMARY KEY",
90             fields => ["CustomerID"],
91         },
92         #{
93         #    name => "UniqueEmail",
94         #    type => "UNIQUE",
95         #    fields => ["email"],
96         #},
97     ],
98 });
99
100 table_ok( $scma->get_table("ContactDetails_Customer"), {
101     name => "ContactDetails_Customer",
102     fields => [
103     {
104         name => "ContactDetailsID",
105         data_type => "INT",
106         size => 10,
107         default_value => undef,
108         is_nullable => 0,
109         is_primary_key => 1,
110         is_auto_increment => 0,
111         is_foreign_key => 1,
112     },
113     {
114         name => "CustomerID",
115         data_type => "INT",
116         size => 10,
117         default_value => undef,
118         is_nullable => 0,
119         is_primary_key => 1,
120         is_auto_increment => 0,
121         is_foreign_key => 1,
122     },
123     ],
124     constraints => [
125         {
126             type => "FOREIGN KEY",
127             fields => ["ContactDetailsID"],
128             reference_table => "ContactDetails",
129             reference_fields => ["ContactDetailsID"],
130         },
131         {
132             type => "FOREIGN KEY",
133             fields => ["CustomerID"],
134             reference_table => "Customer",
135             reference_fields => ["CustomerID"],
136         },
137         {
138             type => "PRIMARY KEY",
139             fields => ["ContactDetailsID","CustomerID"],
140         },
141     ],
142 });
143
144 table_ok( $scma->get_table("ContactDetails"), {
145     name => "ContactDetails",
146     fields => [
147     {
148         name => "address",
149         data_type => "VARCHAR",
150         size => "255",
151         default_value => undef,
152         is_nullable => 1,
153         is_primary_key => 0,
154     },
155     {
156         name => "telephone",
157         data_type => "VARCHAR",
158         size => "255",
159         default_value => undef,
160         is_nullable => 1,
161         is_primary_key => 0,
162     },
163     {
164         name => "ContactDetailsID",
165         data_type => "INT",
166         size => 10,
167         default_value => undef,
168         is_nullable => 0,
169         is_primary_key => 1,
170         is_auto_increment => 1,
171     },
172     ],
173     constraints => [
174         {
175             type => "PRIMARY KEY",
176             fields => ["ContactDetailsID"],
177         },
178     ],
179 });
180
181 table_ok( $scma->get_table("Order"), {
182     name => "Order",
183     fields => [
184     {
185         name => "invoiceNumber",
186         data_type => "INT",
187         size => 10,
188         default_value => undef,
189         is_nullable => 0,
190         is_primary_key => 1,
191         is_auto_increment => 1,
192     },
193     {
194         name => "orderDate",
195         data_type => "DATE",
196         default_value => undef,
197         is_nullable => 0,
198         is_primary_key => 0,
199     },
200     {
201         name => "CustomerID",
202         data_type => "INT",
203         size => 10,
204         default_value => undef,
205         is_nullable => 0,
206         is_primary_key => 0,
207         is_foreign_key => 1,
208     },
209     ],
210     constraints => [
211         {
212             type => "PRIMARY KEY",
213             fields => ["invoiceNumber"],
214         },
215         {
216             type => "FOREIGN KEY",
217             fields => ["CustomerID"],
218             reference_table => "Customer",
219             reference_fields => ["CustomerID"],
220         },
221     ],
222     # TODO
223     #indexes => [
224     #    {
225     #        name => "idxOrderDate",
226     #        type => "INDEX",
227     #        fields => ["orderDate"],
228     #    },
229     #],
230 });
231
232
233 table_ok( $scma->get_table("OrderLine"), {
234     name => "OrderLine",
235     fields => [
236     {
237         name => "lineNumber",
238         data_type => "INT",
239         size => 255,
240         default_value => 1,
241         is_nullable => 0,
242         is_primary_key => 0,
243     },
244     {
245         name => "quantity",
246         data_type => "INT",
247         size => 255,
248         default_value => 1,
249         is_nullable => 0,
250         is_primary_key => 0,
251     },
252     {
253         name => "OrderLineID",
254         data_type => "INT",
255         size => 10,
256         default_value => undef,
257         is_nullable => 0,
258         is_primary_key => 1,
259         is_auto_increment => 1,
260     },
261     {
262         name => "invoiceNumber",
263         data_type => "INT",
264         size => 10,
265         default_value => undef,
266         is_nullable => 1,
267         is_primary_key => 1,
268     },
269     ],
270     constraints => [
271         {
272             type => "PRIMARY KEY",
273             fields => ["OrderLineID","invoiceNumber"],
274         },
275         {
276             type => "FOREIGN KEY",
277             fields => ["invoiceNumber"],
278             reference_table => "Order",
279             reference_fields => ["invoiceNumber"],
280         },
281     ],
282 });