Added a few more tests.
[dbsrgits/SQL-Translator.git] / t / 02mysql-parser.t
CommitLineData
0494e672 1#!/usr/bin/perl
2# vim: set ft=perl:
3#
4# NOTE!!!!
5# For now, all this is testing is that Parse::RecDescent does not
6# die with an error! I am not verifying the validity of the data
7# returned here, just that the parser actually completed its parsing!
8#
9
b8661f30 10use strict;
44fb27ae 11
3a0b6f0a 12use Test::More tests => 19;
b8661f30 13use SQL::Translator;
0494e672 14use SQL::Translator::Parser::MySQL qw(parse);
15
251b6ff5 16{
17 my $tr = SQL::Translator->new;
18 my $data = q|create table sessions (
19 id char(32) not null primary key,
20 a_session text
21 );|;
22
23 my $val = parse($tr, $data);
24
25 # $val holds the processed data structure.
26
27 # The data structure should have one key:
3a0b6f0a 28 is( scalar keys %{$val}, 1, 'Right number of tables' );
251b6ff5 29
30 # The data structure should have a single key, named sessions
3a0b6f0a 31 ok( defined $val->{'sessions'}, 'Found "sessions" table' );
251b6ff5 32
33 # $val->{'sessions'} should have a single index (since we haven't
34 # defined an index, but have defined a primary key)
35 my $indices = $val->{'sessions'}->{'indices'};
3a0b6f0a 36 is( scalar @{$indices || []}, 1, 'Correct index number' );
251b6ff5 37
3a0b6f0a 38 is( $indices->[0]->{'type'}, 'primary_key', 'Correct index type' );
39 is( $indices->[0]->{'fields'}->[0], 'id', 'Correct index name' );
251b6ff5 40
41 # $val->{'sessions'} should have two fields, id and a_sessionn
42 my $fields = $val->{'sessions'}->{'fields'};
3a0b6f0a 43 is( scalar keys %{$fields}, 2, 'Correct fields number' );
251b6ff5 44
3a0b6f0a 45 is( $fields->{'id'}->{'data_type'}, 'char',
46 'Correct field type: id (char)' );
251b6ff5 47
3a0b6f0a 48 is ( $fields->{'a_session'}->{'data_type'}, 'text',
49 'Correct field type: a_session (text)' );
251b6ff5 50
3a0b6f0a 51 is( $fields->{'id'}->{'is_primary_key'}, 1,
52 'Correct key identification (id == key)' );
251b6ff5 53
3a0b6f0a 54 ok( ! defined $fields->{'a_session'}->{'is_primary_key'},
55 'Correct key identification (a_session != key)' );
251b6ff5 56
57 # Test that the order is being maintained by the internal order
58 # data element
59 my @order = sort { $fields->{$a}->{'order'}
60 <=>
61 $fields->{$b}->{'order'}
62 } keys %{$fields};
63
3a0b6f0a 64 ok( $order[0] eq 'id' && $order[1] eq 'a_session', 'Ordering of fields' );
251b6ff5 65}
66
67{
68 my $tr = SQL::Translator->new;
69 my $data = parse($tr,
70 q[
71 CREATE TABLE check (
72 id int(7) unsigned zerofill NOT NULL default '0000000'
73 auto_increment primary key,
74 successful date NOT NULL default '0000-00-00',
75 unsuccessful date default '0000-00-00',
76 i1 int(11) default '0' not null,
77 s1 set('a','b','c') default 'b',
78 e1 enum('a','b','c') default 'c',
79 name varchar(30) default NULL,
80 foo_type enum('vk','ck') NOT NULL default 'vk',
81 date timestamp,
82 time_stamp2 timestamp,
83 KEY (i1),
84 UNIQUE (date, i1),
85 KEY date_idx (date),
86 KEY name_idx (name(10))
87 ) TYPE=MyISAM PACK_KEYS=1;
88 ]
89 );
90
3a0b6f0a 91 is( scalar keys %$data, 1, 'Right number of tables' );
92 ok( defined $data->{'check'}, 'Found "check" table' );
93
94 my $fields = $data->{'check'}{'fields'};
95 is( scalar keys %$fields, 10, 'Correct number of fields' );
96
97 is( $fields->{'i1'}{'data_type'}, 'int', 'i1 an int' );
98 is( join(',', @{$fields->{'i1'}{'size'}}), '11', 'i1 of size "11"' );
99
100 my @order = sort {
101 $fields->{$a}->{'order'} <=> $fields->{$b}->{'order'}
102 } keys %$fields;
103
104 is( $order[3], 'i1', 'Found the "i1" field' );
251b6ff5 105}
106
107{
108 my $tr = SQL::Translator->new;
109 my $data = parse($tr,
110 q[
111 CREATE TABLE orders (
3a0b6f0a 112 order_id integer NOT NULL auto_increment,
251b6ff5 113 member_id varchar(255),
114 billing_address_id int,
115 shipping_address_id int,
116 credit_card_id int,
117 status smallint NOT NULL,
118 store_id varchar(255) NOT NULL REFERENCES store,
119 tax decimal(8,2),
120 shipping_charge decimal(8,2),
121 price_paid decimal(8,2),
122 PRIMARY KEY (order_id),
123 KEY (status),
124 KEY (billing_address_id),
125 KEY (shipping_address_id),
126 KEY (member_id, store_id),
127 FOREIGN KEY (status) REFERENCES order_status(id),
128 FOREIGN KEY (billing_address_id) REFERENCES address(address_id),
129 FOREIGN KEY (shipping_address_id) REFERENCES address(address_id)
130 ) TYPE=INNODB;
131 ]
132 ) or die $tr->error;
133
3a0b6f0a 134 is( scalar keys %{$data}, 1, 'Parsed correct number of tables' );
135 ok( defined $data->{'orders'}, 'Found "orders" table' );
251b6ff5 136}