Skip to content

Commit

Permalink
Add service unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
katybaulch committed Jan 2, 2025
1 parent bea964e commit 4bc2f97
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 0 deletions.
42 changes: 42 additions & 0 deletions tests/mocks/repos/corpus_type_repo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from typing import Optional

from pytest import MonkeyPatch

from app.errors import RepositoryError
from app.model.corpus_type import CorpusTypeReadDTO


def mock_corpus_type_repo(corpus_type_repo, monkeypatch: MonkeyPatch, mocker):
corpus_type_repo.valid = True
corpus_type_repo.return_empty = False
corpus_type_repo.throw_repository_error = False

def maybe_throw():
if corpus_type_repo.throw_repository_error:
raise RepositoryError("bad corpus type repo")

def mock_all(_, org_id: Optional[int]) -> list[CorpusTypeReadDTO]:
maybe_throw()
if corpus_type_repo.return_empty:
return []
return [
CorpusTypeReadDTO(
name=f"test_name_{x}", description=f"test_description_{x}", metadata={}
)
for x in range(2)
]

def mock_get(_, name: str) -> Optional[CorpusTypeReadDTO]:
maybe_throw()

if not corpus_type_repo.return_empty:
return CorpusTypeReadDTO(
name="test_name", description="test_description", metadata={}
)
return None

monkeypatch.setattr(corpus_type_repo, "all", mock_all)
mocker.spy(corpus_type_repo, "all")

monkeypatch.setattr(corpus_type_repo, "get", mock_get)
mocker.spy(corpus_type_repo, "get")
9 changes: 9 additions & 0 deletions tests/unit_tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
collection_repo,
config_repo,
corpus_repo,
corpus_type_repo,
document_repo,
event_repo,
family_repo,
Expand All @@ -48,6 +49,7 @@
from tests.mocks.repos.collection_repo import mock_collection_repo
from tests.mocks.repos.config_repo import mock_config_repo
from tests.mocks.repos.corpus_repo import mock_corpus_repo
from tests.mocks.repos.corpus_type_repo import mock_corpus_type_repo
from tests.mocks.repos.db_client_corpus_helpers import mock_corpus_helpers_db_client
from tests.mocks.repos.db_client_metadata import mock_metadata_db_client
from tests.mocks.repos.document_repo import mock_document_repo
Expand Down Expand Up @@ -150,6 +152,13 @@ def corpus_repo_mock(monkeypatch, mocker):
yield corpus_repo


@pytest.fixture
def corpus_type_repo_mock(monkeypatch, mocker):
"""Mocks the repository for a single test."""
mock_corpus_type_repo(corpus_type_repo, monkeypatch, mocker)
yield corpus_type_repo


@pytest.fixture
def db_client_metadata_mock(monkeypatch, mocker):
"""Mocks the repository for a single test."""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import pytest

import app.service.corpus_type as corpus_type_service
from app.errors import RepositoryError


def test_all(corpus_type_repo_mock, admin_user_context):
result = corpus_type_service.all(admin_user_context)
assert result is not None
assert corpus_type_repo_mock.all.call_count == 1


def test_all_returns_empty_list_if_no_results(
corpus_type_repo_mock, admin_user_context
):
corpus_type_repo_mock.return_empty = True
result = corpus_type_service.all(admin_user_context)
assert result == []
assert corpus_type_repo_mock.all.call_count == 1


def test_all_raises_db_error(corpus_type_repo_mock, bad_user_context):
corpus_type_repo_mock.throw_repository_error = True
with pytest.raises(RepositoryError) as e:
corpus_type_service.all(bad_user_context)
expected_msg = "bad corpus type repo"
assert e.value.message == expected_msg
assert corpus_type_repo_mock.all.call_count == 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import app.service.corpus_type as corpus_type_service


def test_get(corpus_type_repo_mock):
result = corpus_type_service.get("some_corpus_type_name")
assert result is not None
assert result.name == "test_name"
assert corpus_type_repo_mock.get.call_count == 1


def test_get_returns_none(corpus_type_repo_mock):
corpus_type_repo_mock.return_empty = True
result = corpus_type_service.get("some_corpus_type_name")
assert result is not None
assert corpus_type_repo_mock.get.call_count == 1

0 comments on commit 4bc2f97

Please sign in to comment.