-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bea964e
commit 4bc2f97
Showing
4 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
tests/unit_tests/service/corpus_type/test_all_corpus_type_service.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
15 changes: 15 additions & 0 deletions
15
tests/unit_tests/service/corpus_type/test_get_corpus_type_service.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |