Koha/t/db_dependent/Stats.t
Mathieu Saby 8a02608a47 Bug 11230 - Refactor C4::Stats::UpdateStats and add UT
This patch
- refactors C4::Stats::UpdateStats (it now takes a hashref as unique parameter, instead of a list of parameters)
- add UT for it
- change the calls made to this sub in C4::Accounts and C4::Circulation

Additionnaly it also
- adds POD to C4::Stats::TotalPaid
- adds some comments to C4::Stats::TotalPaid (I think I found some errors in it)

To test :
1. run "prove t/db_dependant/Stats.t -v"
2. make some circulation operations (checkout, checkin, renew, localuse)
check the operations are rightly recorded in Statistics table (with a SQL query like "SELECT * FROM statistics WHERE datetime LIKE "2013-11-15%", if you run your test on the 15th november)
3. make some fine payments operations (writeoff, payment)
check the operations are rightly recorded in Statistics table (with a SQL query like "SELECT * FROM statistics WHERE datetime LIKE "2013-11-15%", if you run your test on the 15th november)

Note that there is probably an issue to fix in Accounts.pm : the user is saved instead of the branch. But this is not the purpose of this patch, so I kept the previous behavior for the moment.

Signed-off-by: Bernardo Gonzalez Kriegel <bgkriegel@gmail.com>
Work, test pass, isse/return/writeoff recorded on statistics
Removed a "=back" to make happy koha-qa
No other errors

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@gmail.com>
2014-07-27 11:29:28 -03:00

133 lines
4.4 KiB
Perl

#!/usr/bin/perl
use Modern::Perl;
use C4::Stats;
use Test::More tests => 17;
BEGIN {
use_ok('C4::Stats');
}
can_ok(
'C4::Stats',
qw(UpdateStats
TotalPaid
)
);
#Start transaction
my $dbh = C4::Context->dbh;
$dbh->{RaiseError} = 1;
$dbh->{AutoCommit} = 0;
#
# Test UpdateStats
#
is (UpdateStats () ,undef, "UpdateStats returns undef if no params");
my $params = {
branch => "BRA",
itemnumber => 31,
borrowernumber => 5,
amount =>5.1,
other => "bla",
itemtype => "BK",
accountno => 51,
ccode => "CODE",
};
my $return_error;
# returns undef and croaks if type is not allowed
$params -> {type} = "bla";
eval {UpdateStats($params)};
$return_error = $@;
isnt ($return_error,'',"UpdateStats returns undef and croaks if type is not allowed");
delete $params->{type};
# returns undef and croaks if type is missing
eval {UpdateStats($params)};
$return_error = $@;
isnt ($return_error,'',"UpdateStats returns undef and croaks if no type given");
$params -> {type} = undef;
# returns undef and croaks if type is undef
eval {UpdateStats($params)};
$return_error = $@;
isnt ($return_error,'',"UpdateStats returns undef and croaks if type is undef");
# returns undef and croaks if mandatory params are missing
my @allowed_circulation_types = qw (renew issue localuse return);
my @allowed_accounts_types = qw (writeoff payment);
my @circulation_mandatory_keys = qw (branch borrowernumber itemnumber ccode itemtype); #don't check type here
my @accounts_mandatory_keys = qw (branch borrowernumber amount); #don't check type here
my @missing_errors = ();
foreach my $key (@circulation_mandatory_keys) {
my $value = $params->{$key};
delete $params->{$key};
foreach my $type (@allowed_circulation_types) {
$params->{type} = $type;
eval {UpdateStats($params)};
$return_error = $@;
push @missing_errors, "key:$key for type:$type" unless $return_error;
}
$params->{$key} = $value;
}
foreach my $key (@accounts_mandatory_keys) {
my $value = $params->{$key};
delete $params->{$key};
foreach my $type (@allowed_accounts_types) {
$params->{type} = $type;
eval {UpdateStats($params)};
$return_error = $@;
push @missing_errors, "key:$key for type:$type" unless $return_error;
}
$params->{$key} = $value;
}
is (join (", ", @missing_errors),'',"UpdateStats returns undef and croaks if mandatory params are missing");
# returns undef and croaks if forbidden params are given
$params -> {type} = "return";
$params -> {newparam} = "true";
eval {UpdateStats($params)};
$return_error = $@;
isnt ($return_error,'',"UpdateStats returns undef and croaks if a forbidden param is given");
delete $params->{newparam};
# save the params in the right database fields
$dbh->do(q|DELETE FROM statistics|);
$params = {
branch => "BRA",
itemnumber => 31,
borrowernumber => 5,
amount =>5.1,
other => "bla",
itemtype => "BK",
accountno => 51,
ccode => "CODE",
type => "return"
};
UpdateStats ($params);
my $sth = $dbh->prepare("SELECT * FROM statistics");
$sth->execute();
my $line = ${ $sth->fetchall_arrayref( {} ) }[0];
is ($params-> {branch}, $line->{branch}, "UpdateStats save branch param in branch field of statistics table");
is ($params-> {type}, $line->{type}, "UpdateStats save type param in type field of statistics table");
is ($params-> {borrowernumber}, $line->{borrowernumber}, "UpdateStats save borrowernumber param in borrowernumber field of statistics table");
cmp_ok($params-> {amount},'==', $line->{value}, "UpdateStats save amount param in value field of statistics table");
is ($params-> {other}, $line->{other}, "UpdateStats save other param in other field of statistics table");
is ($params-> {itemtype}, $line->{itemtype}, "UpdateStats save itemtype param in itemtype field of statistics table");
is ($params-> {accountno}, $line->{proccode}, "UpdateStats save accountno param in proccode field of statistics table");
is ($params-> {ccode}, $line->{ccode}, "UpdateStats save ccode param in ccode field of statistics table");
#
# Test TotalPaid
#
is (TotalPaid (),undef,"TotalPaid returns undef if no params are given");
# More tests to write!
#End transaction
$dbh->rollback;