From 69c19e28b49d1732601c79c9aacf8de4b754b9d8 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Mon, 3 Dec 2012 15:27:46 +0100 Subject: [PATCH] Bug 7167: Adds Unit tests for C4::Update::Database Signed-off-by: Chris Nighswonger --- C4/Update/Database.pm | 1 - t/db_dependent/UpdateDatabase.t | 101 ++++++++++++++++++ .../update_database/versions/3.99.01.001.sql | 3 + .../update_database/versions/3.99.01.002.sql | 4 + .../update_database/versions/3.99.01.003.sql | 3 + .../update_database/versions/3.99.01.004.pl | 44 ++++++++ 6 files changed, 155 insertions(+), 1 deletion(-) create mode 100644 t/db_dependent/UpdateDatabase.t create mode 100644 t/db_dependent/data/update_database/versions/3.99.01.001.sql create mode 100644 t/db_dependent/data/update_database/versions/3.99.01.002.sql create mode 100644 t/db_dependent/data/update_database/versions/3.99.01.003.sql create mode 100644 t/db_dependent/data/update_database/versions/3.99.01.004.pl diff --git a/C4/Update/Database.pm b/C4/Update/Database.pm index 608e73b794..5bbc9503ec 100644 --- a/C4/Update/Database.pm +++ b/C4/Update/Database.pm @@ -51,7 +51,6 @@ our $dbh = C4::Context->dbh; sub get_versions_path { return C4::Context->config('intranetdir') . '/installer/data/mysql/versions'; - } =head2 get_filepath diff --git a/t/db_dependent/UpdateDatabase.t b/t/db_dependent/UpdateDatabase.t new file mode 100644 index 0000000000..f004ab327a --- /dev/null +++ b/t/db_dependent/UpdateDatabase.t @@ -0,0 +1,101 @@ +#!/usr/bin/perl + +use Modern::Perl; +use Test::More; +use Test::MockModule; +use C4::Context; +use Data::Dumper; + +BEGIN { + use_ok('C4::Update::Database'); +} + +clean_db(); + +my $already_applied_before = C4::Update::Database::list_versions_already_applied; +my $nb_applied_before = scalar @$already_applied_before; +my $nb_status_ok_before = grep {$_->{status}} @$already_applied_before; + +my $updatedatabasemodule = new Test::MockModule('C4::Update::Database'); +$updatedatabasemodule->mock('get_versions_path', sub {C4::Context->config('intranetdir') . '/t/db_dependent/data/update_database/versions'}); + +my $availables = C4::Update::Database::list_versions_available; +my $already_applied = C4::Update::Database::list_versions_already_applied; + +my $report; + +is ( scalar @$availables, 4, "There are 4 new available updates" ); + +$report = C4::Update::Database::execute_version( "3.99.01.001" ); +is ( $report->{'3.99.01.001'}, 'OK', "There is no error for the 1st version" ); + +$already_applied = C4::Update::Database::list_versions_already_applied; +is ( scalar @$already_applied, $nb_applied_before + 1, "There is 1 already applied version" ); + +$report = C4::Update::Database::execute_version( "3.99.01.001" ); +is ( $report->{"3.99.01.001"}{error}, 'ALREADY_EXISTS', "There is an 'already exist' error" ); +is ( $report->{"3.99.01.001"}{old_version}, '3.99.01.001', "This version had already executed in version 3.99.01.001" ); + + +my $queries = C4::Update::Database::get_queries( C4::Update::Database::get_filepath( "3.99.01.002" ) ); +my $expected = { + queries => [ + 'CREATE TABLE UpdateDatabase_testFOOBIS ( `version` varchar(32) DEFAULT NULL)' + ], + 'comments' => [ + 'This is a comment', + 'This is aanother comment' + ] +}; +is_deeply ( $queries, $expected, "The version 002 contains 1 query and 2 comments" ); + +$report = C4::Update::Database::execute_version( "3.99.01.002" ); +is ( $report->{'3.99.01.002'}, 'OK', "There is no error for the 2nd version" ); + +$report = C4::Update::Database::execute_version( "3.99.01.003" ); +$expected = { + '3.99.01.003' => [ + q{Error : 1050 => Table 'UpdateDatabase_testFOO' already exists} + ] +}; +is_deeply ( $report, $expected, "There is an error for the 3rd version" ); + +$report = C4::Update::Database::execute_version( "3.99.01.004" ); +is ( $report->{'3.99.01.004'}, 'OK', "There is no error for the 4th version" ); + + +$already_applied = C4::Update::Database::list_versions_already_applied; +is ( grep( {$_->{status}} @$already_applied ), $nb_status_ok_before + 3, "There are 3 new versions with a status OK" ); + +C4::Update::Database::mark_as_ok( "3.99.01.003" ); +$already_applied = C4::Update::Database::list_versions_already_applied; +is ( grep( {$_->{status}} @$already_applied ), $nb_status_ok_before + 4, "There are 4 new versions with a status OK" ); + + +clean_db(); + +sub clean_db { + my $dbh = C4::Context->dbh; + local $dbh->{PrintError} = 0; + local $dbh->{RaiseError} = 0; + $dbh->do( q{ + DELETE FROM updatedb_error WHERE version LIKE "3.99.01.%"; + }); + $dbh->do( q{ + DELETE FROM updatedb_query WHERE version LIKE "3.99.01.%"; + }); + $dbh->do( q{ + DELETE FROM updatedb_report WHERE version LIKE "3.99.01.%"; + }); + $dbh->do( q{ + DROP TABLE UpdateDatabase_testFOO; + }); + $dbh->do( q{ + DROP TABLE UpdateDatabase_testFOOBIS; + }); + $dbh->do( q{ + DELETE FROM systempreferences WHERE variable = "UpdateDatabase::testsyspref1" OR variable = "UpdateDatabase::testsyspref2" + }); +} + +done_testing; diff --git a/t/db_dependent/data/update_database/versions/3.99.01.001.sql b/t/db_dependent/data/update_database/versions/3.99.01.001.sql new file mode 100644 index 0000000000..c13c2e9faf --- /dev/null +++ b/t/db_dependent/data/update_database/versions/3.99.01.001.sql @@ -0,0 +1,3 @@ +-- This is a comment +DELIMITER ; +CREATE TABLE UpdateDatabase_testFOO ( `version` varchar(32) DEFAULT NULL); diff --git a/t/db_dependent/data/update_database/versions/3.99.01.002.sql b/t/db_dependent/data/update_database/versions/3.99.01.002.sql new file mode 100644 index 0000000000..291f71b4a9 --- /dev/null +++ b/t/db_dependent/data/update_database/versions/3.99.01.002.sql @@ -0,0 +1,4 @@ +-- This is a comment +-- This is aanother comment +DELIMITER ; +CREATE TABLE UpdateDatabase_testFOOBIS ( `version` varchar(32) DEFAULT NULL); diff --git a/t/db_dependent/data/update_database/versions/3.99.01.003.sql b/t/db_dependent/data/update_database/versions/3.99.01.003.sql new file mode 100644 index 0000000000..d43b8f2a00 --- /dev/null +++ b/t/db_dependent/data/update_database/versions/3.99.01.003.sql @@ -0,0 +1,3 @@ +-- table FOO again +DELIMITER ; +CREATE TABLE UpdateDatabase_testFOO ( `version` varchar(32) DEFAULT NULL); diff --git a/t/db_dependent/data/update_database/versions/3.99.01.004.pl b/t/db_dependent/data/update_database/versions/3.99.01.004.pl new file mode 100644 index 0000000000..b79f7de48c --- /dev/null +++ b/t/db_dependent/data/update_database/versions/3.99.01.004.pl @@ -0,0 +1,44 @@ +#!/usr/bin/perl + +# You write good Perl, so you start with Modern::Perl, of course +use Modern::Perl; + +# then you load Packages that could be usefull +use C4::Context; +# Loading this package is usefull if you need to check if a table exist (TableExists) +use C4::Update::Database; + +# you *must* have the sub _get_queries +# it returns an array of all SQL that have to be executed +# this array will be stored "forever" in your Koha database +# thus, you will be able to know which SQL has been executed +# at the time of upgrade. Very handy, because since then +# your database configuration may have changed and you'll wonder +# what has really be executed, not what would be executed today ! + +# put in an array the SQL to execute +# put in an array the comments +sub _get_queries { + my @queries; + my @comments; + push @comments, "Add sample feature"; + unless ( C4::Update::Database::TableExists('testtable') ) { + push @queries, qq{ + CREATE TABLE `UpdateDatabase_testtable` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `source` text DEFAULT NULL, + `text` mediumtext NOT NULL, + `timestamp` datetime NOT NULL, + PRIMARY KEY (`id`) + ) ENGINE=InnoDB DEFAULT CHARSET=utf8 + }; + push @comments, qq { * Added the table UpdateDatabase_testtable that did not exist}; + } + push @queries, qq{INSERT IGNORE INTO `systempreferences` (variable,value,explanation,options,type) VALUES('UpdateDatabase::testsyspref1',0,'Enable or disable display of Quote of the Day on the OPAC home page',NULL,'YesNo')}; + push @queries, qq{INSERT IGNORE INTO `systempreferences` (variable,value,explanation,options,type) VALUES('UpdateDatabase::testsyspref2',0,'Enable or disable display of Quote of the Day on the OPAC home page',NULL,'YesNo')}; + push @comments , qq{ * Added 2 sysprefs}; + +# return queries and comments + return { queries => \@queries, comments => \@comments }; +} +1; -- 2.39.5