From 1bb3eb68689d988770e62ce77214307301e3b539 Mon Sep 17 00:00:00 2001 From: islean Date: Wed, 15 Jan 2025 13:51:38 +0100 Subject: [PATCH] Remove case validation --- .../validate_case_order.py | 102 ------------------ .../test_validate_generic_order.py | 40 ------- 2 files changed, 142 deletions(-) delete mode 100644 cg/services/orders/validate_order_services/validate_case_order.py delete mode 100644 tests/services/orders/test_validate_order_service/test_validate_generic_order.py diff --git a/cg/services/orders/validate_order_services/validate_case_order.py b/cg/services/orders/validate_order_services/validate_case_order.py deleted file mode 100644 index 24fe4a5376..0000000000 --- a/cg/services/orders/validate_order_services/validate_case_order.py +++ /dev/null @@ -1,102 +0,0 @@ -from cg.exc import OrderError -from cg.models.orders.constants import OrderType -from cg.models.orders.order import OrderIn -from cg.models.orders.samples import Of1508Sample, OrderInSample -from cg.services.orders.store_order_services.store_order_service import ValidateOrderService -from cg.store.models import Customer, Sample -from cg.store.store import Store - - -class ValidateCaseOrderService(ValidateOrderService): - - def __init__(self, status_db: Store): - self.status_db = status_db - - def validate_order(self, order: OrderIn) -> None: - self._validate_subject_sex(samples=order.samples, customer_id=order.customer) - self._validate_samples_available_to_customer( - samples=order.samples, customer_id=order.customer - ) - self._validate_case_names_are_unique(samples=order.samples, customer_id=order.customer) - if order.order_type == OrderType.RNAFUSION: - self._validate_only_one_sample_per_case(samples=order.samples) - - def _validate_subject_sex(self, samples: [Of1508Sample], customer_id: str): - """Validate that sex is consistent with existing samples, skips samples of unknown sex - - Args: - samples (list[dict]): Samples to validate - customer_id (str): Customer that the samples belong to - Returns: - Nothing - """ - sample: Of1508Sample - for sample in samples: - subject_id: str = sample.subject_id - if not subject_id: - continue - new_gender: str = sample.sex - if new_gender == "unknown": - continue - - existing_samples: list[Sample] = self.status_db.get_samples_by_customer_and_subject_id( - customer_internal_id=customer_id, subject_id=subject_id - ) - existing_sample: Sample - for existing_sample in existing_samples: - previous_gender = existing_sample.sex - if previous_gender == "unknown": - continue - - if previous_gender != new_gender: - raise OrderError( - f"Sample gender inconsistency for subject_id: {subject_id}: previous gender {previous_gender}, new gender {new_gender}" - ) - - def _validate_samples_available_to_customer( - self, samples: list[OrderInSample], customer_id: str - ) -> None: - """Validate that the customer have access to all samples""" - sample: Of1508Sample - for sample in samples: - if not sample.internal_id: - continue - - existing_sample: Sample = self.status_db.get_sample_by_internal_id( - internal_id=sample.internal_id - ) - - data_customer: Customer = self.status_db.get_customer_by_internal_id( - customer_internal_id=customer_id - ) - - if existing_sample.customer not in data_customer.collaborators: - raise OrderError(f"Sample not available: {sample.name}") - - def _validate_case_names_are_unique( - self, samples: list[OrderInSample], customer_id: str - ) -> None: - """Validate that the names of all cases are unused for all samples""" - - customer: Customer = self.status_db.get_customer_by_internal_id( - customer_internal_id=customer_id - ) - - sample: Of1508Sample - for sample in samples: - if self._is_rerun_of_existing_case(sample=sample): - continue - if self.status_db.get_case_by_name_and_customer( - customer=customer, case_name=sample.family_name - ): - raise OrderError(f"Case name {sample.family_name} already in use") - - @staticmethod - def _is_rerun_of_existing_case(sample: Of1508Sample) -> bool: - return sample.case_internal_id is not None - - @staticmethod - def _validate_only_one_sample_per_case(samples: list[Of1508Sample]) -> None: - """Validates that each case contains only one sample.""" - if len({sample.family_name for sample in samples}) != len(samples): - raise OrderError("Each case in an RNAFUSION order must have exactly one sample.") diff --git a/tests/services/orders/test_validate_order_service/test_validate_generic_order.py b/tests/services/orders/test_validate_order_service/test_validate_generic_order.py deleted file mode 100644 index b7eb36a6bf..0000000000 --- a/tests/services/orders/test_validate_order_service/test_validate_generic_order.py +++ /dev/null @@ -1,40 +0,0 @@ -import pytest - -from cg.exc import OrderError -from cg.models.orders.constants import OrderType -from cg.models.orders.order import OrderIn -from cg.store.store import Store - - -def test__validate_one_sample_per_case_multiple_samples( - base_store: Store, - rnafusion_order_to_submit: dict, -): - """Tests the validation of an RNAFUSION order where two samples have the same family_name.""" - ### GIVEN an RNAFUSION order where the first and last sample share the same case - order_data = OrderIn.parse_obj(obj=rnafusion_order_to_submit, project=OrderType.RNAFUSION) - order_data.samples[-1].family_name = order_data.samples[0].family_name - validator = ValidateCaseOrderService(base_store) - - ### WHEN validating that each case has only one sample - ### THEN an OrderError should be raised - - with pytest.raises(OrderError): - validator._validate_only_one_sample_per_case(order_data.samples) - - -def test__validate_one_sample_per_case_unique_samples( - base_store: Store, - rnafusion_order_to_submit: dict, -): - """Tests the validation of an RNAFUSION order where all samples have unique family_name.""" - ### GIVEN an RNAFUSION order with unique family names - order_data: OrderIn = OrderIn.parse_obj( - obj=rnafusion_order_to_submit, project=OrderType.RNAFUSION - ) - validator = ValidateCaseOrderService(base_store) - - ### WHEN validating that each case has only one sample - validator._validate_only_one_sample_per_case(order_data.samples) - - ### THEN no errors should be raised