Skip to content

Commit

Permalink
feat(refactor): Rename test fixture
Browse files Browse the repository at this point in the history
  • Loading branch information
henrikstranneheim committed Jul 7, 2023
1 parent c696e3b commit a0dcc00
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 49 deletions.
8 changes: 4 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ def fixture_trailblazer_tmp_dir(tmpdir_factory) -> Path:


@pytest.fixture(name="trailblazer_context")
def fixture_trailblazer_context(sample_store: MockStore) -> Dict[str, MockStore]:
def fixture_trailblazer_context(analysis_store: MockStore) -> Dict[str, MockStore]:
"""Trailblazer context to be used in CLI."""
return {"trailblazer": sample_store}
return {"trailblazer": analysis_store}


@pytest.fixture(name="store")
Expand Down Expand Up @@ -109,8 +109,8 @@ def fixture_raw_analyses(sample_data: Dict[str, list]) -> List[dict]:
return analyses


@pytest.fixture(name="sample_store")
def fixture_sample_store(
@pytest.fixture(name="analysis_store")
def fixture_analysis_store(
raw_analyses: List[dict],
archived_user_email: str,
archived_username: str,
Expand Down
92 changes: 47 additions & 45 deletions tests/store/test_store_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ def test_setup_db(store: MockStore):
assert store.engine.table_names()


def test_analysis(sample_store):
def test_analysis(analysis_store):
# GIVEN a store with an analysis
existing_analysis = sample_store.analyses().first()
existing_analysis = analysis_store.analyses().first()

# WHEN accessing it by ID
analysis_obj = sample_store.analysis(existing_analysis.id)
analysis_obj = analysis_store.analysis(existing_analysis.id)

# THEN it should return the same analysis
assert analysis_obj == existing_analysis
Expand All @@ -32,7 +32,7 @@ def test_analysis(sample_store):
missing_analysis_id = 12312423534

# WHEN accessing the analysis
analysis_obj = sample_store.analysis(missing_analysis_id)
analysis_obj = analysis_store.analysis(missing_analysis_id)

# THEN it should return None
assert analysis_obj is None
Expand All @@ -47,55 +47,55 @@ def test_analysis(sample_store):
("escapedgoat", True), # pending
],
)
def test_is_latest_analysis_ongoing(sample_store, family: str, expected_bool: bool):
def test_is_latest_analysis_ongoing(analysis_store, family: str, expected_bool: bool):
# GIVEN an analysis
sample_store.update_ongoing_analyses()
analysis_objs = sample_store.analyses(case_id=family).first()
analysis_store.update_ongoing_analyses()
analysis_objs = analysis_store.analyses(case_id=family).first()
assert analysis_objs is not None

# WHEN checking if the family has an ongoing analysis status
is_ongoing = sample_store.is_latest_analysis_ongoing(case_id=family)
is_ongoing = analysis_store.is_latest_analysis_ongoing(case_id=family)

# THEN it should return the expected result
assert is_ongoing is expected_bool


def test_set_analysis_uploaded(sample_store, timestamp_now: datetime):
def test_set_analysis_uploaded(analysis_store, timestamp_now: datetime):
"""Test setting analysis uploaded at for an analysis."""

# GIVEN a store with an analysis
analysis_obj: Analysis = sample_store.analyses().first()
analysis_obj: Analysis = analysis_store.analyses().first()
uploaded_at: datetime = timestamp_now

# WHEN setting an analysis uploaded at
sample_store.set_analysis_uploaded(case_id=analysis_obj.family, uploaded_at=uploaded_at)
analysis_store.set_analysis_uploaded(case_id=analysis_obj.family, uploaded_at=uploaded_at)

# THEN the column uploaded_at should be updated
assert analysis_obj.uploaded_at == uploaded_at


def test_set_analysis_failed(sample_store):
def test_set_analysis_failed(analysis_store):
"""Test setting analysis to failed for an analysis."""

# GIVEN a store with an analysis
analysis_obj: Analysis = sample_store.analyses().first()
analysis_obj: Analysis = analysis_store.analyses().first()

# WHEN setting analysis to failed
sample_store.set_analysis_status(case_id=analysis_obj.family, status="failed")
analysis_store.set_analysis_status(case_id=analysis_obj.family, status="failed")

# THEN the column status should be updated with failed.
assert analysis_obj.status == "failed"


def test_add_comment(sample_store):
def test_add_comment(analysis_store):
"""Test adding comment to an analysis object."""

# GIVEN a store with an analysis
analysis_obj: Analysis = sample_store.analyses().first()
analysis_obj: Analysis = analysis_store.analyses().first()
comment: str = "test comment"

# WHEN adding a comment
sample_store.add_comment(case_id=analysis_obj.family, comment=comment)
analysis_store.add_comment(case_id=analysis_obj.family, comment=comment)

# THEN a comment should have been added
assert analysis_obj.comment == comment
Expand All @@ -110,14 +110,14 @@ def test_add_comment(sample_store):
("escapedgoat", False), # pending
],
)
def test_is_latest_analysis_failed(sample_store, family: str, expected_bool: bool):
def test_is_latest_analysis_failed(analysis_store, family: str, expected_bool: bool):
# GIVEN an analysis
sample_store.update_ongoing_analyses()
analysis_objs = sample_store.analyses(case_id=family).first()
analysis_store.update_ongoing_analyses()
analysis_objs = analysis_store.analyses(case_id=family).first()
assert analysis_objs is not None

# WHEN checking if the family has a failed analysis status
is_failed = sample_store.is_latest_analysis_failed(case_id=family)
is_failed = analysis_store.is_latest_analysis_failed(case_id=family)

