-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add application ordertype endpoint (#3864)(minor)
## Description Closes Clinical-Genomics/add-new-tech#130 Adds the endpoint requesting applications given the order types ### Added - Endpoint function `get_application_order_types` in `cg/server/endpoints/applications.py` - Error handler for endpoint in `cg/server/endpoints/error_handler.py` - Aplication web service that returns the response to the endpoint in `cg/services/application/service.py` - ApplicationResponse model - Join application order query function `_get_join_application_ordertype_query` in `cg/store/base.py` - CRUD function `link_order_types_to_application`. to create `OrderTypeApplication` entries (create) - CRUF function `get_active_applications_by_order_type` (read) - Status filter module `cg/store/filters/status_ordertype_application_filters.py` for OrderTypeApplication table and one filter function - Attribute `order_types` to the `Appliacation` model in the database - Attribute `application` to the `OrderTypeApplication` model in the database - Tests for the filter function - Test for the CRUD (read) function - Fixtures - Store helpers function Co-authored-by: Isak Ohlsson Ångnell <[email protected]>
- Loading branch information
Showing
14 changed files
with
224 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import logging | ||
from functools import wraps | ||
from http import HTTPStatus | ||
|
||
from flask import jsonify | ||
|
||
from cg.store.exc import EntryNotFoundError | ||
|
||
LOG = logging.getLogger(__name__) | ||
|
||
|
||
def handle_missing_entries(func): | ||
@wraps(func) | ||
def wrapper(*args, **kwargs): | ||
try: | ||
return func(*args, **kwargs) | ||
except EntryNotFoundError as error: | ||
LOG.error(error) | ||
return jsonify(error=str(error)), HTTPStatus.NOT_FOUND | ||
except Exception as error: | ||
LOG.error(f"Unexpected error in endpoint: {error}") | ||
return ( | ||
jsonify(error="An error occurred while processing your request."), | ||
HTTPStatus.INTERNAL_SERVER_ERROR, | ||
) | ||
|
||
return wrapper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from pydantic import BaseModel | ||
|
||
|
||
class ApplicationResponse(BaseModel): | ||
applications: list[str] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from cg.models.orders.constants import OrderType | ||
from cg.services.application.models import ApplicationResponse | ||
from cg.store.models import Application | ||
from cg.store.store import Store | ||
|
||
|
||
def create_application_response(app_tags: list[str]) -> ApplicationResponse: | ||
return ApplicationResponse(applications=app_tags) | ||
|
||
|
||
class ApplicationsWebService: | ||
|
||
def __init__(self, store: Store): | ||
self.store = store | ||
|
||
def get_valid_applications(self, order_type: OrderType) -> ApplicationResponse: | ||
applications: list[Application] = self.store.get_active_applications_by_order_type( | ||
order_type | ||
) | ||
app_tags: list[str] = [application.tag for application in applications] | ||
return create_application_response(app_tags) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from enum import Enum | ||
|
||
from sqlalchemy.orm import Query | ||
|
||
from cg.models.orders.constants import OrderType | ||
from cg.store.models import OrderTypeApplication | ||
|
||
|
||
def filter_applications_by_order_type( | ||
order_type_applications: Query, order_type: OrderType, **kwargs | ||
) -> Query: | ||
"""Return application by order type.""" | ||
return order_type_applications.filter(OrderTypeApplication.order_type == order_type) | ||
|
||
|
||
def apply_order_type_application_filter( | ||
filter_functions: list[callable], | ||
order_type_applications: Query, | ||
order_type: OrderType = None, | ||
) -> Query: | ||
"""Apply filtering functions to the ordertype_applications query and return filtered results.""" | ||
|
||
for filter_function in filter_functions: | ||
order_type_applications: Query = filter_function( | ||
order_type_applications=order_type_applications, | ||
order_type=order_type, | ||
) | ||
return order_type_applications | ||
|
||
|
||
class OrderTypeApplicationFilter(Enum): | ||
"""Define OrderTypeApplication filter functions.""" | ||
|
||
BY_ORDER_TYPE = filter_applications_by_order_type |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import pytest | ||
|
||
from cg.models.orders.constants import OrderType | ||
from cg.store.exc import EntryNotFoundError | ||
from cg.store.models import Application | ||
from cg.store.store import Store | ||
|
||
|
||
def test_get_active_applications_by_order_type_no_application(store: Store): | ||
"""Test that if there are not applications for a given type an error is raised.""" | ||
# GIVEN a store with applications without order types | ||
applications: list[Application] = store.get_applications() | ||
for application in applications: | ||
assert not application.order_types | ||
|
||
# GIVEN an order type | ||
order_type: OrderType = OrderType.PACBIO_LONG_READ | ||
|
||
# WHEN getting active applications by order type | ||
with pytest.raises(EntryNotFoundError): | ||
store.get_active_applications_by_order_type(order_type) | ||
|
||
# THEN an EntryNotFoundError is raised |
36 changes: 36 additions & 0 deletions
36
tests/store/filters/test_status_order_type_application_filters.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from sqlalchemy.orm import Query | ||
|
||
from cg.models.orders.constants import OrderType | ||
from cg.store.filters.status_ordertype_application_filters import filter_applications_by_order_type | ||
from cg.store.models import Application, OrderTypeApplication | ||
from cg.store.store import Store | ||
from tests.store_helpers import StoreHelpers | ||
|
||
|
||
def test_filter_applications_by_order_type(applications_store: Store, helpers: StoreHelpers): | ||
# GIVEN a store with applications | ||
applications: list[Application] = applications_store.get_applications() | ||
assert applications | ||
|
||
# GIVEN an order type | ||
order_type = OrderType.PACBIO_LONG_READ | ||
|
||
# GIVEN that one application has the given order type | ||
helpers.add_application_order_type( | ||
store=applications_store, application=applications[0], order_types=[order_type] | ||
) | ||
|
||
# GIVEN that another application has a different order type | ||
helpers.add_application_order_type( | ||
store=applications_store, application=applications[1], order_types=[OrderType.BALSAMIC] | ||
) | ||
|
||
# WHEN filtering applications by order type | ||
order_type_applications: Query = applications_store._get_query(table=OrderTypeApplication) | ||
filtered_order_type_applications: Query = filter_applications_by_order_type( | ||
order_type_applications=order_type_applications, order_type=order_type | ||
) | ||
|
||
# THEN assert that only the applications with the given order type are returned | ||
assert filtered_order_type_applications.count() == 1 | ||
assert filtered_order_type_applications.first().order_type == order_type |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters