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