# THEN it should return the expected result
assert is_failed is expected_bool
Expand All @@ -132,14 +132,14 @@ def test_is_latest_analysis_failed(sample_store, family: str, expected_bool: boo
("escapedgoat", False), # pending
],
)
def test_is_latest_analysis_completed(sample_store, family: str, expected_bool: bool):
def test_is_latest_analysis_completed(analysis_store, family: str, expected_bool: bool):
# GIVEN an analysis
sample_store.update_ongoing_analyses()
analysis_objs = sample_store.analyses(case_id=family).first()
analysis_store.update_ongoing_analyses()
analysis_objs = analysis_store.analyses(case_id=family).first()
assert analysis_objs is not None

# WHEN checking if the family has a failed analysis status
is_failed = sample_store.is_latest_analysis_completed(case_id=family)
is_failed = analysis_store.is_latest_analysis_completed(case_id=family)

# THEN it should return the expected result
assert is_failed is expected_bool
Expand All @@ -154,14 +154,14 @@ def test_is_latest_analysis_completed(sample_store, family: str, expected_bool:
("escapedgoat", "pending"), # pending
],
)
def test_get_latest_analysis_status(sample_store, family: str, expected_status: str):
def test_get_latest_analysis_status(analysis_store, family: str, expected_status: str):
# GIVEN an analysis
sample_store.update_ongoing_analyses()
analysis_objs = sample_store.analyses(case_id=family).first()
analysis_store.update_ongoing_analyses()
analysis_objs = analysis_store.analyses(case_id=family).first()
assert analysis_objs is not None

# WHEN checking if the family has an analysis status
status = sample_store.get_latest_analysis_status(case_id=family)
status = analysis_store.get_latest_analysis_status(case_id=family)

# THEN it should return the expected result
assert status is expected_status
Expand All @@ -184,52 +184,52 @@ def test_get_latest_analysis_status(sample_store, family: str, expected_status:
("trueferret", "running"),
],
)
def test_update(sample_store, case_id, status):
def test_update(analysis_store, case_id, status):
# GIVEN an analysis
analysis_obj = sample_store.get_latest_analysis(case_id)
analysis_obj = analysis_store.get_latest_analysis(case_id)

# WHEN database is updated once
sample_store.update_run_status(analysis_obj.id)
analysis_store.update_run_status(analysis_obj.id)

# THEN analysis status is changed to what is expected
assert analysis_obj.status == status

# WHEN database is updated a second time
sample_store.update_run_status(analysis_obj.id)
analysis_store.update_run_status(analysis_obj.id)

# THEN the status is still what is expected, and no database errors were raised
assert analysis_obj.status == status


def test_mark_analyses_deleted(sample_store):
def test_mark_analyses_deleted(analysis_store):
# GIVEN case_id for a case that is not deleted
case_id = "liberatedunicorn"
analysis_obj = sample_store.get_latest_analysis(case_id)
analysis_obj = analysis_store.get_latest_analysis(case_id)
assert not analysis_obj.is_deleted

# WHEN running command
sample_store.mark_analyses_deleted(case_id=case_id)
analysis_obj = sample_store.get_latest_analysis(case_id)
analysis_store.mark_analyses_deleted(case_id=case_id)
analysis_obj = analysis_store.get_latest_analysis(case_id)

# THEN analysis is marked deleted
assert analysis_obj.is_deleted


def test_update_tower_jobs(sample_store: MockStore, tower_jobs: List[dict], case_id: str):
def test_update_tower_jobs(analysis_store: MockStore, tower_jobs: List[dict], case_id: str):
"""Assess that jobs are successfully updated when using NF Tower."""

# GIVEN an analysis without failed jobs
analysis: Analysis = sample_store.get_latest_analysis(case_id=case_id)
analysis: Analysis = analysis_store.get_latest_analysis(case_id=case_id)
assert not analysis.failed_jobs

# WHEN jobs are updated
sample_store.update_jobs(analysis=analysis, jobs=tower_jobs)
analysis_store.update_jobs(analysis=analysis, jobs=tower_jobs)

# THEN analysis object should have failed jobs
assert len(analysis.failed_jobs) == 3

# WHEN jobs are updated a second time
sample_store.update_jobs(analysis=analysis, jobs=tower_jobs[:2])
analysis_store.update_jobs(analysis=analysis, jobs=tower_jobs[:2])

# THEN failed jobs should be updated
assert len(analysis.failed_jobs) == 2
Expand All @@ -243,22 +243,24 @@ def test_update_tower_jobs(sample_store: MockStore, tower_jobs: List[dict], case
(CaseIDs.COMPLETED, TrailblazerStatus.QC.value, 1),
],
)
def test_update_tower_run_status(sample_store: MockStore, case_id: str, status: str, progress: int):
def test_update_tower_run_status(
analysis_store: MockStore, case_id: str, status: str, progress: int
):
"""Assess that an analysis status is successfully updated when using NF Tower."""

# GIVEN an analysis with pending status
analysis: Analysis = sample_store.get_latest_analysis(case_id=case_id)
analysis: Analysis = analysis_store.get_latest_analysis(case_id=case_id)
assert analysis.status == TrailblazerStatus.PENDING.value

# WHEN database is updated once
sample_store.update_run_status(analysis_id=analysis.id)
analysis_store.update_run_status(analysis_id=analysis.id)

# THEN analysis status is changed to what is expected
assert analysis.status == status
assert analysis.progress == progress

# WHEN database is updated a second time
sample_store.update_run_status(analysis_id=analysis.id)
analysis_store.update_run_status(analysis_id=analysis.id)

# THEN the status is still as before, and no database errors were raised
assert analysis.status == status
Expand Down

0 comments on commit a0dcc00

Please sign in to comment.