From: Justin Hunter Date: Wed, 27 Jan 2010 07:18:41 +0000 (-0800) Subject: add tests and test data X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=d4c630fa5bb9860106694743d475aa05187700d7;p=dbsrgits%2FSQL-Translator-2.0-ish.git add tests and test data --- diff --git a/t/07p_args.t b/t/07p_args.t new file mode 100644 index 0000000..32cca1d --- /dev/null +++ b/t/07p_args.t @@ -0,0 +1,76 @@ +use SQL::Translator; +use Test::More; + +sub silly_parser { + my ($tr, $data) = @_; + my $pargs = $tr->parser_args; + + my @fields = split /$pargs->{'delimiter'}/, $data; + + my $schema = $tr->schema; + my $table = $schema->add_table( name => 'foo') or die $schema->error; + for my $value ( @fields ) { + my $field = $table->add_field( name => $value ) or die $table->error; + } + + return 1; +} + +# The "data" to be parsed +my $data = q(Id|Name|Phone Number|Favorite Flavor|); + +my $tr = SQL::Translator->new; + +# Pass parser_args as an explicit method call +$tr->parser(\&silly_parser); +$tr->parser_args(delimiter => '\|'); + +my $pargs = $tr->parser_args; +$tr->translate(\$data); +my $schema = $tr->schema; + +is($pargs->{'delimiter'}, '\|', "parser_args works when called directly"); +my @tables = $schema->get_tables; +is(scalar @tables, 1, "right number of tables"); +my $table = shift @tables; +my @fields = $table->get_fields; +is(scalar @fields, 4, "right number of fields"); + +# +# Blow away the existing schema object. +# +$tr->schema (undef); + +# Now, pass parser_args indirectly... +$tr->parser(\&silly_parser, { delimiter => "\t" }); +$data =~ s/\|/\t/g; + +$pargs = $tr->parser_args; +$tr->translate(\$data); + +is($pargs->{'delimiter'}, "\t", + "parser_args works when called indirectly"); + +@tables = $schema->get_tables; +is(scalar @tables, 1, "right number of tables"); +$table = shift @tables; +@fields = $table->get_fields; +is(scalar @fields, 4, "right number of fields"); + +undef $tr; +$tr = SQL::Translator->new(parser => \&silly_parser, + parser_args => { delimiter => ":" }); +$data =~ s/\t/:/g; +$pargs = $tr->parser_args; +$tr->translate(\$data); + +is($pargs->{'delimiter'}, ":", + "parser_args works when called as constructor arg"); + +@tables = $schema->get_tables; +is(scalar @tables, 1, "right number of tables"); +$table = shift @tables; +@fields = $table->get_fields; +is(scalar @fields, 4, "right number of fields with new delimiter"); + +done_testing; diff --git a/t/38-mysql-producer.t b/t/38-mysql-producer.t new file mode 100644 index 0000000..442b028 --- /dev/null +++ b/t/38-mysql-producer.t @@ -0,0 +1,448 @@ +#!/usr/bin/perl -w +# vim:filetype=perl + +# +# Note that the bulk of the testing for the mysql producer is in +# 08postgres-to-mysql.t. This test is for additional stuff that can't be tested +# using an Oracle schema as source e.g. extra attributes. +# + +use strict; +use Test::More; +use Test::Exception; +#use Test::SQL::Translator qw(maybe_plan); + +use Data::Dumper; +use FindBin qw/$Bin/; + +# Testing 1,2,3,4... +#============================================================================= + +#BEGIN { +# maybe_plan(40, +# 'YAML', +# 'SQL::Translator::Producer::MySQL', +# 'Test::Differences', +# ) +#} +use Test::Differences; +use SQL::Translator; + +# Main test. +{ +my $yaml_in = <new( + show_warnings => 1, + no_comments => 1, +# debug => 1, + from => "YAML", + to => "MySQL", + quote_table_names => 1, + quote_field_names => 1 + ); + + my $out = $sqlt->translate(data => $yaml_in) + or die "Translate error:".$sqlt->error; + ok $out ne "", "Produced something!"; + eq_or_diff $out, $mysql_out, "Scalar output looks right with quoting"; + + my @out = $sqlt->translate(data => $yaml_in) + or die "Translat eerror:".$sqlt->error; + is_deeply \@out, \@stmts_no_drop, "Array output looks right with quoting"; + + + @{$sqlt}{qw/quote_table_names quote_field_names/} = (0,0); + $out = $sqlt->translate(data => $yaml_in) + or die "Translate error:".$sqlt->error; + + @out = $sqlt->translate(data => $yaml_in) + or die "Translate error:".$sqlt->error; + $mysql_out =~ s/`//g; + my @unquoted_stmts = map { s/`//g; $_} @stmts_no_drop; + eq_or_diff $out, $mysql_out, "Output looks right without quoting"; + is_deeply \@out, \@unquoted_stmts, "Array output looks right without quoting"; + + @{$sqlt}{qw/add_drop_table quote_field_names quote_table_names/} = (1,1,1); + @out = $sqlt->translate(data => $yaml_in) + or die "Translat eerror:".$sqlt->error; + $out = $sqlt->translate(data => $yaml_in) + or die "Translat eerror:".$sqlt->error; + + eq_or_diff $out, join(";\n\n", @stmts) . ";\n\n", "Output looks right with DROP TABLEs"; + is_deeply \@out, \@stmts, "Array output looks right with DROP TABLEs"; +} + +############################################################################### +# New alter/add subs + +my $table = SQL::Translator::Schema::Table->new( name => 'mytable'); + +my $field1 = SQL::Translator::Schema::Field->new( name => 'myfield', + table => $table, + data_type => 'VARCHAR', + size => 10, + default_value => undef, + is_auto_increment => 0, + is_nullable => 1, + is_foreign_key => 0, + is_unique => 0 ); + +my $field1_sql = SQL::Translator::Producer::MySQL::create_field($field1); + +is($field1_sql, 'myfield VARCHAR(10)', 'Create field works'); + +my $field2 = SQL::Translator::Schema::Field->new( name => 'myfield', + table => $table, + data_type => 'VARCHAR', + size => 25, + default_value => undef, + is_auto_increment => 0, + is_nullable => 0, + is_foreign_key => 0, + is_unique => 0 ); + +my $alter_field = SQL::Translator::Producer::MySQL::alter_field($field1, + $field2); +is($alter_field, 'ALTER TABLE mytable CHANGE COLUMN myfield myfield VARCHAR(25) NOT NULL', 'Alter field works'); + +my $add_field = SQL::Translator::Producer::MySQL::add_field($field1); + +is($add_field, 'ALTER TABLE mytable ADD COLUMN myfield VARCHAR(10)', 'Add field works'); + +my $drop_field = SQL::Translator::Producer::MySQL::drop_field($field2); +is($drop_field, 'ALTER TABLE mytable DROP COLUMN myfield', 'Drop field works'); + +my $field3 = SQL::Translator::Schema::Field->new( name => 'myfield', + table => $table, + data_type => 'boolean', + is_nullable => 0, + is_foreign_key => 0, + is_unique => 0 ); + +my $field3_sql = SQL::Translator::Producer::MySQL::create_field($field3, { mysql_version => 4.1 }); +is($field3_sql, 'myfield boolean NOT NULL', 'For Mysql >= 4, use boolean type'); +$field3_sql = SQL::Translator::Producer::MySQL::create_field($field3, { mysql_version => 3.22 }); +is($field3_sql, "myfield enum('0','1') NOT NULL", 'For Mysql < 4, use enum for boolean type'); +$field3_sql = SQL::Translator::Producer::MySQL::create_field($field3,); +is($field3_sql, "myfield enum('0','1') NOT NULL", 'When no version specified, use enum for boolean type'); + +my $number_sizes = { + '3, 2' => 'double', + 12 => 'bigint', + 1 => 'tinyint', + 4 => 'int', +}; +for my $size (keys %$number_sizes) { + my $expected = $number_sizes->{$size}; + my $number_field = SQL::Translator::Schema::Field->new( + name => "numberfield_$expected", + table => $table, + data_type => 'number', + size => $size, + is_nullable => 1, + is_foreign_key => 0, + is_unique => 0 + ); + + is( + SQL::Translator::Producer::MySQL::create_field($number_field), + "numberfield_$expected $expected($size)", + "Use $expected for NUMBER types of size $size" + ); +} + +my $varchars; +for my $size (qw/255 256 65535 65536/) { + $varchars->{$size} = SQL::Translator::Schema::Field->new( + name => "vch_$size", + table => $table, + data_type => 'varchar', + size => $size, + is_nullable => 1, + ); +} + + +is ( + SQL::Translator::Producer::MySQL::create_field($varchars->{255}, { mysql_version => 5.000003 }), + 'vch_255 varchar(255)', + 'VARCHAR(255) is not substituted with TEXT for Mysql >= 5.0.3' +); +is ( + SQL::Translator::Producer::MySQL::create_field($varchars->{255}, { mysql_version => 5.0 }), + 'vch_255 varchar(255)', + 'VARCHAR(255) is not substituted with TEXT for Mysql < 5.0.3' +); +is ( + SQL::Translator::Producer::MySQL::create_field($varchars->{255}), + 'vch_255 varchar(255)', + 'VARCHAR(255) is not substituted with TEXT when no version specified', +); + + +is ( + SQL::Translator::Producer::MySQL::create_field($varchars->{256}, { mysql_version => 5.000003 }), + 'vch_256 varchar(256)', + 'VARCHAR(256) is not substituted with TEXT for Mysql >= 5.0.3' +); +is ( + SQL::Translator::Producer::MySQL::create_field($varchars->{256}, { mysql_version => 5.0 }), + 'vch_256 text', + 'VARCHAR(256) is substituted with TEXT for Mysql < 5.0.3' +); +is ( + SQL::Translator::Producer::MySQL::create_field($varchars->{256}), + 'vch_256 text', + 'VARCHAR(256) is substituted with TEXT when no version specified', +); + + +is ( + SQL::Translator::Producer::MySQL::create_field($varchars->{65535}, { mysql_version => 5.000003 }), + 'vch_65535 varchar(65535)', + 'VARCHAR(65535) is not substituted with TEXT for Mysql >= 5.0.3' +); +is ( + SQL::Translator::Producer::MySQL::create_field($varchars->{65535}, { mysql_version => 5.0 }), + 'vch_65535 text', + 'VARCHAR(65535) is substituted with TEXT for Mysql < 5.0.3' +); +is ( + SQL::Translator::Producer::MySQL::create_field($varchars->{65535}), + 'vch_65535 text', + 'VARCHAR(65535) is substituted with TEXT when no version specified', +); + + +is ( + SQL::Translator::Producer::MySQL::create_field($varchars->{65536}, { mysql_version => 5.000003 }), + 'vch_65536 text', + 'VARCHAR(65536) is substituted with TEXT for Mysql >= 5.0.3' +); +is ( + SQL::Translator::Producer::MySQL::create_field($varchars->{65536}, { mysql_version => 5.0 }), + 'vch_65536 text', + 'VARCHAR(65536) is substituted with TEXT for Mysql < 5.0.3' +); +is ( + SQL::Translator::Producer::MySQL::create_field($varchars->{65536}), + 'vch_65536 text', + 'VARCHAR(65536) is substituted with TEXT when no version specified', +); + + +{ + my $view1 = SQL::Translator::Schema::View->new( name => 'view_foo', + fields => [qw/id name/], + sql => 'SELECT id, name FROM thing', + extra => { + mysql_definer => 'CURRENT_USER', + mysql_algorithm => 'MERGE', + mysql_security => 'DEFINER', + }); + my $create_opts = { add_replace_view => 1, no_comments => 1 }; + my $view1_sql1 = SQL::Translator::Producer::MySQL::create_view($view1, $create_opts); + + my $view_sql_replace = "CREATE OR REPLACE + ALGORITHM = MERGE + DEFINER = CURRENT_USER + SQL SECURITY DEFINER + VIEW view_foo ( id, name ) AS ( + SELECT id, name FROM thing + )"; + is($view1_sql1, $view_sql_replace, 'correct "CREATE OR REPLACE VIEW" SQL'); + + + my $view2 = SQL::Translator::Schema::View->new( name => 'view_foo', + fields => [qw/id name/], + sql => 'SELECT id, name FROM thing',); + my $create2_opts = { add_replace_view => 0, no_comments => 1 }; + my $view1_sql2 = SQL::Translator::Producer::MySQL::create_view($view2, $create2_opts); + my $view_sql_noreplace = "CREATE + VIEW view_foo ( id, name ) AS ( + SELECT id, name FROM thing + )"; + is($view1_sql2, $view_sql_noreplace, 'correct "CREATE VIEW" SQL'); + + { + my %extra = $view1->extra; + is_deeply \%extra, + { + 'mysql_algorithm' => 'MERGE', + 'mysql_definer' => 'CURRENT_USER', + 'mysql_security' => 'DEFINER' + }, + 'Extra attributes'; + } + + $view1->remove_extra(qw/mysql_definer mysql_security/); + { + my %extra = $view1->extra; + is_deeply \%extra, { 'mysql_algorithm' => 'MERGE', }, 'Extra attributes after first reset_extra call'; + } + + $view1->remove_extra(); + { + my %extra = $view1->extra; + is_deeply \%extra, {}, 'Extra attributes completely removed'; + } +} + +{ + + # certain types do not support a size, see also: + # http://dev.mysql.com/doc/refman/5.1/de/create-table.html + for my $type (qw/date time timestamp datetime year/) { + my $field = SQL::Translator::Schema::Field->new( + name => "my$type", + table => $table, + data_type => $type, + size => 10, + default_value => undef, + is_auto_increment => 0, + is_nullable => 1, + is_foreign_key => 0, + is_unique => 0 + ); + my $sql = SQL::Translator::Producer::MySQL::create_field($field); + is($sql, "my$type $type", "Skip length param for type $type"); + } +} + +done_testing; diff --git a/t/47postgres-producer.t b/t/47postgres-producer.t new file mode 100644 index 0000000..60600e5 --- /dev/null +++ b/t/47postgres-producer.t @@ -0,0 +1,352 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Test::More; +use Test::Exception; +use Data::Dumper; +use FindBin qw/$Bin/; +use Test::Differences; +use SQL::Translator; +use SQL::Translator::Object::Table; +use SQL::Translator::Object::Column; + +my $PRODUCER = \&SQL::Translator::Producer::PostgreSQL::create_field; + +my $table = SQL::Translator::Object::Table->new( name => 'mytable'); + +my $field1 = SQL::Translator::Object::Column->new( name => 'myfield', + table => $table, + data_type => 'VARCHAR', + size => 10, + default_value => undef, + is_auto_increment => 0, + is_nullable => 1, + is_foreign_key => 0, + is_unique => 0 ); + +my $field1_sql = SQL::Translator::Producer::PostgreSQL::create_field($field1); + +is($field1_sql, 'myfield character varying(10)', 'Create field works'); + +my $field2 = SQL::Translator::Object::Column->new( name => 'myfield', + table => $table, + data_type => 'VARCHAR', + size => 25, + default_value => undef, + is_auto_increment => 0, + is_nullable => 0, + is_foreign_key => 0, + is_unique => 0 ); + +my $alter_field = SQL::Translator::Producer::PostgreSQL::alter_field($field1, + $field2); +is($alter_field, qq[ALTER TABLE mytable ALTER COLUMN myfield SET NOT NULL +ALTER TABLE mytable ALTER COLUMN myfield TYPE character varying(25)], + 'Alter field works'); + +$field1->name('field3'); +my $add_field = SQL::Translator::Producer::PostgreSQL::add_field($field1); + +is($add_field, 'ALTER TABLE mytable ADD COLUMN field3 character varying(10)', 'Add field works'); + +my $drop_field = SQL::Translator::Producer::PostgreSQL::drop_field($field2); +is($drop_field, 'ALTER TABLE mytable DROP COLUMN myfield', 'Drop field works'); + +my $field3 = SQL::Translator::Object::Column->new( name => 'time_field', + table => $table, + data_type => 'TIME', + default_value => undef, + is_auto_increment => 0, + is_nullable => 0, + is_foreign_key => 0, + is_unique => 0 ); + +my $field3_sql = SQL::Translator::Producer::PostgreSQL::create_field($field3); + +is($field3_sql, 'time_field time NOT NULL', 'Create time field works'); + +my $field3_datetime_with_TZ = SQL::Translator::Object::Column->new( + name => 'datetime_with_TZ', + table => $table, + data_type => 'timestamp with time zone', + size => 7, +); + +my $field3_datetime_with_TZ_sql = + SQL::Translator::Producer::PostgreSQL::create_field( + $field3_datetime_with_TZ + ); + +is( + $field3_datetime_with_TZ_sql, + 'datetime_with_TZ timestamp(6) with time zone', + 'Create time field with time zone and size, works' +); + +my $field3_time_without_TZ = SQL::Translator::Object::Column->new( + name => 'time_without_TZ', + table => $table, + data_type => 'time without time zone', + size => 2, +); + +my $field3_time_without_TZ_sql + = SQL::Translator::Producer::PostgreSQL::create_field( + $field3_time_without_TZ + ); + +is( + $field3_time_without_TZ_sql, + 'time_without_TZ time(2) without time zone', + 'Create time field without time zone but with size, works' +); + +my $field4 = SQL::Translator::Object::Column->new( name => 'bytea_field', + table => $table, + data_type => 'bytea', + size => '16777215', + default_value => undef, + is_auto_increment => 0, + is_nullable => 0, + is_foreign_key => 0, + is_unique => 0 ); + +my $field4_sql = SQL::Translator::Producer::PostgreSQL::create_field($field4); + +is($field4_sql, 'bytea_field bytea NOT NULL', 'Create bytea field works'); + +my $field5 = SQL::Translator::Object::Column->new( name => 'enum_field', + table => $table, + data_type => 'enum', + extra => { list => [ 'Foo', 'Bar' ] }, + is_auto_increment => 0, + is_nullable => 0, + is_foreign_key => 0, + is_unique => 0 ); + +my $field5_sql = SQL::Translator::Producer::PostgreSQL::create_field($field5,{ postgres_version => 8.3 }); + +is($field5_sql, 'enum_field mytable_enum_field_type NOT NULL', 'Create real enum field works'); + + + + +my $field6 = SQL::Translator::Object::Column->new( + name => 'character', + table => $table, + data_type => 'character', + size => '123', + default_value => 'foobar', + is_auto_increment => 0, + is_nullable => 0, + is_foreign_key => 0, + is_unique => 0); + +my $field7 = SQL::Translator::Object::Column->new( + name => 'character', + table => $table, + data_type => 'character', + size => '123', + default_value => undef, + is_auto_increment => 0, + is_nullable => 0, + is_foreign_key => 0, + is_unique => 0); + +$alter_field = SQL::Translator::Producer::PostgreSQL::alter_field($field6, + $field7); + +is($alter_field, q(ALTER TABLE mytable ALTER COLUMN character DROP DEFAULT), 'DROP DEFAULT'); + +$field7->default_value(q(foo'bar')); + +$alter_field = SQL::Translator::Producer::PostgreSQL::alter_field($field6, + $field7); + +is($alter_field, q(ALTER TABLE mytable ALTER COLUMN character SET DEFAULT 'foo''bar'''), 'DEFAULT with escaping'); + +$field7->default_value(\q(foobar)); + +$alter_field = SQL::Translator::Producer::PostgreSQL::alter_field($field6, + $field7); + +is($alter_field, q(ALTER TABLE mytable ALTER COLUMN character SET DEFAULT foobar), 'DEFAULT unescaped if scalarref'); + +$field7->is_nullable(1); +$field7->default_value(q(foobar)); + +$alter_field = SQL::Translator::Producer::PostgreSQL::alter_field($field6, + $field7); + +is($alter_field, q(ALTER TABLE mytable ALTER COLUMN character DROP NOT NULL), 'DROP NOT NULL'); + +my $field8 = SQL::Translator::Object::Column->new( name => 'ts_field', + table => $table, + data_type => 'timestamp with time zone', + size => 6, + is_auto_increment => 0, + is_nullable => 0, + is_foreign_key => 0, + is_unique => 0 ); + +my $field8_sql = SQL::Translator::Producer::PostgreSQL::create_field($field8,{ postgres_version => 8.3 }); + +is($field8_sql, 'ts_field timestamp(6) with time zone NOT NULL', 'timestamp with precision'); + +my $field9 = SQL::Translator::Object::Column->new( name => 'time_field', + table => $table, + data_type => 'time with time zone', + size => 6, + is_auto_increment => 0, + is_nullable => 0, + is_foreign_key => 0, + is_unique => 0 ); + +my $field9_sql = SQL::Translator::Producer::PostgreSQL::create_field($field9,{ postgres_version => 8.3 }); + +is($field9_sql, 'time_field time(6) with time zone NOT NULL', 'time with precision'); + +my $field10 = SQL::Translator::Object::Column->new( name => 'interval_field', + table => $table, + data_type => 'interval', + size => 6, + is_auto_increment => 0, + is_nullable => 0, + is_foreign_key => 0, + is_unique => 0 ); + +my $field10_sql = SQL::Translator::Producer::PostgreSQL::create_field($field10,{ postgres_version => 8.3 }); + +is($field10_sql, 'interval_field interval(6) NOT NULL', 'time with precision'); + + +my $field11 = SQL::Translator::Object::Column->new( name => 'time_field', + table => $table, + data_type => 'time without time zone', + size => 6, + is_auto_increment => 0, + is_nullable => 0, + is_foreign_key => 0, + is_unique => 0 ); + +my $field11_sql = SQL::Translator::Producer::PostgreSQL::create_field($field11,{ postgres_version => 8.3 }); + +is($field11_sql, 'time_field time(6) without time zone NOT NULL', 'time with precision'); + + + +my $field12 = SQL::Translator::Object::Column->new( name => 'time_field', + table => $table, + data_type => 'timestamp', + is_auto_increment => 0, + is_nullable => 0, + is_foreign_key => 0, + is_unique => 0 ); + +my $field12_sql = SQL::Translator::Producer::PostgreSQL::create_field($field12,{ postgres_version => 8.3 }); + +is($field12_sql, 'time_field timestamp NOT NULL', 'time with precision'); + + +{ + # let's test default values! -- rjbs, 2008-09-30 + my %field = ( + table => $table, + data_type => 'VARCHAR', + size => 10, + is_auto_increment => 0, + is_nullable => 1, + is_foreign_key => 0, + is_unique => 0, + ); + + { + my $simple_default = SQL::Translator::Object::Column->new( + %field, + name => 'str_default', + default_value => 'foo', + ); + + is( + $PRODUCER->($simple_default), + q{str_default character varying(10) DEFAULT 'foo'}, + 'default str', + ); + } + + { + my $null_default = SQL::Translator::Object::Column->new( + %field, + name => 'null_default', + default_value => \'NULL', + ); + + is( + $PRODUCER->($null_default), + q{null_default character varying(10) DEFAULT NULL}, + 'default null', + ); + } + + { + my $null_default = SQL::Translator::Object::Column->new( + %field, + name => 'null_default_2', + default_value => 'NULL', # XXX: this should go away + ); + + is( + $PRODUCER->($null_default), + q{null_default_2 character varying(10) DEFAULT NULL}, + 'default null from special cased string', + ); + } + + { + my $func_default = SQL::Translator::Object::Column->new( + %field, + name => 'func_default', + default_value => \'func(funky)', + ); + + is( + $PRODUCER->($func_default), + q{func_default character varying(10) DEFAULT func(funky)}, + 'unquoted default from scalar ref', + ); + } +} + + +my $view1 = SQL::Translator::Schema::View->new( + name => 'view_foo', + fields => [qw/id name/], + sql => 'SELECT id, name FROM thing', +); +my $create_opts = { add_replace_view => 1, no_comments => 1 }; +my $view1_sql1 = SQL::Translator::Producer::PostgreSQL::create_view($view1, $create_opts); + +my $view_sql_replace = "CREATE VIEW view_foo ( id, name ) AS + SELECT id, name FROM thing +"; +is($view1_sql1, $view_sql_replace, 'correct "CREATE OR REPLACE VIEW" SQL'); + +my $view2 = SQL::Translator::Schema::View->new( + name => 'view_foo2', + sql => 'SELECT id, name FROM thing', + extra => { + 'temporary' => '1', + 'check_option' => 'cascaded', + }, +); +my $create2_opts = { add_replace_view => 1, no_comments => 1 }; +my $view2_sql1 = SQL::Translator::Producer::PostgreSQL::create_view($view2, $create2_opts); + +my $view2_sql_replace = "CREATE TEMPORARY VIEW view_foo2 AS + SELECT id, name FROM thing + WITH CASCADED CHECK OPTION"; +is($view2_sql1, $view2_sql_replace, 'correct "CREATE OR REPLACE VIEW" SQL 2'); + +done_testing; diff --git a/t/56-sqlite-producer.t b/t/56-sqlite-producer.t new file mode 100644 index 0000000..2bf3881 --- /dev/null +++ b/t/56-sqlite-producer.t @@ -0,0 +1,34 @@ +use strict; +use Test::More; +use FindBin qw/$Bin/; + +use SQL::Translator::Object::View; +use SQL::Translator::Producer::SQL::SQLite; + +{ + my $view1 = SQL::Translator::Object::View->new( name => 'view_foo', + fields => [qw/id name/], + sql => 'SELECT id, name FROM thing', + extra => { + temporary => 1, + if_not_exists => 1, + }); + my $create_opts = { no_comments => 1 }; + my $view1_sql1 = SQL::Translator::Producer::SQL::SQLite::create_view($view1, $create_opts); + + my $view_sql_replace = "CREATE TEMPORARY VIEW IF NOT EXISTS view_foo AS + SELECT id, name FROM thing"; + is($view1_sql1, $view_sql_replace, 'correct "CREATE TEMPORARY VIEW" SQL'); + + + my $view2 = SQL::Translator::Object::View->new( name => 'view_foo', + fields => [qw/id name/], + sql => 'SELECT id, name FROM thing',); + + my $view1_sql2 = SQL::Translator::Producer::SQL::SQLite::create_view($view2, $create_opts); + my $view_sql_noreplace = "CREATE VIEW view_foo AS + SELECT id, name FROM thing"; + is($view1_sql2, $view_sql_noreplace, 'correct "CREATE VIEW" SQL'); +} + +done_testing; diff --git a/t/62roundtrip_datacheck.t b/t/62roundtrip_datacheck.t index ffcb9da..0bdf0c7 100644 --- a/t/62roundtrip_datacheck.t +++ b/t/62roundtrip_datacheck.t @@ -1,14 +1,9 @@ use warnings; use strict; -use Test::SQL::Translator; +use Test::More; use Test::Differences; use FindBin qw/$Bin/; -BEGIN { - maybe_plan(1, 'SQL::Translator::Parser::XML', - 'SQL::Translator::Producer::XML'); -} - # It's very hard to read and modify YAML by hand. Thus we # use an XML file for definitions, and generate a YAML from # it in Makefile.PL, so we do not saddle the user with XML @@ -31,11 +26,15 @@ sub _parse_to_xml { my $tr = SQL::Translator->new; $tr->no_comments (1); # this will drop the XML header + open (my $fh, '<', $fn) or die "$fn: $!"; + my $xml = $tr->translate ( parser => $type, - file => $fn, + data => do { local $/; <$fh> }, producer => 'XML', ) or die $tr->error; return $xml; } + +done_testing; diff --git a/t/data/roundtrip.xml b/t/data/roundtrip.xml new file mode 100644 index 0000000..db07e42 --- /dev/null +++ b/t/data/roundtrip.xml @@ -0,0 +1,115 @@ + + + + + + + + + + + + + + + + + + + Hello emptytagdef + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + +
+
+ + + + SELECT email FROM Basic WHERE (email IS NOT NULL) + + + + + + + update modified=timestamp(); + + + + update modified2=timestamp(); + + + + + + + select foo from bar + Go Sox! + + + + +