Robustify the tests a little.
[dbsrgits/SQL-Translator.git] / t / 02mysql-parser.t
CommitLineData
0494e672 1#!/usr/bin/perl
2# vim: set ft=perl:
3#
0494e672 4
b8661f30 5use strict;
44fb27ae 6
fbc0552f 7use Test::More;
b8661f30 8use SQL::Translator;
2ab84c11 9use SQL::Translator::Schema::Constants;
0494e672 10
fbc0552f 11eval {
12 require SQL::Translator::Parser::MySQL;
13 SQL::Translator::Parser::MySQL->import('parse');
14};
15if ($@) {
16 plan skip_all => "$@";
17}
18else {
19 plan tests => 180;
20}
21
251b6ff5 22{
23 my $tr = SQL::Translator->new;
24 my $data = q|create table sessions (
2ab84c11 25 id char(32) not null default '0' primary key,
251b6ff5 26 a_session text
27 );|;
28
29 my $val = parse($tr, $data);
30
2ab84c11 31 my $schema = $tr->schema;
e3252059 32 is( $schema->is_valid, 1, 'Schema is valid' );
2ab84c11 33 my @tables = $schema->get_tables;
34 is( scalar @tables, 1, 'Right number of tables (1)' );
35 my $table = shift @tables;
36 is( $table->name, 'sessions', 'Found "sessions" table' );
251b6ff5 37
2ab84c11 38 my @fields = $table->get_fields;
39 is( scalar @fields, 2, 'Right number of fields (2)' );
40 my $f1 = shift @fields;
41 my $f2 = shift @fields;
42 is( $f1->name, 'id', 'First field name is "id"' );
43 is( $f1->data_type, 'char', 'Type is "char"' );
44 is( $f1->size, 32, 'Size is "32"' );
45 is( $f1->is_nullable, 0, 'Field cannot be null' );
46 is( $f1->default_value, '0', 'Default value is "0"' );
47 is( $f1->is_primary_key, 1, 'Field is PK' );
251b6ff5 48
2ab84c11 49 is( $f2->name, 'a_session', 'Second field name is "a_session"' );
50 is( $f2->data_type, 'text', 'Type is "text"' );
0c1c8d2a 51 is( $f2->size, 65_535, 'Size is "65,535"' );
2ab84c11 52 is( $f2->is_nullable, 1, 'Field can be null' );
53 is( $f2->default_value, undef, 'Default value is undefined' );
54 is( $f2->is_primary_key, 0, 'Field is not PK' );
251b6ff5 55
2ab84c11 56 my @indices = $table->get_indices;
57 is( scalar @indices, 0, 'Right number of indices (0)' );
251b6ff5 58
2ab84c11 59 my @constraints = $table->get_constraints;
60 is( scalar @constraints, 1, 'Right number of constraints (1)' );
61 my $c = shift @constraints;
62 is( $c->type, PRIMARY_KEY, 'Constraint is a PK' );
63 is( join(',', $c->fields), 'id', 'Constraint is on "id"' );
251b6ff5 64}
65
66{
67 my $tr = SQL::Translator->new;
68 my $data = parse($tr,
69 q[
70 CREATE TABLE check (
2ab84c11 71 check_id int(7) unsigned zerofill NOT NULL default '0000000'
251b6ff5 72 auto_increment primary key,
73 successful date NOT NULL default '0000-00-00',
74 unsuccessful date default '0000-00-00',
75 i1 int(11) default '0' not null,
76 s1 set('a','b','c') default 'b',
77 e1 enum('a','b','c') default 'c',
78 name varchar(30) default NULL,
79 foo_type enum('vk','ck') NOT NULL default 'vk',
80 date timestamp,
81 time_stamp2 timestamp,
82 KEY (i1),
83 UNIQUE (date, i1),
84 KEY date_idx (date),
85 KEY name_idx (name(10))
86 ) TYPE=MyISAM PACK_KEYS=1;
87 ]
88 );
89
2ab84c11 90 my $schema = $tr->schema;
e3252059 91 is( $schema->is_valid, 1, 'Schema is valid' );
2ab84c11 92 my @tables = $schema->get_tables;
93 is( scalar @tables, 1, 'Right number of tables (1)' );
94 my $table = shift @tables;
95 is( $table->name, 'check', 'Found "check" table' );
96
97 my @fields = $table->get_fields;
98 is( scalar @fields, 10, 'Right number of fields (10)' );
99 my $f1 = shift @fields;
100 is( $f1->name, 'check_id', 'First field name is "check_id"' );
101 is( $f1->data_type, 'int', 'Type is "int"' );
102 is( $f1->size, 7, 'Size is "7"' );
103 is( $f1->is_nullable, 0, 'Field cannot be null' );
104 is( $f1->default_value, '0000000', 'Default value is "0000000"' );
105 is( $f1->is_primary_key, 1, 'Field is PK' );
106 is( $f1->is_auto_increment, 1, 'Field is auto inc' );
107 my %extra = $f1->extra;
108 ok( defined $extra{'unsigned'}, 'Field is unsigned' );
109 ok( defined $extra{'zerofill'}, 'Field is zerofill' );
110
111 my $f2 = shift @fields;
112 is( $f2->name, 'successful', 'Second field name is "successful"' );
113 is( $f2->data_type, 'date', 'Type is "date"' );
114 is( $f2->size, 0, 'Size is "0"' );
115 is( $f2->is_nullable, 0, 'Field cannot be null' );
116 is( $f2->default_value, '0000-00-00', 'Default value is "0000-00-00"' );
117 is( $f2->is_primary_key, 0, 'Field is not PK' );
118
119 my $f3 = shift @fields;
120 is( $f3->name, 'unsuccessful', 'Third field name is "unsuccessful"' );
121 is( $f3->data_type, 'date', 'Type is "date"' );
122 is( $f3->size, 0, 'Size is "0"' );
123 is( $f3->is_nullable, 1, 'Field can be null' );
124 is( $f3->default_value, '0000-00-00', 'Default value is "0000-00-00"' );
125 is( $f3->is_primary_key, 0, 'Field is not PK' );
126
127 my $f4 = shift @fields;
128 is( $f4->name, 'i1', 'Fourth field name is "i1"' );
129 is( $f4->data_type, 'int', 'Type is "int"' );
130 is( $f4->size, 11, 'Size is "11"' );
131 is( $f4->is_nullable, 0, 'Field cannot be null' );
132 is( $f4->default_value, '0', 'Default value is "0"' );
133 is( $f4->is_primary_key, 0, 'Field is not PK' );
3a0b6f0a 134
2ab84c11 135 my $f5 = shift @fields;
136 is( $f5->name, 's1', 'Fifth field name is "s1"' );
137 is( $f5->data_type, 'set', 'Type is "set"' );
138 is( $f5->size, 1, 'Size is "1"' );
139 is( $f5->is_nullable, 1, 'Field can be null' );
140 is( $f5->default_value, 'b', 'Default value is "b"' );
141 is( $f5->is_primary_key, 0, 'Field is not PK' );
142 my %f5extra = $f5->extra;
143 is( join(',', @{ $f5extra{'list'} || [] }), 'a,b,c', 'List is "a,b,c"' );
3a0b6f0a 144
2ab84c11 145 my $f6 = shift @fields;
146 is( $f6->name, 'e1', 'Sixth field name is "e1"' );
147 is( $f6->data_type, 'enum', 'Type is "enum"' );
148 is( $f6->size, 1, 'Size is "1"' );
149 is( $f6->is_nullable, 1, 'Field can be null' );
150 is( $f6->default_value, 'c', 'Default value is "c"' );
151 is( $f6->is_primary_key, 0, 'Field is not PK' );
152 my %f6extra = $f6->extra;
153 is( join(',', @{ $f6extra{'list'} || [] }), 'a,b,c', 'List is "a,b,c"' );
3a0b6f0a 154
2ab84c11 155 my $f7 = shift @fields;
156 is( $f7->name, 'name', 'Seventh field name is "name"' );
157 is( $f7->data_type, 'varchar', 'Type is "varchar"' );
158 is( $f7->size, 30, 'Size is "30"' );
159 is( $f7->is_nullable, 1, 'Field can be null' );
160 is( $f7->default_value, 'NULL', 'Default value is "NULL"' );
161 is( $f7->is_primary_key, 0, 'Field is not PK' );
3a0b6f0a 162
2ab84c11 163 my $f8 = shift @fields;
164 is( $f8->name, 'foo_type', 'Eighth field name is "foo_type"' );
165 is( $f8->data_type, 'enum', 'Type is "enum"' );
166 is( $f8->size, 2, 'Size is "2"' );
167 is( $f8->is_nullable, 0, 'Field cannot be null' );
168 is( $f8->default_value, 'vk', 'Default value is "vk"' );
169 is( $f8->is_primary_key, 0, 'Field is not PK' );
170 my %f8extra = $f8->extra;
171 is( join(',', @{ $f8extra{'list'} || [] }), 'vk,ck', 'List is "vk,ck"' );
172
173 my $f9 = shift @fields;
174 is( $f9->name, 'date', 'Ninth field name is "date"' );
175 is( $f9->data_type, 'timestamp', 'Type is "timestamp"' );
176 is( $f9->size, 0, 'Size is "0"' );
177 is( $f9->is_nullable, 1, 'Field can be null' );
178 is( $f9->default_value, undef, 'Default value is undefined' );
179 is( $f9->is_primary_key, 0, 'Field is not PK' );
180
181 my $f10 = shift @fields;
182 is( $f10->name, 'time_stamp2', 'Tenth field name is "time_stamp2"' );
183 is( $f10->data_type, 'timestamp', 'Type is "timestamp"' );
184 is( $f10->size, 0, 'Size is "0"' );
185 is( $f10->is_nullable, 1, 'Field can be null' );
186 is( $f10->default_value, undef, 'Default value is undefined' );
187 is( $f10->is_primary_key, 0, 'Field is not PK' );
188
189 my @indices = $table->get_indices;
190 is( scalar @indices, 3, 'Right number of indices (3)' );
191
192 my $i1 = shift @indices;
193 is( $i1->name, '', 'No name on index' );
194 is( $i1->type, NORMAL, 'Normal index' );
195 is( join(',', $i1->fields ), 'i1', 'Index is on field "i1"' );
196
197 my $i2 = shift @indices;
198 is( $i2->name, 'date_idx', 'Name is "date_idx"' );
199 is( $i2->type, NORMAL, 'Normal index' );
200 is( join(',', $i2->fields ), 'date', 'Index is on field "date"' );
201
202 my $i3 = shift @indices;
203 is( $i3->name, 'name_idx', 'Name is "name_idx"' );
204 is( $i3->type, NORMAL, 'Normal index' );
205 is( join(',', $i3->fields ), 'name(10)', 'Index is on field "name(10)"' );
206
207 my @constraints = $table->get_constraints;
208 is( scalar @constraints, 2, 'Right number of constraints (2)' );
209
210 my $c1 = shift @constraints;
211 is( $c1->type, PRIMARY_KEY, 'Constraint is a PK' );
212 is( join(',', $c1->fields), 'check_id', 'Constraint is on "check_id"' );
213
214 my $c2 = shift @constraints;
215 is( $c2->type, UNIQUE, 'Constraint is UNIQUE' );
216 is( join(',', $c2->fields), 'date,i1', 'Constraint is on "date, i1"' );
251b6ff5 217}
218
219{
220 my $tr = SQL::Translator->new;
221 my $data = parse($tr,
222 q[
223 CREATE TABLE orders (
3a0b6f0a 224 order_id integer NOT NULL auto_increment,
251b6ff5 225 member_id varchar(255),
226 billing_address_id int,
227 shipping_address_id int,
228 credit_card_id int,
229 status smallint NOT NULL,
230 store_id varchar(255) NOT NULL REFERENCES store,
231 tax decimal(8,2),
232 shipping_charge decimal(8,2),
233 price_paid decimal(8,2),
234 PRIMARY KEY (order_id),
235 KEY (status),
236 KEY (billing_address_id),
237 KEY (shipping_address_id),
238 KEY (member_id, store_id),
556d0509 239 FOREIGN KEY (status) REFERENCES order_status(id) MATCH FULL ON DELETE CASCADE ON UPDATE CASCADE,
251b6ff5 240 FOREIGN KEY (billing_address_id) REFERENCES address(address_id),
241 FOREIGN KEY (shipping_address_id) REFERENCES address(address_id)
242 ) TYPE=INNODB;
2ab84c11 243
244 CREATE TABLE address (
245 address_id int NOT NULL auto_increment,
246 recipient varchar(255) NOT NULL,
247 address1 varchar(255) NOT NULL,
248 address2 varchar(255),
249 city varchar(255) NOT NULL,
250 state varchar(255) NOT NULL,
251 postal_code varchar(255) NOT NULL,
252 phone varchar(255),
253 PRIMARY KEY (address_id)
254 ) TYPE=INNODB;
251b6ff5 255 ]
256 ) or die $tr->error;
257
2ab84c11 258 my $schema = $tr->schema;
e3252059 259 is( $schema->is_valid, 1, 'Schema is valid' );
2ab84c11 260 my @tables = $schema->get_tables;
261 is( scalar @tables, 2, 'Right number of tables (2)' );
262
263 my $t1 = shift @tables;
264 is( $t1->name, 'orders', 'Found "orders" table' );
265
266 my @fields = $t1->get_fields;
267 is( scalar @fields, 10, 'Right number of fields (10)' );
268
269 my $f1 = shift @fields;
270 is( $f1->name, 'order_id', 'First field name is "order_id"' );
271 is( $f1->data_type, 'int', 'Type is "int"' );
272 is( $f1->size, 11, 'Size is "11"' );
273 is( $f1->is_nullable, 0, 'Field cannot be null' );
274 is( $f1->default_value, undef, 'Default value is undefined' );
275 is( $f1->is_primary_key, 1, 'Field is PK' );
276 is( $f1->is_auto_increment, 1, 'Field is auto inc' );
277
278 my $f2 = shift @fields;
279 is( $f2->name, 'member_id', 'Second field name is "member_id"' );
280 is( $f2->data_type, 'varchar', 'Type is "varchar"' );
281 is( $f2->size, 255, 'Size is "255"' );
282 is( $f2->is_nullable, 1, 'Field can be null' );
283 is( $f2->default_value, undef, 'Default value is undefined' );
284
285 my $f3 = shift @fields;
286 is( $f3->name, 'billing_address_id',
287 'Third field name is "billing_address_id"' );
288 is( $f3->data_type, 'int', 'Type is "int"' );
289 is( $f3->size, 11, 'Size is "11"' );
290
291 my $f4 = shift @fields;
292 is( $f4->name, 'shipping_address_id',
293 'Fourth field name is "shipping_address_id"' );
294 is( $f4->data_type, 'int', 'Type is "int"' );
295 is( $f4->size, 11, 'Size is "11"' );
296
297 my $f5 = shift @fields;
298 is( $f5->name, 'credit_card_id', 'Fifth field name is "credit_card_id"' );
299 is( $f5->data_type, 'int', 'Type is "int"' );
300 is( $f5->size, 11, 'Size is "11"' );
301
302 my $f6 = shift @fields;
303 is( $f6->name, 'status', 'Sixth field name is "status"' );
304 is( $f6->data_type, 'smallint', 'Type is "smallint"' );
305 is( $f6->size, 6, 'Size is "6"' );
306 is( $f6->is_nullable, 0, 'Field cannot be null' );
307
308 my $f7 = shift @fields;
309 is( $f7->name, 'store_id', 'Seventh field name is "store_id"' );
310 is( $f7->data_type, 'varchar', 'Type is "varchar"' );
311 is( $f7->size, 255, 'Size is "255"' );
312 is( $f7->is_nullable, 0, 'Field cannot be null' );
313 is( $f7->is_foreign_key, 1, 'Field is a FK' );
314 my $fk_ref = $f7->foreign_key_reference;
315 isa_ok( $fk_ref, 'SQL::Translator::Schema::Constraint', 'FK' );
316 is( $fk_ref->reference_table, 'store', 'FK is to "store" table' );
317
318 my $f8 = shift @fields;
319 is( $f8->name, 'tax', 'Eighth field name is "tax"' );
320 is( $f8->data_type, 'decimal', 'Type is "decimal"' );
321 is( $f8->size, '8,2', 'Size is "8,2"' );
322
323 my $f9 = shift @fields;
324 is( $f9->name, 'shipping_charge', 'Ninth field name is "shipping_charge"' );
325 is( $f9->data_type, 'decimal', 'Type is "decimal"' );
326 is( $f9->size, '8,2', 'Size is "8,2"' );
327
328 my $f10 = shift @fields;
329 is( $f10->name, 'price_paid', 'Tenth field name is "price_paid"' );
330 is( $f10->data_type, 'decimal', 'Type is "decimal"' );
331 is( $f10->size, '8,2', 'Size is "8,2"' );
332
333 my @indices = $t1->get_indices;
334 is( scalar @indices, 4, 'Right number of indices (4)' );
335
336 my $i1 = shift @indices;
337 is( $i1->type, NORMAL, 'First index is normal' );
338 is( join(',', $i1->fields), 'status', 'Index is on "status"' );
339
340 my $i2 = shift @indices;
341 is( $i2->type, NORMAL, 'Second index is normal' );
342 is( join(',', $i2->fields), 'billing_address_id',
343 'Index is on "billing_address_id"' );
344
345 my $i3 = shift @indices;
346 is( $i3->type, NORMAL, 'Third index is normal' );
347 is( join(',', $i3->fields), 'shipping_address_id',
348 'Index is on "shipping_address_id"' );
349
350 my $i4 = shift @indices;
351 is( $i4->type, NORMAL, 'Third index is normal' );
352 is( join(',', $i4->fields), 'member_id,store_id',
353 'Index is on "member_id,store_id"' );
354
355 my @constraints = $t1->get_constraints;
356 is( scalar @constraints, 5, 'Right number of constraints (5)' );
357
358 my $c1 = shift @constraints;
359 is( $c1->type, PRIMARY_KEY, 'Constraint is a PK' );
360 is( join(',', $c1->fields), 'order_id', 'Constraint is on "order_id"' );
361
362 my $c2 = shift @constraints;
363 is( $c2->type, FOREIGN_KEY, 'Constraint is a FK' );
364 is( join(',', $c2->fields), 'status', 'Constraint is on "status"' );
365 is( $c2->reference_table, 'order_status', 'To table "order_status"' );
366 is( join(',', $c2->reference_fields), 'id', 'To field "id"' );
367
368 my $c3 = shift @constraints;
369 is( $c3->type, FOREIGN_KEY, 'Constraint is a FK' );
370 is( join(',', $c3->fields), 'billing_address_id',
371 'Constraint is on "billing_address_id"' );
372 is( $c3->reference_table, 'address', 'To table "address"' );
373 is( join(',', $c3->reference_fields), 'address_id',
374 'To field "address_id"' );
375
376 my $c4 = shift @constraints;
377 is( $c4->type, FOREIGN_KEY, 'Constraint is a FK' );
378 is( join(',', $c4->fields), 'shipping_address_id',
379 'Constraint is on "shipping_address_id"' );
380 is( $c4->reference_table, 'address', 'To table "address"' );
381 is( join(',', $c4->reference_fields), 'address_id',
382 'To field "address_id"' );
383
384 my $c5 = shift @constraints;
385 is( $c5->type, FOREIGN_KEY, 'Constraint is a FK' );
386 is( join(',', $c5->fields), 'store_id', 'Constraint is on "store_id"' );
387 is( $c5->reference_table, 'store', 'To table "store"' );
c9ca0061 388 is( join(',', map { $_ || '' } $c5->reference_fields), '',
389 'No reference fields defined' );
2ab84c11 390
391 my $t2 = shift @tables;
392 is( $t2->name, 'address', 'Found "address" table' );
393
394 my @t2_fields = $t2->get_fields;
395 is( scalar @t2_fields, 8, 'Right number of fields (8)' );
251b6ff5 396}