Escape quotes in string values in producers
[dbsrgits/SQL-Translator.git] / t / 02mysql-parser.t
index 2b4bfa1..1bc8888 100644 (file)
@@ -12,7 +12,7 @@ use Test::SQL::Translator qw(maybe_plan);
 use FindBin qw/$Bin/;
 
 BEGIN {
-    maybe_plan(337, "SQL::Translator::Parser::MySQL");
+    maybe_plan(346, "SQL::Translator::Parser::MySQL");
     SQL::Translator::Parser::MySQL->import('parse');
 }
 
@@ -78,13 +78,14 @@ 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',
+              foo_type enum('vk','c''k') NOT NULL default 'vk',
               date timestamp,
               time_stamp2 timestamp,
               foo_enabled bit(1) default b'0',
               bar_enabled bit(1) default b"1",
+              long_foo_enabled bit(10) default b'1010101',
               KEY (i1),
               UNIQUE (date, i1) USING BTREE,
               KEY date_idx (date),
@@ -101,7 +102,7 @@ BEGIN {
     is( $table->name, 'check', 'Found "check" table' );
 
     my @fields = $table->get_fields;
-    is( scalar @fields, 12, 'Right number of fields (12)' );
+    is( scalar @fields, 13, 'Right number of fields (13)' );
     my $f1 = shift @fields;
     is( $f1->name, 'check_id', 'First field name is "check_id"' );
     is( $f1->data_type, 'int', 'Type is "int"' );
@@ -169,12 +170,12 @@ BEGIN {
     my $f8 = shift @fields;
     is( $f8->name, 'foo_type', 'Eighth field name is "foo_type"' );
     is( $f8->data_type, 'enum', 'Type is "enum"' );
-    is( $f8->size, 2, 'Size is "2"' );
+    is( $f8->size, 3, 'Size is "2"' );
     is( $f8->is_nullable, 0, 'Field cannot be null' );
     is( $f8->default_value, 'vk', 'Default value is "vk"' );
     is( $f8->is_primary_key, 0, 'Field is not PK' );
     my %f8extra = $f8->extra;
-    is( join(',', @{ $f8extra{'list'} || [] }), 'vk,ck', 'List is "vk,ck"' );
+    is( join(',', @{ $f8extra{'list'} || [] }), 'vk,c\'k', 'List is "vk,c\'k"' );
 
     my $f9 = shift @fields;
     is( $f9->name, 'date', 'Ninth field name is "date"' );
@@ -208,6 +209,14 @@ BEGIN {
     is( $f12->default_value, '1', 'Default value is 1' );
     is( $f12->is_primary_key, 0, 'Field is not PK' );
 
+    my $f13 = shift @fields;
+    is( $f13->name, 'long_foo_enabled', 'Thirteenth field name is "long_foo_enabled"' );
+    is( $f13->data_type, 'bit', 'Type is "bit"' );
+    is( $f13->size, 10, 'Size is "10"' );
+    is( $f13->is_nullable, 1, 'Field can be null' );
+    is( $f13->default_value, '1010101', 'Default value is 1010101' );
+    is( $f13->is_primary_key, 0, 'Field is not PK' );
+
     my @indices = $table->get_indices;
     is( scalar @indices, 3, 'Right number of indices (3)' );
 
@@ -244,7 +253,7 @@ BEGIN {
         q[
             CREATE TABLE orders (
               order_id                  integer NOT NULL auto_increment,
-              member_id                 varchar(255) comment 'fk to member',
+              member_id                 varchar(255) comment 'fk to ''member''',
               billing_address_id        int,
               shipping_address_id       int,
               credit_card_id            int,
@@ -303,7 +312,7 @@ BEGIN {
     is( $f2->data_type, 'varchar', 'Type is "varchar"' );
     is( $f2->size, 255, 'Size is "255"' );
     is( $f2->is_nullable, 1, 'Field can be null' );
-    is( $f2->comments, 'fk to member', 'Field comment OK' );
+    is( $f2->comments, 'fk to \'member\'', 'Field comment OK' );
     is( $f2->default_value, undef, 'Default value is undefined' );
 
     my $f3 = shift @fields;
@@ -499,7 +508,7 @@ BEGIN {
 #    charset table option
 #
 {
-    my $tr = SQL::Translator->new(parser_args => {mysql_parser_version => 50003});
+    my $tr = SQL::Translator->new(parser_args => {mysql_parser_version => 50013});
     my $data = parse($tr,
         q[
             DELIMITER ;;
@@ -512,11 +521,13 @@ BEGIN {
             DELIMITER ;
             CREATE TABLE one (
               `op` varchar(255) character set latin1 collate latin1_bin default NULL,
-              `last_modified` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
+              `last_modified` timestamp NOT NULL default Current_Timestamp on update CURRENT_TIMESTAMP,
+              `created_at` datetime NOT NULL Default CURRENT_TIMESTAMP(),
             ) TYPE=INNODB DEFAULT CHARSET=latin1;
 
             /*!50001 CREATE ALGORITHM=UNDEFINED */
             /*!50013 DEFINER=`cmdomain`@`localhost` SQL SECURITY DEFINER */
+            /*!50014 DEFINER=`BOGUS` */
             /*! VIEW `vs_asset` AS
                 select `a`.`asset_id` AS `asset_id`,`a`.`fq_name` AS `fq_name`,
                 `cfgmgmt_mig`.`ap_extract_folder`(`a`.`fq_name`) AS `folder_name`,
@@ -603,7 +614,7 @@ BEGIN {
     is( $table1->name, 'one', 'Found "one" table' );
 
     my @fields = $table1->get_fields;
-    is(scalar @fields, 2, 'Right number of fields (2) on table one');
+    is(scalar @fields, 3, 'Right number of fields (3) on table one');
     my $tableTypeFound = 0;
     my $charsetFound = 0;
     for my $t1_option_ref ( $table1->options ) {
@@ -634,7 +645,16 @@ BEGIN {
       \'CURRENT_TIMESTAMP',
       'Field has right default value'
     );
-    is( $t1f2->extra('on update'), 'CURRENT_TIMESTAMP', 'Field has right on update qualifier' );
+    is_deeply( $t1f2->extra('on update'), \'CURRENT_TIMESTAMP', 'Field has right on update qualifier' );
+
+    my $t1f3 = shift @fields;
+    is( $t1f3->data_type, 'datetime', 'Field is a datetime' );
+    ok( !$t1f3->is_nullable, 'Field is not nullable' );
+    is_deeply(
+      $t1f3->default_value,
+      \'CURRENT_TIMESTAMP',
+      'Field has right default value'
+    );
 
     my @views = $schema->get_views;
     is( scalar @views, 3, 'Right number of views (3)' );
@@ -643,9 +663,31 @@ BEGIN {
     is( $view1->name, 'vs_asset', 'Found "vs_asset" view' );
     is( $view2->name, 'vs_asset2', 'Found "vs_asset2" view' );
     is( $view3->name, 'vs_asset3', 'Found "vs_asset3" view' );
-    like($view1->sql, qr/ALGORITHM=UNDEFINED/, "Detected algorithm");
     like($view1->sql, qr/vs_asset/, "Detected view vs_asset");
-    unlike($view1->sql, qr/cfgmgmt_mig/, "Did not detect cfgmgmt_mig");
+
+    # KYC - commenting this out as I don't understand why this string
+    # should /not/ be detected when it is in the SQL - 2/28/12
+    # like($view1->sql, qr/cfgmgmt_mig/, "Did not detect cfgmgmt_mig");
+
+    is( join(',', $view1->fields),
+        join(',', qw[ asset_id fq_name folder_name asset_name annotation
+            asset_type foreign_asset_id foreign_asset_id2 date_created
+            date_modified container_id creator_id modifier_id user_access
+        ] ),
+        'First view has correct fields'
+    );
+
+    my @options = $view1->options;
+
+    is_deeply(
+      \@options,
+      [
+        'ALGORITHM=UNDEFINED',
+        'DEFINER=`cmdomain`@`localhost`',
+        'SQL SECURITY DEFINER',
+      ],
+      'Only version 50013 options parsed',
+    );
 
     my @procs = $schema->get_procedures;
     is( scalar @procs, 2, 'Right number of procedures (2)' );
@@ -858,7 +900,7 @@ ok ($@, 'Exception thrown on invalid version string');
     is( $f2->data_type, 'varchar', 'Type is "varchar"' );
     is( $f2->size, 12, 'Size is "12"' );
     is( $f2->is_nullable, 0, 'Field can not be null' );
-    is( $f2->default_value, "test single quotes like in you''re", "Single quote in default value is escaped properly" );
+    is( $f2->default_value, "test single quotes like in you're", "Single quote in default value is unescaped properly" );
     is( $f2->is_primary_key, 0, 'Field is not PK' );
 
     # this is more of a sanity test because the original sqlt regex for default looked for an escaped quote represented as \'
@@ -867,7 +909,7 @@ ok ($@, 'Exception thrown on invalid version string');
     is( $f3->data_type, 'varchar', 'Type is "varchar"' );
     is( $f3->size, 20, 'Size is "20"' );
     is( $f3->is_nullable, 0, 'Field can not be null' );
-    is( $f3->default_value, "test single quotes escaped like you\\'re", "Single quote in default value is escaped properly" );
+    is( $f3->default_value, "test single quotes escaped like you're", "Single quote in default value is unescaped properly" );
     is( $f3->is_primary_key, 0, 'Field is not PK' );
 }