Skip to content

Commit

Permalink
enhance storing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
diitaz93 committed Jan 20, 2025
1 parent 69c82c3 commit 5096370
Show file tree
Hide file tree
Showing 11 changed files with 304 additions and 254 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ def freshdesk_client() -> FreshdeskClient:


@pytest.fixture
def order_validation_service(store_with_all_test_applications: Store) -> OrderValidationService:
return OrderValidationService(store_with_all_test_applications)
def order_validation_service(store_to_submit_and_validate_orders: Store) -> OrderValidationService:
return OrderValidationService(store_to_submit_and_validate_orders)


@pytest.fixture(scope="function")
Expand All @@ -42,6 +42,8 @@ def ticket_handler(store: Store, freshdesk_client: FreshdeskClient) -> TicketHan

@pytest.fixture
def storing_service_registry(
store_with_all_test_applications: Store, lims_api: MockLimsAPI
store_to_submit_and_validate_orders: Store, lims_api: MockLimsAPI
) -> StoringServiceRegistry:
return setup_storing_service_registry(lims=lims_api, status_db=store_with_all_test_applications)
return setup_storing_service_registry(
lims=lims_api, status_db=store_to_submit_and_validate_orders
)
63 changes: 2 additions & 61 deletions tests/fixture_plugins/orders_fixtures/store_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,74 +2,15 @@

import pytest

from cg.constants.sequencing import SeqLibraryPrepCategory
from cg.models.orders.constants import OrderType
from cg.services.orders.storing.constants import MAF_ORDER_ID
from cg.services.orders.validation.workflows.balsamic.models.order import BalsamicOrder
from cg.services.orders.validation.workflows.mip_dna.models.order import MipDnaOrder
from cg.store.models import Application, ApplicationVersion, Customer, Order
from cg.store.models import ApplicationVersion, Customer, Order
from cg.store.store import Store
from tests.store_helpers import StoreHelpers


@pytest.fixture
def balsamic_submit_store(
base_store: Store, balsamic_order: BalsamicOrder, helpers: StoreHelpers
) -> Store:
for _, _, sample in balsamic_order.enumerated_new_samples:
if not base_store.get_application_by_tag(sample.application):
application_version = helpers.ensure_application_version(
store=base_store, application_tag=sample.application
)
base_store.session.add(application_version)
base_store.session.commit()
return base_store


@pytest.fixture
def store_with_rml_applications(store: Store, helpers: StoreHelpers) -> Store:
app_tags: list[str] = ["RMLP15R100", "RMLP15R200", "RMLP15R400", "RMLP15R500"]
for tag in app_tags:
helpers.ensure_application_version(
store=store,
application_tag=tag,
prep_category=SeqLibraryPrepCategory.READY_MADE_LIBRARY,
)
helpers.ensure_customer(store=store, customer_id="cust000")
return store


@pytest.fixture
def metagenome_storing_store(base_store: Store, helpers: StoreHelpers) -> Store:
metagenome_application: Application = helpers.ensure_application_version(
store=base_store, application_tag="METPCFR030"
).application
metagenome_application.order_types = [OrderType.METAGENOME]
metagenome_application: Application = helpers.ensure_application_version(
store=base_store, application_tag="METWPFR030"
).application
metagenome_application.order_types = [OrderType.METAGENOME]
customer = base_store.get_customer_by_internal_id("cust000")
helpers.ensure_user(store=base_store, customer=customer)
return base_store


@pytest.fixture
def mip_dna_submit_store(
base_store: Store, mip_dna_order: MipDnaOrder, helpers: StoreHelpers
) -> Store:
for _, _, sample in mip_dna_order.enumerated_new_samples:
if not base_store.get_application_by_tag(sample.application):
application_version = helpers.ensure_application_version(
store=base_store, application_tag=sample.application
)
base_store.session.add(application_version)
base_store.session.commit()
return base_store


