Bug 18882: DBIC Schema changes
[koha.git] / t / db_dependent / Stats.t
blob7675a1628abb5fc75089b55cfd068296c9e08b68
1 #!/usr/bin/perl
3 use Modern::Perl;
4 use C4::Stats;
6 use Test::More tests => 20;
8 BEGIN {
9 use_ok('C4::Stats');
11 can_ok(
12 'C4::Stats',
13 qw(UpdateStats
14 TotalPaid
18 #Start transaction
19 my $dbh = C4::Context->dbh;
20 $dbh->{RaiseError} = 1;
21 $dbh->{AutoCommit} = 0;
24 # Test UpdateStats
27 is (UpdateStats () ,undef, "UpdateStats returns undef if no params");
29 my $params = {
30 branch => "BRA",
31 itemnumber => 31,
32 borrowernumber => 5,
33 amount =>5.1,
34 other => "bla",
35 itemtype => "BK",
36 location => "LOC",
37 accountno => 51,
38 ccode => "CODE",
40 my $return_error;
42 # returns undef and croaks if type is not allowed
43 $params -> {type} = "bla";
44 eval {UpdateStats($params)};
45 $return_error = $@;
46 isnt ($return_error,'',"UpdateStats returns undef and croaks if type is not allowed");
48 delete $params->{type};
49 # returns undef and croaks if type is missing
50 eval {UpdateStats($params)};
51 $return_error = $@;
52 isnt ($return_error,'',"UpdateStats returns undef and croaks if no type given");
54 $params -> {type} = undef;
55 # returns undef and croaks if type is undef
56 eval {UpdateStats($params)};
57 $return_error = $@;
58 isnt ($return_error,'',"UpdateStats returns undef and croaks if type is undef");
60 # returns undef and croaks if mandatory params are missing
61 my @allowed_circulation_types = qw (renew issue localuse return);
62 my @allowed_accounts_types = qw (writeoff payment);
63 my @circulation_mandatory_keys = qw (branch borrowernumber itemnumber ccode itemtype); #don't check type here
64 my @accounts_mandatory_keys = qw (branch borrowernumber amount); #don't check type here
66 my @missing_errors = ();
67 foreach my $key (@circulation_mandatory_keys) {
68 my $value = $params->{$key};
69 delete $params->{$key};
70 foreach my $type (@allowed_circulation_types) {
71 $params->{type} = $type;
72 eval {UpdateStats($params)};
73 $return_error = $@;
74 push @missing_errors, "key:$key for type:$type" unless $return_error;
76 $params->{$key} = $value;
78 foreach my $key (@accounts_mandatory_keys) {
79 my $value = $params->{$key};
80 delete $params->{$key};
81 foreach my $type (@allowed_accounts_types) {
82 $params->{type} = $type;
83 eval {UpdateStats($params)};
84 $return_error = $@;
85 push @missing_errors, "key:$key for type:$type" unless $return_error;
87 $params->{$key} = $value;
90 is (join (", ", @missing_errors),'',"UpdateStats returns undef and croaks if mandatory params are missing");
92 # returns undef and croaks if forbidden params are given
93 $params -> {type} = "return";
94 $params -> {newparam} = "true";
95 eval {UpdateStats($params)};
96 $return_error = $@;
97 isnt ($return_error,'',"UpdateStats returns undef and croaks if a forbidden param is given");
98 delete $params->{newparam};
100 # save the params in the right database fields
101 $dbh->do(q|DELETE FROM statistics|);
102 $params = {
103 branch => "BRA",
104 itemnumber => 31,
105 borrowernumber => 5,
106 amount =>5.1,
107 other => "bla",
108 itemtype => "BK",
109 location => "LOC",
110 accountno => 51,
111 ccode => "CODE",
112 type => "return"
114 UpdateStats ($params);
115 my $sth = $dbh->prepare("SELECT * FROM statistics");
116 $sth->execute();
117 my $line = ${ $sth->fetchall_arrayref( {} ) }[0];
118 is ($params->{branch}, $line->{branch}, "UpdateStats save branch param in branch field of statistics table");
119 is ($params->{type}, $line->{type}, "UpdateStats save type param in type field of statistics table");
120 is ($params->{borrowernumber}, $line->{borrowernumber}, "UpdateStats save borrowernumber param in borrowernumber field of statistics table");
121 cmp_ok($params->{amount},'==', $line->{value}, "UpdateStats save amount param in value field of statistics table");
122 is ($params->{other}, $line->{other}, "UpdateStats save other param in other field of statistics table");
123 is ($params->{itemtype}, $line->{itemtype}, "UpdateStats save itemtype param in itemtype field of statistics table");
124 is ($params->{location}, $line->{location}, "UpdateStats save location param in location field of statistics table");
125 is ($params->{accountno}, $line->{proccode}, "UpdateStats save accountno param in proccode field of statistics table");
126 is ($params->{ccode}, $line->{ccode}, "UpdateStats save ccode param in ccode field of statistics table");
128 $dbh->do(q|DELETE FROM statistics|);
129 $params = {
130 branch => "BRA",
131 itemnumber => 31,
132 borrowernumber => 5,
133 amount => 5.1,
134 other => "bla",
135 itemtype => "BK",
136 accountno => 51,
137 ccode => "CODE",
138 type => "return"
140 UpdateStats($params);
141 $sth = $dbh->prepare("SELECT * FROM statistics");
142 $sth->execute();
143 $line = ${ $sth->fetchall_arrayref( {} ) }[0];
144 is( $line->{location}, undef,
145 "UpdateStats sets location to NULL if no location is passed in." );
147 $dbh->do(q|DELETE FROM statistics|);
148 $params = {
149 branch => "BRA",
150 itemnumber => 31,
151 borrowernumber => 5,
152 amount => 5.1,
153 other => "bla",
154 itemtype => "BK",
155 location => undef,
156 accountno => 51,
157 ccode => "CODE",
158 type => "return"
160 UpdateStats($params);
161 $sth = $dbh->prepare("SELECT * FROM statistics");
162 $sth->execute();
163 $line = ${ $sth->fetchall_arrayref( {} ) }[0];
164 is( $line->{location}, undef,
165 "UpdateStats sets location to NULL if undef is passed in." );
168 # Test TotalPaid
171 is (TotalPaid (),undef,"TotalPaid returns undef if no params are given");
172 # More tests to write!
174 #End transaction
175 $dbh->rollback;