Lines 28-47
use Koha::Statistics;
Link Here
|
28 |
|
28 |
|
29 |
use t::lib::TestBuilder; |
29 |
use t::lib::TestBuilder; |
30 |
|
30 |
|
31 |
our $schema = Koha::Database->new->schema; |
31 |
our $schema = Koha::Database->new->schema; |
32 |
our $builder = t::lib::TestBuilder->new; |
32 |
our $builder = t::lib::TestBuilder->new; |
33 |
|
33 |
|
34 |
our $test_params = { # No FK checks here |
34 |
our $test_params = { # No FK checks here |
35 |
branch => "BRA", |
35 |
branch => "BRA", |
36 |
itemnumber => 31, |
36 |
itemnumber => 31, |
37 |
borrowernumber => 5, |
37 |
borrowernumber => 5, |
38 |
categorycode => 'S', |
38 |
categorycode => 'S', |
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 |
|
46 |
|
47 |
subtest 'Basic Koha object tests' => sub { |
47 |
subtest 'Basic Koha object tests' => sub { |
Lines 52-73
subtest 'Basic Koha object tests' => sub {
Link Here
|
52 |
my $item = $builder->build_sample_item; |
52 |
my $item = $builder->build_sample_item; |
53 |
my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
53 |
my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
54 |
|
54 |
|
55 |
Koha::Statistic->insert({ |
55 |
Koha::Statistic->insert( |
56 |
type => 'issue', |
56 |
{ |
57 |
branch => $library->branchcode, |
57 |
type => 'issue', |
58 |
itemnumber => $item->itemnumber, |
58 |
branch => $library->branchcode, |
59 |
borrowernumber => $patron->borrowernumber, |
59 |
itemnumber => $item->itemnumber, |
60 |
itemtype => $item->effective_itemtype, |
60 |
borrowernumber => $patron->borrowernumber, |
61 |
location => $item->location, |
61 |
itemtype => $item->effective_itemtype, |
62 |
ccode => $item->ccode, |
62 |
location => $item->location, |
63 |
interface => C4::Context->interface, |
63 |
ccode => $item->ccode, |
64 |
}); |
64 |
interface => C4::Context->interface, |
|
|
65 |
} |
66 |
); |
65 |
|
67 |
|
66 |
my $stat = Koha::Statistics->search( { itemnumber => $item->itemnumber } )->next; |
68 |
my $stat = Koha::Statistics->search( { itemnumber => $item->itemnumber } )->next; |
67 |
is( $stat->borrowernumber, $patron->borrowernumber, 'Patron is there' ); |
69 |
is( $stat->borrowernumber, $patron->borrowernumber, 'Patron is there' ); |
68 |
is( $stat->branch, $library->branchcode, 'Library is there' ); |
70 |
is( $stat->branch, $library->branchcode, 'Library is there' ); |
69 |
is( ref( $stat->item ), 'Koha::Item', '->item returns a Koha::Item object' ); |
71 |
is( ref( $stat->item ), 'Koha::Item', '->item returns a Koha::Item object' ); |
70 |
is( $stat->item->itemnumber, $item->itemnumber, '->item works great' ); |
72 |
is( $stat->item->itemnumber, $item->itemnumber, '->item works great' ); |
71 |
|
73 |
|
72 |
$schema->storage->txn_rollback; |
74 |
$schema->storage->txn_rollback; |
73 |
}; |
75 |
}; |
Lines 77-103
subtest 'Test exceptions in ->new' => sub {
Link Here
|
77 |
$schema->storage->txn_begin; |
79 |
$schema->storage->txn_begin; |
78 |
|
80 |
|
79 |
throws_ok { Koha::Statistic->new } 'Koha::Exceptions::BadParameter', '->new called without params'; |
81 |
throws_ok { Koha::Statistic->new } 'Koha::Exceptions::BadParameter', '->new called without params'; |
80 |
#FIXME Should we remove this for sake of consistency? |
82 |
|
|
|
83 |
#FIXME Should we remove this for sake of consistency? |
81 |
|
84 |
|
82 |
# Type is missing |
85 |
# Type is missing |
83 |
my $params = { %$test_params }; |
86 |
my $params = {%$test_params}; |
84 |
throws_ok { Koha::Statistic->new($params) } 'Koha::Exceptions::WrongParameter', '->new called without type'; |
87 |
throws_ok { Koha::Statistic->new($params) } 'Koha::Exceptions::WrongParameter', '->new called without type'; |
85 |
|
88 |
|
86 |
# Type is not allowed |
89 |
# Type is not allowed |
87 |
$params = { %$test_params }; |
90 |
$params = {%$test_params}; |
88 |
$params ->{type} = "bla"; |
91 |
$params->{type} = "bla"; |
89 |
throws_ok { Koha::Statistic->new($params) } 'Koha::Exceptions::WrongParameter', '->new called with wrong type'; |
92 |
throws_ok { Koha::Statistic->new($params) } 'Koha::Exceptions::WrongParameter', '->new called with wrong type'; |
90 |
|
93 |
|
91 |
# Test mandatory accounts/circulation keys |
94 |
# Test mandatory accounts/circulation keys |
92 |
$params = { %$test_params }; |
95 |
$params = {%$test_params}; |
93 |
$params->{type} = 'payment'; |
96 |
$params->{type} = 'payment'; |
94 |
delete $params->{amount}; |
97 |
delete $params->{amount}; |
95 |
throws_ok { Koha::Statistic->new($params) } 'Koha::Exceptions::MissingParameter', '->new called for accounts without amount'; |
98 |
throws_ok { Koha::Statistic->new($params) } 'Koha::Exceptions::MissingParameter', |
|
|
99 |
'->new called for accounts without amount'; |
96 |
$params->{amount} = 0; |
100 |
$params->{amount} = 0; |
97 |
lives_ok { Koha::Statistic->new($params) } '->new accepts zero amount'; |
101 |
lives_ok { Koha::Statistic->new($params) } '->new accepts zero amount'; |
98 |
$params->{type} = 'issue'; |
102 |
$params->{type} = 'issue'; |
99 |
delete $params->{itemnumber}; |
103 |
delete $params->{itemnumber}; |
100 |
throws_ok { Koha::Statistic->new($params) } 'Koha::Exceptions::MissingParameter', '->new called for circulation without itemnumber'; |
104 |
throws_ok { Koha::Statistic->new($params) } 'Koha::Exceptions::MissingParameter', |
|
|
105 |
'->new called for circulation without itemnumber'; |
101 |
|
106 |
|
102 |
$schema->storage->txn_rollback; |
107 |
$schema->storage->txn_rollback; |
103 |
}; |
108 |
}; |
Lines 107-122
subtest 'Test ->insert (fka UpdateStats)' => sub {
Link Here
|
107 |
$schema->storage->txn_begin; |
112 |
$schema->storage->txn_begin; |
108 |
|
113 |
|
109 |
# save the params in the right database fields |
114 |
# save the params in the right database fields |
110 |
my $statistic = insert_and_fetch({ %$test_params, type => 'return' }); |
115 |
my $statistic = insert_and_fetch( { %$test_params, type => 'return' } ); |
111 |
is( $statistic->branch, $test_params->{branch}, "Check branch" ); |
116 |
is( $statistic->branch, $test_params->{branch}, "Check branch" ); |
112 |
is( $statistic->type, 'return', "Check type" ); |
117 |
is( $statistic->type, 'return', "Check type" ); |
113 |
is( $statistic->borrowernumber, $test_params->{borrowernumber}, "Check borrowernumber" ); |
118 |
is( $statistic->borrowernumber, $test_params->{borrowernumber}, "Check borrowernumber" ); |
114 |
is( $statistic->value, $test_params->{amount}, "Check value" ); |
119 |
is( $statistic->value, $test_params->{amount}, "Check value" ); |
115 |
is( $statistic->other, $test_params->{other}, "Check other" ); |
120 |
is( $statistic->other, $test_params->{other}, "Check other" ); |
116 |
is( $statistic->itemtype, $test_params->{itemtype}, "Check itemtype" ); |
121 |
is( $statistic->itemtype, $test_params->{itemtype}, "Check itemtype" ); |
117 |
is( $statistic->location, $test_params->{location}, "Check location" ); |
122 |
is( $statistic->location, $test_params->{location}, "Check location" ); |
118 |
is( $statistic->ccode, $test_params->{ccode}, "Check ccode" ); |
123 |
is( $statistic->ccode, $test_params->{ccode}, "Check ccode" ); |
119 |
is( $statistic->interface, $test_params->{interface}, "Check interface" ); |
124 |
is( $statistic->interface, $test_params->{interface}, "Check interface" ); |
120 |
|
125 |
|
121 |
# Test location with undef and empty string |
126 |
# Test location with undef and empty string |
122 |
my $params = { %$test_params, type => 'return' }; |
127 |
my $params = { %$test_params, type => 'return' }; |
Lines 137-143
subtest 'Test ->insert (fka UpdateStats)' => sub {
Link Here
|
137 |
is( $statistic->other, undef, "Other is NULL if passed undef" ); |
142 |
is( $statistic->other, undef, "Other is NULL if passed undef" ); |
138 |
|
143 |
|
139 |
# Test amount versus value; value is the db column, amount is the legacy name (to be deprecated?) |
144 |
# Test amount versus value; value is the db column, amount is the legacy name (to be deprecated?) |
140 |
$params = { %$test_params, type => 'return', value => 0 }; |
145 |
$params = { %$test_params, type => 'return', value => 0 }; |
141 |
$statistic = insert_and_fetch($params); |
146 |
$statistic = insert_and_fetch($params); |
142 |
is( $statistic->value, 0, "Value is zero, overriding non-zero amount" ); |
147 |
is( $statistic->value, 0, "Value is zero, overriding non-zero amount" ); |
143 |
delete $params->{value}; |
148 |
delete $params->{value}; |
Lines 148-155
subtest 'Test ->insert (fka UpdateStats)' => sub {
Link Here
|
148 |
}; |
153 |
}; |
149 |
|
154 |
|
150 |
sub insert_and_fetch { |
155 |
sub insert_and_fetch { |
151 |
my $params = shift; |
156 |
my $params = shift; |
152 |
my $statistic = Koha::Statistic->insert($params); |
157 |
my $statistic = Koha::Statistic->insert($params); |
153 |
return Koha::Statistics->search({ borrowernumber => $test_params->{borrowernumber} })->last; |
158 |
return Koha::Statistics->search( { borrowernumber => $test_params->{borrowernumber} } )->last; |
154 |
# FIXME discard_changes would be nicer, but we dont have a PK (yet) |
159 |
|
|
|
160 |
# FIXME discard_changes would be nicer, but we dont have a PK (yet) |
155 |
} |
161 |
} |
156 |
- |
|
|