X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F10_10informix_common.t;h=6ebd0a21f64a4b9a23889904cd4c81427cab1c98;hb=462914a7047b5a165395c0f583f2d09ee317e3e2;hp=f3d5d5301ab086af96128e3b5a563b372fcae04f;hpb=c4a69b87bd3d3fdda08f05d363311a6e9d3fc0f7;p=dbsrgits%2FDBIx-Class-Schema-Loader.git diff --git a/t/10_10informix_common.t b/t/10_10informix_common.t index f3d5d53..6ebd0a2 100644 --- a/t/10_10informix_common.t +++ b/t/10_10informix_common.t @@ -1,3 +1,6 @@ +use DBIx::Class::Schema::Loader::Optional::Dependencies + -skip_all_without => 'test_rdbms_informix'; + use strict; use warnings; use Test::More; @@ -5,6 +8,9 @@ use Test::Exception; use Try::Tiny; use File::Path 'rmtree'; use DBIx::Class::Schema::Loader 'make_schema_at'; +use DBIx::Class::Schema::Loader::Utils 'split_name'; +use String::ToIdentifier::EN::Unicode 'to_identifier'; +use namespace::clean; use lib qw(t/lib); @@ -24,7 +30,7 @@ my $password = $ENV{DBICTEST_INFORMIX_PASS} || ''; my ($schema, $extra_schema); # for cleanup in END for extra tests -my $tester = dbixcsl_common_tests->new( +dbixcsl_common_tests->new( vendor => 'Informix', auto_inc_pk => 'serial primary key', null => '', @@ -60,9 +66,9 @@ my $tester = dbixcsl_common_tests->new( numeric => { data_type => 'numeric' }, decimal => { data_type => 'numeric' }, dec => { data_type => 'numeric' }, - 'numeric(6,3)' => { data_type => 'numeric', size => [6,3] }, - 'decimal(6,3)' => { data_type => 'numeric', size => [6,3] }, - 'dec(6,3)' => { data_type => 'numeric', size => [6,3] }, + 'numeric(6,3)' => { data_type => 'numeric', size => [6,3] }, + 'decimal(6,3)' => { data_type => 'numeric', size => [6,3] }, + 'dec(6,3)' => { data_type => 'numeric', size => [6,3] }, # Boolean Type # XXX this should map to 'boolean' @@ -127,12 +133,12 @@ my $tester = dbixcsl_common_tests->new( => { data_type => 'set' }, }, extra => { - count => 24, + count => 26 * 2, run => sub { ($schema) = @_; SKIP: { - skip 'Set the DBICTEST_INFORMIX_EXTRADB_DSN, _USER and _PASS environment variables to run the multi-database tests', 24 + skip 'Set the DBICTEST_INFORMIX_EXTRADB_DSN, _USER and _PASS environment variables to run the multi-database tests', 26 * 2 unless $ENV{DBICTEST_INFORMIX_EXTRADB_DSN}; $extra_schema = $schema->clone; @@ -141,7 +147,6 @@ my $tester = dbixcsl_common_tests->new( }); my $dbh1 = $schema->storage->dbh; - my $dbh2 = $extra_schema->storage->dbh; $dbh1->do(<<'EOF'); CREATE TABLE informix_loader_test4 ( @@ -153,9 +158,29 @@ EOF CREATE TABLE informix_loader_test5 ( id SERIAL PRIMARY KEY, value VARCHAR(100), - four_id INTEGER UNIQUE REFERENCES informix_loader_test4 (id) + four_id INTEGER REFERENCES informix_loader_test4 (id) ) EOF + $dbh1->do(<<'EOF'); +ALTER TABLE informix_loader_test5 ADD CONSTRAINT UNIQUE (four_id) CONSTRAINT loader_test5_uniq +EOF + + my $db1 = db_name($schema); + + $dbh1->disconnect; + + my $dbh2 = $extra_schema->storage->dbh; + + $dbh2->do(<<'EOF'); + CREATE TABLE informix_loader_test5 ( + pk SERIAL PRIMARY KEY, + value VARCHAR(100), + four_id INTEGER + ) +EOF + $dbh2->do(<<'EOF'); +ALTER TABLE informix_loader_test5 ADD CONSTRAINT UNIQUE (four_id) CONSTRAINT loader_test5_uniq +EOF $dbh2->do(<<"EOF"); CREATE TABLE informix_loader_test6 ( id SERIAL PRIMARY KEY, @@ -169,146 +194,186 @@ EOF six_id INTEGER UNIQUE REFERENCES informix_loader_test6 (id) ) EOF - lives_and { - my @warns; - local $SIG{__WARN__} = sub { - push @warns, $_[0] unless $_[0] =~ /\bcollides\b/ - || $_[0] =~ /unreferencable/; - }; - - make_schema_at( - 'InformixMultiDatabase', - { - naming => 'current', - db_schema => { '%' => '%' }, - dump_directory => EXTRA_DUMP_DIR, - quiet => 1, - }, - [ $dsn, $user, $password ], - ); - diag join "\n", @warns if @warns; + my $db2 = db_name($extra_schema); + + $dbh2->disconnect; + + my $db1_moniker = join '', map ucfirst lc, split_name to_identifier $db1; + my $db2_moniker = join '', map ucfirst lc, split_name to_identifier $db2; + + foreach my $db_schema ({ $db1 => '%', $db2 => '%' }, { '%' => '%' }) { + lives_and { + my @warns; + local $SIG{__WARN__} = sub { + push @warns, $_[0] unless $_[0] =~ /\bcollides\b/ + || $_[0] =~ /unreferencable/; + }; + + make_schema_at( + 'InformixMultiDatabase', + { + naming => 'current', + db_schema => $db_schema, + dump_directory => EXTRA_DUMP_DIR, + quiet => 1, + }, + [ $dsn, $user, $password ], + ); + + InformixMultiDatabase->storage->disconnect; - is @warns, 0; - } 'dumped schema for all databases with no warnings'; + diag join "\n", @warns if @warns; - my $test_schema; + is @warns, 0; + } "dumped schema for databases $db1 and $db2 with no warnings"; - lives_and { - ok $test_schema = InformixMultiDatabase->connect($dsn, $user, $password); - } 'connected test schema'; + my $test_schema; - my ($rsrc, $rs, $row, $rel_info, %uniqs); + lives_and { + ok $test_schema = InformixMultiDatabase->connect($dsn, $user, $password); + } 'connected test schema'; - lives_and { - ok $rsrc = $test_schema->source('InformixLoaderTest4'); - } 'got source for table in database one'; + my ($rsrc, $rs, $row, $rel_info, %uniqs); - is try { $rsrc->column_info('id')->{is_auto_increment} }, 1, - 'column in database one'; + lives_and { + ok $rsrc = $test_schema->source("InformixLoaderTest4"); + } 'got source for table in database one'; - is try { $rsrc->column_info('value')->{data_type} }, 'varchar', - 'column in database one'; + is try { $rsrc->column_info('id')->{is_auto_increment} }, 1, + 'column in database one'; - is try { $rsrc->column_info('value')->{size} }, 100, - 'column in database one'; + is try { $rsrc->column_info('value')->{data_type} }, 'varchar', + 'column in database one'; - lives_and { - ok $rs = $test_schema->resultset('InformixLoaderTest4'); - } 'got resultset for table in database one'; + is try { $rsrc->column_info('value')->{size} }, 100, + 'column in database one'; - lives_and { - ok $row = $rs->create({ value => 'foo' }); - } 'executed SQL on table in database one'; + lives_and { + ok $rs = $test_schema->resultset("InformixLoaderTest4"); + } 'got resultset for table in database one'; - $rel_info = try { $rsrc->relationship_info('informix_loader_test5') }; + lives_and { + ok $row = $rs->create({ value => 'foo' }); + } 'executed SQL on table in database one'; - is_deeply $rel_info->{cond}, { - 'foreign.four_id' => 'self.id' - }, 'relationship in database one'; + $rel_info = try { $rsrc->relationship_info("informix_loader_test5") }; - is $rel_info->{attrs}{accessor}, 'single', - 'relationship in database one'; + is_deeply $rel_info->{cond}, { + 'foreign.four_id' => 'self.id' + }, 'relationship in database one'; - is $rel_info->{attrs}{join_type}, 'LEFT', - 'relationship in database one'; + is $rel_info->{attrs}{accessor}, 'single', + 'relationship in database one'; - lives_and { - ok $rsrc = $test_schema->source('InformixLoaderTest5'); - } 'got source for table in database one'; + is $rel_info->{attrs}{join_type}, 'LEFT', + 'relationship in database one'; - %uniqs = try { $rsrc->unique_constraints }; + lives_and { + ok $rsrc = $test_schema->source("${db1_moniker}InformixLoaderTest5"); + } 'got source for table in database one'; - is keys %uniqs, 2, - 'got unique and primary constraint in database one'; + %uniqs = try { $rsrc->unique_constraints }; - lives_and { - ok $rsrc = $test_schema->source('InformixLoaderTest6'); - } 'got source for table in database two'; + is keys %uniqs, 2, + 'got unique and primary constraint in database one'; - is try { $rsrc->column_info('id')->{is_auto_increment} }, 1, - 'column in database two introspected correctly'; + delete $uniqs{primary}; - is try { $rsrc->column_info('value')->{data_type} }, 'varchar', - 'column in database two introspected correctly'; + is_deeply ((values %uniqs)[0], ['four_id'], + 'correct unique constraint in database one'); - is try { $rsrc->column_info('value')->{size} }, 100, - 'column in database two introspected correctly'; + lives_and { + ok $rsrc = $test_schema->source("InformixLoaderTest6"); + } 'got source for table in database two'; - lives_and { - ok $rs = $test_schema->resultset('InformixLoaderTest6'); - } 'got resultset for table in database two'; + is try { $rsrc->column_info('id')->{is_auto_increment} }, 1, + 'column in database two introspected correctly'; - lives_and { - ok $row = $rs->create({ value => 'foo' }); - } 'executed SQL on table in database two'; + is try { $rsrc->column_info('value')->{data_type} }, 'varchar', + 'column in database two introspected correctly'; - $rel_info = try { $rsrc->relationship_info('informix_loader_test7') }; + is try { $rsrc->column_info('value')->{size} }, 100, + 'column in database two introspected correctly'; - is_deeply $rel_info->{cond}, { - 'foreign.six_id' => 'self.id' - }, 'relationship in database two'; + lives_and { + ok $rs = $test_schema->resultset("InformixLoaderTest6"); + } 'got resultset for table in database two'; - is $rel_info->{attrs}{accessor}, 'single', - 'relationship in database two'; + lives_and { + ok $row = $rs->create({ value => 'foo' }); + } 'executed SQL on table in database two'; - is $rel_info->{attrs}{join_type}, 'LEFT', - 'relationship in database two'; + $rel_info = try { $rsrc->relationship_info('informix_loader_test7') }; - lives_and { - ok $rsrc = $test_schema->source('InformixLoaderTest7'); - } 'got source for table in database two'; + is_deeply $rel_info->{cond}, { + 'foreign.six_id' => 'self.id' + }, 'relationship in database two'; - %uniqs = try { $rsrc->unique_constraints }; + is $rel_info->{attrs}{accessor}, 'single', + 'relationship in database two'; - is keys %uniqs, 2, - 'got unique and primary constraint in database two'; + is $rel_info->{attrs}{join_type}, 'LEFT', + 'relationship in database two'; + + lives_and { + ok $rsrc = $test_schema->source("InformixLoaderTest7"); + } 'got source for table in database two'; + + %uniqs = try { $rsrc->unique_constraints }; + + is keys %uniqs, 2, + 'got unique and primary constraint in database two'; + + delete $uniqs{primary}; + + is_deeply ((values %uniqs)[0], ['six_id'], + 'correct unique constraint in database two'); + } } }, }, -); +)->run_tests(); -if( !$dsn ) { - $tester->skip_tests('You need to set the DBICTEST_INFORMIX_DSN, _USER, and _PASS environment variables'); -} -else { - $tester->run_tests(); +sub db_name { + my $schema = shift; + + # When we clone the schema, it still references the original loader, which + # references the original schema. + local $schema->loader->{schema} = $schema; + + return $schema->loader->_current_db; + + $schema->storage->disconnect; } END { if (not $ENV{SCHEMA_LOADER_TESTS_NOCLEANUP}) { if (my $dbh2 = try { $extra_schema->storage->dbh }) { - my $dbh1 = $schema->storage->dbh; try { $dbh2->do('DROP TABLE informix_loader_test7'); $dbh2->do('DROP TABLE informix_loader_test6'); + $dbh2->do('DROP TABLE informix_loader_test5'); + } + catch { + die "Error dropping test tables: $_"; + }; + + $dbh2->disconnect; + } + + if (my $dbh1 = try { $schema->storage->dbh }) { + + try { $dbh1->do('DROP TABLE informix_loader_test5'); $dbh1->do('DROP TABLE informix_loader_test4'); } catch { die "Error dropping test tables: $_"; }; + + $dbh1->disconnect; } rmtree EXTRA_DUMP_DIR;