[Koha-patches] [PATCH] test framework - two improvements

Galen Charlton galen.charlton at liblime.com
Wed Apr 30 22:45:43 CEST 2008


[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
---
 t/Makefile              |    3 +-
 t/database_dependent.pl |   71 ++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 72 insertions(+), 2 deletions(-)

diff --git a/t/Makefile b/t/Makefile
index 226b554..cfe15d2 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 6844a6d..57a848d 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.
-- 
1.5.5.rc0.16.g02b00




More information about the Koha-patches mailing list