From 413c1aa2645c5128b199d32f1891c41dbe4dcbbd Mon Sep 17 00:00:00 2001 From: yuema137 <3124558229@qq.com> Date: Mon, 8 Apr 2024 16:43:07 -0500 Subject: [PATCH] Add test_batchq.py --- tests/test_batchq.py | 65 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 tests/test_batchq.py diff --git a/tests/test_batchq.py b/tests/test_batchq.py new file mode 100644 index 0000000..761b2ea --- /dev/null +++ b/tests/test_batchq.py @@ -0,0 +1,65 @@ +import os +from typing import Dict, Any +from copy import deepcopy +from pydantic import ValidationError +from utilix.batchq import JobSubmission, QOSNotFoundError, FormatError +import pytest +from unittest.mock import patch + +import os +from typing import Dict, Any +from pydantic import ValidationError +from utilix.batchq import JobSubmission, QOSNotFoundError +import pytest +from unittest.mock import patch + +# Fixture to provide a sample valid JobSubmission instance +@pytest.fixture +def valid_job_submission() -> JobSubmission: + return JobSubmission( + jobstring="Hello World", + qos="xenon1t", + hours=10, + container="xenonnt-development.simg", + ) + +def test_valid_jobstring(valid_job_submission: JobSubmission): + """ Test case to check if a valid jobstring is accepted. """ + assert valid_job_submission.jobstring == "Hello World" + +def test_invalid_qos(): + """ Test case to check if the appropriate validation error is raised when an invalid value is provided for the qos field. """ + with pytest.raises(QOSNotFoundError) as exc_info: + JobSubmission(jobstring="Hello World", qos="invalid_qos", hours=10, container="xenonnt-development.simg") + assert "QOS invalid_qos is not in the list of available qos" in str(exc_info.value) + +def test_valid_qos(valid_job_submission: JobSubmission): + """ Test case to check if a valid qos is accepted. """ + assert valid_job_submission.qos == "xenon1t" + +def test_invalid_hours(): + """ Test case to check if the appropriate validation error is raised when an invalid value is provided for the hours field. """ + with pytest.raises(ValidationError) as exc_info: + JobSubmission(jobstring="Hello World", qos="xenon1t", hours=100, container="xenonnt-development.simg") + assert "Hours must be between 0 and 72" in str(exc_info.value) + +def test_valid_hours(valid_job_submission: JobSubmission): + """ Test case to check if a valid hours value is accepted. """ + assert valid_job_submission.hours == 10 + +def test_invalid_container(): + """ Test case to check if the appropriate validation error is raised when an invalid value is provided for the container field. """ + with pytest.raises(FormatError) as exc_info: + JobSubmission(jobstring="Hello World", qos="xenon1t", hours=10, container="invalid.ext") + assert "Container must end with .simg" in str(exc_info.value) + +def test_valid_container(valid_job_submission: JobSubmission): + """ Test case to check if a valid container value is accepted. """ + assert valid_job_submission.container == "xenonnt-development.simg" + +def test_container_exists(valid_job_submission: JobSubmission, tmp_path: str): + """ Test case to check if the appropriate validation error is raised when the specified container does not exist. """ + with patch.dict("utilix.batchq.SINGULARITY_DIR", {"xenon1t": str(tmp_path)}): + with pytest.raises(FileNotFoundError) as exc_info: + JobSubmission(**valid_job_submission.dict()) + assert "Singularity image xenonnt-development.simg does not exist" in str(exc_info.value) \ No newline at end of file