# Only MySQL uses this
$self->{innodb} ||= '';
+
+ # DB2 doesn't support this
+ $self->{null} = 'NULL' unless defined $self->{null};
$self->{verbose} = $ENV{TEST_VERBOSE} || 0;
sub run_tests {
my $self = shift;
- plan tests => 3 + 2 * (131 + ($self->{extra}->{count} || 0));
+ plan tests => 3 + 134 + ($self->{extra}->{count} || 0);
$self->create();
# First, with in-memory classes
my $schema_class = $self->setup_schema(@connect_info);
$self->test_schema($schema_class);
-
- # Then, with dumped classes
$self->drop_tables;
- $self->create;
- $self->{dump} = 1;
-
- unshift @INC, $DUMP_DIR;
- $self->reload_schema($schema_class);
- $schema_class->connection(@connect_info);
- $self->test_schema($schema_class);
}
sub setup_schema {
my $debug = ($self->{verbose} > 1) ? 1 : 0;
my %loader_opts = (
- constraint => qr/^(?:\S+\.)?(?:$self->{vendor}_)?loader_test[0-9]+$/i,
+ constraint => qr/^(?:\S+\.)?(?:$self->{vendor}_)?loader_test[0-9]+s?$/i,
relationships => 1,
additional_classes => 'TestAdditional',
additional_base_classes => 'TestAdditionalBase',
};
ok(!$@, "Loader initialization") or diag $@;
+
+ my $warn_count = 2;
+ $warn_count++ if grep /ResultSetManager/, @loader_warnings;
+
if($self->{skip_rels}) {
SKIP: {
- is(scalar(@loader_warnings), 2, "No loader warnings")
+ is(scalar(@loader_warnings), $warn_count, "No loader warnings")
or diag @loader_warnings;
skip "No missing PK warnings without rels", 1;
}
}
else {
- is(scalar(@loader_warnings), 3, "Expected loader warning")
+ $warn_count++;
+ is(scalar(@loader_warnings), $warn_count, "Expected loader warning")
or diag @loader_warnings;
- like($loader_warnings[0], qr/loader_test9 has no primary key/i,
+ is(grep(/loader_test9 has no primary key/, @loader_warnings), 1,
"Missing PK warning");
}
}
$classes->{$table_name} = $schema_class . q{::} . $source_name;
}
- my $moniker1 = $monikers->{loader_test1};
- my $class1 = $classes->{loader_test1};
+ my $moniker1 = $monikers->{loader_test1s};
+ my $class1 = $classes->{loader_test1s};
my $rsobj1 = $conn->resultset($moniker1);
+ check_no_duplicate_unique_constraints($class1);
my $moniker2 = $monikers->{loader_test2};
my $class2 = $classes->{loader_test2};
my $rsobj2 = $conn->resultset($moniker2);
+ check_no_duplicate_unique_constraints($class2);
my $moniker23 = $monikers->{LOADER_TEST23};
my $class23 = $classes->{LOADER_TEST23};
}
ok($uniq1_test, "Unique constraint");
+ is($moniker1, 'LoaderTest1', 'moniker singularisation');
+
my %uniq2 = $class2->unique_constraints;
my $uniq2_test = 0;
foreach my $ucname (keys %uniq2) {
isa_ok( $rs_rel4->first, $class4);
# find on multi-col pk
- my $obj5 = $rsobj5->find({id1 => 1, id2 => 1});
+ my $obj5 =
+ eval { $rsobj5->find({id1 => 1, iD2 => 1}) } ||
+ eval { $rsobj5->find({id1 => 1, id2 => 1}) };
+ die $@ if $@;
+
is( $obj5->id2, 1, "Find on multi-col PK" );
# mulit-col fk def
isa_ok( $obj6->loader_test2, $class2);
isa_ok( $obj6->loader_test5, $class5);
- ok($class6->column_info('loader_test2')->{is_foreign_key}, 'Foreign key detected');
+ ok($class6->column_info('loader_test2_id')->{is_foreign_key}, 'Foreign key detected');
ok($class6->column_info('id')->{is_foreign_key}, 'Foreign key detected');
- ok($class6->column_info('id2')->{is_foreign_key}, 'Foreign key detected');
+
+ my $id2_info = eval { $class6->column_info('id2') } ||
+ $class6->column_info('Id2');
+ ok($id2_info->{is_foreign_key}, 'Foreign key detected');
# fk that references a non-pk key (UNIQUE)
my $obj8 = $rsobj8->find(1);
$self->{extra}->{run}->($conn, $monikers, $classes) if $self->{extra}->{run};
}
+sub check_no_duplicate_unique_constraints {
+ my ($class) = @_;
+
+ # unique_constraints() automatically includes the PK, if any
+ my %uc_cols;
+ ++$uc_cols{ join ", ", @$_ }
+ for values %{ { $class->unique_constraints } };
+ my $dup_uc = grep { $_ > 1 } values %uc_cols;
+
+ is($dup_uc, 0, "duplicate unique constraints ($class)")
+ or diag "uc_cols: @{[ %uc_cols ]}";
+}
+
sub dbconnect {
my ($self, $complain) = @_;
my $make_auto_inc = $self->{auto_inc_cb} || sub {};
my @statements = (
qq{
- CREATE TABLE loader_test1 (
+ CREATE TABLE loader_test1s (
id $self->{auto_inc_pk},
dat VARCHAR(32) NOT NULL UNIQUE
) $self->{innodb}
},
- $make_auto_inc->(qw/loader_test1 id/),
+ $make_auto_inc->(qw/loader_test1s id/),
- q{ INSERT INTO loader_test1 (dat) VALUES('foo') },
- q{ INSERT INTO loader_test1 (dat) VALUES('bar') },
- q{ INSERT INTO loader_test1 (dat) VALUES('baz') },
+ q{ INSERT INTO loader_test1s (dat) VALUES('foo') },
+ q{ INSERT INTO loader_test1s (dat) VALUES('bar') },
+ q{ INSERT INTO loader_test1s (dat) VALUES('baz') },
qq{
CREATE TABLE loader_test2 (
id1 INTEGER NOT NULL,
iD2 INTEGER NOT NULL,
dat VARCHAR(8),
- PRIMARY KEY (id1,id2)
+ PRIMARY KEY (id1,iD2)
) $self->{innodb}
},
- q{ INSERT INTO loader_test5 (id1,id2,dat) VALUES (1,1,'aaa') },
+ q{ INSERT INTO loader_test5 (id1,iD2,dat) VALUES (1,1,'aaa') },
qq{
CREATE TABLE loader_test6 (
id INTEGER NOT NULL PRIMARY KEY,
Id2 INTEGER,
- loader_test2 INTEGER,
+ loader_test2_id INTEGER,
dat VARCHAR(8),
- FOREIGN KEY (loader_test2) REFERENCES loader_test2 (id),
+ FOREIGN KEY (loader_test2_id) REFERENCES loader_test2 (id),
FOREIGN KEY(id,Id2) REFERENCES loader_test5 (id1,iD2)
) $self->{innodb}
},
- (q{ INSERT INTO loader_test6 (id, id2,loader_test2,dat) } .
+ (q{ INSERT INTO loader_test6 (id, Id2,loader_test2_id,dat) } .
q{ VALUES (1, 1,1,'aaa') }),
qq{
CREATE TABLE loader_test32 (
id INTEGER NOT NULL PRIMARY KEY,
rel1 INTEGER NOT NULL,
- rel2 INTEGER,
+ rel2 INTEGER $self->{null},
FOREIGN KEY (rel1) REFERENCES loader_test31(id),
FOREIGN KEY (rel2) REFERENCES loader_test31(id)
) $self->{innodb}
CREATE TABLE loader_test34 (
id INTEGER NOT NULL PRIMARY KEY,
rel1 INTEGER NOT NULL,
- rel2 INTEGER,
+ rel2 INTEGER $self->{null},
FOREIGN KEY (id,rel1) REFERENCES loader_test33(id1,id2),
FOREIGN KEY (id,rel2) REFERENCES loader_test33(id1,id2)
) $self->{innodb}
CREATE TABLE loader_test10 (
id10 $self->{auto_inc_pk},
subject VARCHAR(8),
- loader_test11 INTEGER
+ loader_test11 INTEGER $self->{null}
) $self->{innodb}
},
$make_auto_inc->(qw/loader_test10 id10/),
CREATE TABLE loader_test11 (
id11 $self->{auto_inc_pk},
message VARCHAR(8) DEFAULT 'foo',
- loader_test10 INTEGER,
+ loader_test10 INTEGER $self->{null},
FOREIGN KEY (loader_test10) REFERENCES loader_test10 (id10)
) $self->{innodb}
},
my $self = shift;
my @tables = qw/
- loader_test1
+ loader_test1s
loader_test2
LOADER_TEST23
LoAdEr_test24
/;
my @tables_auto_inc = (
- [ qw/loader_test1 id/ ],
+ [ qw/loader_test1s id/ ],
[ qw/loader_test2 id/ ],
);
$dbh->disconnect;
}
-sub reload_schema {
- my ($self, $schema) = @_;
-
- for my $source ($schema->sources) {
- Class::Unload->unload( $schema->class( $source ) );
- Class::Unload->unload( ref $schema->resultset( $source ) );
- }
-
- Class::Unload->unload( $schema );
- eval "require $schema" or die $@;
-}
-
sub DESTROY {
my $self = shift;
- $self->drop_tables if $self->{_created};
- rmtree $DUMP_DIR;
+ unless ($ENV{SCHEMA_LOADER_TESTS_NOCLEANUP}) {
+ $self->drop_tables if $self->{_created};
+ rmtree $DUMP_DIR
+ }
}
1;