Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
islean committed Jan 28, 2025
1 parent 7eec70e commit 7a65589
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 62 deletions.
11 changes: 10 additions & 1 deletion cg/store/crud/read.py
Original file line number Diff line number Diff line change
Expand Up @@ -1658,6 +1658,11 @@ def _get_related_samples_query(
SampleFilter.BY_CUSTOMER_ENTRY_IDS,
],
)
if samples.count() != 1:
samples: list[Sample] = samples.all()
raise CgDataError(
f"No unique DNA sample could be found: found {len(samples)} samples: {[sample.internal_id for sample in samples]}"
)
return samples

def get_uploaded_related_dna_cases(self, rna_case: Case) -> list[Case]:
Expand All @@ -1666,6 +1671,10 @@ def get_uploaded_related_dna_cases(self, rna_case: Case) -> list[Case]:
related_dna_cases: list[Case] = []
collaborators: set[Customer] = rna_case.customer.collaborators
for rna_sample in rna_case.samples:
if not rna_sample.subject_id:
raise CgDataError(
f"Failed to link RNA sample {rna_sample.internal_id} to DNA samples - subject_id field is empty."
)

related_dna_samples_query: Query = self._get_related_samples_query(
sample=rna_sample,
Expand All @@ -1678,7 +1687,7 @@ def get_uploaded_related_dna_cases(self, rna_case: Case) -> list[Case]:
)
related_dna_cases.extend(uploaded_dna_cases)
if not related_dna_cases:
raise CaseNotFoundError(
raise CgDataError(
f"No matching uploaded DNA cases for case {rna_case.internal_id} ({rna_case.name})."
)
return related_dna_cases
Expand Down
52 changes: 26 additions & 26 deletions tests/meta/upload/scout/test_meta_upload_scoutapi_rna.py
Original file line number Diff line number Diff line change
Expand Up @@ -559,8 +559,8 @@ def test_create_rna_dna_collections(

# WHEN running the method to create a list of RNADNACollections
# with the relationships between RNA/DNA samples and DNA cases
rna_dna_collections: list[RNADNACollection] = upload_scout_api.create_rna_dna_collections(
rna_case
rna_dna_collections: list[RNADNACollection] = (
upload_scout_api.status_db.get_related_dna_cases_with_samples(rna_case)
)

# THEN the output should be a list of RNADNACollections
Expand All @@ -585,15 +585,15 @@ def test_add_rna_sample(

# WHEN running the method to create a list of RNADNACollections
# with the relationships between RNA/DNA samples and DNA cases
rna_dna_collections: list[RNADNACollection] = upload_scout_api.create_rna_dna_collections(
rna_dna_collections: list[RNADNACollection] = rna_store.get_related_dna_cases_with_samples(
rna_case
)

# THEN the resulting RNADNACollections should contain all RNA samples in the case
assert rna_sample_list
for sample in rna_sample_list:
assert sample.internal_id in [
rna_dna_collection.rna_sample_internal_id for rna_dna_collection in rna_dna_collections
rna_dna_collection.rna_sample_id for rna_dna_collection in rna_dna_collections
]


Expand All @@ -609,13 +609,15 @@ def test_link_rna_sample_to_dna_sample(
rna_sample: Sample = rna_store.get_sample_by_internal_id(rna_sample_son_id)

# WHEN creating an RNADNACollection for an RNA sample
rna_dna_collection: RNADNACollection = upload_scout_api.create_rna_dna_collection(rna_sample)
rna_dna_collection: list[RNADNACollection] = rna_store.get_related_dna_cases_with_samples(
rna_sample.links[0].case
)

# THEN the RNADNACollection should contain the RNA sample
assert rna_sample_son_id == rna_dna_collection.rna_sample_internal_id
assert rna_sample_son_id == rna_dna_collection[0].rna_sample_id

# THEN the RNADNACollection should have its dna_sample_id set to the related dna_sample_id
assert dna_sample_son_id == rna_dna_collection.dna_sample_name
assert dna_sample_son_id == rna_dna_collection[0].dna_sample_name


def test_add_dna_cases_to_dna_sample(
Expand All @@ -632,10 +634,12 @@ def test_add_dna_cases_to_dna_sample(
dna_case: Case = rna_store.get_case_by_internal_id(internal_id=dna_case_id)

# WHEN adding creating the RNADNACollection
rna_dna_collection: RNADNACollection = upload_scout_api.create_rna_dna_collection(rna_sample)
rna_dna_collection: list[RNADNACollection] = rna_store.get_related_dna_cases_with_samples(
rna_sample.links[0].case
)

# THEN the DNA cases should contain the DNA_case name associated with the DNA sample
assert dna_case.internal_id in rna_dna_collection.dna_case_ids
assert dna_case.internal_id in rna_dna_collection[0].dna_case_ids


def test_map_dna_cases_to_dna_sample_incorrect_workflow(
Expand Down Expand Up @@ -776,8 +780,8 @@ def test_upload_rna_multiqc_report_to_successful_dna_case_in_scout(
)

# WHEN finding the related DNA case
dna_case_ids: Set[str] = upload_mip_analysis_scout_api.get_unique_dna_cases_related_to_rna_case(
case_id=rna_case_id
dna_case_ids: Set[str] = upload_mip_analysis_scout_api.get_related_uploaded_dna_cases(
rna_case_id
)

# THEN the api should know that it should find related DNA cases
Expand All @@ -802,7 +806,7 @@ def test_upload_tomte_rna_multiqc_report_to_successful_dna_case_in_scout(

caplog.set_level(logging.INFO)

# GIVEN an RNA case, and an store with an rna connected to it
# GIVEN an RNA case, and a store with an rna connected to it
upload_mip_analysis_scout_api.status_db: Store = rna_store

# GIVEN an RNA case with a multiqc-htlml report
Expand All @@ -819,8 +823,8 @@ def test_upload_tomte_rna_multiqc_report_to_successful_dna_case_in_scout(
)

# WHEN finding the related DNA case
dna_case_ids: Set[str] = upload_mip_analysis_scout_api.get_unique_dna_cases_related_to_rna_case(
case_id=rna_case_id
dna_case_ids: Set[str] = upload_mip_analysis_scout_api.get_related_uploaded_dna_cases(
rna_case_id
)

# THEN the api should know that it should find related DNA cases
Expand Down Expand Up @@ -862,8 +866,8 @@ def test_upload_rna_delivery_report_to_successful_dna_case_in_scout(
)

# WHEN finding the related DNA case
dna_case_ids: Set[str] = upload_mip_analysis_scout_api.get_unique_dna_cases_related_to_rna_case(
case_id=rna_case_id
dna_case_ids: Set[str] = upload_mip_analysis_scout_api.get_related_uploaded_dna_cases(
rna_case_id
)

# THEN the api should know that it should find related DNA cases
Expand Down Expand Up @@ -905,16 +909,12 @@ def test_upload_tomte_rna_delivery_report_to_successful_dna_case_in_scout(
)

# WHEN finding the related DNA case
dna_case_ids: Set[str] = (
upload_tomte_analysis_scout_api.get_unique_dna_cases_related_to_rna_case(
case_id=rna_case_id
)
dna_case_ids: Set[str] = upload_tomte_analysis_scout_api.get_related_uploaded_dna_cases(
rna_case_id
)

dna_case_ids: Set[str] = (
upload_tomte_analysis_scout_api.get_unique_dna_cases_related_to_rna_case(
case_id=rna_case_id
)
dna_case_ids: Set[str] = upload_tomte_analysis_scout_api.get_related_uploaded_dna_cases(
rna_case_id
)
# THEN the dna case id should have been mentioned in the logging (and used in the upload)

Expand Down Expand Up @@ -956,8 +956,8 @@ def test_upload_rna_report_to_not_yet_uploaded_dna_case_in_scout(
)[0]

# WHEN finding the related DNA case with no successful upload
dna_case_ids: Set[str] = upload_mip_analysis_scout_api.get_unique_dna_cases_related_to_rna_case(
case_id=rna_case_id
dna_case_ids: Set[str] = upload_mip_analysis_scout_api.get_related_uploaded_dna_cases(
rna_case_id
)
dna_case: Case = rna_store.get_case_by_internal_id(internal_id=list(dna_case_ids)[0])
dna_case.analyses[0].uploaded_at = None
Expand Down
36 changes: 1 addition & 35 deletions tests/store/crud/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,25 +146,6 @@ def store_with_rna_and_dna_samples_and_cases(store: Store, helpers: StoreHelpers
customer_id="cust001",
)

helpers.add_sample(
store=store,
internal_id="related_dna_sample_2",
application_tag=SeqLibraryPrepCategory.TARGETED_GENOME_SEQUENCING.value,
application_type=SeqLibraryPrepCategory.TARGETED_GENOME_SEQUENCING.value,
subject_id="subject_1",
is_tumour=True,
customer_id="cust000",
)
helpers.add_sample(
store=store,
internal_id="related_dna_sample_3",
application_tag=SeqLibraryPrepCategory.WHOLE_EXOME_SEQUENCING.value,
application_type=SeqLibraryPrepCategory.WHOLE_EXOME_SEQUENCING.value,
subject_id="subject_1",
is_tumour=True,
customer_id="cust000",
)

helpers.add_sample(
store=store,
internal_id="not_related_dna_sample",
Expand Down Expand Up @@ -238,22 +219,7 @@ def related_dna_samples(store_with_rna_and_dna_samples_and_cases: Store) -> list
)
)

related_dna_sample_2: Sample = (
store_with_rna_and_dna_samples_and_cases.get_sample_by_internal_id(
internal_id="related_dna_sample_2"
)
)
related_dna_sample_3: Sample = (
store_with_rna_and_dna_samples_and_cases.get_sample_by_internal_id(
internal_id="related_dna_sample_3"
)
)

return [
related_dna_sample_1,
related_dna_sample_2,
related_dna_sample_3,
]
return [related_dna_sample_1]


@pytest.fixture
Expand Down

0 comments on commit 7a65589

Please sign in to comment.