disable trailing 0 stripping for Informix defaults, as it is unsafe
[dbsrgits/DBIx-Class-Schema-Loader.git] / t / lib / dbixcsl_common_tests.pm
index c8fd70e..ab6d8cb 100644 (file)
@@ -4,6 +4,7 @@ use strict;
 use warnings;
 
 use Test::More;
+use Test::Exception;
 use DBIx::Class::Schema::Loader;
 use Class::Unload;
 use File::Path;
@@ -87,7 +88,7 @@ sub run_tests {
 
     my $extra_count = $self->{extra}{count} || 0;
 
-    plan tests => @connect_info * (174 + $extra_count + ($self->{data_type_tests}{test_count} || 0));
+    plan tests => @connect_info * (178 + $extra_count + ($self->{data_type_tests}{test_count} || 0));
 
     foreach my $info_idx (0..$#connect_info) {
         my $info = $connect_info[$info_idx];
@@ -107,33 +108,59 @@ sub run_tests {
 sub run_only_extra_tests {
     my ($self, $connect_info) = @_;
 
-    plan tests => @$connect_info * (4 + ($self->{extra}{count} || 0));
+    plan tests => @$connect_info * (4 + ($self->{extra}{count} || 0) + ($self->{data_type_tests}{test_count} || 0));
 
     foreach my $info_idx (0..$#$connect_info) {
         my $info = $connect_info->[$info_idx];
 
         @{$self}{qw/dsn user password connect_info_opts/} = @$info;
 
-        my $dbh = $self->dbconnect(0);
-        $dbh->do($_) for @{ $self->{extra}{create} || [] };
+        $self->drop_extra_tables_only;
+
+        my $dbh = $self->dbconnect(1);
+        {
+            # Silence annoying but harmless postgres "NOTICE:  CREATE TABLE..."
+            local $SIG{__WARN__} = sub {
+                my $msg = shift;
+                warn $msg unless $msg =~ m{^NOTICE:\s+CREATE TABLE};
+            };
+
+
+            $dbh->do($_) for @{ $self->{extra}{create} || [] };
+            $dbh->do($self->{data_type_tests}{ddl}) if $self->{data_type_tests}{ddl};
+        }
         $self->{_created} = 1;
 
-        my $result_count = grep /CREATE (?:TABLE|VIEW)/i, @{ $self->{extra}{create} || [] };
+        my $file_count = grep /CREATE (?:TABLE|VIEW)/i, @{ $self->{extra}{create} || [] };
+        $file_count++; # schema
+        $file_count++ if $self->{data_type_tests}{ddl};
 
-        my $schema_class = $self->setup_schema($info, $result_count + 1);
+        my $schema_class = $self->setup_schema($info, $file_count);
         my ($monikers, $classes) = $self->monikers_and_classes($schema_class);
         my $conn = $schema_class->clone;
 
+        $self->test_data_types($conn);
         $self->{extra}{run}->($conn, $monikers, $classes) if $self->{extra}{run};
 
         if (not ($ENV{SCHEMA_LOADER_TESTS_NOCLEANUP} && $info_idx == $#$connect_info)) {
-            $dbh->do($_) for @{ $self->{extra}{pre_drop_ddl} || [] };
-            $dbh->do("DROP TABLE $_") for @{ $self->{extra}{drop} || [] };
+            $self->drop_extra_tables_only;
             rmtree $DUMP_DIR;
         }
     }
 }
 
+sub drop_extra_tables_only {
+    my $self = shift;
+
+    my $dbh = $self->dbconnect(0);
+    $dbh->do($_) for @{ $self->{extra}{pre_drop_ddl} || [] };
+    $dbh->do("DROP TABLE $_") for @{ $self->{extra}{drop} || [] };
+
+    if (my $data_type_table = $self->{data_type_tests}{table_name}) {
+        $dbh->do("DROP TABLE $data_type_table");
+    }
+}
+
 # defined in sub create
 my (@statements, @statements_reltests, @statements_advanced,
     @statements_advanced_sqlite, @statements_inline_rels,
@@ -234,7 +261,8 @@ sub setup_schema {
         }
         else {
             SKIP: {
-                is scalar(@loader_warnings), $warn_count, 'Correct number of warnings';
+                is scalar(@loader_warnings), $warn_count, 'Correct number of warnings'
+                    or diag @loader_warnings;
                 skip "not testing standard sources", 1;
             }
         }
@@ -265,8 +293,8 @@ sub test_schema {
     my $rsobj2   = $conn->resultset($moniker2);
     check_no_duplicate_unique_constraints($class2);
 
-    my $moniker23 = $monikers->{LOADER_TEST23} || $monikers->{loader_test23};
-    my $class23   = $classes->{LOADER_TEST23}  || $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} || $monikers->{loader_test24};
@@ -284,7 +312,13 @@ sub test_schema {
     isa_ok( $rsobj35, "DBIx::Class::ResultSet" );
 
     my @columns_lt2 = $class2->columns;
-    is_deeply( \@columns_lt2, [ qw/id dat dat2/ ], "Column Ordering" );
+    is_deeply( \@columns_lt2, [ qw/id dat dat2 set_primary_key dbix_class_testcomponent/ ], "Column Ordering" );
+
+    is $class2->column_info('set_primary_key')->{accessor}, undef,
+        'accessor for column name that conflicts with a result base class method removed';
+
+    is $class2->column_info('dbix_class_testcomponent')->{accessor}, undef,
+        'accessor for column name that conflicts with a component class method removed';
 
     my %uniq1 = $class1->unique_constraints;
     my $uniq1_test = 0;
@@ -395,8 +429,8 @@ sub test_schema {
         'constant integer default',
     );
 
-    is(
-        $class35->column_info('a_double')->{default_value}, 10.555,
+    cmp_ok(
+        $class35->column_info('a_double')->{default_value}, '==', 10.555,
         'constant numeric default',
     );
 
@@ -828,7 +862,7 @@ sub test_schema {
     }
 
     # rescan and norewrite test
-    SKIP: {
+    {
         my @statements_rescan = (
             qq{
                 CREATE TABLE loader_test30 (
@@ -857,20 +891,20 @@ sub test_schema {
 
         my $before_digest = $digest->digest;
 
+        $conn->storage->disconnect; # needed for Firebird and Informix
         my $dbh = $self->dbconnect(1);
 
         {
             # Silence annoying but harmless postgres "NOTICE:  CREATE TABLE..."
             local $SIG{__WARN__} = sub {
                 my $msg = shift;
-                print STDERR $msg unless $msg =~ m{^NOTICE:\s+CREATE TABLE};
+                warn $msg unless $msg =~ m{^NOTICE:\s+CREATE TABLE};
             };
 
             $dbh->do($_) for @statements_rescan;
         }
 
         $dbh->disconnect;
-        $conn->storage->disconnect; # needed for Firebird
 
         sleep 1;
 
@@ -891,16 +925,43 @@ sub test_schema {
         my $rsobj30   = $conn->resultset('LoaderTest30');
         isa_ok($rsobj30, 'DBIx::Class::ResultSet');
 
-        skip 'no rels', 2 if $self->{skip_rels};
+        SKIP: {
+            skip 'no rels', 2 if $self->{skip_rels};
 
-        my $obj30 = $rsobj30->find(123);
-        isa_ok( $obj30->loader_test2, $class2);
+            my $obj30 = $rsobj30->find(123);
+            isa_ok( $obj30->loader_test2, $class2);
 
-        ok($rsobj30->result_source->column_info('loader_test2')->{is_foreign_key},
-           'Foreign key detected');
+            ok($rsobj30->result_source->column_info('loader_test2')->{is_foreign_key},
+               'Foreign key detected');
+        }
+
+        $conn->storage->disconnect; # for Firebird
+        $conn->storage->dbh->do("DROP TABLE loader_test30");
+
+        @new = do {
+            local $SIG{__WARN__} = sub {};
+            $conn->rescan;
+        };
+        is_deeply(\@new, [], 'no new tables on rescan');
+
+        throws_ok { $conn->resultset('LoaderTest30') }
+            qr/Can't find source/,
+            'source unregistered for dropped table after rescan';
     }
 
-    # test data types
+    $self->test_data_types($conn);
+
+    # run extra tests
+    $self->{extra}{run}->($conn, $monikers, $classes) if $self->{extra}{run};
+
+    $self->drop_tables unless $ENV{SCHEMA_LOADER_TESTS_NOCLEANUP};
+
+    $conn->storage->disconnect;
+}
+
+sub test_data_types {
+    my ($self, $conn) = @_;
+
     if ($self->{data_type_tests}{test_count}) {
         my $data_type_tests = $self->{data_type_tests};
         my $columns = $data_type_tests->{columns};
@@ -929,13 +990,6 @@ sub test_schema {
                 "test column $col_name has definition: $text_col_def expecting: $text_expected_info";
         }
     }
-
-    # run extra tests
-    $self->{extra}{run}->($conn, $monikers, $classes) if $self->{extra}{run};
-
-    $self->drop_tables unless $ENV{SCHEMA_LOADER_TESTS_NOCLEANUP};
-
-    $conn->storage->disconnect;
 }
 
 sub monikers_and_classes {
@@ -1017,11 +1071,14 @@ sub create {
         q{ INSERT INTO loader_test1s (dat) VALUES('bar') }, 
         q{ INSERT INTO loader_test1s (dat) VALUES('baz') }, 
 
+        # also test method collision
         qq{ 
             CREATE TABLE loader_test2 (
                 id $self->{auto_inc_pk},
                 dat VARCHAR(32) NOT NULL,
                 dat2 VARCHAR(32) NOT NULL,
+                set_primary_key INTEGER $self->{null},
+                dbix_class_testcomponent INTEGER $self->{null},
                 UNIQUE (dat2, dat)
             ) $self->{innodb}
         },
@@ -1033,7 +1090,7 @@ sub create {
         q{ INSERT INTO loader_test2 (dat, dat2) VALUES('ddd', 'www') }, 
 
         qq{
-            CREATE TABLE LOADER_TEST23 (
+            CREATE TABLE LOADER_test23 (
                 ID INTEGER NOT NULL PRIMARY KEY,
                 DAT VARCHAR(32) NOT NULL UNIQUE
             ) $self->{innodb}
@@ -1349,9 +1406,16 @@ sub create {
         },
         $make_auto_inc->(qw/loader_test11 id11/),
 
-        (q{ ALTER TABLE loader_test10 ADD CONSTRAINT } .
-         q{ loader_test11_fk FOREIGN KEY (loader_test11) } .
-         q{ REFERENCES loader_test11 (id11) }),
+        (lc($self->{vendor}) ne 'informix' ?
+            (q{ ALTER TABLE loader_test10 ADD CONSTRAINT loader_test11_fk } .
+             q{ FOREIGN KEY (loader_test11) } .
+             q{ REFERENCES loader_test11 (id11) })
+        :
+            (q{ ALTER TABLE loader_test10 ADD CONSTRAINT } .
+             q{ FOREIGN KEY (loader_test11) } .
+             q{ REFERENCES loader_test11 (id11) } .
+             q{ CONSTRAINT loader_test11_fk })
+        ),
     );
 
     @statements_advanced_sqlite = (
@@ -1430,7 +1494,7 @@ sub create {
     # Silence annoying but harmless postgres "NOTICE:  CREATE TABLE..."
     local $SIG{__WARN__} = sub {
         my $msg = shift;
-        print STDERR $msg unless $msg =~ m{^NOTICE:\s+CREATE TABLE};
+        warn $msg unless $msg =~ m{^NOTICE:\s+CREATE TABLE};
     };
 
     $dbh->do($_) for (@statements);
@@ -1466,7 +1530,7 @@ sub drop_tables {
     my @tables = qw/
         loader_test1s
         loader_test2
-        LOADER_TEST23
+        LOADER_test23
         LoAdEr_test24
         loader_test35
         loader_test36
@@ -1612,13 +1676,22 @@ sub setup_data_type_tests {
     my %seen_col_names;
 
     while (my ($col_def, $expected_info) = each %$types) {
-        (my $type_alias = lc($col_def)) =~ s/\( ([^)]+) \)//xg;
+        (my $type_alias = $col_def) =~ s/\( ([^)]+) \)//xg;
 
         my $size = $1;
         $size = '' unless defined $size;
         $size =~ s/\s+//g;
         my @size = split /,/, $size;
 
+        # some DBs don't like very long column names
+        if ($self->{vendor} =~ /^firebird|sqlanywhere\z/i) {
+            my ($col_def, $default) = $type_alias =~ /^(.*)(default.*)?\z/i;
+
+            $type_alias = substr $col_def, 0, 15;
+
+            $type_alias .= '_with_dflt' if $default;
+        }
+
         $type_alias =~ s/\s/_/g;
         $type_alias =~ s/\W//g;
 
@@ -1627,10 +1700,13 @@ sub setup_data_type_tests {
         if (@size) {
             my $size_name = join '_', apply { s/\W//g } @size;
 
-            $col_name .= "_with_size_$size_name";
+            $col_name .= "_sz_$size_name";
         }
 
-        $col_name .= "_$seen_col_names{$col_name}" if $seen_col_names{$col_name}++;
+        # XXX would be better to check _loader->preserve_case
+        $col_name = lc $col_name;
+
+        $col_name .= '_' . $seen_col_names{$col_name} if $seen_col_names{$col_name}++;
 
         $ddl .= "    $col_name $col_def,\n";