[Koha-patches] [PATCH] Bug 10337: Unit tests is based on an existing database

Srdjan srdjan at catalyst.net.nz
Fri Jul 19 09:55:20 CEST 2013


From: Jonathan Druart <jonathan.druart at biblibre.com>

This patchs adds a new unit tests file which will launched before all
others db_dependent tests.
It will drop and recreate the database with sample data.

Prerequisite:
The database 'koha_ut' have to be created with
  CREATE DATABASE $dbname CHARACTER SET utf8 COLLATE utf8_bin

http://bugs.koha-community.org/show_bug.cgi?id=10481
---
 t/db_dependent/000-init_db.t | 113 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 113 insertions(+)
 create mode 100644 t/db_dependent/000-init_db.t

diff --git a/t/db_dependent/000-init_db.t b/t/db_dependent/000-init_db.t
new file mode 100644
index 0000000..16d60f6
--- /dev/null
+++ b/t/db_dependent/000-init_db.t
@@ -0,0 +1,113 @@
+use Modern::Perl;
+use Test::More tests => 34;
+
+use C4::Context;
+use t::lib::Mocks;
+
+t::lib::Mocks::mock_config('database', 'koha_ut');
+
+
+my $koha_structure_file = C4::Context->config('intranetdir') . '/installer/data/mysql/kohastructure.sql';
+my $sample_files_optional = C4::Context->config('intranetdir') . '/installer/data/mysql/en/optional/*.sql';
+my $sample_files_mandatory = C4::Context->config('intranetdir') . '/installer/data/mysql/en/mandatory/*.sql';
+my $marc21_sample_files_mandatory = C4::Context->config('intranetdir') . '/installer/data/mysql/en/marcflavour/marc21/*/*.sql';
+my $unimarc_sample_files_mandatory = C4::Context->config('intranetdir') . '/installer/data/mysql/en/marcflavour/unimarc/mandatory/*.sql';
+my $sample_only_param_tables = C4::Context->config('intranetdir') . '/installer/data/mysql/sample_only_param_tables.sql';
+my $sysprefs = C4::Context->config('intranetdir') . '/installer/data/mysql/sysprefs.sql';
+
+my $version = get_version();
+
+our $dbh = C4::Context->dbh;
+our $user = C4::Context->config('user');
+our $pass = C4::Context->config('pass');
+our $dbname = C4::Context->config("database");
+
+# TODO All unit tests launch after this one should use the same database name
+# instead of the dbname defined in the $KOHA_CONF file
+# We have to mock C4::Context->config('database') with koha_ut everywhere
+die q{The SQL database name should be 'koha_ut'}
+    if $dbname ne 'koha_ut';
+
+recreate_db( $dbname );
+# Force C4::Context to recreate a new db handler
+$dbh->disconnect;
+$dbh = C4::Context->dbh;
+initialize_data();
+update_database();
+
+
+# We can't insert unimarc AND marc21 files
+#for my $f ( glob $unimarc_sample_files_mandatory ) {
+#    execute_sqlfile( $f, "inserting $f");
+#}
+
+
+# FIXME This file crashes
+#execute_sqlfile( $sample_only_param_tables, "inserting $sample_only_param_tables" );
+
+sub recreate_db {
+    my $dbname = shift;
+    ok( $dbh->do(qq{
+        DROP DATABASE $dbname
+    }), "drop database $dbname" );
+
+    is( $dbh->do(qq{
+        CREATE DATABASE $dbname CHARACTER SET utf8 COLLATE utf8_bin
+    }), 1, "create database $dbname" );
+}
+
+sub initialize_data {
+    execute_sqlfile( $koha_structure_file, "inserting koha db structure" );
+
+    for my $f ( glob $sample_files_mandatory ) {
+        execute_sqlfile( $f, "inserting $f");
+    }
+
+
+    for my $f ( glob $sample_files_optional ) {
+        execute_sqlfile( $f, "inserting $f");
+    }
+
+    for my $f ( glob $marc21_sample_files_mandatory ) {
+        execute_sqlfile( $f, "inserting $f");
+    }
+    execute_sqlfile( $sysprefs, "inserting $sysprefs");
+
+    # set marcflavour (MARC21)
+    $dbh->do( q{
+        INSERT INTO `systempreferences` (variable,value,explanation,options,type)
+        VALUES ('marcflavour','MARC21','Define global MARC flavor (MARC21 or UNIMARC) used for character encoding','MARC21|UNIMARC','Choice')
+    } );
+
+    # set version
+    $dbh->do( qq{
+        INSERT INTO systempreferences(variable, value, options, explanation, type)
+        VALUES ('Version', '$version', NULL, 'The Koha database version. WARNING: Do not change this value manually, it is maintained by the webinstaller', NULL)
+    } );
+}
+
+sub execute_sqlfile {
+    my ( $filepath, $msg ) = @_;
+    is( system( qq{/usr/bin/mysql -u $user -p'$pass' -D $dbname < $filepath} ), 0, $msg );
+}
+
+sub get_version {
+    do C4::Context->config('intranetdir') . '/kohaversion.pl';
+    my $version = kohaversion();
+    $version =~ s/(\d)\.(\d{2})\.(\d{2})\.(\d{3})/$1.$2$3$4/;
+    return $version;
+}
+
+sub update_database {
+    my $src_path = C4::Context->config('intranetdir');
+    my $update_db_path = $src_path . '/installer/data/mysql/updatedatabase.pl';
+
+    my $file = `cat $update_db_path`;
+    $file =~ s/exit;//;
+    eval $file;
+    if ($@) {
+        fail("updatedatabase.pl process failed: $@");
+    } else {
+        pass("updatedatabase.pl process succeeded.");
+    }
+}
-- 
1.8.1.2


More information about the Koha-patches mailing list