Skip to content

Commit

Permalink
(Improve order flow) Remove old case validation (#4096) (patch)
Browse files Browse the repository at this point in the history
### Fixed

- Removed old case order validation
  • Loading branch information
islean authored Jan 15, 2025
1 parent 64caf0f commit 5a253b5
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 272 deletions.
102 changes: 0 additions & 102 deletions cg/services/orders/validate_order_services/validate_case_order.py

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from cg.services.order_validation_service.workflows.mutant.models.order import MutantOrder
from cg.services.order_validation_service.workflows.pacbio_long_read.models.order import PacbioOrder
from cg.services.order_validation_service.workflows.rml.models.order import RmlOrder
from cg.services.order_validation_service.workflows.rna_fusion.models.order import RnaFusionOrder


@pytest.fixture(scope="session")
Expand Down Expand Up @@ -156,6 +157,6 @@ def all_orders_to_submit(
OrderType.MIP_RNA: MipRnaOrder.model_validate(mip_rna_order_to_submit),
OrderType.PACBIO_LONG_READ: PacbioOrder.model_validate(pacbio_order_to_submit),
OrderType.RML: RmlOrder.model_validate(rml_order_to_submit),
# OrderType.RNAFUSION: RnaFusionOrder.model_validate(rnafusion_order_to_submit),
OrderType.RNAFUSION: RnaFusionOrder.model_validate(rnafusion_order_to_submit),
OrderType.SARS_COV_2: MutantOrder.model_validate(sarscov2_order_to_submit),
}
126 changes: 0 additions & 126 deletions tests/meta/orders/test_meta_orders_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
from cg.services.order_validation_service.workflows.balsamic.models.order import BalsamicOrder
from cg.services.order_validation_service.workflows.mip_dna.models.order import MipDnaOrder
from cg.services.order_validation_service.workflows.mip_rna.models.order import MipRnaOrder
from cg.services.orders.validate_order_services.validate_case_order import ValidateCaseOrderService
from cg.store.models import Case, Customer, Pool, Sample
from cg.store.store import Store
from tests.store_helpers import StoreHelpers
Expand Down Expand Up @@ -416,131 +415,6 @@ def test_submit_unique_sample_case_name(
# Then no exception about duplicate names should be thrown


def test_validate_sex_inconsistent_sex(
orders_api: OrdersAPI, mip_dna_order_to_submit: dict, helpers: StoreHelpers
):
# GIVEN we have an order with a sample that is already in the database but with different sex
order_data = MipDnaOrder.model_validate(mip_dna_order_to_submit)
store = orders_api.validation_service.store
customer: Customer = store.get_customer_by_internal_id(customer_internal_id=order_data.customer)

# add sample with different sex than in order
samples: list[Sample] = [sample for _, _, sample in order_data.enumerated_new_samples]
for sample in samples:
sample_obj: Sample = helpers.add_sample(
store=store,
customer_id=customer.internal_id,
sex=Sex.MALE if sample.sex == Sex.FEMALE else Sex.FEMALE,
name=sample.name,
subject_id=sample.subject_id,
)
store.session.add(sample_obj)
store.session.commit()
assert sample_obj.sex != sample.sex

validator = ValidateCaseOrderService(status_db=store)

# WHEN calling _validate_sex
# THEN an OrderError should be raised on non-matching sex
with pytest.raises(OrderError):
validator._validate_subject_sex(samples=samples, customer_id=order_data.customer)


def test_validate_sex_consistent_sex(
orders_api: OrdersAPI, mip_dna_order_to_submit: dict, helpers: StoreHelpers
):
# GIVEN we have an order with a sample that is already in the database and with same gender
order_data = MipDnaOrder.model_validate(mip_dna_order_to_submit)
store = orders_api.validation_service.store
customer: Customer = store.get_customer_by_internal_id(customer_internal_id=order_data.customer)

# add sample with different sex than in order
samples: list[Sample] = [sample for _, _, sample in order_data.enumerated_new_samples]
for sample in samples:
sample_obj: Sample = helpers.add_sample(
store=store,
customer_id=customer.internal_id,
sex=sample.sex,
name=sample.name,
subject_id=sample.subject_id,
)
store.session.add(sample_obj)
store.session.commit()
assert sample_obj.sex == sample.sex

validator = ValidateCaseOrderService(status_db=store)

# WHEN calling _validate_sex
validator._validate_subject_sex(samples=samples, customer_id=order_data.customer)

# THEN no OrderError should be raised on non-matching sex


def test_validate_sex_unknown_existing_sex(
orders_api: OrdersAPI, mip_dna_order_to_submit: dict, helpers: StoreHelpers
):
# GIVEN we have an order with a sample that is already in the database and with different gender but the existing is
# of type "unknown"
order_data = MipDnaOrder.model_validate(mip_dna_order_to_submit)
store = orders_api.validation_service.store
customer: Customer = store.get_customer_by_internal_id(customer_internal_id=order_data.customer)

# add sample with different sex than in order
samples: list[Sample] = [sample for _, _, sample in order_data.enumerated_new_samples]
for sample in samples:
sample_obj: Sample = helpers.add_sample(
store=store,
customer_id=customer.internal_id,
sex=Sex.UNKNOWN,
name=sample.name,
subject_id=sample.subject_id,
)
store.session.add(sample_obj)
store.session.commit()
assert sample_obj.sex != sample.sex

validator = ValidateCaseOrderService(store)

# WHEN calling _validate_sex
validator._validate_subject_sex(samples=samples, customer_id=order_data.customer)

# THEN no OrderError should be raised on non-matching sex


def test_validate_sex_unknown_new_sex(
orders_api: OrdersAPI, mip_dna_order_to_submit: dict, helpers: StoreHelpers
):
# GIVEN we have an order with a sample that is already in the database and with different gender but the new is of
# type "unknown"
order_data = MipDnaOrder.model_validate(mip_dna_order_to_submit)
store = orders_api.validation_service.store
customer: Customer = store.get_customer_by_internal_id(customer_internal_id=order_data.customer)

# add sample with different sex than in order
samples: list[Sample] = [sample for _, _, sample in order_data.enumerated_new_samples]
for sample in samples:
sample_obj: Sample = helpers.add_sample(
store=store,
customer_id=customer.internal_id,
sex=sample.sex,
name=sample.name,
subject_id=sample.subject_id,
)
sample.sex = "unknown"
store.session.add(sample_obj)
store.session.commit()

for sample in samples:
assert sample_obj.sex != sample.sex

validator = ValidateCaseOrderService(store)

# WHEN calling _validate_sex
validator._validate_subject_sex(samples=samples, customer_id=order_data.customer)

# THEN no OrderError should be raised on non-matching sex


@pytest.mark.xfail(reason="Change in order validation")
@pytest.mark.parametrize(
"order_type",
Expand Down
71 changes: 71 additions & 0 deletions tests/services/order_validation_service/test_case_sample_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,77 @@ def test_validate_sex_subject_id_clash(valid_order: OrderWithCases, sample_store
assert isinstance(errors[0], SexSubjectIdError)


def test_validate_sex_subject_id_no_clash(valid_order: OrderWithCases, sample_store: Store):
# GIVEN an existing sample
sample = sample_store.session.query(Sample).first()

# GIVEN an order and sample with the same customer and subject id
valid_order.customer = sample.customer.internal_id
valid_order.cases[0].samples[0].subject_id = "subject"
sample.subject_id = "subject"

# GIVEN that the order's sample has a matching sex to the one in StatusDB
valid_order.cases[0].samples[0].sex = SexEnum.female
sample.sex = SexEnum.female

# WHEN validating the order
errors: list[SexSubjectIdError] = validate_subject_sex_consistency(
order=valid_order,
store=sample_store,
)

# THEN no error should be returned
assert not errors


def test_validate_sex_subject_id_existing_sex_unknown(
valid_order: OrderWithCases, sample_store: Store
):
# GIVEN an existing sample
sample = sample_store.session.query(Sample).first()

# GIVEN an order and sample with the same customer and subject id
valid_order.customer = sample.customer.internal_id
valid_order.cases[0].samples[0].subject_id = "subject"
sample.subject_id = "subject"

# GIVEN a sample in the order that has a known sex and the existing sample's sex is unknown
valid_order.cases[0].samples[0].sex = SexEnum.female
sample.sex = SexEnum.unknown

# WHEN validating the order
errors: list[SexSubjectIdError] = validate_subject_sex_consistency(
order=valid_order,
store=sample_store,
)

# THEN no error should be returned
assert not errors


def test_validate_sex_subject_id_new_sex_unknown(valid_order: OrderWithCases, sample_store: Store):
# GIVEN an existing sample
sample = sample_store.session.query(Sample).first()

# GIVEN an order and sample with the same customer and subject id
valid_order.customer = sample.customer.internal_id
valid_order.cases[0].samples[0].subject_id = "subject"
sample.subject_id = "subject"

# GIVEN a sample in the order that has an unknown sex and the existing sample's sex is known
valid_order.cases[0].samples[0].sex = SexEnum.unknown
sample.sex = SexEnum.female

# WHEN validating the order
errors: list[SexSubjectIdError] = validate_subject_sex_consistency(
order=valid_order,
store=sample_store,
)

# THEN no error should be returned
assert not errors


def test_validate_sample_names_different_from_case_names(
order_with_samples_having_same_names_as_cases: OrderWithCases,
):
Expand Down

This file was deleted.

0 comments on commit 5a253b5

Please sign in to comment.