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