From: Galen Charlton Date: Wed, 30 Apr 2008 20:45:43 +0000 (-0500) Subject: test framework - two improvements X-Git-Tag: v3.00.00-stableRC1~486 X-Git-Url: http://git.rot13.org/?a=commitdiff_plain;h=aad276c3cd88a25cfb229fe140ac8cf27c56b17d;p=koha.git 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 --- diff --git a/t/Makefile b/t/Makefile index 226b554ad9..cfe15d2fd7 100644 --- a/t/Makefile +++ b/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 diff --git a/t/database_dependent.pl b/t/database_dependent.pl index 6844a6d77b..57a848d054 100644 --- a/t/database_dependent.pl +++ b/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.