Skip to content

Commit

Permalink
tidy unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
diversemix committed Sep 25, 2023
1 parent 21cabf9 commit 87c7f00
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 84 deletions.
15 changes: 14 additions & 1 deletion unit_tests/mocks/services/collection_service.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,48 @@
from typing import Optional
from pytest import MonkeyPatch
from app.errors import RepositoryError

from app.model.collection import CollectionReadDTO, CollectionWriteDTO
from unit_tests.helpers.collection import create_collection_dto


def mock_collection_service(collection_service, monkeypatch: MonkeyPatch, mocker):
collection_service.missing = False
collection_service.throw_repository_error = False

def maybe_throw():
if collection_service.throw_repository_error:
raise RepositoryError("bad repo")


def mock_get_all_collections():
maybe_throw()
return [create_collection_dto("test")]

def mock_get_collection(import_id: str) -> Optional[CollectionReadDTO]:
maybe_throw()
if not collection_service.missing:
return create_collection_dto(import_id)

def mock_search_collections(q: str) -> list[CollectionReadDTO]:
if q == "empty":
maybe_throw()
if collection_service.missing:
return []
else:
return [create_collection_dto("search1")]

def mock_update_collection(data: CollectionWriteDTO) -> Optional[CollectionReadDTO]:
maybe_throw()
if not collection_service.missing:
return create_collection_dto(data.import_id, data.title, data.description)

def mock_create_collection(data: CollectionWriteDTO) -> Optional[CollectionReadDTO]:
maybe_throw()
if not collection_service.missing:
return create_collection_dto(data.import_id, data.title, data.description)

def mock_delete_collection(import_id: str) -> bool:
maybe_throw()
return not collection_service.missing

monkeypatch.setattr(collection_service, "get", mock_get_collection)
Expand Down
27 changes: 14 additions & 13 deletions unit_tests/routers/test_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from unit_tests.helpers.collection import create_collection_dto


