|
Lines 30-174
my $dbh = C4::Context->dbh;
Link Here
|
| 30 |
|
30 |
|
| 31 |
subtest 'UpdateStats' => sub { |
31 |
subtest 'UpdateStats' => sub { |
| 32 |
plan tests => 17; |
32 |
plan tests => 17; |
| 33 |
is (UpdateStats () ,undef, "UpdateStats returns undef if no params"); |
33 |
is (UpdateStats () ,undef, "UpdateStats returns undef if no params"); |
| 34 |
|
34 |
|
| 35 |
my $params = { |
35 |
my $params = { |
| 36 |
branch => "BRA", |
36 |
branch => "BRA", |
| 37 |
itemnumber => 31, |
37 |
itemnumber => 31, |
| 38 |
borrowernumber => 5, |
38 |
borrowernumber => 5, |
| 39 |
amount =>5.1, |
39 |
amount =>5.1, |
| 40 |
other => "bla", |
40 |
other => "bla", |
| 41 |
itemtype => "BK", |
41 |
itemtype => "BK", |
| 42 |
location => "LOC", |
42 |
location => "LOC", |
| 43 |
ccode => "CODE", |
43 |
ccode => "CODE", |
| 44 |
interface => "INTERFACE", |
44 |
interface => "INTERFACE", |
| 45 |
}; |
45 |
}; |
| 46 |
my $return_error; |
46 |
my $return_error; |
| 47 |
|
47 |
|
| 48 |
# returns undef and croaks if type is not allowed |
48 |
# returns undef and croaks if type is not allowed |
| 49 |
$params -> {type} = "bla"; |
49 |
$params -> {type} = "bla"; |
| 50 |
eval {UpdateStats($params)}; |
50 |
eval {UpdateStats($params)}; |
| 51 |
$return_error = $@; |
51 |
$return_error = $@; |
| 52 |
isnt ($return_error,'',"UpdateStats returns undef and croaks if type is not allowed"); |
52 |
isnt ($return_error,'',"UpdateStats returns undef and croaks if type is not allowed"); |
| 53 |
|
53 |
|
| 54 |
delete $params->{type}; |
54 |
delete $params->{type}; |
| 55 |
# returns undef and croaks if type is missing |
55 |
# returns undef and croaks if type is missing |
| 56 |
eval {UpdateStats($params)}; |
56 |
eval {UpdateStats($params)}; |
| 57 |
$return_error = $@; |
57 |
$return_error = $@; |
| 58 |
isnt ($return_error,'',"UpdateStats returns undef and croaks if no type given"); |
58 |
isnt ($return_error,'',"UpdateStats returns undef and croaks if no type given"); |
| 59 |
|
59 |
|
| 60 |
$params -> {type} = undef; |
60 |
$params -> {type} = undef; |
| 61 |
# returns undef and croaks if type is undef |
61 |
# returns undef and croaks if type is undef |
| 62 |
eval {UpdateStats($params)}; |
62 |
eval {UpdateStats($params)}; |
| 63 |
$return_error = $@; |
63 |
$return_error = $@; |
| 64 |
isnt ($return_error,'',"UpdateStats returns undef and croaks if type is undef"); |
64 |
isnt ($return_error,'',"UpdateStats returns undef and croaks if type is undef"); |
| 65 |
|
65 |
|
| 66 |
# returns undef and croaks if mandatory params are missing |
66 |
# returns undef and croaks if mandatory params are missing |
| 67 |
my @allowed_circulation_types = qw (renew issue localuse return onsite_checkout recall); |
67 |
my @allowed_circulation_types = qw (renew issue localuse return onsite_checkout recall); |
| 68 |
my @allowed_accounts_types = qw (writeoff payment); |
68 |
my @allowed_accounts_types = qw (writeoff payment); |
| 69 |
my @circulation_mandatory_keys = qw (branch borrowernumber itemnumber ccode itemtype); #don't check type here |
69 |
my @circulation_mandatory_keys = qw (branch borrowernumber itemnumber ccode itemtype); #don't check type here |
| 70 |
my @accounts_mandatory_keys = qw (branch borrowernumber amount); #don't check type here |
70 |
my @accounts_mandatory_keys = qw (branch borrowernumber amount); #don't check type here |
| 71 |
|
71 |
|
| 72 |
my @missing_errors = (); |
72 |
my @missing_errors = (); |
| 73 |
foreach my $key (@circulation_mandatory_keys) { |
73 |
foreach my $key (@circulation_mandatory_keys) { |
| 74 |
my $value = $params->{$key}; |
74 |
my $value = $params->{$key}; |
| 75 |
delete $params->{$key}; |
75 |
delete $params->{$key}; |
| 76 |
foreach my $type (@allowed_circulation_types) { |
76 |
foreach my $type (@allowed_circulation_types) { |
| 77 |
$params->{type} = $type; |
77 |
$params->{type} = $type; |
| 78 |
eval {UpdateStats($params)}; |
78 |
eval {UpdateStats($params)}; |
| 79 |
$return_error = $@; |
79 |
$return_error = $@; |
| 80 |
push @missing_errors, "key:$key for type:$type" unless $return_error; |
80 |
push @missing_errors, "key:$key for type:$type" unless $return_error; |
|
|
81 |
} |
| 82 |
$params->{$key} = $value; |
| 81 |
} |
83 |
} |
| 82 |
$params->{$key} = $value; |
84 |
foreach my $key (@accounts_mandatory_keys) { |
| 83 |
} |
85 |
my $value = $params->{$key}; |
| 84 |
foreach my $key (@accounts_mandatory_keys) { |
86 |
delete $params->{$key}; |
| 85 |
my $value = $params->{$key}; |
87 |
foreach my $type (@allowed_accounts_types) { |
| 86 |
delete $params->{$key}; |
88 |
$params->{type} = $type; |
| 87 |
foreach my $type (@allowed_accounts_types) { |
89 |
eval {UpdateStats($params)}; |
| 88 |
$params->{type} = $type; |
90 |
$return_error = $@; |
| 89 |
eval {UpdateStats($params)}; |
91 |
push @missing_errors, "key:$key for type:$type" unless $return_error; |
| 90 |
$return_error = $@; |
92 |
} |
| 91 |
push @missing_errors, "key:$key for type:$type" unless $return_error; |
93 |
$params->{$key} = $value; |
|
|
94 |
|
| 92 |
} |
95 |
} |
| 93 |
$params->{$key} = $value; |
96 |
is (join (", ", @missing_errors),'',"UpdateStats returns undef and croaks if mandatory params are missing"); |
| 94 |
|
97 |
|
| 95 |
} |
98 |
# returns undef and croaks if forbidden params are given |
| 96 |
is (join (", ", @missing_errors),'',"UpdateStats returns undef and croaks if mandatory params are missing"); |
99 |
$params -> {type} = "return"; |
| 97 |
|
100 |
$params -> {newparam} = "true"; |
| 98 |
# returns undef and croaks if forbidden params are given |
101 |
eval {UpdateStats($params)}; |
| 99 |
$params -> {type} = "return"; |
102 |
$return_error = $@; |
| 100 |
$params -> {newparam} = "true"; |
103 |
isnt ($return_error,'',"UpdateStats returns undef and croaks if a forbidden param is given"); |
| 101 |
eval {UpdateStats($params)}; |
104 |
delete $params->{newparam}; |
| 102 |
$return_error = $@; |
105 |
|
| 103 |
isnt ($return_error,'',"UpdateStats returns undef and croaks if a forbidden param is given"); |
106 |
# save the params in the right database fields |
| 104 |
delete $params->{newparam}; |
107 |
$dbh->do(q|DELETE FROM statistics|); |
| 105 |
|
108 |
$params = { |
| 106 |
# save the params in the right database fields |
109 |
branch => "BRA", |
| 107 |
$dbh->do(q|DELETE FROM statistics|); |
110 |
itemnumber => 31, |
| 108 |
$params = { |
111 |
borrowernumber => 5, |
| 109 |
branch => "BRA", |
112 |
amount =>5.1, |
| 110 |
itemnumber => 31, |
113 |
other => "bla", |
| 111 |
borrowernumber => 5, |
114 |
itemtype => "BK", |
| 112 |
amount =>5.1, |
115 |
location => "LOC", |
| 113 |
other => "bla", |
116 |
ccode => "CODE", |
| 114 |
itemtype => "BK", |
117 |
type => "return", |
| 115 |
location => "LOC", |
118 |
interface => "INTERFACE", |
| 116 |
ccode => "CODE", |
119 |
}; |
| 117 |
type => "return", |
120 |
UpdateStats ($params); |
| 118 |
interface => "INTERFACE", |
121 |
my $sth = $dbh->prepare("SELECT * FROM statistics"); |
| 119 |
}; |
122 |
$sth->execute(); |
| 120 |
UpdateStats ($params); |
123 |
my $line = ${ $sth->fetchall_arrayref( {} ) }[0]; |
| 121 |
my $sth = $dbh->prepare("SELECT * FROM statistics"); |
124 |
is ($params->{branch}, $line->{branch}, "UpdateStats save branch param in branch field of statistics table"); |
| 122 |
$sth->execute(); |
125 |
is ($params->{type}, $line->{type}, "UpdateStats save type param in type field of statistics table"); |
| 123 |
my $line = ${ $sth->fetchall_arrayref( {} ) }[0]; |
126 |
is ($params->{borrowernumber}, $line->{borrowernumber}, "UpdateStats save borrowernumber param in borrowernumber field of statistics table"); |
| 124 |
is ($params->{branch}, $line->{branch}, "UpdateStats save branch param in branch field of statistics table"); |
127 |
cmp_ok($params->{amount},'==', $line->{value}, "UpdateStats save amount param in value field of statistics table"); |
| 125 |
is ($params->{type}, $line->{type}, "UpdateStats save type param in type field of statistics table"); |
128 |
is ($params->{other}, $line->{other}, "UpdateStats save other param in other field of statistics table"); |
| 126 |
is ($params->{borrowernumber}, $line->{borrowernumber}, "UpdateStats save borrowernumber param in borrowernumber field of statistics table"); |
129 |
is ($params->{itemtype}, $line->{itemtype}, "UpdateStats save itemtype param in itemtype field of statistics table"); |
| 127 |
cmp_ok($params->{amount},'==', $line->{value}, "UpdateStats save amount param in value field of statistics table"); |
130 |
is ($params->{location}, $line->{location}, "UpdateStats save location param in location field of statistics table"); |
| 128 |
is ($params->{other}, $line->{other}, "UpdateStats save other param in other field of statistics table"); |
131 |
is ($params->{ccode}, $line->{ccode}, "UpdateStats save ccode param in ccode field of statistics table"); |
| 129 |
is ($params->{itemtype}, $line->{itemtype}, "UpdateStats save itemtype param in itemtype field of statistics table"); |
132 |
is ($params->{interface}, $line->{interface}, "UpdateStats save interface param in interface field of statistics table"); |
| 130 |
is ($params->{location}, $line->{location}, "UpdateStats save location param in location field of statistics table"); |
133 |
|
| 131 |
is ($params->{ccode}, $line->{ccode}, "UpdateStats save ccode param in ccode field of statistics table"); |
134 |
$dbh->do(q|DELETE FROM statistics|); |
| 132 |
is ($params->{interface}, $line->{interface}, "UpdateStats save interface param in interface field of statistics table"); |
135 |
$params = { |
| 133 |
|
136 |
branch => "BRA", |
| 134 |
$dbh->do(q|DELETE FROM statistics|); |
137 |
itemnumber => 31, |
| 135 |
$params = { |
138 |
borrowernumber => 5, |
| 136 |
branch => "BRA", |
139 |
amount => 5.1, |
| 137 |
itemnumber => 31, |
140 |
other => "bla", |
| 138 |
borrowernumber => 5, |
141 |
itemtype => "BK", |
| 139 |
amount => 5.1, |
142 |
ccode => "CODE", |
| 140 |
other => "bla", |
143 |
type => "return", |
| 141 |
itemtype => "BK", |
144 |
interface => "INTERFACE", |
| 142 |
ccode => "CODE", |
145 |
}; |
| 143 |
type => "return", |
146 |
UpdateStats($params); |
| 144 |
interface => "INTERFACE", |
147 |
$sth = $dbh->prepare("SELECT * FROM statistics"); |
| 145 |
}; |
148 |
$sth->execute(); |
| 146 |
UpdateStats($params); |
149 |
$line = ${ $sth->fetchall_arrayref( {} ) }[0]; |
| 147 |
$sth = $dbh->prepare("SELECT * FROM statistics"); |
150 |
is( $line->{location}, undef, |
| 148 |
$sth->execute(); |
151 |
"UpdateStats sets location to NULL if no location is passed in." ); |
| 149 |
$line = ${ $sth->fetchall_arrayref( {} ) }[0]; |
152 |
|
| 150 |
is( $line->{location}, undef, |
153 |
$dbh->do(q|DELETE FROM statistics|); |
| 151 |
"UpdateStats sets location to NULL if no location is passed in." ); |
154 |
$params = { |
| 152 |
|
155 |
branch => "BRA", |
| 153 |
$dbh->do(q|DELETE FROM statistics|); |
156 |
itemnumber => 31, |
| 154 |
$params = { |
157 |
borrowernumber => 5, |
| 155 |
branch => "BRA", |
158 |
amount => 5.1, |
| 156 |
itemnumber => 31, |
159 |
other => "bla", |
| 157 |
borrowernumber => 5, |
160 |
itemtype => "BK", |
| 158 |
amount => 5.1, |
161 |
location => undef, |
| 159 |
other => "bla", |
162 |
ccode => "CODE", |
| 160 |
itemtype => "BK", |
163 |
type => "return", |
| 161 |
location => undef, |
164 |
interface => "interface" |
| 162 |
ccode => "CODE", |
165 |
}; |
| 163 |
type => "return", |
166 |
UpdateStats($params); |
| 164 |
interface => "interface" |
167 |
$sth = $dbh->prepare("SELECT * FROM statistics"); |
| 165 |
}; |
168 |
$sth->execute(); |
| 166 |
UpdateStats($params); |
169 |
$line = ${ $sth->fetchall_arrayref( {} ) }[0]; |
| 167 |
$sth = $dbh->prepare("SELECT * FROM statistics"); |
170 |
is( $line->{location}, undef, |
| 168 |
$sth->execute(); |
171 |
"UpdateStats sets location to NULL if undef is passed in." ); |
| 169 |
$line = ${ $sth->fetchall_arrayref( {} ) }[0]; |
|
|
| 170 |
is( $line->{location}, undef, |
| 171 |
"UpdateStats sets location to NULL if undef is passed in." ); |
| 172 |
}; |
172 |
}; |
| 173 |
|
173 |
|
| 174 |
$schema->storage->txn_rollback; |
174 |
$schema->storage->txn_rollback; |
| 175 |
- |
|
|