Fixed YAML testi. Failed due to sqlfairy version increase and YAML not putting
[dbsrgits/SQL-Translator.git] / t / 02mysql-parser.t
index 7d3ba41..64c3c1b 100644 (file)
@@ -4,10 +4,15 @@
 
 use strict;
 
-use Test::More tests => 180;
+use Test::More;
 use SQL::Translator;
-use SQL::Translator::Parser::MySQL qw(parse);
 use SQL::Translator::Schema::Constants;
+use Test::SQL::Translator qw(maybe_plan);
+
+BEGIN {
+    maybe_plan(199, "SQL::Translator::Parser::MySQL");
+    SQL::Translator::Parser::MySQL->import('parse');
+}
 
 {
     my $tr = SQL::Translator->new;
@@ -38,7 +43,7 @@ use SQL::Translator::Schema::Constants;
 
     is( $f2->name, 'a_session', 'Second field name is "a_session"' );
     is( $f2->data_type, 'text', 'Type is "text"' );
-    is( $f2->size, 65000, 'Size is "65000"' );
+    is( $f2->size, 65_535, 'Size is "65,535"' );
     is( $f2->is_nullable, 1, 'Field can be null' );
     is( $f2->default_value, undef, 'Default value is undefined' );
     is( $f2->is_primary_key, 0, 'Field is not PK' );
@@ -226,7 +231,7 @@ use SQL::Translator::Schema::Constants;
               KEY (billing_address_id),
               KEY (shipping_address_id),
               KEY (member_id, store_id),
-              FOREIGN KEY (status)              REFERENCES order_status(id),
+              FOREIGN KEY (status)              REFERENCES order_status(id) MATCH FULL ON DELETE CASCADE ON UPDATE CASCADE,
               FOREIGN KEY (billing_address_id)  REFERENCES address(address_id),
               FOREIGN KEY (shipping_address_id) REFERENCES address(address_id)
             ) TYPE=INNODB;
@@ -375,7 +380,8 @@ use SQL::Translator::Schema::Constants;
     is( $c5->type, FOREIGN_KEY, 'Constraint is a FK' );
     is( join(',', $c5->fields), 'store_id', 'Constraint is on "store_id"' );
     is( $c5->reference_table, 'store', 'To table "store"' );
-    is( join(',', $c5->reference_fields), '', 'No reference fields defined' );
+    is( join(',', map { $_ || '' } $c5->reference_fields), '', 
+        'No reference fields defined' );
 
     my $t2  = shift @tables;
     is( $t2->name, 'address', 'Found "address" table' );
@@ -383,3 +389,77 @@ use SQL::Translator::Schema::Constants;
     my @t2_fields = $t2->get_fields;
     is( scalar @t2_fields, 8, 'Right number of fields (8)' );
 }
+
+# djh Tests for:
+#    USE database ;
+#    ALTER TABLE ADD FOREIGN KEY
+#    trailing comma on last create definition
+#    Ignoring INSERT statements
+#
+{
+    my $tr = SQL::Translator->new;
+    my $data = parse($tr, 
+        q[
+            USE database_name;
+
+            CREATE TABLE one (
+              id                     integer NOT NULL auto_increment,
+              two_id                 integer NOT NULL auto_increment,
+              some_data              text,
+              PRIMARY KEY (id),
+              INDEX (two_id),
+            ) TYPE=INNODB;
+
+            CREATE TABLE two (
+              id                     int NOT NULL auto_increment,
+              one_id                 int NOT NULL auto_increment,
+              some_data              text,
+              PRIMARY KEY (id),
+              INDEX (one_id),
+              FOREIGN KEY (one_id) REFERENCES one (id),
+            ) TYPE=INNODB;
+
+            ALTER TABLE one ADD FOREIGN KEY (two_id) REFERENCES two (id);
+
+            INSERT absolutely *#! any old $£ ? rubbish ;
+        ]
+    ) or die $tr->error;
+
+    my $schema = $tr->schema;
+    is( $schema->is_valid, 1, 'Schema is valid' );
+    my $db_name = $schema->name;
+    is( $db_name, 'database_name', 'Database name extracted from USE' );
+    my @tables = $schema->get_tables;
+    is( scalar @tables, 2, 'Right number of tables (2)' );
+    my $table1 = shift @tables;
+    is( $table1->name, 'one', 'Found "one" table' );
+    my $table2 = shift @tables;
+    is( $table2->name, 'two', 'Found "two" table' );
+
+    my @constraints = $table1->get_constraints;
+    is(scalar @constraints, 2, 'Right number of constraints (2) on table one');
+
+    my $t1c1 = shift @constraints;
+    is( $t1c1->type, PRIMARY_KEY, 'Constraint is a PK' );
+    is( join(',', $t1c1->fields), 'id', 'Constraint is on "id"' );
+
+    my $t1c2 = shift @constraints;
+    is( $t1c2->type, FOREIGN_KEY, 'Constraint is a FK' );
+    is( join(',', $t1c2->fields), 'two_id', 'Constraint is on "two_id"' );
+    is( $t1c2->reference_table, 'two', 'To table "two"' );
+    is( join(',', $t1c2->reference_fields), 'id', 'To field "id"' );
+
+    @constraints = $table2->get_constraints;
+    is(scalar @constraints, 2, 'Right number of constraints (2) on table two');
+
+    my $t2c1 = shift @constraints;
+    is( $t2c1->type, PRIMARY_KEY, 'Constraint is a PK' );
+    is( join(',', $t2c1->fields), 'id', 'Constraint is on "id"' );
+
+    my $t2c2 = shift @constraints;
+    is( $t2c2->type, FOREIGN_KEY, 'Constraint is a FK' );
+    is( join(',', $t2c2->fields), 'one_id', 'Constraint is on "one_id"' );
+    is( $t2c2->reference_table, 'one', 'To table "one"' );
+    is( join(',', $t2c2->reference_fields), 'id', 'To field "id"' );
+}
+