add test for bad view in mssql
Rafael Kitover [Mon, 11 Jan 2010 22:43:43 +0000 (22:43 +0000)]
lib/DBIx/Class/Schema/Loader/DBI.pm
t/25backcompat_v4.t
t/lib/dbixcsl_common_tests.pm
t/lib/dbixcsl_mssql_extra_tests.pm

index 726599c..2912247 100644 (file)
@@ -118,6 +118,14 @@ sub _filter_tables {
         }
         else {
             warn "Bad table or view '$table', ignoring: $@\n";
+            local $@;
+            eval {
+                my $schema = $self->schema;
+                # in older DBIC it's a private method
+                my $unregister = $schema->can('unregister_source')
+                    || $schema->can('_unregister_source');
+                $schema->$unregister($self->_table2moniker($table));
+            };
         }
     }
 
index 60362aa..5fc2511 100644 (file)
@@ -425,8 +425,7 @@ sub run_loader {
     foreach my $source_name ($schema->sources) {
         my $table_name = $schema->source($source_name)->from;
         $monikers{$table_name} = $source_name;
-        $classes{$table_name}  = "${SCHEMA_CLASS}::" . (
-            $loader_opts{use_namespaces} ? 'Result::' : '') . $source_name;
+        $classes{$table_name}  = $schema->source($source_name)->result_class;
     }
 
     return {
index 7687cde..85f0c78 100644 (file)
@@ -123,7 +123,7 @@ sub setup_schema {
 
        my $expected_count = 34;
 
-       $expected_count += @{ $self->{extra}{drop} || [] };
+       $expected_count += @{ $self->{extra}{create} || [] };
 
        $expected_count -= grep /CREATE TABLE/, @statements_inline_rels
            if $self->{no_inline_rels};
@@ -140,6 +140,10 @@ sub setup_schema {
 
        $warn_count++ for grep /^Bad table or view/, @loader_warnings;
 
+       my $vendor = $self->{vendor};
+       $warn_count++ for grep /${vendor}_\S+ has no primary key/,
+           @loader_warnings;
+
         if($self->{skip_rels}) {
             SKIP: {
                 is(scalar(@loader_warnings), $warn_count, "No loader warnings")
index ea38461..bafe790 100644 (file)
@@ -1,6 +1,7 @@
 package dbixcsl_mssql_extra_tests;
 
 use Test::More;
+use Test::Exception;
 
 my $vendor = 'mssql';
 
@@ -9,6 +10,9 @@ sub vendor {
     $vendor = shift;
 }
 
+# for cleanup in END
+my $saved_dbh;
+
 sub extra { +{
     create => [
         qq{
@@ -26,9 +30,22 @@ sub extra { +{
                 ts DATETIME DEFAULT getdate()
             )
         },
+        qq{
+            CREATE TABLE ${vendor}_loader_test3 (
+                id INT IDENTITY NOT NULL PRIMARY KEY
+            )
+        },
+        qq{
+            CREATE VIEW ${vendor}_loader_test4 AS
+            SELECT * FROM ${vendor}_loader_test3
+        },
+    ],
+    drop   => [
+        "[${vendor}_loader_test1.dot]",
+        "${vendor}_loader_test2",
+        "${vendor}_loader_test3"
     ],
-    drop   => [ "[${vendor}_loader_test1.dot]", "${vendor}_loader_test2"  ],
-    count  => 13,
+    count  => 15,
     run    => sub {
         my ($schema, $monikers, $classes) = @_;
 
@@ -84,7 +101,35 @@ 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.
+        $saved_dbh = $schema->storage->dbh;
+        $saved_dbh->do("DROP TABLE ${vendor}_loader_test3");
+
+        my @warnings;
+        {
+            local $SIG{__WARN__} = sub { push @warnings, $_[0] };
+            $schema->rescan;
+        }
+        ok ((grep /^Bad table or view '${vendor}_loader_test4'/, @warnings),
+            'bad view ignored');
+
+        throws_ok {
+            $schema->resultset("${vendor_titlecased}LoaderTest4")
+        } qr/Can't find source/,
+            'no source registered for bad view';
     },
 }}
 
+# Clean up the bad view, table will be cleaned up in drops
+END {
+    local $@;
+    eval {
+        $saved_dbh->do($_) for (
+"CREATE TABLE ${vendor}_loader_test3 (id INT IDENTITY NOT NULL PRIMARY KEY)",
+"DROP VIEW ${vendor}_loader_test4"
+        );
+    };
+}
+
 1;