Remove MySQL year(2) test
[dbsrgits/DBIx-Class-Schema-Loader.git] / t / 10_07mssql_common.t
index ccdc8ff..185405c 100644 (file)
@@ -2,7 +2,8 @@ use strict;
 use warnings;
 use Test::More;
 use Test::Exception;
-use DBIx::Class::Schema::Loader::Utils 'warnings_exist_silent';
+use DBIx::Class::Optional::Dependencies;
+use DBIx::Class::Schema::Loader::Utils qw/warnings_exist_silent sigwarn_silencer/;
 use Try::Tiny;
 use File::Path 'rmtree';
 use DBIx::Class::Schema::Loader 'make_schema_at';
@@ -26,17 +27,24 @@ use constant EXTRA_DUMP_DIR => "$tdir/mssql_extra_dump";
 # for extra tests cleanup
 my $schema;
 
-my ($dsns, $common_version);
+my (%dsns, $common_version);
 
 for (qw/MSSQL MSSQL_ODBC MSSQL_ADO/) {
   next unless $ENV{"DBICTEST_${_}_DSN"};
 
-  $dsns->{$_}{dsn} = $ENV{"DBICTEST_${_}_DSN"};
-  $dsns->{$_}{user} = $ENV{"DBICTEST_${_}_USER"};
-  $dsns->{$_}{password} = $ENV{"DBICTEST_${_}_PASS"};
+  (my $dep_group = lc "rdbms_$_") =~ s/mssql$/mssql_sybase/;
+  if (!DBIx::Class::Optional::Dependencies->req_ok_for($dep_group)) {
+      diag 'You need to install ' . DBIx::Class::Optional::Dependencies->req_missing_for($dep_group)
+          . " to test with $_";
+      next;
+  }
+
+  $dsns{$_}{dsn} = $ENV{"DBICTEST_${_}_DSN"};
+  $dsns{$_}{user} = $ENV{"DBICTEST_${_}_USER"};
+  $dsns{$_}{password} = $ENV{"DBICTEST_${_}_PASS"};
 
   require DBI;
-  my $dbh = DBI->connect (@{$dsns->{$_}}{qw/dsn user password/}, { RaiseError => 1, PrintError => 0} );
+  my $dbh = DBI->connect (@{$dsns{$_}}{qw/dsn user password/}, { RaiseError => 1, PrintError => 0} );
   my $srv_ver = eval {
     $dbh->get_info(18)
       ||
@@ -51,7 +59,7 @@ for (qw/MSSQL MSSQL_ODBC MSSQL_ADO/) {
 }
 
 plan skip_all => 'You need to set the DBICTEST_MSSQL_DSN, _USER and _PASS and/or the DBICTEST_MSSQL_ODBC_DSN, _USER and _PASS environment variables'
-  unless $dsns;
+  unless %dsns;
 
 my $mssql_2008_new_data_types = {
   date     => { data_type => 'date' },
@@ -90,10 +98,11 @@ my $tester = dbixcsl_common_tests->new(
     vendor      => 'mssql',
     auto_inc_pk => 'INTEGER IDENTITY NOT NULL PRIMARY KEY',
     default_function_def => 'DATETIME DEFAULT getdate()',
-    connect_info => [values %$dsns],
+    connect_info => [ map { $dsns{$_} } sort keys %dsns ],
     preserve_case_mode_is_exclusive => 1,
     quote_char => [ qw/[ ]/ ],
     basic_date_datatype => ($common_version >= 10) ? 'DATE' : 'SMALLDATETIME',
+    default_on_clause => 'NO ACTION',
     data_types => {
         # http://msdn.microsoft.com/en-us/library/ms187752.aspx
 
@@ -200,6 +209,20 @@ my $tester = dbixcsl_common_tests->new(
                     [Five_Id] INT REFERENCES [MSSQL_Loader_Test5] ([Id])
                 )
             },
+            # 8 through 12 are used for the multi-schema tests and 13 through 16 are used for multi-db tests
+            q{
+                create table mssql_loader_test17 (
+                    id int identity primary key
+                )
+            },
+            q{
+                create table mssql_loader_test18 (
+                    id int identity primary key,
+                    seventeen_id int,
+                    foreign key (seventeen_id) references mssql_loader_test17(id)
+                        on delete set default on update set null
+                )
+            },
         ],
         pre_drop_ddl => [
             'CREATE TABLE mssql_loader_test3 (id INT IDENTITY NOT NULL PRIMARY KEY)',
@@ -210,8 +233,10 @@ my $tester = dbixcsl_common_tests->new(
             'mssql_loader_test3',
             'MSSQL_Loader_Test6',
             'MSSQL_Loader_Test5',
+            'mssql_loader_test17',
+            'mssql_loader_test18',
         ],
-        count  => 10 + 30 * 2 + 26 * 2, # extra + multi-schema + mutli-db
+        count  => 14 + 30 * 2 + 26 * 2, # extra + multi-schema + mutli-db
         run    => sub {
             my ($monikers, $classes, $self);
             ($schema, $monikers, $classes, $self) = @_;
@@ -286,6 +311,19 @@ my $tester = dbixcsl_common_tests->new(
             } qr/Can't find source/,
                 'no source registered for bad view';
 
+            # test on delete/update fk clause introspection
+            ok ((my $rel_info = $schema->source('MssqlLoaderTest18')->relationship_info('seventeen')),
+                'got rel info');
+
+            is $rel_info->{attrs}{on_delete}, 'SET DEFAULT',
+                'ON DELETE clause introspected correctly';
+
+            is $rel_info->{attrs}{on_update}, 'SET NULL',
+                'ON UPDATE clause introspected correctly';
+
+            is $rel_info->{attrs}{is_deferrable}, 1,
+                'is_deferrable defaults to 1';
+
             SKIP: {
                 my $dbh = $schema->storage->dbh;
 
@@ -362,7 +400,6 @@ EOF
                             {
                                 naming => 'current',
                                 db_schema => $db_schema,
-                                moniker_parts => [qw/schema name/],
                                 dump_directory => EXTRA_DUMP_DIR,
                                 quiet => 1,
                             },
@@ -381,7 +418,7 @@ EOF
                     } 'connected test schema';
 
                     lives_and {
-                        ok $rsrc = $test_schema->source('DbicslDashTestMssqlLoaderTest8');
+                        ok $rsrc = $test_schema->source('MssqlLoaderTest8');
                     } 'got source for table in schema name with dash';
 
                     is try { $rsrc->column_info('id')->{is_auto_increment} }, 1,
@@ -394,7 +431,7 @@ EOF
                         'column in schema name with dash';
 
                     lives_and {
-                        ok $rs = $test_schema->resultset('DbicslDashTestMssqlLoaderTest8');
+                        ok $rs = $test_schema->resultset('MssqlLoaderTest8');
                     } 'got resultset for table in schema name with dash';
 
                     lives_and {
@@ -428,7 +465,7 @@ EOF
                         'correct unique constraint in schema name with dash');
 
                     lives_and {
-                        ok $rsrc = $test_schema->source('DbicslDotTestMssqlLoaderTest10');
+                        ok $rsrc = $test_schema->source('MssqlLoaderTest10');
                     } 'got source for table in schema name with dot';
 
                     is try { $rsrc->column_info('id')->{is_auto_increment} }, 1,
@@ -441,7 +478,7 @@ EOF
                         'column in schema name with dot introspected correctly';
 
                     lives_and {
-                        ok $rs = $test_schema->resultset('DbicslDotTestMssqlLoaderTest10');
+                        ok $rs = $test_schema->resultset('MssqlLoaderTest10');
                     } 'got resultset for table in schema name with dot';
 
                     lives_and {
@@ -461,7 +498,7 @@ EOF
                         'relationship in schema name with dot';
 
                     lives_and {
-                        ok $rsrc = $test_schema->source('DbicslDotTestMssqlLoaderTest11');
+                        ok $rsrc = $test_schema->source('MssqlLoaderTest11');
                     } 'got source for table in schema name with dot';
 
                     %uniqs = try { $rsrc->unique_constraints };
@@ -475,28 +512,33 @@ EOF
                         'correct unique constraint in schema name with dot');
 
                     lives_and {
-                        ok $test_schema->source('DbicslDotTestMssqlLoaderTest10')
+                        ok $test_schema->source('MssqlLoaderTest10')
                             ->has_relationship('mssql_loader_test8');
                     } 'cross-schema relationship in multi-db_schema';
 
                     lives_and {
-                        ok $test_schema->source('DbicslDashTestMssqlLoaderTest8')
+                        ok $test_schema->source('MssqlLoaderTest8')
                             ->has_relationship('mssql_loader_test10s');
                     } 'cross-schema relationship in multi-db_schema';
 
                     lives_and {
-                        ok $test_schema->source('DbicslDashTestMssqlLoaderTest12')
+                        ok $test_schema->source('MssqlLoaderTest12')
                             ->has_relationship('mssql_loader_test11');
                     } 'cross-schema relationship in multi-db_schema';
 
                     lives_and {
-                        ok $test_schema->source('DbicslDotTestMssqlLoaderTest11')
+                        ok $test_schema->source('MssqlLoaderTest11')
                             ->has_relationship('mssql_loader_test12s');
                     } 'cross-schema relationship in multi-db_schema';
                 }
             }
 
             SKIP: {
+                # for ADO
+                local $SIG{__WARN__} = sigwarn_silencer(
+                    qr/Changed database context/
+                );
+
                 my $dbh = $schema->storage->dbh;
 
                 try {
@@ -504,6 +546,7 @@ EOF
                     $dbh->do('CREATE DATABASE dbicsl_test1');
                 }
                 catch {
+                    diag "no CREATE DATABASE privileges: '$_'";
                     skip "no CREATE DATABASE privileges", 26 * 2;
                 };
 
@@ -565,7 +608,6 @@ EOF
                             {
                                 naming => 'current',
                                 db_schema => $db_schema,
-                                moniker_parts => [qw/database name/],
                                 dump_directory => EXTRA_DUMP_DIR,
                                 quiet => 1,
                             },
@@ -586,7 +628,7 @@ EOF
                     my ($rsrc, $rs, $row, $rel_info, %uniqs);
 
                     lives_and {
-                        ok $rsrc = $test_schema->source('DbicslTest1MssqlLoaderTest13');
+                        ok $rsrc = $test_schema->source('MssqlLoaderTest13');
                     } 'got source for table in database one';
 
                     is try { $rsrc->column_info('id')->{is_auto_increment} }, 1,
@@ -599,7 +641,7 @@ EOF
                         'column in database one';
 
                     lives_and {
-                        ok $rs = $test_schema->resultset('DbicslTest1MssqlLoaderTest13');
+                        ok $rs = $test_schema->resultset('MssqlLoaderTest13');
                     } 'got resultset for table in database one';
 
                     lives_and {
@@ -633,7 +675,7 @@ EOF
                         'correct unique constraint in database one');
 
                     lives_and {
-                        ok $rsrc = $test_schema->source('DbicslTest2MssqlLoaderTest15');
+                        ok $rsrc = $test_schema->source('MssqlLoaderTest15');
                     } 'got source for table in database two';
 
                     is try { $rsrc->column_info('id')->{is_auto_increment} }, 1,
@@ -646,7 +688,7 @@ EOF
                         'column in database two introspected correctly';
 
                     lives_and {
-                        ok $rs = $test_schema->resultset('DbicslTest2MssqlLoaderTest15');
+                        ok $rs = $test_schema->resultset('MssqlLoaderTest15');
                     } 'got resultset for table in database two';
 
                     lives_and {
@@ -666,7 +708,7 @@ EOF
                         'relationship in database two';
 
                     lives_and {
-                        ok $rsrc = $test_schema->source('DbicslTest2MssqlLoaderTest16');
+                        ok $rsrc = $test_schema->source('MssqlLoaderTest16');
                     } 'got source for table in database two';
 
                     %uniqs = try { $rsrc->unique_constraints };
@@ -722,6 +764,11 @@ sub cleanup_schemas {
 sub cleanup_databases {
     return if $ENV{SCHEMA_LOADER_TESTS_NOCLEANUP};
 
+    # for ADO
+    local $SIG{__WARN__} = sigwarn_silencer(
+        qr/Changed database context/
+    );
+
     my $dbh = $schema->storage->dbh;
 
     $dbh->do('USE dbicsl_test1');