Broke the 1 test out into 11 different tests, each one of which tests a specific...
[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 use SQL::Translator;
12 use SQL::Translator::Parser::MySQL qw(parse);
13
14 $SQL::Translator::DEBUG = 0;
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 BEGIN { print "1..11\n"; }
23
24 my $val = parse($tr, $data);
25
26 # $val holds the processed data structure.
27
28 # The data structure should have one key:
29 print "not " if (scalar keys %{$val} != 1);
30 print "ok 1\n";
31
32 # The data structure should have a single key, named sessions
33 print "not " unless (defined $val->{'sessions'});
34 print qq(ok 2 # has a key named "sessions"\n);
35
36 # $val->{'sessions'} should have a single index (since we haven't
37 # defined an index, but have defined a primary key)
38 my $indeces = $val->{'sessions'}->{'indeces'};
39 print "not " unless (scalar @{$indeces} == 1);
40 print "ok 3 # correct index number\n";
41
42 print "not " unless ($indeces->[0]->{'type'} eq 'primary_key');
43 print "ok 4 # correct index type\n";
44 print "not " unless ($indeces->[0]->{'fields'}->[0] eq 'id');
45 print "ok 5 # correct index name\n";
46
47 # $val->{'sessions'} should have two fields, id and a_sessionn
48 my $fields = $val->{'sessions'}->{'fields'};
49 print "not " unless (scalar keys %{$fields} == 2);
50 print "ok 6 # correct fields number\n";
51
52 print "not " unless ($fields->{'id'}->{'data_type'} eq 'char');
53 print "ok 7 # correct field type: id (char)\n";
54
55 print "not " unless ($fields->{'a_session'}->{'data_type'} eq 'text');
56 print "ok 8 # correct field type: a_session (text)\n";
57
58 print "not " unless ($fields->{'id'}->{'is_primary_key'} == 1);
59 print "ok 9 # correct key identification (id == key)\n";
60
61 print "not " if (defined $fields->{'a_session'}->{'is_primary_key'});
62 print "ok 10 # correct key identification (a_session != key)\n";
63
64 # Test that the order is being maintained by the internal order
65 # data element
66 my @order = sort { $fields->{$a}->{'order'}
67                              <=>
68                    $fields->{$b}->{'order'}
69                  } keys %{$fields};
70 print "not " unless ($order[0] eq 'id' && $order[1] eq 'a_session');
71 print "ok 11 # ordering of fields\n";