Commit | Line | Data |
f992cfaa |
1 | #!/usr/bin/perl -w |
55e462b4 |
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; |
090d438c |
17 | use Test::SQL::Translator; |
55e462b4 |
18 | use SQL::Translator; |
19 | use SQL::Translator::Schema::Constants; |
20 | |
55e462b4 |
21 | # Testing 1,2,3,.. |
22 | #============================================================================= |
23 | |
2d691ec1 |
24 | BEGIN { |
b1789409 |
25 | maybe_plan(335, |
2d691ec1 |
26 | 'SQL::Translator::Parser::XML::XMI::SQLFairy', |
27 | 'SQL::Translator::Producer::MySQL'); |
28 | } |
55e462b4 |
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; |
090d438c |
44 | print "Translator:",Dumper($obj) if DEBUG; |
55e462b4 |
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"); |
ae15bb99 |
54 | is_deeply( \@tblnames, |
55 | [qw/Order OrderLine Customer ContactDetails ContactDetails_Customer/] |
56 | ,"tables"); |
55e462b4 |
57 | |
090d438c |
58 | table_ok( $scma->get_table("Customer"), { |
55e462b4 |
59 | name => "Customer", |
528c5443 |
60 | extra => { |
61 | mysql_table_type => 'InnoDB' |
62 | }, |
55e462b4 |
63 | fields => [ |
64 | { |
55e462b4 |
65 | name => "name", |
66 | data_type => "VARCHAR", |
090d438c |
67 | size => 255, |
55e462b4 |
68 | default_value => undef, |
69 | is_nullable => 0, |
70 | is_primary_key => 0, |
71 | }, |
72 | { |
73 | name => "email", |
74 | data_type => "VARCHAR", |
090d438c |
75 | size => 255, |
55e462b4 |
76 | default_value => undef, |
77 | is_nullable => 1, |
78 | is_primary_key => 0, |
79 | }, |
f992cfaa |
80 | { |
81 | name => "CustomerID", |
82 | data_type => "INT", |
090d438c |
83 | size => 10, |
f992cfaa |
84 | default_value => undef, |
85 | is_nullable => 0, |
86 | is_primary_key => 1, |
090d438c |
87 | is_auto_increment => 1, |
f992cfaa |
88 | }, |
55e462b4 |
89 | ], |
090d438c |
90 | constraints => [ |
91 | { |
92 | type => "PRIMARY KEY", |
93 | fields => ["CustomerID"], |
94 | }, |
55e462b4 |
95 | #{ |
090d438c |
96 | # name => "UniqueEmail", |
97 | # type => "UNIQUE", |
98 | # fields => ["email"], |
99 | #}, |
100 | ], |
101 | }); |
55e462b4 |
102 | |
090d438c |
103 | table_ok( $scma->get_table("ContactDetails_Customer"), { |
ae15bb99 |
104 | name => "ContactDetails_Customer", |
528c5443 |
105 | extra => { |
106 | mysql_table_type => 'InnoDB' |
107 | }, |
ae15bb99 |
108 | fields => [ |
109 | { |
110 | name => "ContactDetailsID", |
111 | data_type => "INT", |
090d438c |
112 | size => 10, |
ae15bb99 |
113 | default_value => undef, |
114 | is_nullable => 0, |
115 | is_primary_key => 1, |
116 | is_auto_increment => 0, |
090d438c |
117 | is_foreign_key => 1, |
ae15bb99 |
118 | }, |
119 | { |
120 | name => "CustomerID", |
121 | data_type => "INT", |
090d438c |
122 | size => 10, |
ae15bb99 |
123 | default_value => undef, |
124 | is_nullable => 0, |
125 | is_primary_key => 1, |
126 | is_auto_increment => 0, |
090d438c |
127 | is_foreign_key => 1, |
ae15bb99 |
128 | }, |
129 | ], |
090d438c |
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 | }); |
ae15bb99 |
149 | |
090d438c |
150 | table_ok( $scma->get_table("ContactDetails"), { |
ae15bb99 |
151 | name => "ContactDetails", |
528c5443 |
152 | extra => { |
153 | mysql_table_type => 'InnoDB' |
154 | }, |
ae15bb99 |
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", |
090d438c |
175 | size => 10, |
ae15bb99 |
176 | default_value => undef, |
177 | is_nullable => 0, |
178 | is_primary_key => 1, |
179 | is_auto_increment => 1, |
180 | }, |
181 | ], |
090d438c |
182 | constraints => [ |
183 | { |
184 | type => "PRIMARY KEY", |
185 | fields => ["ContactDetailsID"], |
186 | }, |
187 | ], |
188 | }); |
ae15bb99 |
189 | |
090d438c |
190 | table_ok( $scma->get_table("Order"), { |
55e462b4 |
191 | name => "Order", |
528c5443 |
192 | extra => { |
193 | mysql_table_type => 'InnoDB' |
194 | }, |
55e462b4 |
195 | fields => [ |
196 | { |
f992cfaa |
197 | name => "invoiceNumber", |
198 | data_type => "INT", |
090d438c |
199 | size => 10, |
55e462b4 |
200 | default_value => undef, |
201 | is_nullable => 0, |
f992cfaa |
202 | is_primary_key => 1, |
090d438c |
203 | is_auto_increment => 1, |
55e462b4 |
204 | }, |
205 | { |
f992cfaa |
206 | name => "orderDate", |
207 | data_type => "DATE", |
55e462b4 |
208 | default_value => undef, |
209 | is_nullable => 0, |
f992cfaa |
210 | is_primary_key => 0, |
55e462b4 |
211 | }, |
212 | { |
f992cfaa |
213 | name => "CustomerID", |
55e462b4 |
214 | data_type => "INT", |
090d438c |
215 | size => 10, |
55e462b4 |
216 | default_value => undef, |
217 | is_nullable => 0, |
218 | is_primary_key => 0, |
219 | is_foreign_key => 1, |
220 | }, |
221 | ], |
090d438c |
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"), { |
55e462b4 |
246 | name => "OrderLine", |
528c5443 |
247 | extra => { |
248 | mysql_table_type => 'InnoDB' |
249 | }, |
55e462b4 |
250 | fields => [ |
251 | { |
252 | name => "lineNumber", |
253 | data_type => "INT", |
090d438c |
254 | size => 255, |
55e462b4 |
255 | default_value => 1, |
256 | is_nullable => 0, |
f992cfaa |
257 | is_primary_key => 0, |
55e462b4 |
258 | }, |
259 | { |
260 | name => "quantity", |
261 | data_type => "INT", |
090d438c |
262 | size => 255, |
55e462b4 |
263 | default_value => 1, |
264 | is_nullable => 0, |
265 | is_primary_key => 0, |
266 | }, |
267 | { |
f992cfaa |
268 | name => "OrderLineID", |
269 | data_type => "INT", |
090d438c |
270 | size => 10, |
f992cfaa |
271 | default_value => undef, |
272 | is_nullable => 0, |
273 | is_primary_key => 1, |
090d438c |
274 | is_auto_increment => 1, |
f992cfaa |
275 | }, |
276 | { |
277 | name => "invoiceNumber", |
55e462b4 |
278 | data_type => "INT", |
090d438c |
279 | size => 10, |
55e462b4 |
280 | default_value => undef, |
281 | is_nullable => 1, |
f992cfaa |
282 | is_primary_key => 1, |
55e462b4 |
283 | }, |
284 | ], |
090d438c |
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 | }); |