Changed grammar to bring it more inline with the official MySQL YACC
[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;
44fb27ae 11
12use Test::More tests => 11;
b8661f30 13use SQL::Translator;
0494e672 14use SQL::Translator::Parser::MySQL qw(parse);
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 22my $val = parse($tr, $data);
0494e672 23
b8661f30 24# $val holds the processed data structure.
25
26# The data structure should have one key:
44fb27ae 27is(scalar keys %{$val}, 1);
b8661f30 28
29# The data structure should have a single key, named sessions
44fb27ae 30ok(defined $val->{'sessions'});
b8661f30 31
32# $val->{'sessions'} should have a single index (since we haven't
33# defined an index, but have defined a primary key)
44fcd0b5 34my $indices = $val->{'sessions'}->{'indices'};
4d878d2f 35is(scalar @{$indices || []}, 1, "correct index number");
b8661f30 36
44fb27ae 37is($indices->[0]->{'type'}, 'primary_key', "correct index type");
38is($indices->[0]->{'fields'}->[0], 'id', "correct index name");
b8661f30 39
40# $val->{'sessions'} should have two fields, id and a_sessionn
41my $fields = $val->{'sessions'}->{'fields'};
44fb27ae 42is(scalar keys %{$fields}, 2, "correct fields number");
b8661f30 43
44fb27ae 44is($fields->{'id'}->{'data_type'}, 'char',
45 "correct field type: id (char)");
b8661f30 46
44fb27ae 47is ($fields->{'a_session'}->{'data_type'}, 'text',
48 "correct field type: a_session (text)");
b8661f30 49
44fb27ae 50is($fields->{'id'}->{'is_primary_key'}, 1,
51 "correct key identification (id == key)");
b8661f30 52
44fb27ae 53ok(! defined $fields->{'a_session'}->{'is_primary_key'},
54 "correct key identification (a_session != key)");
b8661f30 55
56# Test that the order is being maintained by the internal order
57# data element
58my @order = sort { $fields->{$a}->{'order'}
59 <=>
60 $fields->{$b}->{'order'}
61 } keys %{$fields};
44fb27ae 62
63ok($order[0] eq 'id' && $order[1] eq 'a_session', "ordering of fields");