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