6 use Test::More tests => 17;
19 my $dbh = C4::Context->dbh;
20 $dbh->{RaiseError} = 1;
21 $dbh->{AutoCommit} = 0;
27 is (UpdateStats () ,undef, "UpdateStats returns undef if no params");
41 # returns undef and croaks if type is not allowed
42 $params -> {type} = "bla";
43 eval {UpdateStats($params)};
45 isnt ($return_error,'',"UpdateStats returns undef and croaks if type is not allowed");
47 delete $params->{type};
48 # returns undef and croaks if type is missing
49 eval {UpdateStats($params)};
51 isnt ($return_error,'',"UpdateStats returns undef and croaks if no type given");
53 $params -> {type} = undef;
54 # returns undef and croaks if type is undef
55 eval {UpdateStats($params)};
57 isnt ($return_error,'',"UpdateStats returns undef and croaks if type is undef");
59 # returns undef and croaks if mandatory params are missing
60 my @allowed_circulation_types = qw (renew issue localuse return);
61 my @allowed_accounts_types = qw (writeoff payment);
62 my @circulation_mandatory_keys = qw (branch borrowernumber itemnumber ccode itemtype); #don't check type here
63 my @accounts_mandatory_keys = qw (branch borrowernumber amount); #don't check type here
65 my @missing_errors = ();
66 foreach my $key (@circulation_mandatory_keys) {
67 my $value = $params->{$key};
68 delete $params->{$key};
69 foreach my $type (@allowed_circulation_types) {
70 $params->{type} = $type;
71 eval {UpdateStats($params)};
73 push @missing_errors, "key:$key for type:$type" unless $return_error;
75 $params->{$key} = $value;
77 foreach my $key (@accounts_mandatory_keys) {
78 my $value = $params->{$key};
79 delete $params->{$key};
80 foreach my $type (@allowed_accounts_types) {
81 $params->{type} = $type;
82 eval {UpdateStats($params)};
84 push @missing_errors, "key:$key for type:$type" unless $return_error;
86 $params->{$key} = $value;
89 is (join (", ", @missing_errors),'',"UpdateStats returns undef and croaks if mandatory params are missing");
91 # returns undef and croaks if forbidden params are given
92 $params -> {type} = "return";
93 $params -> {newparam} = "true";
94 eval {UpdateStats($params)};
96 isnt ($return_error,'',"UpdateStats returns undef and croaks if a forbidden param is given");
97 delete $params->{newparam};
99 # save the params in the right database fields
100 $dbh->do(q|DELETE FROM statistics|);
112 UpdateStats ($params);
113 my $sth = $dbh->prepare("SELECT * FROM statistics");
115 my $line = ${ $sth->fetchall_arrayref( {} ) }[0];
116 is ($params-> {branch}, $line->{branch}, "UpdateStats save branch param in branch field of statistics table");
117 is ($params-> {type}, $line->{type}, "UpdateStats save type param in type field of statistics table");
118 is ($params-> {borrowernumber}, $line->{borrowernumber}, "UpdateStats save borrowernumber param in borrowernumber field of statistics table");
119 cmp_ok($params-> {amount},'==', $line->{value}, "UpdateStats save amount param in value field of statistics table");
120 is ($params-> {other}, $line->{other}, "UpdateStats save other param in other field of statistics table");
121 is ($params-> {itemtype}, $line->{itemtype}, "UpdateStats save itemtype param in itemtype field of statistics table");
122 is ($params-> {accountno}, $line->{proccode}, "UpdateStats save accountno param in proccode field of statistics table");
123 is ($params-> {ccode}, $line->{ccode}, "UpdateStats save ccode param in ccode field of statistics table");
129 is (TotalPaid (),undef,"TotalPaid returns undef if no params are given");
130 # More tests to write!