Use precompiled Parse::RecDescent parsers for moar speed
[dbsrgits/SQL-Translator.git] / t / 02mysql-parser.t
index 321f2ff..4dd921c 100644 (file)
@@ -9,9 +9,10 @@ use SQL::Translator;
 use SQL::Translator::Schema::Constants;
 use SQL::Translator::Utils qw//;
 use Test::SQL::Translator qw(maybe_plan);
+use FindBin qw/$Bin/;
 
 BEGIN {
-    maybe_plan(317, "SQL::Translator::Parser::MySQL");
+    maybe_plan(337, "SQL::Translator::Parser::MySQL");
     SQL::Translator::Parser::MySQL->import('parse');
 }
 
@@ -77,11 +78,13 @@ BEGIN {
               unsuccessful date default '0000-00-00',
               i1 int(11) default '0' not null,
               s1 set('a','b','c') default 'b',
-              e1 enum('a','b','c') default 'c',
+              e1 enum('a','b','c') default "c",
               name varchar(30) default NULL,
               foo_type enum('vk','ck') NOT NULL default 'vk',
               date timestamp,
               time_stamp2 timestamp,
+              foo_enabled bit(1) default b'0',
+              bar_enabled bit(1) default b"1",
               KEY (i1),
               UNIQUE (date, i1) USING BTREE,
               KEY date_idx (date),
@@ -98,7 +101,7 @@ BEGIN {
     is( $table->name, 'check', 'Found "check" table' );
 
     my @fields = $table->get_fields;
-    is( scalar @fields, 10, 'Right number of fields (10)' );
+    is( scalar @fields, 12, 'Right number of fields (12)' );
     my $f1 = shift @fields;
     is( $f1->name, 'check_id', 'First field name is "check_id"' );
     is( $f1->data_type, 'int', 'Type is "int"' );
@@ -189,6 +192,22 @@ BEGIN {
     is( $f10->default_value, undef, 'Default value is undefined' );
     is( $f10->is_primary_key, 0, 'Field is not PK' );
 
+    my $f11 = shift @fields;
+    is( $f11->name, 'foo_enabled', 'Eleventh field name is "foo_enabled"' );
+    is( $f11->data_type, 'bit', 'Type is "bit"' );
+    is( $f11->size, 1, 'Size is "1"' );
+    is( $f11->is_nullable, 1, 'Field can be null' );
+    is( $f11->default_value, '0', 'Default value is 0' );
+    is( $f11->is_primary_key, 0, 'Field is not PK' );
+
+    my $f12 = shift @fields;
+    is( $f12->name, 'bar_enabled', 'Twelveth field name is "bar_enabled"' );
+    is( $f12->data_type, 'bit', 'Type is "bit"' );
+    is( $f12->size, 1, 'Size is "1"' );
+    is( $f12->is_nullable, 1, 'Field can be null' );
+    is( $f12->default_value, '1', 'Default value is 1' );
+    is( $f12->is_primary_key, 0, 'Field is not PK' );
+
     my @indices = $table->get_indices;
     is( scalar @indices, 3, 'Right number of indices (3)' );
 
@@ -812,7 +831,7 @@ ok ($@, 'Exception thrown on invalid version string');
         id char(32) not null default '0' primary key,
         ssn varchar(12) NOT NULL default 'test single quotes like in you''re',
         user varchar(20) NOT NULL default 'test single quotes escaped like you\'re',
-        key using btree (ssn) 
+        key using btree (ssn)
     );|;
 
     my $val = parse($tr, $data);
@@ -851,3 +870,48 @@ ok ($@, 'Exception thrown on invalid version string');
     is( $f3->default_value, "test single quotes escaped like you\\'re", "Single quote in default value is escaped properly" );
     is( $f3->is_primary_key, 0, 'Field is not PK' );
 }
+
+{
+    # silence PR::D from spewing on STDERR
+    local $::RD_ERRORS = 0;
+    local $::RD_WARN = 0;
+    local $::RD_HINT = 0;
+    my $tr = SQL::Translator->new;
+    my $data = q|create table "sessions" (
+        id char(32) not null default,
+        ssn varchar(12) NOT NULL default 'test single quotes like in you''re',
+        user varchar(20) NOT NULL default 'test single quotes escaped like you\'re',
+        key using btree (ssn)
+    );|;
+
+    my $val= parse($tr,$data);
+    ok ($tr->error =~ /Parse failed\./, 'Parse failed error without default value');
+}
+
+{
+    # make sure empty string default value still works
+    my $tr = SQL::Translator->new;
+    my $data = q|create table "sessions" (
+        id char(32) not null DEFAULT '',
+        ssn varchar(12) NOT NULL default "",
+        key using btree (ssn)
+    );|;
+    my $val= parse($tr,$data);
+
+    my @fields = $tr->schema->get_table('sessions')->get_fields;
+    is (scalar @fields, 2, 'Both fields parsed correctly');
+    for (@fields) {
+      my $def = $_->default_value;
+      ok( (defined $def and $def eq ''), "Defaults on field $_ correct" );
+    }
+}
+
+{
+    # test rt70437 and rt71468
+    my $file = "$Bin/data/mysql/cashmusic_db.sql";
+    ok (-f $file,"File exists");
+    my $tr = SQL::Translator->new( parser => 'MySQL');
+    ok ($tr->translate($file),'File translated');
+    ok (!$tr->error, 'no error');
+    ok (my $schema = $tr->schema, 'got schema');
+}