X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F47postgres-producer.t;h=8297b81ed91c1b64a767abc4b296aa70fcaf29de;hb=aaea005bda82f5f81f20fd742287874a58045fa6;hp=447930f991aca502625fa0baf57eb756e906fe91;hpb=681dc480882f7c16d800c98f9769d5121c4c6b30;p=dbsrgits%2FSQL-Translator.git diff --git a/t/47postgres-producer.t b/t/47postgres-producer.t index 447930f..8297b81 100644 --- a/t/47postgres-producer.t +++ b/t/47postgres-producer.t @@ -14,7 +14,7 @@ use FindBin qw/$Bin/; #============================================================================= BEGIN { - maybe_plan(51, + maybe_plan(57, 'SQL::Translator::Producer::PostgreSQL', 'Test::Differences', ) @@ -24,6 +24,25 @@ use SQL::Translator; my $PRODUCER = \&SQL::Translator::Producer::PostgreSQL::create_field; +{ + my $table = SQL::Translator::Schema::Table->new( name => 'foo.bar' ); + my $field = SQL::Translator::Schema::Field->new( name => 'baz', + table => $table, + data_type => 'VARCHAR', + size => 10, + default_value => 'quux', + is_auto_increment => 0, + is_nullable => 0, + is_foreign_key => 0, + is_unique => 0 ); + $table->add_field($field); + my ($create, $fks) = SQL::Translator::Producer::PostgreSQL::create_table($table, { quote_table_names => q{"} }); + is($table->name, 'foo.bar'); + + my $expected = "--\n-- Table: foo.bar\n--\nCREATE TABLE \"foo\".\"bar\" (\n \"baz\" character varying(10) DEFAULT 'quux' NOT NULL\n)"; + is($create, $expected); +} + my $table = SQL::Translator::Schema::Table->new( name => 'mytable'); my $field1 = SQL::Translator::Schema::Field->new( name => 'myfield', @@ -124,13 +143,45 @@ for my $name ( 'foo', undef ) { } else { is($fk_constraint_fk_ref->[0], 'ALTER TABLE mytable ADD FOREIGN KEY (myfield) - REFERENCES mytable2 (myfield_2) DEFERRABLE', 'Create named Foreign Key Constraint works'); + REFERENCES mytable2 (myfield_2) DEFERRABLE', 'Create un-named Foreign Key Constraint works'); my $alter_fk_constraint = SQL::Translator::Producer::PostgreSQL::alter_drop_constraint($fk_constraint); - is($alter_fk_constraint, 'ALTER TABLE mytable DROP CONSTRAINT mytable_myfield_fkey', 'Alter drop named Foreign Key constraint works'); + is($alter_fk_constraint, 'ALTER TABLE mytable DROP CONSTRAINT mytable_myfield_fkey', 'Alter drop un-named Foreign Key constraint works'); } } +# check named, and unnamed primary keys +for my $name ( 'foo', undef ) { + my $pk_constraint = SQL::Translator::Schema::Constraint->new( + table => $table, + name => $name, + fields => [qw(myfield)], + type => 'PRIMARY_KEY', + ); + my $pk_constraint_2 = SQL::Translator::Schema::Constraint->new( + table => $table, + name => $name, + fields => [qw(myfield)], + type => 'PRIMARY_KEY', + ); + + my ($pk_constraint_def_ref, $pk_constraint_pk_ref ) = SQL::Translator::Producer::PostgreSQL::create_constraint($pk_constraint); + + if ( $name ) { + is($pk_constraint_def_ref->[0], "CONSTRAINT $name PRIMARY KEY (myfield)", 'Create Primary Key Constraint works'); + + # ToDo: may we should check if the constraint name was valid, or if next + # unused_name created has choosen a different one + my $alter_pk_constraint = SQL::Translator::Producer::PostgreSQL::alter_drop_constraint($pk_constraint); + is($alter_pk_constraint, "ALTER TABLE mytable DROP CONSTRAINT $name", 'Alter drop Primary Key constraint works'); + } + else { + is($pk_constraint_def_ref->[0], 'PRIMARY KEY (myfield)', 'Create un-named Primary Key Constraint works'); + + my $alter_pk_constraint = SQL::Translator::Producer::PostgreSQL::alter_drop_constraint($pk_constraint); + is($alter_pk_constraint, 'ALTER TABLE mytable DROP CONSTRAINT mytable_pkey', 'Alter drop un-named Foreign Key constraint works'); + } +} my $alter_field = SQL::Translator::Producer::PostgreSQL::alter_field($field1, $field2); @@ -199,14 +250,14 @@ my $field3_datetime_with_TZ = SQL::Translator::Schema::Field->new( size => 7, ); -my $field3_datetime_with_TZ_sql = +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', + $field3_datetime_with_TZ_sql, + 'datetime_with_TZ timestamp(6) with time zone', 'Create time field with time zone and size, works' ); @@ -217,14 +268,14 @@ my $field3_time_without_TZ = SQL::Translator::Schema::Field->new( size => 2, ); -my $field3_time_without_TZ_sql +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', + $field3_time_without_TZ_sql, + 'time_without_TZ time(2) without time zone', 'Create time field without time zone but with size, works' ); @@ -500,7 +551,7 @@ is($view2_sql1, $view2_sql_replace, 'correct "CREATE OR REPLACE VIEW" SQL 2'); { my $table = SQL::Translator::Schema::Table->new( name => 'foobar', fields => [qw( foo bar )] ); - my $quote = { quote_table_names => '"', quote_field_names => '"' }; + my $quote = { quote_table_names => '"' }; { my $index = $table->add_index(name => 'myindex', fields => ['foo']); @@ -558,7 +609,7 @@ my $drop_view_8_1_expected = "DROP VIEW view_foo; CREATE VIEW view_foo ( id, name ) AS SELECT id, name FROM thing "; - + is($drop_view_8_1_produced, $drop_view_8_1_expected, "My DROP VIEW statement for 8.1 is correct"); my $drop_view_opts2 = { add_drop_view => 1, no_comments => 1, postgres_version => 9.001 }; @@ -568,5 +619,5 @@ my $drop_view_9_1_expected = "DROP VIEW IF EXISTS view_foo; CREATE VIEW view_foo ( id, name ) AS SELECT id, name FROM thing "; - + is($drop_view_9_1_produced, $drop_view_9_1_expected, "My DROP VIEW statement for 9.1 is correct");