fix new rel tests for older DBIC
[dbsrgits/DBIx-Class-Schema-Loader.git] / t / lib / dbixcsl_common_tests.pm
index 3e67dac..059979d 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;
@@ -85,7 +85,7 @@ sub _custom_column_info {
 sub run_tests {
     my $self = shift;
 
-    plan tests => 157 + ($self->{extra}->{count} || 0);
+    plan tests => 159 + ($self->{extra}->{count} || 0);
 
     $self->create();
 
@@ -99,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
@@ -132,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]); };
@@ -150,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} || [] };
 
@@ -169,14 +168,12 @@ 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;
 
        $warn_count++ for grep /^Bad table or view/, @loader_warnings;
 
-       $warn_count++ for grep /stripping trailing _id/, @loader_warnings;
+       $warn_count++ for grep /renaming \S+ relation/, @loader_warnings;
 
        my $vendor = $self->{vendor};
        $warn_count++ for grep /${vendor}_\S+ has no primary key/,
@@ -197,7 +194,9 @@ sub setup_schema {
                  "Missing PK warning");
         }
     }
-    
+
+    exit if $file_count != $expected_count;
+   
     return $schema_class;
 }
 
@@ -206,11 +205,16 @@ 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;
 
@@ -328,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" );
@@ -373,7 +377,7 @@ sub test_schema {
     );
 
     SKIP: {
-        skip $self->{skip_rels}, 99 if $self->{skip_rels};
+        skip $self->{skip_rels}, 101 if $self->{skip_rels};
 
         my $moniker3 = $monikers->{loader_test3};
         my $class3   = $classes->{loader_test3};
@@ -506,6 +510,13 @@ sub test_schema {
         my $rs_rel4 = $obj3->search_related('loader_test4zes');
         isa_ok( $rs_rel4->first, $class4);
 
+        # check rel naming with prepositions
+        ok ($rsobj4->result_source->has_relationship('loader_test5s_to'),
+            "rel with preposition 'to' pluralized correctly");
+
+        ok ($rsobj4->result_source->has_relationship('loader_test5s_from'),
+            "rel with preposition 'from' pluralized correctly");
+
         # find on multi-col pk
         my $obj5 = 
            eval { $rsobj5->find({id1 => 1, iD2 => 1}) } ||
@@ -653,9 +664,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: {
@@ -955,7 +966,11 @@ sub create {
                 id1 INTEGER NOT NULL,
                 iD2 INTEGER NOT NULL,
                 dat VARCHAR(8),
-                PRIMARY KEY (id1,iD2)
+                from_id INTEGER,
+                to_id INTEGER,
+                PRIMARY KEY (id1,iD2),
+                FOREIGN KEY (from_id) REFERENCES loader_test4 (id),
+                FOREIGN KEY (to_id) REFERENCES loader_test4 (id)
             ) $self->{innodb}
         },
 
@@ -1382,7 +1397,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 {};
 
@@ -1394,8 +1410,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);
@@ -1404,8 +1420,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;
 }