Add use_moo option
[dbsrgits/DBIx-Class-Schema-Loader.git] / t / 10_04db2_common.t
index 03b4b54..0533dc0 100644 (file)
@@ -1,3 +1,6 @@
+use DBIx::Class::Schema::Loader::Optional::Dependencies
+    -skip_all_without => 'test_rdbms_db2';
+
 use strict;
 use warnings;
 use Test::More;
@@ -17,9 +20,6 @@ my $dsn      = $ENV{DBICTEST_DB2_DSN} || '';
 my $user     = $ENV{DBICTEST_DB2_USER} || '';
 my $password = $ENV{DBICTEST_DB2_PASS} || '';
 
-plan skip_all => 'You need to set the DBICTEST_DB2_DSN, _USER, and _PASS environment variables'
-    unless ($dsn && $user);
-
 my ($schema, $schemas_created); # for cleanup in END for extra tests
 
 my $srv_ver = do {
@@ -37,7 +37,7 @@ my $extra_graphics_data_types = {
     'dbclob'           => { data_type => 'dbclob' },
 };
 
-my $tester = dbixcsl_common_tests->new(
+dbixcsl_common_tests->new(
     vendor         => 'DB2',
     auto_inc_pk    => 'INTEGER GENERATED BY DEFAULT AS IDENTITY NOT NULL PRIMARY KEY',
     dsn            => $dsn,
@@ -46,6 +46,8 @@ my $tester = dbixcsl_common_tests->new(
     null           => '',
     preserve_case_mode_is_exclusive => 1,
     quote_char                      => '"',
+    default_is_deferrable => 1,
+    default_on_clause => 'NO ACTION',
     data_types => {
         # http://publib.boulder.ibm.com/infocenter/db2luw/v8/index.jsp?topic=/com.ibm.db2.udb.doc/admin/r0008483.htm
         #
@@ -104,11 +106,41 @@ my $tester = dbixcsl_common_tests->new(
 #        datalink           => { data_type => 'datalink' },
     },
     extra => {
-        count => 28 * 2,
+        create => [
+            # 4 through 8 are used for the multi-schema tests
+            q{
+                create table db2_loader_test9 (
+                    id int generated by default as identity not null primary key
+                )
+            },
+            q{
+                create table db2_loader_test10 (
+                    id int generated by default as identity not null primary key,
+                    nine_id int,
+                    foreign key (nine_id) references db2_loader_test9(id)
+                        on delete set null on update restrict
+                )
+            },
+        ],
+        drop  => [ qw/db2_loader_test9 db2_loader_test10/ ],
+        count => 4 + 30 * 2,
         run => sub {
-            SKIP: {
-                $schema = shift;
+            $schema = shift;
+
+            # test on delete/update fk clause introspection
+            ok ((my $rel_info = $schema->source('Db2LoaderTest10')->relationship_info('nine')),
+                'got rel info');
+
+            is $rel_info->{attrs}{on_delete}, 'SET NULL',
+                'ON DELETE clause introspected correctly';
 
+            is $rel_info->{attrs}{on_update}, 'RESTRICT',
+                'ON UPDATE clause introspected correctly';
+
+            is $rel_info->{attrs}{is_deferrable}, 1,
+                'DEFERRABLE defaults to 1';
+
+            SKIP: {
                 my $dbh = $schema->storage->dbh;
 
                 try {
@@ -116,7 +148,7 @@ my $tester = dbixcsl_common_tests->new(
                 }
                 catch {
                     $schemas_created = 0;
-                    skip "no CREATE SCHEMA privileges", 28 * 2;
+                    skip "no CREATE SCHEMA privileges", 30 * 2;
                 };
 
                 $dbh->do(<<"EOF");
@@ -129,12 +161,22 @@ EOF
                     CREATE TABLE "dbicsl-test".db2_loader_test5 (
                         id INT GENERATED BY DEFAULT AS IDENTITY NOT NULL PRIMARY KEY,
                         value VARCHAR(100),
-                        four_id INTEGER NOT NULL UNIQUE,
+                        four_id INTEGER NOT NULL,
+                        CONSTRAINT loader_test5_uniq UNIQUE (four_id),
                         FOREIGN KEY (four_id) REFERENCES "dbicsl-test".db2_loader_test4 (id)
                     )
 EOF
                 $dbh->do('CREATE SCHEMA "dbicsl.test"');
                 $dbh->do(<<"EOF");
+                    CREATE TABLE "dbicsl.test".db2_loader_test5 (
+                        pk INT GENERATED BY DEFAULT AS IDENTITY NOT NULL PRIMARY KEY,
+                        value VARCHAR(100),
+                        four_id INTEGER NOT NULL,
+                        CONSTRAINT loader_test5_uniq UNIQUE (four_id),
+                        FOREIGN KEY (four_id) REFERENCES "dbicsl-test".db2_loader_test4 (id)
+                    )
+EOF
+                $dbh->do(<<"EOF");
                     CREATE TABLE "dbicsl.test".db2_loader_test6 (
                         id INT GENERATED BY DEFAULT AS IDENTITY NOT NULL PRIMARY KEY,
                         value VARCHAR(100),
@@ -213,7 +255,7 @@ EOF
                         ok $row = $rs->create({ value => 'foo' });
                     } 'executed SQL on table in schema name with dash';
 
-                    $rel_info = try { $rsrc->relationship_info('db2_loader_test5') };
+                    $rel_info = try { $rsrc->relationship_info('dbicsl_dash_test_db2_loader_test5') };
 
                     is_deeply $rel_info->{cond}, {
                         'foreign.four_id' => 'self.id'
@@ -226,7 +268,7 @@ EOF
                         'relationship in schema name with dash';
 
                     lives_and {
-                        ok $rsrc = $test_schema->source('Db2LoaderTest5');
+                        ok $rsrc = $test_schema->source('DbicslDashTestDb2LoaderTest5');
                     } 'got source for table in schema name with dash';
 
                     %uniqs = try { $rsrc->unique_constraints };
@@ -234,6 +276,11 @@ EOF
                     is keys %uniqs, 2,
                         'got unique and primary constraint in schema name with dash';
 
+                    delete $uniqs{primary};
+
+                    is_deeply ((values %uniqs)[0], ['four_id'],
+                        'correct unique constraint in schema name with dash');
+
                     lives_and {
                         ok $rsrc = $test_schema->source('Db2LoaderTest6');
                     } 'got source for table in schema name with dot';
@@ -276,6 +323,11 @@ EOF
                     is keys %uniqs, 2,
                         'got unique and primary constraint in schema name with dot';
 
+                    delete $uniqs{primary};
+
+                    is_deeply ((values %uniqs)[0], ['six_id'],
+                        'correct unique constraint in schema name with dot');
+
                     lives_and {
                         ok $test_schema->source('Db2LoaderTest6')
                             ->has_relationship('db2_loader_test4');
@@ -300,9 +352,7 @@ EOF
 
         },
     },
-);
-
-$tester->run_tests();
+)->run_tests();
 
 END {
     if (not $ENV{SCHEMA_LOADER_TESTS_NOCLEANUP}) {
@@ -311,6 +361,7 @@ END {
                                '"dbicsl.test".db2_loader_test7',
                                '"dbicsl.test".db2_loader_test6',
                                '"dbicsl-test".db2_loader_test5',
+                               '"dbicsl.test".db2_loader_test5',
                                '"dbicsl-test".db2_loader_test4') {
                 try {
                     $dbh->do("DROP TABLE $table");