|
Line 0
Link Here
|
|
|
1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
use Modern::Perl; |
| 4 |
use Test::More; |
| 5 |
use Test::MockModule; |
| 6 |
use C4::Context; |
| 7 |
use Data::Dumper; |
| 8 |
|
| 9 |
BEGIN { |
| 10 |
use_ok('C4::Update::Database'); |
| 11 |
} |
| 12 |
|
| 13 |
clean_db(); |
| 14 |
|
| 15 |
my $already_applied_before = C4::Update::Database::list_versions_already_applied; |
| 16 |
my $nb_applied_before = scalar @$already_applied_before; |
| 17 |
my $nb_status_ok_before = grep {$_->{status}} @$already_applied_before; |
| 18 |
|
| 19 |
my $updatedatabasemodule = new Test::MockModule('C4::Update::Database'); |
| 20 |
$updatedatabasemodule->mock('get_versions_path', sub {C4::Context->config('intranetdir') . '/t/db_dependent/data/update_database/versions'}); |
| 21 |
|
| 22 |
my $availables = C4::Update::Database::list_versions_available; |
| 23 |
my $already_applied = C4::Update::Database::list_versions_already_applied; |
| 24 |
|
| 25 |
my $report; |
| 26 |
|
| 27 |
is ( scalar @$availables, 4, "There are 4 new available updates" ); |
| 28 |
|
| 29 |
$report = C4::Update::Database::execute_version( "3.99.01.001" ); |
| 30 |
is ( $report->{'3.99.01.001'}, 'OK', "There is no error for the 1st version" ); |
| 31 |
|
| 32 |
$already_applied = C4::Update::Database::list_versions_already_applied; |
| 33 |
is ( scalar @$already_applied, $nb_applied_before + 1, "There is 1 already applied version" ); |
| 34 |
|
| 35 |
$report = C4::Update::Database::execute_version( "3.99.01.001" ); |
| 36 |
is ( $report->{"3.99.01.001"}{error}, 'ALREADY_EXISTS', "There is an 'already exist' error" ); |
| 37 |
is ( $report->{"3.99.01.001"}{old_version}, '3.99.01.001', "This version had already executed in version 3.99.01.001" ); |
| 38 |
|
| 39 |
|
| 40 |
my $queries = C4::Update::Database::get_queries( C4::Update::Database::get_filepath( "3.99.01.002" ) ); |
| 41 |
my $expected = { |
| 42 |
queries => [ |
| 43 |
'CREATE TABLE UpdateDatabase_testFOOBIS ( `version` varchar(32) DEFAULT NULL)' |
| 44 |
], |
| 45 |
'comments' => [ |
| 46 |
'This is a comment', |
| 47 |
'This is aanother comment' |
| 48 |
] |
| 49 |
}; |
| 50 |
is_deeply ( $queries, $expected, "The version 002 contains 1 query and 2 comments" ); |
| 51 |
|
| 52 |
$report = C4::Update::Database::execute_version( "3.99.01.002" ); |
| 53 |
is ( $report->{'3.99.01.002'}, 'OK', "There is no error for the 2nd version" ); |
| 54 |
|
| 55 |
$report = C4::Update::Database::execute_version( "3.99.01.003" ); |
| 56 |
$expected = { |
| 57 |
'3.99.01.003' => [ |
| 58 |
q{Error : 1050 => Table 'UpdateDatabase_testFOO' already exists} |
| 59 |
] |
| 60 |
}; |
| 61 |
is_deeply ( $report, $expected, "There is an error for the 3rd version" ); |
| 62 |
|
| 63 |
$report = C4::Update::Database::execute_version( "3.99.01.004" ); |
| 64 |
is ( $report->{'3.99.01.004'}, 'OK', "There is no error for the 4th version" ); |
| 65 |
|
| 66 |
|
| 67 |
$already_applied = C4::Update::Database::list_versions_already_applied; |
| 68 |
is ( grep( {$_->{status}} @$already_applied ), $nb_status_ok_before + 3, "There are 3 new versions with a status OK" ); |
| 69 |
|
| 70 |
C4::Update::Database::mark_as_ok( "3.99.01.003" ); |
| 71 |
$already_applied = C4::Update::Database::list_versions_already_applied; |
| 72 |
is ( grep( {$_->{status}} @$already_applied ), $nb_status_ok_before + 4, "There are 4 new versions with a status OK" ); |
| 73 |
|
| 74 |
|
| 75 |
clean_db(); |
| 76 |
|
| 77 |
sub clean_db { |
| 78 |
my $dbh = C4::Context->dbh; |
| 79 |
local $dbh->{PrintError} = 0; |
| 80 |
local $dbh->{RaiseError} = 0; |
| 81 |
$dbh->do( q{ |
| 82 |
DELETE FROM updatedb_error WHERE version LIKE "3.99.01.%"; |
| 83 |
}); |
| 84 |
$dbh->do( q{ |
| 85 |
DELETE FROM updatedb_query WHERE version LIKE "3.99.01.%"; |
| 86 |
}); |
| 87 |
$dbh->do( q{ |
| 88 |
DELETE FROM updatedb_report WHERE version LIKE "3.99.01.%"; |
| 89 |
}); |
| 90 |
$dbh->do( q{ |
| 91 |
DROP TABLE UpdateDatabase_testFOO; |
| 92 |
}); |
| 93 |
$dbh->do( q{ |
| 94 |
DROP TABLE UpdateDatabase_testFOOBIS; |
| 95 |
}); |
| 96 |
$dbh->do( q{ |
| 97 |
DELETE FROM systempreferences WHERE variable = "UpdateDatabase::testsyspref1" OR variable = "UpdateDatabase::testsyspref2" |
| 98 |
}); |
| 99 |
} |
| 100 |
|
| 101 |
done_testing; |