86 lines
3.2 KiB
Python
86 lines
3.2 KiB
Python
"""Unit tests for JobRepository class."""
|
|
|
|
import unittest
|
|
from unittest.mock import patch, MagicMock, _patch_dict
|
|
from uuid import UUID
|
|
|
|
from data_store.repositories.job_repository import JobRepository
|
|
|
|
|
|
class TestJobRepository(unittest.TestCase):
|
|
"""Unit tests for JobRepository class."""
|
|
|
|
patcher: _patch_dict
|
|
repo: JobRepository
|
|
job_id: UUID
|
|
bad_job_id: str
|
|
|
|
@classmethod
|
|
def setUpClass(cls) -> None:
|
|
"""Set up test case with a mocked MinioAdapter."""
|
|
# Patch environment variables for MinioAdapter
|
|
cls.patcher = patch.dict(
|
|
"os.environ",
|
|
{
|
|
"MINIO_ENDPOINT": "localhost:9000",
|
|
"MINIO_ACCESS_KEY": "minioadmin",
|
|
"MINIO_SECRET_KEY": "minioadmin",
|
|
"MINIO_BUCKET": "test-bucket",
|
|
},
|
|
)
|
|
cls.patcher.start()
|
|
# Prepare JobRepository
|
|
cls.repo = JobRepository()
|
|
cls.repo._get = MagicMock(return_value=None) # type: ignore[method-assign]
|
|
cls.repo._put = MagicMock(return_value=None) # type: ignore[method-assign]
|
|
cls.repo._delete = MagicMock(return_value=None) # type: ignore[method-assign]
|
|
# Prepare other test variables
|
|
cls.job_id = UUID("d87022f8-4169-4d69-8512-bbd65cc1cb23")
|
|
cls.bad_job_id = "not-a-uuid"
|
|
|
|
@classmethod
|
|
def tearDownClass(cls) -> None:
|
|
# Stop patching
|
|
cls.patcher.stop()
|
|
|
|
def setUp(self) -> None:
|
|
"""Set up each test with a fresh JobRepository instance."""
|
|
self.repo._get.reset_mock() # type: ignore[attr-defined]
|
|
self.repo._put.reset_mock() # type: ignore[attr-defined]
|
|
self.repo._delete.reset_mock() # type: ignore[attr-defined]
|
|
|
|
def test_should_adhere_to_interface(self) -> None:
|
|
"""Test that JobRepository adheres to JobRepositoryInterface."""
|
|
# Instantiation fails if interface not adhered to
|
|
_ = JobRepository()
|
|
|
|
def test_enter_calls_super_and_returns_self(self) -> None:
|
|
"""Test that __enter__ calls super().__enter__() and returns self."""
|
|
repo = JobRepository()
|
|
with patch(
|
|
"python_repositories.adapters.MinioAdapter.__enter__", return_value=None
|
|
) as mock_super_enter:
|
|
result = repo.__enter__()
|
|
mock_super_enter.assert_called_once()
|
|
self.assertIs(result, repo)
|
|
|
|
def test_get_invalid_job_id_raises_value_error(self) -> None:
|
|
"""Test that get with invalid job_id raises ValueError."""
|
|
with self.assertRaises(ValueError):
|
|
self.repo.get(self.bad_job_id) # type: ignore[arg-type]
|
|
|
|
def test_put_invalid_job_raises_value_error(self) -> None:
|
|
"""Test that put with invalid job raises ValueError."""
|
|
with self.assertRaises(ValueError):
|
|
self.repo.put("not_a_job") # type: ignore[arg-type]
|
|
|
|
def test_delete_invalid_job_id_raises_value_error(self) -> None:
|
|
"""Test that delete with invalid job_id raises ValueError."""
|
|
with self.assertRaises(ValueError):
|
|
self.repo.delete(self.bad_job_id) # type: ignore[arg-type]
|
|
|
|
|
|
# Allows local debugging by running file as script
|
|
if __name__ == "__main__":
|
|
unittest.main()
|