Browse Source

test framework - two improvements

[1] When running the database-dependent tests (cd t, make test),
    all tables in the test database are dropped prior to running
    the installer and test cases.  This means that the test
    database will start with a clean slate.
[2] It is now possible to specify a single test class to run,
    to avoid having to run all of them:

    cd t
    make test TEST_CLASS=Search

    To run all DB-dependent tests, just do the usual

    cd t
    make test

Signed-off-by: Joshua Ferraro <jmf@liblime.com>
3.0.x
Galen Charlton 16 years ago
committed by Joshua Ferraro
parent
commit
aad276c3cd
  1. 3
      t/Makefile
  2. 71
      t/database_dependent.pl

3
t/Makefile

@ -12,6 +12,7 @@ CHMOD = chmod
PERL = /usr/bin/perl
# TEST_FILES = *.t
TEST_FILES = database_dependent.pl
TEST_CLASS =
PROVE = /usr/bin/prove
PROVE_FLAGS = -v
PERL5LIB = ..
@ -57,7 +58,7 @@ $(SCRIPTS) ::
$(CHMOD) 755 $(TEST_SCRIPT_DIR)/$@
test :: config_file $(ZEBRA_CONF_FILES) $(SCRIPTS)
KOHA_CONF=$(TEST_CONF_FILE) PERL5LIB=$(PERL5LIB) $(PROVE) $(PROVE_FLAGS) $(TEST_FILES)
KOHA_CONF=$(TEST_CONF_FILE) PERL5LIB=$(PERL5LIB) TEST_CLASS=$(TEST_CLASS) $(PROVE) $(PROVE_FLAGS) $(TEST_FILES)
test_run_dirs ::
$(MKPATH) run/etc

71
t/database_dependent.pl

@ -17,18 +17,87 @@ use Test::More;
use Test::Class::Load qw ( . ); # run from the t directory
clear_test_database();
create_test_database();
start_zebrasrv();
start_zebraqueue_daemon();
Test::Class->runtests;
if ($ENV{'TEST_CLASS'}) {
# assume only one test class is specified;
# should extend to allow multiples, but that will
# mean changing how test classes are loaded.
eval "KohaTest::$ENV{'TEST_CLASS'}->runtests";
} else {
Test::Class->runtests;
}
stop_zebraqueue_daemon();
stop_zebrasrv();
# stop_zebrasrv();
=head3 clear_test_database
removes all tables from test database so that install starts with a clean slate
=cut
sub clear_test_database {
diag "removing tables from test database";
my $dbh = C4::Context->dbh;
my $schema = C4::Context->config("database");
my @tables = get_all_tables($dbh, $schema);
foreach my $table (@tables) {
drop_all_foreign_keys($dbh, $table);
}
foreach my $table (@tables) {
drop_table($dbh, $table);
}
}
sub get_all_tables {
my ($dbh, $schema) = @_;
my $sth = $dbh->prepare("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = ?");
my @tables = ();
$sth->execute($schema);
while (my ($table) = $sth->fetchrow_array) {
push @tables, $table;
}
$sth->finish;
return @tables;
}
sub drop_all_foreign_keys {
my ($dbh, $table) = @_;
# get the table description
my $sth = $dbh->prepare("SHOW CREATE TABLE $table");
$sth->execute;
my $vsc_structure = $sth->fetchrow;
# split on CONSTRAINT keyword
my @fks = split /CONSTRAINT /,$vsc_structure;
# parse each entry
foreach (@fks) {
# isolate what is before FOREIGN KEY, if there is something, it's a foreign key to drop
$_ = /(.*) FOREIGN KEY.*/;
my $id = $1;
if ($id) {
# we have found 1 foreign, drop it
$dbh->do("ALTER TABLE $table DROP FOREIGN KEY $id");
$id="";
}
}
}
sub drop_table {
my ($dbh, $table) = @_;
$dbh->do("DROP TABLE $table");
}
=head3 create_test_database
sets up the test database.

Loading…
Cancel
Save