Bug 20510: Remove unused sub TotalPaid from C4::Stats
[koha.git] / t / db_dependent / Stats.t
blob4ec1bcefffeb43d14e2c20376238fc762188e803
1 #!/usr/bin/perl
3 use Modern::Perl;
4 use C4::Stats;
6 use Test::More tests => 19;
8 BEGIN {
9 use_ok('C4::Stats');
11 can_ok(
12 'C4::Stats',
13 qw(UpdateStats)
16 #Start transaction
17 my $dbh = C4::Context->dbh;
18 $dbh->{RaiseError} = 1;
19 $dbh->{AutoCommit} = 0;
22 # Test UpdateStats
25 is (UpdateStats () ,undef, "UpdateStats returns undef if no params");
27 my $params = {
28 branch => "BRA",
29 itemnumber => 31,
30 borrowernumber => 5,
31 amount =>5.1,
32 other => "bla",
33 itemtype => "BK",
34 location => "LOC",
35 accountno => 51,
36 ccode => "CODE",
38 my $return_error;
40 # returns undef and croaks if type is not allowed
41 $params -> {type} = "bla";
42 eval {UpdateStats($params)};
43 $return_error = $@;
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)};
49 $return_error = $@;
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)};
55 $return_error = $@;
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)};
71 $return_error = $@;
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)};
82 $return_error = $@;
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)};
94 $return_error = $@;
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|);
100 $params = {
101 branch => "BRA",
102 itemnumber => 31,
103 borrowernumber => 5,
104 amount =>5.1,
105 other => "bla",
106 itemtype => "BK",
107 location => "LOC",
108 accountno => 51,
109 ccode => "CODE",
110 type => "return"
112 UpdateStats ($params);
113 my $sth = $dbh->prepare("SELECT * FROM statistics");
114 $sth->execute();
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|);
127 $params = {
128 branch => "BRA",
129 itemnumber => 31,
130 borrowernumber => 5,
131 amount => 5.1,
132 other => "bla",
133 itemtype => "BK",
134 accountno => 51,
135 ccode => "CODE",
136 type => "return"
138 UpdateStats($params);
139 $sth = $dbh->prepare("SELECT * FROM statistics");
140 $sth->execute();
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|);
146 $params = {
147 branch => "BRA",
148 itemnumber => 31,
149 borrowernumber => 5,
150 amount => 5.1,
151 other => "bla",
152 itemtype => "BK",
153 location => undef,
154 accountno => 51,
155 ccode => "CODE",
156 type => "return"
158 UpdateStats($params);
159 $sth = $dbh->prepare("SELECT * FROM statistics");
160 $sth->execute();
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!
167 #End transaction
168 $dbh->rollback;