Added more tests.
[dbsrgits/SQL-Translator.git] / t / 02mysql-parser.t
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
10 use strict;
11
12 use Test::More tests => 15;
13 use SQL::Translator;
14 use SQL::Translator::Parser::MySQL qw(parse);
15
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:
28     is(scalar keys %{$val}, 1);
29
30     # The data structure should have a single key, named sessions
31     ok(defined $val->{'sessions'});
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'};
36     is(scalar @{$indices || []}, 1, "correct index number");
37
38     is($indices->[0]->{'type'}, 'primary_key', "correct index type");
39     is($indices->[0]->{'fields'}->[0], 'id', "correct index name");
40
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");
44
45     is($fields->{'id'}->{'data_type'}, 'char',
46         "correct field type: id (char)");
47
48     is ($fields->{'a_session'}->{'data_type'}, 'text',
49         "correct field type: a_session (text)");
50
51     is($fields->{'id'}->{'is_primary_key'}, 1, 
52         "correct key identification (id == key)");
53
54     ok(! defined $fields->{'a_session'}->{'is_primary_key'}, 
55         "correct key identification (a_session != key)");
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
64     ok($order[0] eq 'id' && $order[1] eq 'a_session', "ordering of fields");
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     
91     is(scalar keys %{$data}, 1);
92     ok(defined $data->{'check'});
93 }
94
95 {
96     my $tr = SQL::Translator->new;
97     my $data = parse($tr, 
98         q[
99             CREATE TABLE orders (
100               order_id                  int NOT NULL auto_increment,
101               member_id                 varchar(255),
102               billing_address_id        int,
103               shipping_address_id       int,
104               credit_card_id            int,
105               status                    smallint NOT NULL,
106               store_id                  varchar(255) NOT NULL REFERENCES store,
107               tax                       decimal(8,2),
108               shipping_charge           decimal(8,2),
109               price_paid                decimal(8,2),
110               PRIMARY KEY (order_id),
111               KEY (status),
112               KEY (billing_address_id),
113               KEY (shipping_address_id),
114               KEY (member_id, store_id),
115               FOREIGN KEY (status)              REFERENCES order_status(id),
116               FOREIGN KEY (billing_address_id)  REFERENCES address(address_id),
117               FOREIGN KEY (shipping_address_id) REFERENCES address(address_id)
118             ) TYPE=INNODB;
119         ]
120     ) or die $tr->error;
121
122     is(scalar keys %{$data}, 1);
123     ok(defined $data->{'orders'});
124 }