Adding new schema test, commiting fixes to MySQL parser test.
[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 => 11;
13 use SQL::Translator;
14 use SQL::Translator::Parser::MySQL qw(parse);
15
16 my $tr = SQL::Translator->new;
17 my $data = q|create table sessions (
18     id char(32) not null primary key,
19     a_session text
20 );|;
21
22 my $val = parse($tr, $data);
23
24 # $val holds the processed data structure.
25
26 # The data structure should have one key:
27 is(scalar keys %{$val}, 1);
28
29 # The data structure should have a single key, named sessions
30 ok(defined $val->{'sessions'});
31
32 # $val->{'sessions'} should have a single index (since we haven't
33 # defined an index, but have defined a primary key)
34 my $indices = $val->{'sessions'}->{'indices'};
35 is(scalar @{$indices || []}, 1, "correct index number");
36
37 is($indices->[0]->{'type'}, 'primary_key', "correct index type");
38 is($indices->[0]->{'fields'}->[0], 'id', "correct index name");
39
40 # $val->{'sessions'} should have two fields, id and a_sessionn
41 my $fields = $val->{'sessions'}->{'fields'};
42 is(scalar keys %{$fields}, 2, "correct fields number");
43
44 is($fields->{'id'}->{'data_type'}, 'char',
45     "correct field type: id (char)");
46
47 is ($fields->{'a_session'}->{'data_type'}, 'text',
48     "correct field type: a_session (text)");
49
50 is($fields->{'id'}->{'is_primary_key'}, 1, 
51     "correct key identification (id == key)");
52
53 ok(! defined $fields->{'a_session'}->{'is_primary_key'}, 
54     "correct key identification (a_session != key)");
55
56 # Test that the order is being maintained by the internal order
57 # data element
58 my @order = sort { $fields->{$a}->{'order'}
59                              <=>
60                    $fields->{$b}->{'order'}
61                  } keys %{$fields};
62
63 ok($order[0] eq 'id' && $order[1] eq 'a_session', "ordering of fields");