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!
12 use Test::More tests => 19;
14 use SQL::Translator::Parser::MySQL qw(parse);
17 my $tr = SQL::Translator->new;
18 my $data = q|create table sessions (
19 id char(32) not null primary key,
23 my $val = parse($tr, $data);
25 # $val holds the processed data structure.
27 # The data structure should have one key:
28 is( scalar keys %{$val}, 1, 'Right number of tables' );
30 # The data structure should have a single key, named sessions
31 ok( defined $val->{'sessions'}, 'Found "sessions" table' );
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'};
36 is( scalar @{$indices || []}, 1, 'Correct index number' );
38 is( $indices->[0]->{'type'}, 'primary_key', 'Correct index type' );
39 is( $indices->[0]->{'fields'}->[0], 'id', 'Correct index name' );
41 # $val->{'sessions'} should have two fields, id and a_sessionn
42 my $fields = $val->{'sessions'}->{'fields'};
43 is( scalar keys %{$fields}, 2, 'Correct fields number' );
45 is( $fields->{'id'}->{'data_type'}, 'char',
46 'Correct field type: id (char)' );
48 is ( $fields->{'a_session'}->{'data_type'}, 'text',
49 'Correct field type: a_session (text)' );
51 is( $fields->{'id'}->{'is_primary_key'}, 1,
52 'Correct key identification (id == key)' );
54 ok( ! defined $fields->{'a_session'}->{'is_primary_key'},
55 'Correct key identification (a_session != key)' );
57 # Test that the order is being maintained by the internal order
59 my @order = sort { $fields->{$a}->{'order'}
61 $fields->{$b}->{'order'}
64 ok( $order[0] eq 'id' && $order[1] eq 'a_session', 'Ordering of fields' );
68 my $tr = SQL::Translator->new;
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',
82 time_stamp2 timestamp,
86 KEY name_idx (name(10))
87 ) TYPE=MyISAM PACK_KEYS=1;
91 is( scalar keys %$data, 1, 'Right number of tables' );
92 ok( defined $data->{'check'}, 'Found "check" table' );
94 my $fields = $data->{'check'}{'fields'};
95 is( scalar keys %$fields, 10, 'Correct number of fields' );
97 is( $fields->{'i1'}{'data_type'}, 'int', 'i1 an int' );
98 is( join(',', @{$fields->{'i1'}{'size'}}), '11', 'i1 of size "11"' );
101 $fields->{$a}->{'order'} <=> $fields->{$b}->{'order'}
104 is( $order[3], 'i1', 'Found the "i1" field' );
108 my $tr = SQL::Translator->new;
109 my $data = parse($tr,
111 CREATE TABLE orders (
112 order_id integer NOT NULL auto_increment,
113 member_id varchar(255),
114 billing_address_id int,
115 shipping_address_id int,
117 status smallint NOT NULL,
118 store_id varchar(255) NOT NULL REFERENCES store,
120 shipping_charge decimal(8,2),
121 price_paid decimal(8,2),
122 PRIMARY KEY (order_id),
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)
134 is( scalar keys %{$data}, 1, 'Parsed correct number of tables' );
135 ok( defined $data->{'orders'}, 'Found "orders" table' );