Lines 1-6
Link Here
|
1 |
#!/usr/bin/perl |
1 |
#!/usr/bin/perl |
2 |
|
2 |
|
3 |
# Copyright 2019 Koha Development team |
3 |
# Copyright 2013, 2019, 2023 Koha Development team |
4 |
# |
4 |
# |
5 |
# This file is part of Koha |
5 |
# This file is part of Koha |
6 |
# |
6 |
# |
Lines 19-41
Link Here
|
19 |
|
19 |
|
20 |
use Modern::Perl; |
20 |
use Modern::Perl; |
21 |
|
21 |
|
22 |
use Test::More tests => 4; |
22 |
use Test::More tests => 3; |
|
|
23 |
use Test::Exception; |
23 |
|
24 |
|
|
|
25 |
use C4::Context; |
24 |
use Koha::Database; |
26 |
use Koha::Database; |
25 |
use Koha::Statistics; |
27 |
use Koha::Statistics; |
26 |
use C4::Stats qw( UpdateStats ); |
|
|
27 |
|
28 |
|
28 |
use t::lib::TestBuilder; |
29 |
use t::lib::TestBuilder; |
29 |
|
30 |
|
30 |
my $schema = Koha::Database->new->schema; |
31 |
our $schema = Koha::Database->new->schema; |
31 |
$schema->storage->txn_begin; |
32 |
our $builder = t::lib::TestBuilder->new; |
32 |
|
33 |
|
33 |
my $builder = t::lib::TestBuilder->new; |
34 |
our $test_params = { # No FK checks here |
34 |
my $library = $builder->build_object( { class => 'Koha::Libraries' } ); |
35 |
branch => "BRA", |
35 |
my $item = $builder->build_sample_item; |
36 |
itemnumber => 31, |
36 |
my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
37 |
borrowernumber => 5, |
37 |
C4::Stats::UpdateStats( |
38 |
categorycode => 'S', |
38 |
{ |
39 |
amount => 5.1, |
|
|
40 |
other => "bla", |
41 |
itemtype => "BK", |
42 |
location => "LOC", |
43 |
ccode => "CODE", |
44 |
}; |
45 |
|
46 |
subtest 'Basic Koha object tests' => sub { |
47 |
plan tests => 4; |
48 |
$schema->storage->txn_begin; |
49 |
|
50 |
my $library = $builder->build_object( { class => 'Koha::Libraries' } ); |
51 |
my $item = $builder->build_sample_item; |
52 |
my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
53 |
|
54 |
Koha::Statistic->insert({ |
39 |
type => 'issue', |
55 |
type => 'issue', |
40 |
branch => $library->branchcode, |
56 |
branch => $library->branchcode, |
41 |
itemnumber => $item->itemnumber, |
57 |
itemnumber => $item->itemnumber, |
Lines 43-56
C4::Stats::UpdateStats(
Link Here
|
43 |
itemtype => $item->effective_itemtype, |
59 |
itemtype => $item->effective_itemtype, |
44 |
location => $item->location, |
60 |
location => $item->location, |
45 |
ccode => $item->ccode, |
61 |
ccode => $item->ccode, |
46 |
} |
62 |
}); |
47 |
); |
63 |
|
|
|
64 |
my $stat = Koha::Statistics->search( { itemnumber => $item->itemnumber } )->next; |
65 |
is( $stat->borrowernumber, $patron->borrowernumber, 'Patron is there' ); |
66 |
is( $stat->branch, $library->branchcode, 'Library is there' ); |
67 |
is( ref( $stat->item ), 'Koha::Item', '->item returns a Koha::Item object' ); |
68 |
is( $stat->item->itemnumber, $item->itemnumber, '->item works great' ); |
69 |
|
70 |
$schema->storage->txn_rollback; |
71 |
}; |
72 |
|
73 |
subtest 'Test exceptions in ->new' => sub { |
74 |
plan tests => 5; |
75 |
$schema->storage->txn_begin; |
76 |
|
77 |
throws_ok { Koha::Statistic->new } 'Koha::Exceptions::BadParameter', '->new called without params'; |
78 |
#FIXME Should we remove this for sake of consistency? |
79 |
|
80 |
# Type is missing |
81 |
my $params = { %$test_params }; |
82 |
throws_ok { Koha::Statistic->new($params) } 'Koha::Exceptions::WrongParameter', '->new called without type'; |
83 |
|
84 |
# Type is not allowed |
85 |
$params = { %$test_params }; |
86 |
$params ->{type} = "bla"; |
87 |
throws_ok { Koha::Statistic->new($params) } 'Koha::Exceptions::WrongParameter', '->new called with wrong type'; |
88 |
|
89 |
# Test mandatory accounts/circulation keys |
90 |
$params = { %$test_params }; |
91 |
$params->{type} = 'payment'; |
92 |
delete $params->{amount}; |
93 |
throws_ok { Koha::Statistic->new($params) } 'Koha::Exceptions::MissingParameter', '->new called for accounts without amount'; |
94 |
$params->{type} = 'issue'; |
95 |
delete $params->{itemnumber}; |
96 |
throws_ok { Koha::Statistic->new($params) } 'Koha::Exceptions::MissingParameter', '->new called for circulation without itemnumber'; |
97 |
|
98 |
$schema->storage->txn_rollback; |
99 |
}; |
100 |
|
101 |
subtest 'Test ->insert (fka UpdateStats)' => sub { |
102 |
plan tests => 14; |
103 |
$schema->storage->txn_begin; |
104 |
|
105 |
# save the params in the right database fields |
106 |
my $statistic = insert_and_fetch({ %$test_params, type => 'return' }); |
107 |
is( $statistic->branch, $test_params->{branch}, "Check branch" ); |
108 |
is( $statistic->type, 'return', "Check type" ); |
109 |
is( $statistic->borrowernumber, $test_params->{borrowernumber}, "Check borrowernumber" ); |
110 |
is( $statistic->value, $test_params->{amount}, "Check value" ); |
111 |
is( $statistic->other, $test_params->{other}, "Check other" ); |
112 |
is( $statistic->itemtype, $test_params->{itemtype}, "Check itemtype" ); |
113 |
is( $statistic->location, $test_params->{location}, "Check location" ); |
114 |
is( $statistic->ccode, $test_params->{ccode}, "Check ccode" ); |
115 |
|
116 |
# Test location with undef and empty string |
117 |
my $params = { %$test_params, type => 'return' }; |
118 |
delete $params->{location}; |
119 |
$statistic = insert_and_fetch($params); |
120 |
is( $statistic->location, undef, "Location is NULL if not passed" ); |
121 |
$params->{location} = q{}; |
122 |
$statistic = insert_and_fetch($params); |
123 |
is( $statistic->location, q{}, "Location is empty string if passed" ); |
124 |
|
125 |
# Test 'other' with undef and empty string (slightly different behavior from location, using _key_or_default) |
126 |
$params = { %$test_params, type => 'return' }; |
127 |
delete $params->{other}; |
128 |
$statistic = insert_and_fetch($params); |
129 |
is( $statistic->other, q{}, "Other is empty string if not passed" ); |
130 |
$params->{other} = undef; |
131 |
$statistic = insert_and_fetch($params); |
132 |
is( $statistic->other, undef, "Other is NULL if passed undef" ); |
133 |
|
134 |
# Test amount versus value; value is the db column, amount is the legacy name (to be deprecated?) |
135 |
$params = { %$test_params, type => 'return', value => 0 }; |
136 |
$statistic = insert_and_fetch($params); |
137 |
is( $statistic->value, 0, "Value is zero, overriding non-zero amount" ); |
138 |
delete $params->{value}; |
139 |
$statistic = insert_and_fetch($params); |
140 |
is( $statistic->value, 5.1, "No value passed, amount used" ); |
48 |
|
141 |
|
49 |
my $stat = |
142 |
$schema->storage->txn_rollback; |
50 |
Koha::Statistics->search( { itemnumber => $item->itemnumber } )->next; |
143 |
}; |
51 |
is( $stat->borrowernumber, $patron->borrowernumber, 'Patron is there' ); |
|
|
52 |
is( $stat->branch, $library->branchcode, 'Library is there' ); |
53 |
is( ref( $stat->item ), 'Koha::Item', '->item returns a Koha::Item object' ); |
54 |
is( $stat->item->itemnumber, $item->itemnumber, '->item works great' ); |
55 |
|
144 |
|
56 |
$schema->storage->txn_rollback; |
145 |
sub insert_and_fetch { |
|
|
146 |
my $params = shift; |
147 |
my $statistic = Koha::Statistic->insert($params); |
148 |
return Koha::Statistics->search({ borrowernumber => $test_params->{borrowernumber} })->last; |
149 |
# FIXME discard_changes would be nicer, but we dont have a PK (yet) |
150 |
} |