X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F02mysql-parser.t;h=36cef3c4e5a6941655b14633108e04f4eb2a83e2;hb=82e209aa083cf5f796661c71a862ebeb2da67e64;hp=adfac00d69f2d1fe876acc059bcd747379f1f0d5;hpb=2e718597f962f5653de6759e4a68de497f891746;p=dbsrgits%2FSQL-Translator-2.0-ish.git diff --git a/t/02mysql-parser.t b/t/02mysql-parser.t index adfac00..36cef3c 100644 --- a/t/02mysql-parser.t +++ b/t/02mysql-parser.t @@ -14,9 +14,8 @@ use SQL::Translator::Constants qw(:sqlt_types :sqlt_constants); 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)' ); @@ -37,7 +36,7 @@ use SQL::Translator::Constants qw(:sqlt_types :sqlt_constants); 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' ); @@ -60,7 +59,7 @@ use SQL::Translator::Constants qw(:sqlt_types :sqlt_constants); { 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' @@ -77,12 +76,12 @@ use SQL::Translator::Constants qw(:sqlt_types :sqlt_constants); 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)' ); @@ -130,7 +129,7 @@ use SQL::Translator::Constants qw(:sqlt_types :sqlt_constants); 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' ); @@ -140,7 +139,7 @@ use SQL::Translator::Constants qw(:sqlt_types :sqlt_constants); 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' ); @@ -158,7 +157,7 @@ use SQL::Translator::Constants qw(:sqlt_types :sqlt_constants); 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' ); @@ -197,7 +196,7 @@ use SQL::Translator::Constants qw(:sqlt_types :sqlt_constants); 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)' ); @@ -212,8 +211,8 @@ use SQL::Translator::Constants qw(:sqlt_types :sqlt_constants); } { - 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, @@ -264,8 +263,8 @@ use SQL::Translator::Constants qw(:sqlt_types :sqlt_constants); 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' ); @@ -283,23 +282,23 @@ use SQL::Translator::Constants qw(:sqlt_types :sqlt_constants); 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; @@ -309,7 +308,7 @@ use SQL::Translator::Constants qw(:sqlt_types :sqlt_constants); 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; @@ -399,8 +398,8 @@ use SQL::Translator::Constants qw(:sqlt_types :sqlt_constants); # 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; @@ -472,9 +471,9 @@ use SQL::Translator::Constants qw(:sqlt_types :sqlt_constants); # 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` */ @@ -631,8 +630,8 @@ use SQL::Translator::Constants qw(:sqlt_types :sqlt_constants); # 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; ] ); @@ -674,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}'", @@ -685,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, @@ -697,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; @@ -766,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; @@ -798,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; @@ -841,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;