def test_get_all_collections_route_when_ok(
def test_get_all_when_ok(
client: TestClient, collection_service_mock, user_header_token
):
response = client.get("/api/v1/collections", headers=user_header_token)
Expand All @@ -22,7 +22,7 @@ def test_get_all_collections_route_when_ok(
assert collection_service_mock.all.call_count == 1


def test_get_collection_route_when_ok(
def test_get_when_ok(
client: TestClient, collection_service_mock, user_header_token
):
response = client.get("/api/v1/collections/import_id", headers=user_header_token)
Expand All @@ -32,7 +32,7 @@ def test_get_collection_route_when_ok(
assert collection_service_mock.get.call_count == 1


def test_get_collection_route_when_not_found(
def test_get_when_not_found(
client: TestClient, collection_service_mock, user_header_token
):
collection_service_mock.missing = True
Expand All @@ -43,7 +43,7 @@ def test_get_collection_route_when_not_found(
assert collection_service_mock.get.call_count == 1


def test_search_collection_route_when_ok(
def test_search_when_ok(
client: TestClient, collection_service_mock, user_header_token
):
response = client.get("/api/v1/collections/?q=anything", headers=user_header_token)
Expand All @@ -55,17 +55,18 @@ def test_search_collection_route_when_ok(
assert collection_service_mock.search.call_count == 1


def test_search_collection_route_when_not_found(
def test_search_when_not_found(
client: TestClient, collection_service_mock, user_header_token
):
response = client.get("/api/v1/collections/?q=empty", headers=user_header_token)
collection_service_mock.missing = True
response = client.get("/api/v1/collections/?q=stuff", headers=user_header_token)
assert response.status_code == status.HTTP_404_NOT_FOUND
data = response.json()
assert data["detail"] == "Collections not found for term: empty"
assert data["detail"] == "Collections not found for term: stuff"
assert collection_service_mock.search.call_count == 1


def test_update_collection_route_when_ok(
def test_update_when_ok(
client: TestClient, collection_service_mock, user_header_token
):
new_data = create_collection_dto("col1").model_dump()
Expand All @@ -78,7 +79,7 @@ def test_update_collection_route_when_ok(
assert collection_service_mock.update.call_count == 1


def test_update_collection_route_when_not_found(
def test_update_when_not_found(
client: TestClient, collection_service_mock, user_header_token
):
collection_service_mock.missing = True
Expand All @@ -92,7 +93,7 @@ def test_update_collection_route_when_not_found(
assert collection_service_mock.update.call_count == 1


def test_create_collection_route_when_ok(
def test_create_when_ok(
client: TestClient, collection_service_mock, user_header_token
):
new_data = create_collection_dto("col1").model_dump()
Expand All @@ -105,7 +106,7 @@ def test_create_collection_route_when_ok(
assert collection_service_mock.create.call_count == 1


def test_create_collection_route_when_not_found(
def test_create_when_not_found(
client: TestClient, collection_service_mock, user_header_token
):
collection_service_mock.missing = True
Expand All @@ -119,7 +120,7 @@ def test_create_collection_route_when_not_found(
assert collection_service_mock.create.call_count == 1


def test_delete_collection_route_when_ok(
def test_delete_when_ok(
client: TestClient, collection_service_mock, admin_user_header_token
):
response = client.delete(
Expand All @@ -137,7 +138,7 @@ def test_delete_collection_fails_if_not_admin(
assert collection_service_mock.delete.call_count == 0


def test_delete_collection_route_when_not_found(
def test_delete_when_not_found(
client: TestClient, collection_service_mock, admin_user_header_token
):
collection_service_mock.missing = True
Expand Down
4 changes: 2 additions & 2 deletions unit_tests/routers/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from fastapi.testclient import TestClient


def test_get_config(client: TestClient, user_header_token, config_service_mock):
def test_get_when_ok(client: TestClient, user_header_token, config_service_mock):
response = client.get("/api/v1/config", headers=user_header_token)
assert response.status_code == status.HTTP_200_OK
data = response.json()
Expand All @@ -13,7 +13,7 @@ def test_get_config(client: TestClient, user_header_token, config_service_mock):
assert config_service_mock.get.call_count == 1


def test_get_config_when_db_error(
def test_get_when_db_error(
client: TestClient, user_header_token, config_service_mock
):
config_service_mock.throw_repository_error = True
Expand Down
24 changes: 12 additions & 12 deletions unit_tests/routers/test_family.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from unit_tests.helpers.family import create_family_dto


def test_get_all_families_route_when_ok(
def test_get_all_when_ok(
client: TestClient, family_service_mock, user_header_token
):
response = client.get("/api/v1/families", headers=user_header_token)
Expand All @@ -22,7 +22,7 @@ def test_get_all_families_route_when_ok(
assert family_service_mock.all.call_count == 1


def test_get_family_route_when_ok(
def test_get_when_ok(
client: TestClient, family_service_mock, user_header_token
):
response = client.get("/api/v1/families/import_id", headers=user_header_token)
Expand All @@ -32,7 +32,7 @@ def test_get_family_route_when_ok(
assert family_service_mock.get.call_count == 1


def test_get_family_route_when_not_found(
def test_get_when_not_found(
client: TestClient, family_service_mock, user_header_token
):
family_service_mock.missing = True
Expand All @@ -43,7 +43,7 @@ def test_get_family_route_when_not_found(
assert family_service_mock.get.call_count == 1


def test_search_family_route_when_ok(
def test_search_when_ok(
client: TestClient, family_service_mock, user_header_token
):
response = client.get("/api/v1/families/?q=anything", headers=user_header_token)
Expand All @@ -55,7 +55,7 @@ def test_search_family_route_when_ok(
assert family_service_mock.search.call_count == 1


def test_search_family_route_when_not_found(
def test_search_when_not_found(
client: TestClient, family_service_mock, user_header_token
):
response = client.get("/api/v1/families/?q=empty", headers=user_header_token)
Expand All @@ -65,7 +65,7 @@ def test_search_family_route_when_not_found(
assert family_service_mock.search.call_count == 1


def test_update_family_route_when_ok(
def test_update_when_ok(
client: TestClient, family_service_mock, user_header_token
):
new_data = create_family_dto("fam1").model_dump()
Expand All @@ -76,7 +76,7 @@ def test_update_family_route_when_ok(
assert family_service_mock.update.call_count == 1


def test_update_family_route_when_not_found(
def test_update_when_not_found(
client: TestClient, family_service_mock, user_header_token
):
family_service_mock.missing = True
Expand All @@ -88,7 +88,7 @@ def test_update_family_route_when_not_found(
assert family_service_mock.update.call_count == 1


def test_create_family_route_when_ok(
def test_create_when_ok(
client: TestClient, family_service_mock, user_header_token
):
new_data = create_family_dto("fam1").model_dump()
Expand All @@ -99,7 +99,7 @@ def test_create_family_route_when_ok(
assert family_service_mock.create.call_count == 1


def test_create_family_route_when_not_found(
def test_create_when_not_found(
client: TestClient, family_service_mock, user_header_token
):
family_service_mock.missing = True
Expand All @@ -113,23 +113,23 @@ def test_create_family_route_when_not_found(
assert family_service_mock.create.call_count == 1


def test_delete_family_route_when_ok(
def test_delete_when_ok(
client: TestClient, family_service_mock, admin_user_header_token
):
response = client.delete("/api/v1/families/fam1", headers=admin_user_header_token)
assert response.status_code == status.HTTP_200_OK
assert family_service_mock.delete.call_count == 1


def test_delete_family_fails_if_not_admin(
def test_delete_fails_when_not_admin(
client: TestClient, family_service_mock, user_header_token
):
response = client.delete("/api/v1/families/fam1", headers=user_header_token)
assert response.status_code == status.HTTP_403_FORBIDDEN
assert family_service_mock.delete.call_count == 0


def test_delete_family_route_when_not_found(
def test_delete_when_not_found(
client: TestClient, family_service_mock, admin_user_header_token
):
family_service_mock.missing = True
Expand Down
8 changes: 4 additions & 4 deletions unit_tests/service/test_authorisation_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def test_password_hash_matches():
assert auth_service.verify_password(PLAIN_PASSWORD, HASH_PASSWORD) is True


def test_auth_user_raises_when_no_user(
def test_raises_when_user_not_found(
app_user_repo_mock,
):
app_user_repo_mock.error = True
Expand All @@ -24,7 +24,7 @@ def test_auth_user_raises_when_no_user(
assert app_user_repo_mock.get_user_by_email.call_count == 1


def test_auth_user_raises_when_no_match(
def test_raises_when_incorrect_password(
app_user_repo_mock,
):
with pytest.raises(AuthenticationError) as e:
Expand All @@ -34,7 +34,7 @@ def test_auth_user_raises_when_no_match(
assert app_user_repo_mock.get_user_by_email.call_count == 1


def test_auth_user_raises_when_no_password(
def test_raises_when_no_password(
app_user_repo_mock,
):
with pytest.raises(AuthenticationError) as e:
Expand All @@ -44,7 +44,7 @@ def test_auth_user_raises_when_no_password(
assert app_user_repo_mock.get_user_by_email.call_count == 1


def test_auth_user_can_auth(
def test_can_auth(
app_user_repo_mock,
):
token = auth_service.authenticate_user(VALID_USERNAME, PLAIN_PASSWORD)
Expand Down
Loading

0 comments on commit 87c7f00

Please sign in to comment.