*** empty log message ***
[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;
11use SQL::Translator;
0494e672 12use SQL::Translator::Parser::MySQL qw(parse);
13
b8661f30 14$SQL::Translator::DEBUG = 0;
0494e672 15
b8661f30 16my $tr = SQL::Translator->new;
17my $data = q|create table sessions (
18 id char(32) not null primary key,
19 a_session text
20);|;
0494e672 21
b8661f30 22BEGIN { print "1..11\n"; }
0494e672 23
b8661f30 24my $val = parse($tr, $data);
0494e672 25
b8661f30 26# $val holds the processed data structure.
27
28# The data structure should have one key:
29print "not " if (scalar keys %{$val} != 1);
0494e672 30print "ok 1\n";
b8661f30 31
32# The data structure should have a single key, named sessions
33print "not " unless (defined $val->{'sessions'});
34print 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)
38my $indeces = $val->{'sessions'}->{'indeces'};
39print "not " unless (scalar @{$indeces} == 1);
40print "ok 3 # correct index number\n";
41
42print "not " unless ($indeces->[0]->{'type'} eq 'primary_key');
43print "ok 4 # correct index type\n";
44print "not " unless ($indeces->[0]->{'fields'}->[0] eq 'id');
45print "ok 5 # correct index name\n";
46
47# $val->{'sessions'} should have two fields, id and a_sessionn
48my $fields = $val->{'sessions'}->{'fields'};
49print "not " unless (scalar keys %{$fields} == 2);
50print "ok 6 # correct fields number\n";
51
52print "not " unless ($fields->{'id'}->{'data_type'} eq 'char');
53print "ok 7 # correct field type: id (char)\n";
54
55print "not " unless ($fields->{'a_session'}->{'data_type'} eq 'text');
56print "ok 8 # correct field type: a_session (text)\n";
57
58print "not " unless ($fields->{'id'}->{'is_primary_key'} == 1);
59print "ok 9 # correct key identification (id == key)\n";
60
61print "not " if (defined $fields->{'a_session'}->{'is_primary_key'});
62print "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
66my @order = sort { $fields->{$a}->{'order'}
67 <=>
68 $fields->{$b}->{'order'}
69 } keys %{$fields};
70print "not " unless ($order[0] eq 'id' && $order[1] eq 'a_session');
71print "ok 11 # ordering of fields\n";