mssql: clean up extra tests
[dbsrgits/DBIx-Class-Schema-Loader.git] / t / lib / dbixcsl_mssql_extra_tests.pm
index 4319db8..e4767f9 100644 (file)
@@ -1,40 +1,42 @@
 package dbixcsl_mssql_extra_tests;
 
+use strict;
+use warnings;
 use Test::More;
+use Test::Exception;
 
-my $vendor = 'mssql';
-
-sub vendor {
-    shift;
-    $vendor = shift;
-}
+# for cleanup in END
+my $storage;
 
 sub extra { +{
     create => [
-        qq{
-            CREATE TABLE [${vendor}_loader_test1.dot] (
+        q{
+            CREATE TABLE [mssql_loader_test1.dot] (
                 id INT IDENTITY NOT NULL PRIMARY KEY,
                 dat VARCHAR(8)
             )
         },
-        qq{
-            CREATE TABLE ${vendor}_loader_test2 (
-                id INT IDENTITY NOT NULL PRIMARY KEY,
-                dat VARCHAR(100) DEFAULT 'foo',
-                ts DATETIME DEFAULT getdate()
+        q{
+            CREATE TABLE mssql_loader_test3 (
+                id INT IDENTITY NOT NULL PRIMARY KEY
             )
         },
+        q{
+            CREATE VIEW mssql_loader_test4 AS
+            SELECT * FROM mssql_loader_test3
+        },
+    ],
+    drop   => [
+        "[mssql_loader_test1.dot]",
+        "mssql_loader_test3"
     ],
-    drop   => [ "[${vendor}_loader_test1.dot]", "${vendor}_loader_test2"  ],
-    count  => 11,
+    count  => 8,
     run    => sub {
         my ($schema, $monikers, $classes) = @_;
 
 # Test that the table above (with '.' in name) gets loaded correctly.
-        my $vendor_titlecased = "\u\L$vendor";
-
         ok((my $rs = eval {
-            $schema->resultset("${vendor_titlecased}LoaderTest1Dot") }),
+            $schema->resultset($monikers->{'[mssql_loader_test1.dot]'}) }),
             'got a resultset for table with dot in name');
 
         ok((my $from = eval { $rs->result_source->from }),
@@ -42,32 +44,12 @@ sub extra { +{
 
         is ref($from), 'SCALAR', '->table with dot in name is a scalar ref';
 
-        is eval { $$from }, "[${vendor}_loader_test1.dot]",
+        is eval { $$from }, "[mssql_loader_test1.dot]",
             '->table with dot in name has correct name';
 
-# Test that column defaults are set correctly
-        ok(($rs = eval {
-            $schema->resultset("${vendor_titlecased}LoaderTest2") }),
-            'got a resultset for table with column with default value');
-
-        my $rsrc = $rs->result_source;
-
-        is eval { $rsrc->column_info('dat')->{default_value} }, 'foo',
-            'correct default_value for column with literal default';
-
-        ok((my $function_default =
-            eval { $rsrc->column_info('ts')->{default_value} }),
-            'got default_value for column with function default');
-
-        is ref($function_default), 'SCALAR',
-            'default_value for function default is a SCALAR ref';
-
-        is eval { $$function_default }, 'getdate()',
-            'default_value for function default is correct';
-
 # Test that identity columns do not have 'identity' in the data_type, and do
 # have is_auto_increment.
-        my $identity_col_info = $schema->resultset('LoaderTest10')
+        my $identity_col_info = $schema->resultset($monikers->{loader_test10})
             ->result_source->column_info('id10');
 
         is $identity_col_info->{data_type}, 'int',
@@ -75,7 +57,39 @@ sub extra { +{
 
         is $identity_col_info->{is_auto_increment}, 1,
             q{'INT IDENTITY' column has is_auto_increment => 1};
+
+# Test that a bad view (where underlying table is gone) is ignored.
+        $storage = $schema->storage;
+
+        my $dbh = $storage->dbh;
+        $dbh->do("DROP TABLE mssql_loader_test3");
+
+        my @warnings;
+        {
+            local $SIG{__WARN__} = sub { push @warnings, $_[0] };
+            $schema->rescan;
+        }
+        ok ((grep /^Bad table or view 'mssql_loader_test4'/, @warnings),
+            'bad view ignored');
+
+        throws_ok {
+            $schema->resultset($monikers->{mssql_loader_test4})
+        } qr/Can't find source/,
+            'no source registered for bad view';
     },
 }}
 
+# Clean up the bad view
+END {
+    local $@;
+    eval {
+        my $dbh = $storage->dbh;
+        $dbh->do($_) for (
+"CREATE TABLE mssql_loader_test3 (id INT IDENTITY NOT NULL PRIMARY KEY)",
+"DROP VIEW mssql_loader_test4",
+"DROP TABLE mssql_loader_test3",
+        );
+    };
+}
+
 1;