@pytest.fixture
def store_with_all_test_applications(
def store_to_submit_and_validate_orders(
store: Store, helpers: StoreHelpers, customer_id: str
) -> Store:
app_tags: dict[str, list[OrderType]] = {
Expand Down
48 changes: 29 additions & 19 deletions tests/fixture_plugins/orders_fixtures/store_service_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,57 +16,67 @@
from cg.services.orders.storing.implementations.pool_order_service import StorePoolOrderService
from cg.store.store import Store
from tests.mocks.limsmock import MockLimsAPI
from tests.store_helpers import StoreHelpers


@pytest.fixture
def store_generic_order_service(base_store: Store, lims_api: MockLimsAPI) -> StoreCaseOrderService:
return StoreCaseOrderService(status_db=base_store, lims_service=OrderLimsService(lims_api))
def store_generic_order_service(
store_to_submit_and_validate_orders: Store, lims_api: MockLimsAPI
) -> StoreCaseOrderService:
return StoreCaseOrderService(
status_db=store_to_submit_and_validate_orders, lims_service=OrderLimsService(lims_api)
)


@pytest.fixture
def store_pool_order_service(
store_with_rml_applications: Store, lims_api: MockLimsAPI
store_to_submit_and_validate_orders: Store, lims_api: MockLimsAPI
) -> StorePoolOrderService:
return StorePoolOrderService(
status_db=store_with_rml_applications, lims_service=OrderLimsService(lims_api)
status_db=store_to_submit_and_validate_orders, lims_service=OrderLimsService(lims_api)
)


@pytest.fixture
def store_fastq_order_service(base_store: Store, lims_api: MockLimsAPI) -> StoreFastqOrderService:
return StoreFastqOrderService(status_db=base_store, lims_service=OrderLimsService(lims_api))
def store_fastq_order_service(
store_to_submit_and_validate_orders: Store, lims_api: MockLimsAPI
) -> StoreFastqOrderService:
return StoreFastqOrderService(
status_db=store_to_submit_and_validate_orders, lims_service=OrderLimsService(lims_api)
)


@pytest.fixture
def store_pacbio_order_service(base_store: Store, lims_api: MockLimsAPI) -> StorePacBioOrderService:
return StorePacBioOrderService(status_db=base_store, lims_service=OrderLimsService(lims_api))
def store_pacbio_order_service(
store_to_submit_and_validate_orders: Store, lims_api: MockLimsAPI
) -> StorePacBioOrderService:
return StorePacBioOrderService(
status_db=store_to_submit_and_validate_orders, lims_service=OrderLimsService(lims_api)
)


@pytest.fixture
def store_metagenome_order_service(
metagenome_storing_store: Store, lims_api: MockLimsAPI, helpers: StoreHelpers
store_to_submit_and_validate_orders: Store, lims_api: MockLimsAPI
) -> StoreMetagenomeOrderService:
return StoreMetagenomeOrderService(
status_db=metagenome_storing_store, lims_service=OrderLimsService(lims_api)
status_db=store_to_submit_and_validate_orders, lims_service=OrderLimsService(lims_api)
)


@pytest.fixture
def store_microbial_order_service(
base_store: Store, lims_api: MockLimsAPI, helpers: StoreHelpers
store_to_submit_and_validate_orders: Store,
lims_api: MockLimsAPI,
) -> StoreMicrobialOrderService:
helpers.ensure_application_version(store=base_store, application_tag="MWRNXTR003")
helpers.ensure_application_version(store=base_store, application_tag="MWXNXTR003")
helpers.ensure_application_version(store=base_store, application_tag="VWGNXTR001")

return StoreMicrobialOrderService(status_db=base_store, lims_service=OrderLimsService(lims_api))
return StoreMicrobialOrderService(
status_db=store_to_submit_and_validate_orders, lims_service=OrderLimsService(lims_api)
)


@pytest.fixture
def store_microbial_fastq_order_service(
base_store: Store, lims_api: MockLimsAPI
store_to_submit_and_validate_orders: Store, lims_api: MockLimsAPI
) -> StoreMicrobialFastqOrderService:
return StoreMicrobialFastqOrderService(
status_db=base_store, lims_service=OrderLimsService(lims_api)
status_db=store_to_submit_and_validate_orders, lims_service=OrderLimsService(lims_api)
)
4 changes: 2 additions & 2 deletions tests/fixtures/cgweb_orders/sarscov2.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"internal_id": null,
"lab_code": null,
"mother": null,
"name": "sarscov2sample1",
"name": "control-positive",
"organism": "SARS-CoV-2",
"organism_other": "",
"original_lab": "Karolinska University Hospital Solna",
Expand Down Expand Up @@ -94,7 +94,7 @@
"internal_id": null,
"lab_code": null,
"mother": null,
"name": "sarscov2sample2",
"name": "control-negative",
"organism": "SARS-CoV-2",
"organism_other": "",
"original_lab": "Synlab Medilab",
Expand Down
84 changes: 53 additions & 31 deletions tests/services/orders/store_service/test_fastq_order_service.py
Original file line number Diff line number Diff line change
@@ -1,61 +1,77 @@
"""
Module to test the store_order_data_in_status_db method of the StoreFastqOrderService class.
The function store_order_data_in_status_db is never expected to fail, as its input order should
have always been validated before calling the function.
"""

from cg.constants import DataDelivery, Workflow
from cg.constants.sequencing import SeqLibraryPrepCategory
from cg.services.orders.storing.constants import MAF_ORDER_ID
from cg.services.orders.storing.implementations.fastq_order_service import StoreFastqOrderService
from cg.services.orders.validation.workflows.fastq.models.order import FastqOrder
from cg.store.models import Application, Case, Order, Sample
from cg.store.store import Store
from tests.store_helpers import StoreHelpers


def test_store_order_data_in_status_db(
base_store: Store,
store_to_submit_and_validate_orders: Store,
store_fastq_order_service: StoreFastqOrderService,
fastq_order: FastqOrder,
ticket_id_as_int: int,
):
"""Test that a Fastq order is stored in the database."""
# GIVEN a basic store with no samples and a fastq order
assert not base_store._get_query(table=Sample).first()
assert base_store._get_query(table=Case).count() == 0
"""Test that a Fastq order with two WGS samples, one being tumour, is stored in the database."""

# GIVEN a fastq order with two WGS samples, the first one being a tumour sample

# WHEN storing an order with two WGS samples, the first one being a tumour sample
# GIVEN a basic store with no samples nor cases
assert not store_to_submit_and_validate_orders._get_query(table=Sample).first()
assert store_to_submit_and_validate_orders._get_query(table=Case).count() == 0

# WHEN storing the order
new_samples: list[Sample] = store_fastq_order_service.store_order_data_in_status_db(fastq_order)

# THEN it should store the order
assert store_to_submit_and_validate_orders.get_order_by_ticket_id(ticket_id_as_int)

# THEN it should store the samples
assert len(new_samples) == 2
assert len(base_store._get_query(table=Sample).all()) == 2
db_samples: list[Sample] = store_to_submit_and_validate_orders._get_query(table=Sample).all()
assert set(new_samples) == set(db_samples)

# THEN it should create one case for the analysis and one MAF case
cases: list[Case] = base_store._get_query(table=Case).all()
cases: list[Case] = store_to_submit_and_validate_orders._get_query(table=Case).all()
assert len(cases) == 2
assert len(new_samples[0].links) == 2
assert len(db_samples[0].links) == 2
assert cases[0].data_analysis == Workflow.MIP_DNA
assert cases[1].data_analysis == Workflow.RAW_DATA

# THEN the analysis case has allowed data deliveries
assert cases[1].data_delivery in [DataDelivery.FASTQ, DataDelivery.NO_DELIVERY]

# THEN the sample sex should be stored
assert new_samples[0].sex == "male"
assert db_samples[0].sex == "male"

# THEN the MAF order should have one case linked to the tumour negative sample
maf_order: Order = base_store.get_order_by_id(MAF_ORDER_ID)
maf_order: Order = store_to_submit_and_validate_orders.get_order_by_id(MAF_ORDER_ID)
maf_cases: list[Case] = maf_order.cases
assert len(maf_cases) == 1
assert not maf_cases[0].samples[0].is_tumour


def test_store_fastq_samples_non_tumour_wgs_to_mip_maf_case(
base_store: Store, fastq_order: FastqOrder, store_fastq_order_service: StoreFastqOrderService
store_to_submit_and_validate_orders: Store,
fastq_order: FastqOrder,
store_fastq_order_service: StoreFastqOrderService,
):
"""Test that a non-tumour WGS sample creates a MAF case with MIP as data analysis."""
# GIVEN a basic store with no samples
assert not base_store._get_query(table=Sample).first()
assert base_store._get_query(table=Case).count() == 0
# GIVEN a basic store with no samples nor cases
assert not store_to_submit_and_validate_orders._get_query(table=Sample).first()
assert store_to_submit_and_validate_orders._get_query(table=Case).count() == 0

# GIVEN a fastq order with the first sample being a non-tumour WGS sample
base_store.get_application_by_tag(fastq_order.samples[0].application).prep_category = (
SeqLibraryPrepCategory.WHOLE_GENOME_SEQUENCING
)
store_to_submit_and_validate_orders.get_application_by_tag(
fastq_order.samples[0].application
).prep_category = SeqLibraryPrepCategory.WHOLE_GENOME_SEQUENCING
fastq_order.samples[0].tumour = False

# WHEN storing the order
Expand All @@ -69,19 +85,19 @@ def test_store_fastq_samples_non_tumour_wgs_to_mip_maf_case(


def test_store_fastq_samples_tumour_wgs_to_fastq_no_maf_case(
base_store: Store,
store_to_submit_and_validate_orders: Store,
fastq_order: FastqOrder,
store_fastq_order_service: StoreFastqOrderService,
):
"""Test that a tumour WGS sample does not create MAF cases."""
# GIVEN a basic store with no samples
assert not base_store._get_query(table=Sample).first()
assert base_store._get_query(table=Case).count() == 0
assert not store_to_submit_and_validate_orders._get_query(table=Sample).first()
assert store_to_submit_and_validate_orders._get_query(table=Case).count() == 0

# GIVEN a fastq order with the second sample being a tumour WGS sample
base_store.get_application_by_tag(fastq_order.samples[0].application).prep_category = (
SeqLibraryPrepCategory.WHOLE_GENOME_SEQUENCING
)
store_to_submit_and_validate_orders.get_application_by_tag(
fastq_order.samples[0].application
).prep_category = SeqLibraryPrepCategory.WHOLE_GENOME_SEQUENCING
fastq_order.samples[1].tumour = True

# WHEN storing the order
Expand All @@ -95,20 +111,26 @@ def test_store_fastq_samples_tumour_wgs_to_fastq_no_maf_case(


def test_store_fastq_samples_non_wgs_no_maf_case(
base_store: Store,
store_to_submit_and_validate_orders: Store,
fastq_order: FastqOrder,
store_fastq_order_service: StoreFastqOrderService,
helpers: StoreHelpers,
):
"""Test that an order with non-WGS samples creates no MAF cases."""
# GIVEN a basic store with no samples
assert not base_store._get_query(table=Sample).first()
assert base_store._get_query(table=Case).count() == 0
assert not store_to_submit_and_validate_orders._get_query(table=Sample).first()
assert store_to_submit_and_validate_orders._get_query(table=Case).count() == 0

# GIVEN a fastq order with a non-WGS samples
# GIVEN that the store has application versions for the non-WGS workflow
non_wgs_prep_category = SeqLibraryPrepCategory.WHOLE_EXOME_SEQUENCING
non_wgs_applications: Application = base_store._get_query(table=Application).filter(
Application.prep_category == non_wgs_prep_category
helpers.ensure_application_version(
store=store_to_submit_and_validate_orders, prep_category=non_wgs_prep_category
)

# GIVEN a fastq order with a non-WGS samples
non_wgs_applications: Application = store_to_submit_and_validate_orders._get_query(
table=Application
).filter(Application.prep_category == non_wgs_prep_category)
assert non_wgs_applications
for sample in fastq_order.samples:
sample.application = non_wgs_applications[0].tag
Expand Down
Loading

0 comments on commit 5096370

Please sign in to comment.