Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
islean committed Dec 12, 2023
1 parent 4753c72 commit 1f5dc99
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 9 deletions.
8 changes: 4 additions & 4 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 45 additions & 5 deletions tests/meta/archive/test_archive_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
from cg.io.controller import APIRequest
from cg.meta.archive.archive import ARCHIVE_HANDLERS, FileAndSample, SpringArchiveAPI
from cg.meta.archive.ddn_dataflow import (
FAILED_JOB_STATUSES,
ONGOING_JOB_STATUSES,
AuthToken,
DDNDataFlowClient,
GetJobStatusPayload,
Expand Down Expand Up @@ -203,7 +205,11 @@ def test_archive_all_non_archived_spring_files(

@pytest.mark.parametrize(
"job_status, should_date_be_set",
[(JobStatus.COMPLETED, True), (JobStatus.RUNNING, False)],
[
(JobStatus.COMPLETED, True),
(ONGOING_JOB_STATUSES[0], False),
(FAILED_JOB_STATUSES[0], False),
],
)
def test_get_archival_status(
spring_archive_api: SpringArchiveAPI,
Expand Down Expand Up @@ -241,12 +247,19 @@ def test_get_archival_status(
)

# THEN The Archive entry should have been updated
assert bool(file.archive.archived_at) == should_date_be_set
if job_status == FAILED_JOB_STATUSES[0]:
assert not file.archive
else:
assert bool(file.archive.archived_at) == should_date_be_set


@pytest.mark.parametrize(
"job_status, should_date_be_set",
[(JobStatus.COMPLETED, True), (JobStatus.RUNNING, False)],
[
(JobStatus.COMPLETED, True),
(ONGOING_JOB_STATUSES[0], False),
(FAILED_JOB_STATUSES[0], False),
],
)
def test_get_retrieval_status(
spring_archive_api: SpringArchiveAPI,
Expand All @@ -260,14 +273,17 @@ def test_get_retrieval_status(
job_status,
should_date_be_set,
):
"""Tests that the three different categories of retrieval statuses we have identified,
i.e. failed, ongoing and successful, are handled correctly."""

# GIVEN a file with an ongoing archival
file: File = spring_archive_api.housekeeper_api.files().first()
spring_archive_api.housekeeper_api.add_archives(files=[file], archive_task_id=archival_job_id)
spring_archive_api.housekeeper_api.set_archive_retrieval_task_id(
file_id=file.id, retrieval_task_id=retrieval_job_id
)

# WHEN querying the task id and getting a "COMPLETED" response
# WHEN querying the task id
with mock.patch.object(
AuthToken,
"model_validate",
Expand All @@ -288,7 +304,10 @@ def test_get_retrieval_status(
)

# THEN The Archive entry should have been updated
assert bool(file.archive.retrieved_at) == should_date_be_set
if job_status == FAILED_JOB_STATUSES[0]:
assert not file.archive.retrieval_task_id
else:
assert bool(file.archive.retrieved_at) == should_date_be_set


def test_retrieve_samples(
Expand Down Expand Up @@ -354,16 +373,23 @@ def test_delete_file_raises_http_error(
test_auth_token: AuthToken,
archival_job_id: int,
):
"""Tests that an HTTP error is raised when the Miria response is not okay for a delete file request,
and that the file is not removed from Housekeeper."""

# GIVEN a spring file which is archived via Miria
spring_file: File = spring_archive_api.housekeeper_api.files(
tags={SequencingFileTag.SPRING, ArchiveLocations.KAROLINSKA_BUCKET}
).first()
spring_file_path: str = spring_file.path
if not spring_file.archive:
spring_archive_api.housekeeper_api.add_archives(
files=[spring_file], archive_task_id=archival_job_id
)
spring_archive_api.housekeeper_api.set_archive_archived_at(
file_id=spring_file.id, archiving_task_id=archival_job_id
)

# GIVEN that the request returns a failed response
with mock.patch.object(
DDNDataFlowClient,
"_get_auth_token",
Expand All @@ -375,15 +401,24 @@ def test_delete_file_raises_http_error(
), pytest.raises(
HTTPError
):
# WHEN trying to delete the file via Miria and in Housekeeper

# THEN an HTTPError should be raised
spring_archive_api.delete_file(spring_file.path)

# THEN the file should still be in Housekeeper
assert spring_archive_api.housekeeper_api.files(path=spring_file_path)


def test_delete_file_success(
spring_archive_api: SpringArchiveAPI,
ok_delete_file_response: Response,
test_auth_token: AuthToken,
archival_job_id: int,
):
"""Tests that given a successful response from Miria, the file is deleted and removed from Housekeeper."""

# GIVEN a spring file which is archived via Miria
spring_file: File = spring_archive_api.housekeeper_api.files(
tags={SequencingFileTag.SPRING, ArchiveLocations.KAROLINSKA_BUCKET}
).first()
Expand All @@ -396,6 +431,7 @@ def test_delete_file_success(
file_id=spring_file.id, archiving_task_id=archival_job_id
)

# GIVEN that the delete request returns a successful response
with mock.patch.object(
DDNDataFlowClient,
"_get_auth_token",
Expand All @@ -405,6 +441,10 @@ def test_delete_file_success(
"api_request_from_content",
return_value=ok_delete_file_response,
):
# WHEN trying to delete the file via Miria and in Housekeeper

# THEN no error is raised
spring_archive_api.delete_file(spring_file.path)

# THEN the file is removed from Housekeeper
assert not spring_archive_api.housekeeper_api.get_file(spring_file_id)

0 comments on commit 1f5dc99

Please sign in to comment.