Changed grammar to bring it more inline with the official MySQL YACC
[dbsrgits/SQL-Translator.git] / t / 02mysql-parser.t
index 19a672c..cee9620 100644 (file)
@@ -7,21 +7,57 @@
 # returned here, just that the parser actually completed its parsing!
 #
 
-use Symbol;
-use Data::Dumper;
+use strict;
+
+use Test::More tests => 11;
+use SQL::Translator;
 use SQL::Translator::Parser::MySQL qw(parse);
 
-my $datafile = "t/data/mysql/Apache-Session-MySQL.sql";
-my $data;
-my $fh = gensym;
+my $tr = SQL::Translator->new;
+my $data = q|create table sessions (
+    id char(32) not null primary key,
+    a_session text
+);|;
+
+my $val = parse($tr, $data);
+
+# $val holds the processed data structure.
+
+# The data structure should have one key:
+is(scalar keys %{$val}, 1);
+
+# The data structure should have a single key, named sessions
+ok(defined $val->{'sessions'});
+
+# $val->{'sessions'} should have a single index (since we haven't
+# defined an index, but have defined a primary key)
+my $indices = $val->{'sessions'}->{'indices'};
+is(scalar @{$indices || []}, 1, "correct index number");
+
+is($indices->[0]->{'type'}, 'primary_key', "correct index type");
+is($indices->[0]->{'fields'}->[0], 'id', "correct index name");
+
+# $val->{'sessions'} should have two fields, id and a_sessionn
+my $fields = $val->{'sessions'}->{'fields'};
+is(scalar keys %{$fields}, 2, "correct fields number");
+
+is($fields->{'id'}->{'data_type'}, 'char',
+    "correct field type: id (char)");
+
+is ($fields->{'a_session'}->{'data_type'}, 'text',
+    "correct field type: a_session (text)");
 
-open $fh, $datafile or die "Can't open '$datafile' for reading: $!";
-read($fh, $data, -s $datafile);
-close $fh or die "Can't close '$datafile': $!";
+is($fields->{'id'}->{'is_primary_key'}, 1, 
+    "correct key identification (id == key)");
 
-BEGIN { print "1..1\n"; }
+ok(! defined $fields->{'a_session'}->{'is_primary_key'}, 
+    "correct key identification (a_session != key)");
 
-eval { parse($data); };
+# Test that the order is being maintained by the internal order
+# data element
+my @order = sort { $fields->{$a}->{'order'}
+                             <=>
+                   $fields->{$b}->{'order'}
+                 } keys %{$fields};
 
-print "not " if $@;
-print "ok 1\n";
+ok($order[0] eq 'id' && $order[1] eq 'a_session', "ordering of fields");