76e973527f2133cea050ca5cd9d8c0c1339668cf
[koha.git] / t / db_dependent / Stats.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4 use C4::Stats;
5 use Koha::Database;
6
7 use Test::More tests => 19;
8
9 BEGIN {
10     use_ok('C4::Stats');
11 }
12 can_ok(
13     'C4::Stats',
14     qw(UpdateStats)
15 );
16
17 my $schema = Koha::Database->new->schema;
18 $schema->storage->txn_begin;
19 my $dbh = C4::Context->dbh;
20
21 #
22 # Test UpdateStats
23 #
24
25 is (UpdateStats () ,undef, "UpdateStats returns undef if no params");
26
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",
37 };
38 my $return_error;
39
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");
45
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");
51
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");
57
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
63
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;
73     }
74     $params->{$key} = $value;
75 }
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;
84     }
85     $params->{$key} = $value;
86
87 }
88 is (join (", ", @missing_errors),'',"UpdateStats returns undef and croaks if mandatory params are missing");
89
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};
97
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"
111 };
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");
125
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"
137 };
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." );
144
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"
157 };
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." );
164
165 # More tests to write!