|
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-101
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->{type} = 'issue'; |
100 |
$params->{type} = 'issue'; |
| 97 |
delete $params->{itemnumber}; |
101 |
delete $params->{itemnumber}; |
| 98 |
throws_ok { Koha::Statistic->new($params) } 'Koha::Exceptions::MissingParameter', '->new called for circulation without itemnumber'; |
102 |
throws_ok { Koha::Statistic->new($params) } 'Koha::Exceptions::MissingParameter', |
|
|
103 |
'->new called for circulation without itemnumber'; |
| 99 |
|
104 |
|
| 100 |
$schema->storage->txn_rollback; |
105 |
$schema->storage->txn_rollback; |
| 101 |
}; |
106 |
}; |
|
Lines 105-120
subtest 'Test ->insert (fka UpdateStats)' => sub {
Link Here
|
| 105 |
$schema->storage->txn_begin; |
110 |
$schema->storage->txn_begin; |
| 106 |
|
111 |
|
| 107 |
# save the params in the right database fields |
112 |
# save the params in the right database fields |
| 108 |
my $statistic = insert_and_fetch({ %$test_params, type => 'return' }); |
113 |
my $statistic = insert_and_fetch( { %$test_params, type => 'return' } ); |
| 109 |
is( $statistic->branch, $test_params->{branch}, "Check branch" ); |
114 |
is( $statistic->branch, $test_params->{branch}, "Check branch" ); |
| 110 |
is( $statistic->type, 'return', "Check type" ); |
115 |
is( $statistic->type, 'return', "Check type" ); |
| 111 |
is( $statistic->borrowernumber, $test_params->{borrowernumber}, "Check borrowernumber" ); |
116 |
is( $statistic->borrowernumber, $test_params->{borrowernumber}, "Check borrowernumber" ); |
| 112 |
is( $statistic->value, $test_params->{amount}, "Check value" ); |
117 |
is( $statistic->value, $test_params->{amount}, "Check value" ); |
| 113 |
is( $statistic->other, $test_params->{other}, "Check other" ); |
118 |
is( $statistic->other, $test_params->{other}, "Check other" ); |
| 114 |
is( $statistic->itemtype, $test_params->{itemtype}, "Check itemtype" ); |
119 |
is( $statistic->itemtype, $test_params->{itemtype}, "Check itemtype" ); |
| 115 |
is( $statistic->location, $test_params->{location}, "Check location" ); |
120 |
is( $statistic->location, $test_params->{location}, "Check location" ); |
| 116 |
is( $statistic->ccode, $test_params->{ccode}, "Check ccode" ); |
121 |
is( $statistic->ccode, $test_params->{ccode}, "Check ccode" ); |
| 117 |
is( $statistic->interface, $test_params->{interface}, "Check interface" ); |
122 |
is( $statistic->interface, $test_params->{interface}, "Check interface" ); |
| 118 |
|
123 |
|
| 119 |
# Test location with undef and empty string |
124 |
# Test location with undef and empty string |
| 120 |
my $params = { %$test_params, type => 'return' }; |
125 |
my $params = { %$test_params, type => 'return' }; |
|
Lines 135-141
subtest 'Test ->insert (fka UpdateStats)' => sub {
Link Here
|
| 135 |
is( $statistic->other, undef, "Other is NULL if passed undef" ); |
140 |
is( $statistic->other, undef, "Other is NULL if passed undef" ); |
| 136 |
|
141 |
|
| 137 |
# Test amount versus value; value is the db column, amount is the legacy name (to be deprecated?) |
142 |
# Test amount versus value; value is the db column, amount is the legacy name (to be deprecated?) |
| 138 |
$params = { %$test_params, type => 'return', value => 0 }; |
143 |
$params = { %$test_params, type => 'return', value => 0 }; |
| 139 |
$statistic = insert_and_fetch($params); |
144 |
$statistic = insert_and_fetch($params); |
| 140 |
is( $statistic->value, 0, "Value is zero, overriding non-zero amount" ); |
145 |
is( $statistic->value, 0, "Value is zero, overriding non-zero amount" ); |
| 141 |
delete $params->{value}; |
146 |
delete $params->{value}; |
|
Lines 146-153
subtest 'Test ->insert (fka UpdateStats)' => sub {
Link Here
|
| 146 |
}; |
151 |
}; |
| 147 |
|
152 |
|
| 148 |
sub insert_and_fetch { |
153 |
sub insert_and_fetch { |
| 149 |
my $params = shift; |
154 |
my $params = shift; |
| 150 |
my $statistic = Koha::Statistic->insert($params); |
155 |
my $statistic = Koha::Statistic->insert($params); |
| 151 |
return Koha::Statistics->search({ borrowernumber => $test_params->{borrowernumber} })->last; |
156 |
return Koha::Statistics->search( { borrowernumber => $test_params->{borrowernumber} } )->last; |
| 152 |
# FIXME discard_changes would be nicer, but we dont have a PK (yet) |
157 |
|
|
|
158 |
# FIXME discard_changes would be nicer, but we dont have a PK (yet) |
| 153 |
} |
159 |
} |
| 154 |
- |
|
|