6 use Test::More tests => 19;
17 my $dbh = C4::Context->dbh;
18 $dbh->{RaiseError} = 1;
19 $dbh->{AutoCommit} = 0;
25 is (UpdateStats () ,undef, "UpdateStats returns undef if no params");
40 # returns undef and croaks if type is not allowed
41 $params -> {type} = "bla";
42 eval {UpdateStats($params)};
44 isnt ($return_error,'',"UpdateStats returns undef and croaks if type is not allowed");
46 delete $params->{type};
47 # returns undef and croaks if type is missing
48 eval {UpdateStats($params)};
50 isnt ($return_error,'',"UpdateStats returns undef and croaks if no type given");
52 $params -> {type} = undef;
53 # returns undef and croaks if type is undef
54 eval {UpdateStats($params)};
56 isnt ($return_error,'',"UpdateStats returns undef and croaks if type is undef");
58 # returns undef and croaks if mandatory params are missing
59 my @allowed_circulation_types = qw (renew issue localuse return);
60 my @allowed_accounts_types = qw (writeoff payment);
61 my @circulation_mandatory_keys = qw (branch borrowernumber itemnumber ccode itemtype); #don't check type here
62 my @accounts_mandatory_keys = qw (branch borrowernumber amount); #don't check type here
64 my @missing_errors = ();
65 foreach my $key (@circulation_mandatory_keys) {
66 my $value = $params->{$key};
67 delete $params->{$key};
68 foreach my $type (@allowed_circulation_types) {
69 $params->{type} = $type;
70 eval {UpdateStats($params)};
72 push @missing_errors, "key:$key for type:$type" unless $return_error;
74 $params->{$key} = $value;
76 foreach my $key (@accounts_mandatory_keys) {
77 my $value = $params->{$key};
78 delete $params->{$key};
79 foreach my $type (@allowed_accounts_types) {
80 $params->{type} = $type;
81 eval {UpdateStats($params)};
83 push @missing_errors, "key:$key for type:$type" unless $return_error;
85 $params->{$key} = $value;
88 is (join (", ", @missing_errors),'',"UpdateStats returns undef and croaks if mandatory params are missing");
90 # returns undef and croaks if forbidden params are given
91 $params -> {type} = "return";
92 $params -> {newparam} = "true";
93 eval {UpdateStats($params)};
95 isnt ($return_error,'',"UpdateStats returns undef and croaks if a forbidden param is given");
96 delete $params->{newparam};
98 # save the params in the right database fields
99 $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->{location}, $line->{location}, "UpdateStats save location param in location field of statistics table");
123 is ($params->{accountno}, $line->{proccode}, "UpdateStats save accountno param in proccode field of statistics table");
124 is ($params->{ccode}, $line->{ccode}, "UpdateStats save ccode param in ccode field of statistics table");
126 $dbh->do(q|DELETE FROM statistics|);
138 UpdateStats($params);
139 $sth = $dbh->prepare("SELECT * FROM statistics");
141 $line = ${ $sth->fetchall_arrayref( {} ) }[0];
142 is( $line->{location}, undef,
143 "UpdateStats sets location to NULL if no location is passed in." );
145 $dbh->do(q|DELETE FROM statistics|);
158 UpdateStats($params);
159 $sth = $dbh->prepare("SELECT * FROM statistics");
161 $line = ${ $sth->fetchall_arrayref( {} ) }[0];
162 is( $line->{location}, undef,
163 "UpdateStats sets location to NULL if undef is passed in." );
165 # More tests to write!