X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F02mysql-parser.t;h=36cef3c4e5a6941655b14633108e04f4eb2a83e2;hb=91c627095428c510d1a273c467dbad0ab9d6024b;hp=cabbe5854fd361263aea5e7477ad2e81a7bc643b;hpb=92a61e092bb9ca7a7f00297cff26194b8fc76bc7;p=dbsrgits%2FSQL-Translator-2.0-ish.git diff --git a/t/02mysql-parser.t b/t/02mysql-parser.t index cabbe58..36cef3c 100644 --- a/t/02mysql-parser.t +++ b/t/02mysql-parser.t @@ -1,17 +1,8 @@ -#!/usr/bin/perl use strict; - +use warnings; use Test::More; use SQL::Translator; -#use SQL::Translator::Schema::Constants; use SQL::Translator::Constants qw(:sqlt_types :sqlt_constants); -#use SQL::Translator::Utils qw//; -use Test::SQL::Translator qw(maybe_plan); - -BEGIN { - maybe_plan(317, "SQL::Translator::Parser::DDL::MySQL"); -# SQL::Translator::Parser::DDL::MySQL->import('parse'); -} { my $tr = SQL::Translator->new({ from => 'MySQL' }); @@ -23,9 +14,8 @@ BEGIN { fulltext key `session_fulltext` (a_session) );|; - #my $val = $tr->parse(); -# my $val = $tr->parse($data); - my $schema = $tr->parse($data); + my $val = $tr->parse($data); + my $schema = $tr->schema; is( $schema->is_valid, 1, 'Schema is valid' ); my @tables = $schema->get_tables; is( scalar @tables, 1, 'Right number of tables (1)' ); @@ -46,7 +36,7 @@ BEGIN { is( $f2->name, 'a_session', 'Second field name is "a_session"' ); is( $f2->data_type, 'text', 'Type is "text"' ); - is( $f2->size, 65_535, 'Size is "65,535"' ); + is( $f2->size, 0, 'Size is "0"' ); 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' ); @@ -69,7 +59,7 @@ BEGIN { { my $tr = SQL::Translator->new({ from => 'MySQL' }); - my $schema = $tr->parse( + my $val = $tr->parse( q[ CREATE TABLE `check` ( check_id int(7) unsigned zerofill NOT NULL default '0000000' @@ -86,12 +76,12 @@ BEGIN { KEY (i1), UNIQUE (date, i1), KEY date_idx (date), - KEY name_idx (name(10)) + KEY name_idx (name) ) TYPE=MyISAM PACK_KEYS=1; ] - ); + ); ## KEY name_idx (name(10)) -# my $schema = $tr->schema; + my $schema = $tr->schema; is( $schema->is_valid, 1, 'Schema is valid' ); my @tables = $schema->get_tables; is( scalar @tables, 1, 'Right number of tables (1)' ); @@ -139,7 +129,7 @@ BEGIN { my $f5 = shift @fields; is( $f5->name, 's1', 'Fifth field name is "s1"' ); is( $f5->data_type, 'set', 'Type is "set"' ); - is( $f5->size, 1, 'Size is "1"' ); + is( $f5->size, 0, 'Size is "0"' ); is( $f5->is_nullable, 1, 'Field can be null' ); is( $f5->default_value, 'b', 'Default value is "b"' ); is( $f5->is_primary_key, 0, 'Field is not PK' ); @@ -149,7 +139,7 @@ BEGIN { my $f6 = shift @fields; is( $f6->name, 'e1', 'Sixth field name is "e1"' ); is( $f6->data_type, 'enum', 'Type is "enum"' ); - is( $f6->size, 1, 'Size is "1"' ); + is( $f6->size, 0, 'Size is "0"' ); is( $f6->is_nullable, 1, 'Field can be null' ); is( $f6->default_value, 'c', 'Default value is "c"' ); is( $f6->is_primary_key, 0, 'Field is not PK' ); @@ -167,7 +157,7 @@ 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, 0, 'Size is "0"' ); 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' ); @@ -206,7 +196,7 @@ BEGIN { my $i3 = shift @indices; is( $i3->name, 'name_idx', 'Name is "name_idx"' ); is( $i3->type, NORMAL, 'Normal index' ); - is( join(',', $i3->fields ), 'name(10)', 'Index is on field "name(10)"' ); + is( join(',', $i3->fields ), 'name', 'Index is on field "name(10)"' ); ## FIX ME my @constraints = $table->get_constraints; is( scalar @constraints, 2, 'Right number of constraints (2)' ); @@ -221,8 +211,8 @@ BEGIN { } { - my $tr = SQL::Translator->new; - my $data = parse($tr, + my $tr = SQL::Translator->new({ from => 'MySQL' }); + my $val = $tr->parse( q[ CREATE TABLE orders ( order_id integer NOT NULL auto_increment, @@ -273,8 +263,8 @@ BEGIN { my $f1 = shift @fields; is( $f1->name, 'order_id', 'First field name is "order_id"' ); - is( $f1->data_type, 'int', 'Type is "int"' ); - is( $f1->size, 11, 'Size is "11"' ); + is( $f1->data_type, 'integer', 'Type is "integer"' ); + is( $f1->size, 0, 'Size is "0"' ); is( $f1->is_nullable, 0, 'Field cannot be null' ); is( $f1->default_value, undef, 'Default value is undefined' ); is( $f1->is_primary_key, 1, 'Field is PK' ); @@ -292,23 +282,23 @@ BEGIN { is( $f3->name, 'billing_address_id', 'Third field name is "billing_address_id"' ); is( $f3->data_type, 'int', 'Type is "int"' ); - is( $f3->size, 11, 'Size is "11"' ); + is( $f3->size, 0, 'Size is "0"' ); my $f4 = shift @fields; is( $f4->name, 'shipping_address_id', 'Fourth field name is "shipping_address_id"' ); is( $f4->data_type, 'int', 'Type is "int"' ); - is( $f4->size, 11, 'Size is "11"' ); + is( $f4->size, 0, 'Size is "0"' ); my $f5 = shift @fields; is( $f5->name, 'credit_card_id', 'Fifth field name is "credit_card_id"' ); is( $f5->data_type, 'int', 'Type is "int"' ); - is( $f5->size, 11, 'Size is "11"' ); + is( $f5->size, 0, 'Size is "0"' ); my $f6 = shift @fields; is( $f6->name, 'status', 'Sixth field name is "status"' ); is( $f6->data_type, 'smallint', 'Type is "smallint"' ); - is( $f6->size, 6, 'Size is "6"' ); + is( $f6->size, 0, 'Size is "0"' ); is( $f6->is_nullable, 0, 'Field cannot be null' ); my $f7 = shift @fields; @@ -318,7 +308,7 @@ BEGIN { is( $f7->is_nullable, 0, 'Field cannot be null' ); is( $f7->is_foreign_key, 1, 'Field is a FK' ); my $fk_ref = $f7->foreign_key_reference; - isa_ok( $fk_ref, 'SQL::Translator::Schema::Constraint', 'FK' ); + isa_ok( $fk_ref, 'SQL::Translator::Object::Constraint', 'FK' ); is( $fk_ref->reference_table, 'store', 'FK is to "store" table' ); my $f8 = shift @fields; @@ -408,8 +398,8 @@ BEGIN { # Ignoring INSERT statements # { - my $tr = SQL::Translator->new; - my $data = parse($tr, + my $tr = SQL::Translator->new({ from => 'MySQL' }); + my $data = $tr->parse( q[ USE database_name; @@ -481,9 +471,9 @@ BEGIN { # charset table option # { - my $tr = SQL::Translator->new(parser_args => {mysql_parser_version => 50003}); - my $data = parse($tr, - q[ + my $tr = SQL::Translator->new({ from => 'MySQL', parser_args => { mysql_parser_version => 50003 } }); + my $data = $tr->parse( + q[ DELIMITER ;; /*!40101 SET SQL_MODE=@OLD_SQL_MODE */;; /*!50003 CREATE */ /*!50017 DEFINER=`cmdomain`@`localhost` */ @@ -640,8 +630,8 @@ BEGIN { # Tests for collate table option { - my $tr = SQL::Translator->new(parser_args => {mysql_parser_version => 50003}); - my $data = parse($tr, + my $tr = SQL::Translator->new({ from => 'MySQL', parser_args => { mysql_parser_version => 50003 } }); + my $data = $tr->parse( q[ CREATE TABLE test ( id int ) DEFAULT CHARACTER SET latin1 COLLATE latin1_bin; ] ); @@ -683,10 +673,11 @@ my $parse_as = { }, }; +my $tr = SQL::Translator->new; for my $target (keys %$parse_as) { for my $str (keys %{$parse_as->{$target}}) { cmp_ok ( - SQL::Translator::Utils::parse_mysql_version ($str, $target), + $tr->engine_version($str, $target), '==', $parse_as->{$target}{$str}, "'$str' parsed as $target version '$parse_as->{$target}{$str}'", @@ -694,11 +685,11 @@ for my $target (keys %$parse_as) { } } -eval { SQL::Translator::Utils::parse_mysql_version ('bogus5.1') }; +eval { $tr->engine_version ('bogus5.1') }; ok ($@, 'Exception thrown on invalid version string'); { - my $tr = SQL::Translator->new; + my $tr = SQL::Translator->new({ from => 'MySQL' }); my $data = q|create table merge_example ( id int(11) NOT NULL auto_increment, shape_field geometry NOT NULL, @@ -706,7 +697,7 @@ ok ($@, 'Exception thrown on invalid version string'); SPATIAL KEY shape_field (shape_field) ) ENGINE=MRG_MyISAM UNION=(`sometable_0`,`sometable_1`,`sometable_2`);|; - my $val = parse($tr, $data); + my $val = $tr->parse($data); my $schema = $tr->schema; is( $schema->is_valid, 1, 'Schema is valid' ); my @tables = $schema->get_tables; @@ -775,9 +766,9 @@ ok ($@, 'Exception thrown on invalid version string'); ) ENGINE=innodb;|, ); for my $data (@data) { - my $tr = SQL::Translator->new; + my $tr = SQL::Translator->new({ from => 'MySQL' }); - my $val = parse($tr, $data); + my $val = $tr->parse($data); my $schema = $tr->schema; is( $schema->is_valid, 1, 'Schema is valid' ); my @tables = $schema->get_tables; @@ -807,14 +798,14 @@ ok ($@, 'Exception thrown on invalid version string'); } { - my $tr = SQL::Translator->new; + my $tr = SQL::Translator->new({ from => 'MySQL' }); my $data = q|create table "sessions" ( 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', );|; - my $val = parse($tr, $data); + my $val = $tr->parse($data); my $schema = $tr->schema; is( $schema->is_valid, 1, 'Schema is valid' ); my @tables = $schema->get_tables; @@ -850,3 +841,5 @@ 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' ); } + +done_testing;