-
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
9d90789
commit 21cabf9
Showing
8 changed files
with
96 additions
and
12 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
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
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
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
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,22 @@ | ||
from typing import Optional | ||
from pytest import MonkeyPatch | ||
|
||
from sqlalchemy import exc | ||
|
||
from app.model.config import ConfigReadDTO | ||
|
||
|
||
def mock_config_repo(config_repo, monkeypatch: MonkeyPatch, mocker): | ||
config_repo.return_empty = False | ||
config_repo.throw_repository_error = False | ||
|
||
def maybe_throw(): | ||
if config_repo.throw_repository_error: | ||
raise exc.SQLAlchemyError("") | ||
|
||
def mock_get(_) -> Optional[ConfigReadDTO]: | ||
maybe_throw() | ||
return ConfigReadDTO(geographies=[], taxonomies={}, languages={}) | ||
|
||
monkeypatch.setattr(config_repo, "get", mock_get) | ||
mocker.spy(config_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from pytest import MonkeyPatch | ||
from app.errors import RepositoryError | ||
|
||
from app.model.config import ConfigReadDTO | ||
|
||
|
||
def mock_config_service(config_service, monkeypatch: MonkeyPatch, mocker): | ||
config_service.throw_repository_error = False | ||
|
||
def maybe_throw(): | ||
if config_service.throw_repository_error: | ||
raise RepositoryError("bad repo") | ||
|
||
def mock_get_config() -> ConfigReadDTO: | ||
maybe_throw() | ||
return ConfigReadDTO(geographies=[], taxonomies={}, languages={}) | ||
|
||
monkeypatch.setattr(config_service, "get", mock_get_config) | ||
mocker.spy(config_service, "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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Test Approach | ||
|
||
In the routers we mock the service to generate the different behaviours. | ||
|
||
We then test that these behaviours (such as a db error) result in the correct HTTP response (in the case of a db error it should be a 503). | ||
|
||
This ensures we are just unit testing the behaviour of the routers. |
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 |
---|---|---|
@@ -1,6 +1,21 @@ | ||
from fastapi import status | ||
from fastapi.testclient import TestClient | ||
|
||
|
||
def test_get_config(client: TestClient, user_header_token): | ||
# TODO: Finish the config tests by adding mock for repo | ||
pass | ||
def test_get_config(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() | ||
keys = data.keys() | ||
assert "geographies" in keys | ||
assert "taxonomies" in keys | ||
assert "languages" in keys | ||
assert config_service_mock.get.call_count == 1 | ||
|
||
|
||
def test_get_config_when_db_error( | ||
client: TestClient, user_header_token, config_service_mock | ||
): | ||
config_service_mock.throw_repository_error = True | ||
response = client.get("/api/v1/config", headers=user_header_token) | ||
assert response.status_code == status.HTTP_503_SERVICE_UNAVAILABLE |