Bugzilla – Attachment 16116 Details for
Bug 7167
updatedatabase improvements
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 7167: Adds Unit tests for C4::Update::Database
Bug-7167-Adds-Unit-tests-for-C4UpdateDatabase.patch (text/plain), 8.61 KB, created by
Jonathan Druart
on 2013-03-14 10:24:49 UTC
(
hide
)
Description:
Bug 7167: Adds Unit tests for C4::Update::Database
Filename:
MIME Type:
Creator:
Jonathan Druart
Created:
2013-03-14 10:24:49 UTC
Size:
8.61 KB
patch
obsolete
>From 38b7647bd6b91aac8bbdaaa3bbf7337828fa2c6c Mon Sep 17 00:00:00 2001 >From: Jonathan Druart <jonathan.druart@biblibre.com> >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 <cnighswonger@foundations.edu> >--- > C4/Update/Database.pm | 1 - > t/db_dependent/UpdateDatabase.t | 101 ++++++++++++++++++++ > .../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, 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 608e73b..5bbc950 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 0000000..f004ab3 >--- /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 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
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 7167
:
6377
|
6378
|
6379
|
6381
|
6414
|
6415
|
6416
|
6476
|
6477
|
6478
|
6479
|
6722
|
6832
|
7903
|
8178
|
9859
|
9860
|
9861
|
9862
|
10033
|
10034
|
10549
|
10666
|
10667
|
10668
|
10825
|
11129
|
11222
|
11223
|
11224
|
11225
|
11226
|
11227
|
11228
|
11229
|
11230
|
11231
|
11232
|
11235
|
11236
|
11237
|
11238
|
11239
|
11240
|
11241
|
11242
|
11243
|
11244
|
11245
|
11246
|
11571
|
13423
|
13424
|
13425
|
13426
|
13427
|
13428
|
13429
|
13430
|
13431
|
13432
|
13433
|
13434
|
13435
|
13436
|
13437
|
13456
|
13457
|
13509
|
13516
|
13517
|
13518
|
13519
|
13520
|
13521
|
13522
|
13523
|
13524
|
13525
|
13526
|
13527
|
13528
|
13529
|
13733
|
13734
|
13842
|
13845
|
13848
|
13851
|
13852
|
13853
|
13854
|
14310
|
16113
|
16114
|
16115
| 16116 |
16117
|
16596