Skip to content

Commit

Permalink
ACQUI-45: API unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mblenk committed Aug 27, 2024
1 parent b78a0c7 commit 52e9e6f
Show file tree
Hide file tree
Showing 14 changed files with 2,352 additions and 20 deletions.
18 changes: 18 additions & 0 deletions Koha/Plugin/Acquire/Controllers/ControllerUtils.pm
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ sub filter_data_by_group {
my $dataset = $args->{dataset};

my $logged_in_branch = C4::Context::mybranch();
return $dataset if !$logged_in_branch;

my $branch = Koha::Libraries->find( { branchcode => $logged_in_branch } );
my $library_groups = $branch->library_groups;
Expand Down Expand Up @@ -147,4 +148,21 @@ sub add_accounting_values_to_ledgers_or_fund_groups_or_funds {
return $data;
}

sub add_totals_to_fund_allocations {
my ($self, $args) = @_;

my $allocations = $args->{allocations};
my @sorted_allocations = sort { $a->{allocation_amount} <=> $b->{allocation_amount} } @$allocations;

my $total = 0;
foreach my $allocation_index ( 1 .. scalar(@sorted_allocations) ) {
my $allocation = $sorted_allocations[ $allocation_index - 1 ];
$allocation->{allocation_index} = $allocation_index;
$total += $allocation->{allocation_amount};
$allocation->{new_fund_value} = $total;
}

return \@sorted_allocations;
}

1;
13 changes: 2 additions & 11 deletions Koha/Plugin/Acquire/Controllers/FundManagement/FundAllocations.pm
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,9 @@ sub list {
my $filtered_fund_allocations =
Koha::Plugin::Acquire::Controllers::ControllerUtils->filter_data_by_group( { dataset => $fund_allocations } );

my @sorted_allocations =
sort { $a->{fund_allocation_id} <=> $b->{fund_allocation_id} } @{$filtered_fund_allocations};

my $total = 0;
foreach my $allocation_index ( 1 .. scalar(@sorted_allocations) ) {
my $allocation = $sorted_allocations[ $allocation_index - 1 ];
$allocation->{allocation_index} = $allocation_index;
$total += $allocation->{allocation_amount};
$allocation->{new_fund_value} = $total;
}
my $sorted_allocations = Koha::Plugin::Acquire::Controllers::ControllerUtils->add_totals_to_fund_allocations( { allocations => $filtered_fund_allocations } );

return $c->render( status => 200, openapi => \@sorted_allocations );
return $c->render( status => 200, openapi => $sorted_allocations );
} catch {
$c->unhandled_exception($_);
};
Expand Down
6 changes: 3 additions & 3 deletions Koha/Plugin/Acquire/Controllers/FundManagement/FundGroups.pm
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ sub get {
unless ($fund_group) {
return $c->render(
status => 404,
openapi => { error => "Fund allocation not found" }
openapi => { error => "Fund group not found" }
);
}

Expand Down Expand Up @@ -131,7 +131,7 @@ sub update {
unless ($fund_group) {
return $c->render(
status => 404,
openapi => { error => "Fund allocation not found" }
openapi => { error => "Fund group not found" }
);
}

Expand Down Expand Up @@ -188,7 +188,7 @@ sub delete {
unless ($fund_group) {
return $c->render(
status => 404,
openapi => { error => "Fund allocation not found" }
openapi => { error => "Fund group not found" }
);
}

Expand Down
4 changes: 2 additions & 2 deletions Koha/Plugin/Acquire/Controllers/FundManagement/SubFunds.pm
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ sub add {

my $sub_fund = Koha::Acquire::Funds::SubFund->new_from_api($body)->store;

$c->res->headers->location( $c->req->url->to_string . '/' . $sub_fund->fund_id );
$c->res->headers->location( $c->req->url->to_string . '/' . $sub_fund->sub_fund_id );
return $c->render(
status => 201,
openapi => $sub_fund->to_api
Expand Down Expand Up @@ -145,7 +145,7 @@ sub update {

$sub_fund->set_from_api($body)->store;

$c->res->headers->location( $c->req->url->to_string . '/' . $sub_fund->fund_id );
$c->res->headers->location( $c->req->url->to_string . '/' . $sub_fund->sub_fund_id );
return $c->render(
status => 200,
openapi => $sub_fund->to_api
Expand Down
2 changes: 1 addition & 1 deletion Koha/Plugin/Acquire/Controllers/Settings/Settings.pm
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ sub store_settings {
$setting->value($settings->{$key})->store();
}

return $c->render( status => 200, openapi => 'Success' );
return $c->render( status => 201, openapi => 'Success' );

}

Expand Down
2 changes: 1 addition & 1 deletion Koha/Plugin/Acquire/api/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"application/json"
],
"responses": {
"200": {
"201": {
"description": "Settings",
"schema": {
"type": "string"
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"release": "bash ./release_kpz.sh",
"build": "webpack --mode production",
"dev": "webpack --mode development --watch",
"cy:ui": "cypress open",
"cy:cli": "cypress run"
"cy:cli": "cypress run -q",
"test": "docker exec -it kohadev-koha-1 sh -c \"prove /kohadevbox/plugins/koha-plugin-acq2/tests/t/**/*\""
},
"keywords": [],
"author": "",
Expand Down
Loading

0 comments on commit 52e9e6f

Please sign in to comment.