From 32f769155d9990eed781cff98bb303c5b14647db 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 --- C4/Update/Database.pm | 16 +++- t/db_dependent/UpdateDatabase.t | 99 ++++++++++++++++++++ .../data/update_database/versions/3.99.01.001.sql | 3 + .../data/update_database/versions/3.99.01.002.sql | 4 + .../data/update_database/versions/3.99.01.003.sql | 3 + .../data/update_database/versions/3.99.01.004.pl | 44 +++++++++ 6 files changed, 166 insertions(+), 3 deletions(-) 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 41f0ee3..07cdfc1 100644 --- a/C4/Update/Database.pm +++ b/C4/Update/Database.pm @@ -48,7 +48,17 @@ my $list; my $dbh = C4::Context->dbh; -=head2 +=head2 get_versions_path + + return the path to the version files + +=cut + +sub get_versions_path { + return $VERSIONS_PATH; +} + +=head2 get_filepath my $file = get_filepath($version); this sub will return the full path of a given DB update number @@ -57,7 +67,7 @@ my $dbh = C4::Context->dbh; sub get_filepath { my ( $version ) = @_; - my @files = File::Find::Rule->file->name( "$version.sql", "$version.pl" ) ->in( ( $VERSIONS_PATH ) ); + my @files = File::Find::Rule->file->name( "$version.sql", "$version.pl" ) ->in( ( get_versions_path() ) ); if ( scalar @files != 1 ) { die "This version ($version) returned has ".scalar @files." corresponding, need only 1"; @@ -168,7 +178,7 @@ sub execute_version { sub list_versions_available { my @versions; - my @files = File::Find::Rule->file->name( "*.sql", "*.pl" ) ->in( ( $VERSIONS_PATH ) ); + my @files = File::Find::Rule->file->name( "*.sql", "*.pl" ) ->in( ( get_versions_path() ) ); for my $f ( @files ) { my @file_infos = fileparse( $f, qr/\.[^.]*/ ); diff --git a/t/db_dependent/UpdateDatabase.t b/t/db_dependent/UpdateDatabase.t new file mode 100644 index 0000000..163d9f9 --- /dev/null +++ b/t/db_dependent/UpdateDatabase.t @@ -0,0 +1,99 @@ +#!/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 $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 ( C4::Update::Database::is_uptodate(), 0, "The database is not up-to-date" ); + +is ( scalar @$availables, 4, "There are 4 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, 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 ), 3, "There are 3 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 ), 4, "There are 4 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; + }); + $dbh->do( q{ + DELETE FROM updatedb_query; + }); + $dbh->do( q{ + DELETE FROM updatedb_report; + }); + $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 0000000..c13c2e9 --- /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 0000000..291f71b --- /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 0000000..d43b8f2 --- /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 0000000..b79f7de --- /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; -- 1.7.10.4