Firebird: unquoted_ddl option. Revert wrapped dbic code in common tests.
[dbsrgits/DBIx-Class-Schema-Loader.git] / t / lib / dbixcsl_common_tests.pm
index b56fcf5..7e25122 100644 (file)
@@ -30,7 +30,7 @@ sub new {
     # Only MySQL uses this
     $self->{innodb} ||= '';
 
-    # DB2 doesn't support this
+    # DB2 and Firebird don't support 'field type NULL'
     $self->{null} = 'NULL' unless defined $self->{null};
     
     $self->{verbose} = $ENV{TEST_VERBOSE} || 0;
@@ -38,6 +38,8 @@ sub new {
     # Optional extra tables and tests
     $self->{extra} ||= {};
 
+    $self->{date_datatype} ||= 'DATE';
+
     # Not all DBS do SQL-standard CURRENT_TIMESTAMP
     $self->{default_function} ||= "CURRENT_TIMESTAMP";
     $self->{default_function_def} ||= "TIMESTAMP DEFAULT $self->{default_function}";
@@ -97,7 +99,6 @@ sub run_tests {
     # First, with in-memory classes
     my $schema_class = $self->setup_schema(@connect_info);
     $self->test_schema($schema_class);
-    $self->drop_tables;
 }
 
 # defined in sub create
@@ -130,11 +131,14 @@ sub setup_schema {
         use_namespaces          => 0,
         dump_directory          => $DUMP_DIR,
         datetime_timezone       => 'Europe/Berlin',
-        datetime_locale         => 'de_DE'
+        datetime_locale         => 'de_DE',
+        %{ $self->{loader_options} || {} },
     );
 
     $loader_opts{db_schema} = $self->{db_schema} if $self->{db_schema};
 
+    my $file_count;
+    my $expected_count = 36;
     {
        my @loader_warnings;
        local $SIG{__WARN__} = sub { push(@loader_warnings, $_[0]); };
@@ -148,11 +152,8 @@ sub setup_schema {
 
        ok(!$@, "Loader initialization") or diag $@;
 
-       my $file_count;
        find sub { return if -d; $file_count++ }, $DUMP_DIR;
 
-       my $expected_count = 36;
-
        $expected_count += grep /CREATE (?:TABLE|VIEW)/i,
            @{ $self->{extra}{create} || [] };
 
@@ -167,8 +168,6 @@ sub setup_schema {
 
        is $file_count, $expected_count, 'correct number of files generated';
 
-       exit if $file_count != $expected_count;
-
        my $warn_count = 2;
        $warn_count++ if grep /ResultSetManager/, @loader_warnings;
 
@@ -191,11 +190,13 @@ sub setup_schema {
            $warn_count++;
             is(scalar(@loader_warnings), $warn_count, "Expected loader warning")
                 or diag @loader_warnings;
-            is(grep(/loader_test9 has no primary key/, @loader_warnings), 1,
+            is(grep(/loader_test9 has no primary key/i, @loader_warnings), 1,
                  "Missing PK warning");
         }
     }
-    
+
+    exit if $file_count != $expected_count;
+   
     return $schema_class;
 }
 
@@ -204,12 +205,22 @@ sub test_schema {
     my $schema_class = shift;
 
     my $conn = $schema_class->clone;
+
+    ($self->{before_tests_run} || sub {})->($conn);
+
     my $monikers = {};
     my $classes = {};
     foreach my $source_name ($schema_class->sources) {
         my $table_name = $schema_class->source($source_name)->from;
+
+        $table_name = $$table_name if ref $table_name;
+
         $monikers->{$table_name} = $source_name;
         $classes->{$table_name} = $schema_class . q{::} . $source_name;
+
+        # some DBs (Firebird) uppercase everything
+        $monikers->{lc $table_name} = $source_name;
+        $classes->{lc $table_name} = $schema_class . q{::} . $source_name;
     }
 
     my $moniker1 = $monikers->{loader_test1s};
@@ -222,12 +233,12 @@ sub test_schema {
     my $rsobj2   = $conn->resultset($moniker2);
     check_no_duplicate_unique_constraints($class2);
 
-    my $moniker23 = $monikers->{LOADER_TEST23};
-    my $class23   = $classes->{LOADER_TEST23};
+    my $moniker23 = $monikers->{LOADER_TEST23} || $monikers->{loader_test23};
+    my $class23   = $classes->{LOADER_TEST23}  || $classes->{loader_test23};
     my $rsobj23   = $conn->resultset($moniker1);
 
-    my $moniker24 = $monikers->{LoAdEr_test24};
-    my $class24   = $classes->{LoAdEr_test24};
+    my $moniker24 = $monikers->{LoAdEr_test24} || $monikers->{loader_test24};
+    my $class24   = $classes->{LoAdEr_test24}  || $classes->{loader_test24};
     my $rsobj24   = $conn->resultset($moniker2);
 
     my $moniker35 = $monikers->{loader_test35};
@@ -321,7 +332,7 @@ sub test_schema {
         is( $rsobj1->loader_test1_rsmeth, 'all is still well', 'Result set method' );
     }
     
-    ok( $class1->column_info('id')->{is_auto_increment}, 'is_auto_incrment detection' );
+    ok( $class1->column_info('id')->{is_auto_increment}, 'is_auto_increment detection' );
 
     my $obj    = $rsobj1->find(1);
     is( $obj->id,  1, "Find got the right row" );
@@ -646,9 +657,9 @@ sub test_schema {
 
         eval {
             my $obj10_2 = $obj11->loader_test10;
-            $obj10_2->loader_test11( $obj11->id11() );
-            $obj10_2->update();
+            $obj10_2->update({ loader_test11 => $obj11->id11 });
         };
+        diag $@ if $@;
         ok(!$@, "Setting up circular relationship");
 
         SKIP: {
@@ -774,6 +785,7 @@ sub test_schema {
         }
 
         $dbh->disconnect;
+        $conn->storage->disconnect; # needed for Firebird
 
         sleep 1;
 
@@ -804,6 +816,8 @@ sub test_schema {
     }
 
     $self->{extra}->{run}->($conn, $monikers, $classes) if $self->{extra}->{run};
+
+    $conn->storage->disconnect;
 }
 
 sub check_no_duplicate_unique_constraints {
@@ -822,17 +836,25 @@ sub check_no_duplicate_unique_constraints {
 sub dbconnect {
     my ($self, $complain) = @_;
 
-    my $dbh = DBI->connect(
-         $self->{dsn}, $self->{user},
-         $self->{password},
-         {
-             RaiseError => $complain,
-             PrintError => $complain,
-             AutoCommit => 1,
-         }
-    );
+    require DBIx::Class::Storage::DBI;
+    my $storage = DBIx::Class::Storage::DBI->new;
+
+    $complain = defined $complain ? $complain : 1;
+
+    $storage->connect_info([
+        @{ $self }{qw/dsn user password/},
+        {
+            unsafe => 1,
+            RaiseError => $complain,
+            ShowErrorStatement => $complain,
+            PrintError => 0,
+        },
+    ]);
+
+    my $dbh = eval { $storage->dbh };
+    die "Failed to connect to database: $@" if !$dbh;
 
-    die "Failed to connect to database: $DBI::errstr" if !$dbh;
+    $self->{storage} = $storage; # storage DESTROY disconnects
 
     return $dbh;
 }
@@ -898,7 +920,7 @@ sub create {
         qq{
             CREATE TABLE loader_test36 (
                 id INTEGER NOT NULL PRIMARY KEY,
-                a_date DATE,
+                a_date $self->{date_datatype},
                 b_char_as_data VARCHAR(100),
                 c_char_as_data VARCHAR(100)
             ) $self->{innodb}
@@ -1364,7 +1386,8 @@ sub drop_tables {
 
     my $dbh = $self->dbconnect(0);
 
-    $dbh->do("DROP TABLE $_") for @{ $self->{extra}->{drop} || [] };
+    $dbh->do($_) for @{ $self->{extra}{pre_drop_ddl} || [] };
+    $dbh->do("DROP TABLE $_") for @{ $self->{extra}{drop} || [] };
 
     my $drop_auto_inc = $self->{auto_inc_drop_cb} || sub {};
 
@@ -1376,8 +1399,8 @@ sub drop_tables {
         else {
             $dbh->do($drop_fk);
         }
-        $dbh->do("DROP TABLE $_") for (@tables_advanced);
         $dbh->do($_) for map { $drop_auto_inc->(@$_) } @tables_advanced_auto_inc;
+        $dbh->do("DROP TABLE $_") for (@tables_advanced);
 
         unless($self->{no_inline_rels}) {
             $dbh->do("DROP TABLE $_") for (@tables_inline_rels);
@@ -1386,8 +1409,13 @@ sub drop_tables {
             $dbh->do("DROP TABLE $_") for (@tables_implicit_rels);
         }
     }
-    $dbh->do("DROP TABLE $_") for (@tables, @tables_rescan);
     $dbh->do($_) for map { $drop_auto_inc->(@$_) } @tables_auto_inc;
+    $dbh->do("DROP TABLE $_") for (@tables, @tables_rescan);
+    $dbh->disconnect;
+
+# fixup for Firebird
+    $dbh = $self->dbconnect(0);
+    $dbh->do('DROP TABLE loader_test2');
     $dbh->disconnect;
 }