Skip to content

Commit

Permalink
Fix warnings in tests (#2617)(patch)
Browse files Browse the repository at this point in the history
Cleaned up some warnings in the tests due to not following the pattern where the caller is supposed to add objects to the session
  • Loading branch information
seallard authored Oct 26, 2023
1 parent 225a29c commit 8a39503
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 68 deletions.
10 changes: 7 additions & 3 deletions tests/cli/upload/test_cli_upload_observations.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from cg.meta.observations.mip_dna_observations_api import MipDNAObservationsAPI
from cg.models.cg_config import CGConfig
from cg.store import Store
from cg.store.models import Family, Sample
from cg.store.models import Family, FamilySample, Sample
from tests.store_helpers import StoreHelpers


Expand Down Expand Up @@ -143,8 +143,12 @@ def test_get_sequencing_method_exception(
sample_wes: Sample = helpers.add_sample(
store, application_tag=external_wes_application_tag, application_type=SequencingMethod.WES
)
link_1 = store.relate_sample(family=case, sample=sample_wgs, status=PhenotypeStatus.UNKNOWN)
link_2 = store.relate_sample(family=case, sample=sample_wes, status=PhenotypeStatus.UNKNOWN)
link_1: FamilySample = store.relate_sample(
family=case, sample=sample_wgs, status=PhenotypeStatus.UNKNOWN
)
link_2: FamilySample = store.relate_sample(
family=case, sample=sample_wes, status=PhenotypeStatus.UNKNOWN
)
store.session.add_all([link_1, link_2])

# WHEN getting the sequencing method
Expand Down
13 changes: 9 additions & 4 deletions tests/store/api/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from cg.constants.subject import PhenotypeStatus
from cg.meta.orders.pool_submitter import PoolSubmitter
from cg.store import Store
from cg.store.models import FamilySample
from tests.meta.demultiplex.conftest import populated_flow_cell_store
from tests.store_helpers import StoreHelpers

Expand Down Expand Up @@ -478,9 +479,10 @@ def store_with_analyses_for_cases(
completed_at=timestamp_now,
)
sample = helpers.add_sample(analysis_store, delivered_at=timestamp_now)
analysis_store.relate_sample(
link: FamilySample = analysis_store.relate_sample(
family=oldest_analysis.family, sample=sample, status=PhenotypeStatus.UNKNOWN
)
analysis_store.session.add(link)

return analysis_store

Expand Down Expand Up @@ -517,9 +519,10 @@ def store_with_analyses_for_cases_not_uploaded_fluffy(
pipeline=Pipeline.FLUFFY,
)
sample = helpers.add_sample(analysis_store, delivered_at=timestamp_now)
analysis_store.relate_sample(
link: FamilySample = analysis_store.relate_sample(
family=oldest_analysis.family, sample=sample, status=PhenotypeStatus.UNKNOWN
)
analysis_store.session.add(link)
return analysis_store


Expand Down Expand Up @@ -556,9 +559,10 @@ def store_with_analyses_for_cases_not_uploaded_microsalt(
pipeline=Pipeline.MICROSALT,
)
sample = helpers.add_sample(analysis_store, delivered_at=timestamp_now)
analysis_store.relate_sample(
link: FamilySample = analysis_store.relate_sample(
family=oldest_analysis.family, sample=sample, status=PhenotypeStatus.UNKNOWN
)
analysis_store.session.add(link)
return analysis_store


Expand Down Expand Up @@ -596,8 +600,9 @@ def store_with_analyses_for_cases_to_deliver(
pipeline=Pipeline.MIP_DNA,
)
sample = helpers.add_sample(analysis_store, delivered_at=None)
analysis_store.relate_sample(
link: FamilySample = analysis_store.relate_sample(
family=oldest_analysis.family, sample=sample, status=PhenotypeStatus.UNKNOWN
)
analysis_store.session.add(link)

return analysis_store
26 changes: 20 additions & 6 deletions tests/store/api/status/test_analyses_to_clean.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from cg.constants import Pipeline
from cg.store import Store
from cg.store.models import FamilySample


def test_analysis_included(
Expand All @@ -18,7 +19,10 @@ def test_analysis_included(
cleaned_at=None,
)
sample = helpers.add_sample(analysis_store, delivered_at=timestamp_yesterday)
analysis_store.relate_sample(family=analysis.family, sample=sample, status="unknown")
link: FamilySample = analysis_store.relate_sample(
family=analysis.family, sample=sample, status="unknown"
)
analysis_store.session.add(link)

# WHEN calling the analyses_to_clean
analyses_to_clean = analysis_store.get_analyses_to_clean(before=timestamp_now)
Expand All @@ -35,7 +39,9 @@ def test_analysis_excluded(analysis_store: Store, helpers, timestamp_now: dateti
analysis_store, started_at=timestamp_now, uploaded_at=None, cleaned_at=None
)
sample = helpers.add_sample(analysis_store, delivered_at=timestamp_now)
link = analysis_store.relate_sample(family=analysis.family, sample=sample, status="unknown")
link: FamilySample = analysis_store.relate_sample(
family=analysis.family, sample=sample, status="unknown"
)
analysis_store.session.add(link)

# WHEN calling the analyses_to_clean
Expand All @@ -60,7 +66,9 @@ def test_pipeline_included(
cleaned_at=None,
)
sample = helpers.add_sample(analysis_store, delivered_at=timestamp_yesterday)
link = analysis_store.relate_sample(family=analysis.family, sample=sample, status="unknown")
link: FamilySample = analysis_store.relate_sample(
family=analysis.family, sample=sample, status="unknown"
)
analysis_store.session.add(link)

# WHEN calling the analyses_to_clean specifying the used pipeline
Expand All @@ -87,7 +95,9 @@ def test_pipeline_excluded(analysis_store: Store, helpers, timestamp_now: dateti
cleaned_at=None,
)
sample = helpers.add_sample(analysis_store, delivered_at=timestamp_now)
link = analysis_store.relate_sample(family=analysis.family, sample=sample, status="unknown")
link: FamilySample = analysis_store.relate_sample(
family=analysis.family, sample=sample, status="unknown"
)
analysis_store.session.add(link)

# WHEN calling the analyses_to_clean specifying another pipeline
Expand All @@ -110,7 +120,9 @@ def test_non_cleaned_included(
cleaned_at=None,
)
sample = helpers.add_sample(analysis_store, delivered_at=timestamp_yesterday)
link = analysis_store.relate_sample(family=analysis.family, sample=sample, status="unknown")
link: FamilySample = analysis_store.relate_sample(
family=analysis.family, sample=sample, status="unknown"
)
analysis_store.session.add(link)

# WHEN calling the analyses_to_clean
Expand All @@ -131,7 +143,9 @@ def test_cleaned_excluded(analysis_store: Store, helpers, timestamp_now: datetim
cleaned_at=timestamp_now,
)
sample = helpers.add_sample(analysis_store, delivered_at=timestamp_now)
link = analysis_store.relate_sample(family=analysis.family, sample=sample, status="unknown")
link: FamilySample = analysis_store.relate_sample(
family=analysis.family, sample=sample, status="unknown"
)
analysis_store.session.add(link)

# WHEN calling the analyses_to_clean
Expand Down
6 changes: 4 additions & 2 deletions tests/store/api/status/test_analyses_to_delivery_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from cg.constants import DataDelivery, Pipeline
from cg.constants.subject import PhenotypeStatus
from cg.store import Store
from cg.store.models import FamilySample
from cg.utils.date import get_date


Expand All @@ -20,7 +21,7 @@ def test_missing(analysis_store: Store, helpers, timestamp_now):
data_delivery=DataDelivery.SCOUT,
)
sample = helpers.add_sample(analysis_store, delivered_at=timestamp_now)
link = analysis_store.relate_sample(
link: FamilySample = analysis_store.relate_sample(
family=analysis.family, sample=sample, status=PhenotypeStatus.UNKNOWN
)
analysis_store.session.add(link)
Expand Down Expand Up @@ -56,9 +57,10 @@ def test_outdated_analysis(analysis_store, helpers, timestamp_now, timestamp_yes
sample = helpers.add_sample(analysis_store, delivered_at=timestamp_now)

# GIVEN a store sample case relation
analysis_store.relate_sample(
link: FamilySample = analysis_store.relate_sample(
family=analysis.family, sample=sample, status=PhenotypeStatus.UNKNOWN
)
analysis_store.session.add(link)

# WHEN calling the analyses_to_delivery_report
analyses = analysis_store.analyses_to_delivery_report(pipeline=pipeline).all()
Expand Down
11 changes: 8 additions & 3 deletions tests/store/api/status/test_store_api_status_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from cg.constants.constants import CaseActions
from cg.constants.subject import PhenotypeStatus
from cg.store import Store
from cg.store.models import Analysis, Family, Sample
from cg.store.models import Analysis, Family, FamilySample, Sample
from tests.store_helpers import StoreHelpers


Expand Down Expand Up @@ -301,8 +301,13 @@ def test_one_of_two_sequenced_samples(
not_sequenced_sample: Sample = helpers.add_sample(base_store, reads_updated_at=None)

# GIVEN a database with a case with one of one sequenced samples and no analysis
base_store.relate_sample(test_case, sequenced_sample, PhenotypeStatus.UNKNOWN)
base_store.relate_sample(test_case, not_sequenced_sample, PhenotypeStatus.UNKNOWN)
link_1: FamilySample = base_store.relate_sample(
test_case, sequenced_sample, PhenotypeStatus.UNKNOWN
)
link_2: FamilySample = base_store.relate_sample(
test_case, not_sequenced_sample, PhenotypeStatus.UNKNOWN
)
base_store.session.add_all([link_1, link_2])

# WHEN getting cases to analyse
cases: list[Family] = base_store.cases_to_analyze(pipeline=Pipeline.MIP_DNA, threshold=True)
Expand Down
63 changes: 37 additions & 26 deletions tests/store/api/status/test_store_api_status_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from cg.constants import CASE_ACTIONS, DataDelivery, Pipeline, Priority
from cg.store import Store
from cg.store.models import Analysis, Family
from cg.store.models import Analysis, Family, FamilySample


def test_delivered_at_affects_tat(base_store: Store, helpers):
Expand Down Expand Up @@ -113,10 +113,10 @@ def test_samples_flowcell(base_store: Store, helpers):
new_case = add_case(helpers, base_store)
sample_on_flowcell = helpers.add_sample(base_store)
flowcell = helpers.add_flow_cell(base_store, samples=[sample_on_flowcell], status="ondisk")
link_1 = base_store.relate_sample(new_case, sample_on_flowcell, "unknown")
link_1: FamilySample = base_store.relate_sample(new_case, sample_on_flowcell, "unknown")
base_store.session.add(link_1)
sample_not_on_flowcell = helpers.add_sample(base_store)
link_2 = base_store.relate_sample(new_case, sample_not_on_flowcell, "unknown")
link_2: FamilySample = base_store.relate_sample(new_case, sample_not_on_flowcell, "unknown")
base_store.session.add(link_2)

# WHEN getting active cases
Expand Down Expand Up @@ -198,8 +198,8 @@ def test_received_at_is_newest_date(base_store: Store, helpers):
yesteryear = datetime.now() - timedelta(days=365)
newest_sample = helpers.add_sample(base_store, received_at=yesterday)
oldest_sample = helpers.add_sample(base_store, received_at=yesteryear)
link_1 = base_store.relate_sample(new_case, newest_sample, "unknown")
link_2 = base_store.relate_sample(new_case, oldest_sample, "unknown")
link_1: FamilySample = base_store.relate_sample(new_case, newest_sample, "unknown")
link_2: FamilySample = base_store.relate_sample(new_case, oldest_sample, "unknown")
base_store.session.add_all([link_1, link_2])

# WHEN getting active cases
Expand All @@ -220,8 +220,8 @@ def test_prepared_at_is_newest_date(base_store: Store, helpers):
yesteryear = datetime.now() - timedelta(days=365)
newest_sample = helpers.add_sample(base_store, prepared_at=yesterday)
oldest_sample = helpers.add_sample(base_store, prepared_at=yesteryear)
link_1 = base_store.relate_sample(new_case, newest_sample, "unknown")
link_2 = base_store.relate_sample(new_case, oldest_sample, "unknown")
link_1: FamilySample = base_store.relate_sample(new_case, newest_sample, "unknown")
link_2: FamilySample = base_store.relate_sample(new_case, oldest_sample, "unknown")
base_store.session.add_all([link_1, link_2])

# WHEN getting active cases
Expand All @@ -242,8 +242,8 @@ def test_sequenced_at_is_newest_date(base_store: Store, helpers):
yesteryear = datetime.now() - timedelta(days=365)
newest_sample = helpers.add_sample(base_store, reads_updated_at=yesterday)
oldest_sample = helpers.add_sample(base_store, reads_updated_at=yesteryear)
link_1 = base_store.relate_sample(new_case, newest_sample, "unknown")
link_2 = base_store.relate_sample(new_case, oldest_sample, "unknown")
link_1: FamilySample = base_store.relate_sample(new_case, newest_sample, "unknown")
link_2: FamilySample = base_store.relate_sample(new_case, oldest_sample, "unknown")
base_store.session.add_all([link_1, link_2])

# WHEN getting active cases
Expand All @@ -264,8 +264,8 @@ def test_delivered_at_is_newest_date(base_store: Store, helpers):
yesteryear = datetime.now() - timedelta(days=365)
newest_sample = helpers.add_sample(base_store, delivered_at=yesterday)
oldest_sample = helpers.add_sample(base_store, delivered_at=yesteryear)
link_1 = base_store.relate_sample(new_case, newest_sample, "unknown")
link_2 = base_store.relate_sample(new_case, oldest_sample, "unknown")
link_1: FamilySample = base_store.relate_sample(new_case, newest_sample, "unknown")
link_2: FamilySample = base_store.relate_sample(new_case, oldest_sample, "unknown")
base_store.session.add_all([link_1, link_2])

# WHEN getting active cases
Expand All @@ -292,8 +292,8 @@ def test_invoiced_at_is_newest_invoice_date(base_store: Store, helpers):
invoice = base_store.add_invoice(helpers.ensure_customer(base_store))
oldest_sample.invoice = invoice
oldest_sample.invoice.invoiced_at = yesteryear
link_1 = base_store.relate_sample(new_case, newest_sample, "unknown")
link_2 = base_store.relate_sample(new_case, oldest_sample, "unknown")
link_1: FamilySample = base_store.relate_sample(new_case, newest_sample, "unknown")
link_2: FamilySample = base_store.relate_sample(new_case, oldest_sample, "unknown")
base_store.session.add_all([link_1, link_2])

# WHEN getting active cases
Expand Down Expand Up @@ -598,11 +598,14 @@ def test_show_multiple_data_analysis(base_store: Store, helpers):
data_analysis = Pipeline.BALSAMIC
new_case = add_case(helpers, base_store, data_analysis=data_analysis)
sample1 = helpers.add_sample(base_store)
base_store.relate_sample(new_case, sample1, "unknown")
link_1: FamilySample = base_store.relate_sample(new_case, sample1, "unknown")
base_store.session.add(link_1)
new_case2 = add_case(helpers, base_store, case_id="new_case2", data_analysis=data_analysis)
sample2 = helpers.add_sample(base_store)
base_store.relate_sample(new_case, sample2, "unknown")
base_store.relate_sample(new_case2, sample2, "unknown")
link_2: FamilySample = base_store.relate_sample(new_case, sample2, "unknown")
link_3: FamilySample = base_store.relate_sample(new_case2, sample2, "unknown")
base_store.session.add_all([link_2, link_3])
base_store.session.commit()

# WHEN getting active cases by data_analysis
cases = base_store.cases(data_analysis=str(data_analysis)[:-1])
Expand Down Expand Up @@ -826,7 +829,8 @@ def test_only_prepared_cases(base_store: Store, helpers):
base_store.session.add(link)
neg_new_case = add_case(helpers, base_store, "neg_new_case")
neg_sample = helpers.add_sample(base_store, name="neg_sample")
base_store.relate_sample(neg_new_case, neg_sample, "unknown")
link = base_store.relate_sample(neg_new_case, neg_sample, "unknown")
base_store.session.add(link)

# WHEN getting active cases excluding prepared
cases = base_store.cases(only_prepared=True)
Expand All @@ -847,7 +851,8 @@ def test_only_received_cases(base_store: Store, helpers):
base_store.session.add(link)
neg_new_case = add_case(helpers, base_store, "neg_new_case")
neg_sample = helpers.add_sample(base_store, name="neg_sample")
base_store.relate_sample(neg_new_case, neg_sample, "unknown")
link = base_store.relate_sample(neg_new_case, neg_sample, "unknown")
base_store.session.add(link)

# WHEN getting active cases excluding received
cases = base_store.cases(only_received=True)
Expand All @@ -868,7 +873,8 @@ def test_only_sequenced_cases(base_store: Store, helpers):
base_store.session.add(link)
neg_new_case = add_case(helpers, base_store, "neg_new_case")
neg_sample = helpers.add_sample(base_store, name="neg_sample")
base_store.relate_sample(neg_new_case, neg_sample, "unknown")
link = base_store.relate_sample(neg_new_case, neg_sample, "unknown")
base_store.session.add(link)

# WHEN getting active cases excluding sequenced
cases = base_store.cases(only_sequenced=True)
Expand All @@ -889,7 +895,8 @@ def test_only_delivered_cases(base_store: Store, helpers):
base_store.session.add(link)
neg_new_case = add_case(helpers, base_store, "neg_new_case")
neg_sample = helpers.add_sample(base_store, name="neg_sample")
base_store.relate_sample(neg_new_case, neg_sample, "unknown")
link = base_store.relate_sample(neg_new_case, neg_sample, "unknown")
base_store.session.add(link)

# WHEN getting active cases excluding delivered
cases = base_store.cases(only_delivered=True)
Expand All @@ -907,7 +914,8 @@ def test_only_uploaded_cases(base_store: Store, helpers):
helpers.add_analysis(base_store, uploaded_at=datetime.now())
neg_new_case = add_case(helpers, base_store, "neg_new_case")
neg_sample = helpers.add_sample(base_store, name="neg_sample")
base_store.relate_sample(neg_new_case, neg_sample, "unknown")
link = base_store.relate_sample(neg_new_case, neg_sample, "unknown")
base_store.session.add(link)

# WHEN getting active cases excluding uploaded
cases = base_store.cases(only_uploaded=True)
Expand All @@ -925,7 +933,8 @@ def test_only_delivery_reported_cases(base_store: Store, helpers):
helpers.add_analysis(base_store, delivery_reported_at=datetime.now())
neg_new_case = add_case(helpers, base_store, "neg_new_case")
neg_sample = helpers.add_sample(base_store, name="neg_sample")
base_store.relate_sample(neg_new_case, neg_sample, "unknown")
link = base_store.relate_sample(neg_new_case, neg_sample, "unknown")
base_store.session.add(link)

# WHEN getting active cases excluding delivery_reported
cases = base_store.cases(only_delivery_reported=True)
Expand All @@ -948,7 +957,8 @@ def test_only_invoiced_cases(base_store: Store, helpers):
base_store.session.add(link)
neg_new_case = add_case(helpers, base_store, "neg_new_case")
neg_sample = helpers.add_sample(base_store, name="neg_sample")
base_store.relate_sample(neg_new_case, neg_sample, "unknown")
link = base_store.relate_sample(neg_new_case, neg_sample, "unknown")
base_store.session.add(link)

# WHEN getting active cases excluding invoiced
cases = base_store.cases(only_invoiced=True)
Expand All @@ -966,7 +976,8 @@ def test_only_analysed_cases(base_store: Store, helpers):
helpers.add_analysis(base_store, completed_at=datetime.now())
neg_new_case = add_case(helpers, base_store, "neg_new_case")
neg_sample = helpers.add_sample(base_store, name="neg_sample")
base_store.relate_sample(neg_new_case, neg_sample, "unknown")
link = base_store.relate_sample(neg_new_case, neg_sample, "unknown")
base_store.session.add(link)

# WHEN getting active cases excluding not analysed
cases = base_store.cases(only_analysed=True)
Expand Down Expand Up @@ -1331,8 +1342,8 @@ def test_one_of_two_samples_received(base_store: Store, helpers):
new_case = add_case(helpers, base_store)
sample_received = helpers.add_sample(base_store, "sample_received", received_at=datetime.now())
sample_not_received = helpers.add_sample(base_store, "sample_not_received", received_at=None)
link_1 = base_store.relate_sample(new_case, sample_received, "unknown")
link_2 = base_store.relate_sample(new_case, sample_not_received, "unknown")
link_1: FamilySample = base_store.relate_sample(new_case, sample_received, "unknown")
link_2: FamilySample = base_store.relate_sample(new_case, sample_not_received, "unknown")
base_store.session.add_all([link_1, link_2])
assert sample_received.received_at is not None
assert sample_not_received.received_at is None
Expand Down
Loading

0 comments on commit 8a39503

Please sign in to comment.