4 use C4::Stats qw( UpdateStats );
7 use Test::More tests => 18;
10 use_ok('C4::Stats', qw( UpdateStats ));
17 my $schema = Koha::Database->new->schema;
18 $schema->storage->txn_begin;
19 my $dbh = C4::Context->dbh;
25 is (UpdateStats () ,undef, "UpdateStats returns undef if no params");
39 # returns undef and croaks if type is not allowed
40 $params -> {type} = "bla";
41 eval {UpdateStats($params)};
43 isnt ($return_error,'',"UpdateStats returns undef and croaks if type is not allowed");
45 delete $params->{type};
46 # returns undef and croaks if type is missing
47 eval {UpdateStats($params)};
49 isnt ($return_error,'',"UpdateStats returns undef and croaks if no type given");
51 $params -> {type} = undef;
52 # returns undef and croaks if type is undef
53 eval {UpdateStats($params)};
55 isnt ($return_error,'',"UpdateStats returns undef and croaks if type is undef");
57 # returns undef and croaks if mandatory params are missing
58 my @allowed_circulation_types = qw (renew issue localuse return onsite_checkout recall);
59 my @allowed_accounts_types = qw (writeoff payment);
60 my @circulation_mandatory_keys = qw (branch borrowernumber itemnumber ccode itemtype); #don't check type here
61 my @accounts_mandatory_keys = qw (branch borrowernumber amount); #don't check type here
63 my @missing_errors = ();
64 foreach my $key (@circulation_mandatory_keys) {
65 my $value = $params->{$key};
66 delete $params->{$key};
67 foreach my $type (@allowed_circulation_types) {
68 $params->{type} = $type;
69 eval {UpdateStats($params)};
71 push @missing_errors, "key:$key for type:$type" unless $return_error;
73 $params->{$key} = $value;
75 foreach my $key (@accounts_mandatory_keys) {
76 my $value = $params->{$key};
77 delete $params->{$key};
78 foreach my $type (@allowed_accounts_types) {
79 $params->{type} = $type;
80 eval {UpdateStats($params)};
82 push @missing_errors, "key:$key for type:$type" unless $return_error;
84 $params->{$key} = $value;
87 is (join (", ", @missing_errors),'',"UpdateStats returns undef and croaks if mandatory params are missing");
89 # returns undef and croaks if forbidden params are given
90 $params -> {type} = "return";
91 $params -> {newparam} = "true";
92 eval {UpdateStats($params)};
94 isnt ($return_error,'',"UpdateStats returns undef and croaks if a forbidden param is given");
95 delete $params->{newparam};
97 # save the params in the right database fields
98 $dbh->do(q|DELETE FROM statistics|);
110 UpdateStats ($params);
111 my $sth = $dbh->prepare("SELECT * FROM statistics");
113 my $line = ${ $sth->fetchall_arrayref( {} ) }[0];
114 is ($params->{branch}, $line->{branch}, "UpdateStats save branch param in branch field of statistics table");
115 is ($params->{type}, $line->{type}, "UpdateStats save type param in type field of statistics table");
116 is ($params->{borrowernumber}, $line->{borrowernumber}, "UpdateStats save borrowernumber param in borrowernumber field of statistics table");
117 cmp_ok($params->{amount},'==', $line->{value}, "UpdateStats save amount param in value field of statistics table");
118 is ($params->{other}, $line->{other}, "UpdateStats save other param in other field of statistics table");
119 is ($params->{itemtype}, $line->{itemtype}, "UpdateStats save itemtype param in itemtype field of statistics table");
120 is ($params->{location}, $line->{location}, "UpdateStats save location param in location field of statistics table");
121 is ($params->{ccode}, $line->{ccode}, "UpdateStats save ccode param in ccode field of statistics table");
123 $dbh->do(q|DELETE FROM statistics|);
134 UpdateStats($params);
135 $sth = $dbh->prepare("SELECT * FROM statistics");
137 $line = ${ $sth->fetchall_arrayref( {} ) }[0];
138 is( $line->{location}, undef,
139 "UpdateStats sets location to NULL if no location is passed in." );
141 $dbh->do(q|DELETE FROM statistics|);
153 UpdateStats($params);
154 $sth = $dbh->prepare("SELECT * FROM statistics");
156 $line = ${ $sth->fetchall_arrayref( {} ) }[0];
157 is( $line->{location}, undef,
158 "UpdateStats sets location to NULL if undef is passed in." );
160 # More tests to write!