Skip to content

Commit

Permalink
Move all tests under root test dir (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
katybaulch authored Mar 26, 2024
1 parent 2f21b4a commit df91ea6
Show file tree
Hide file tree
Showing 88 changed files with 130 additions and 129 deletions.
3 changes: 1 addition & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ RUN poetry config virtualenvs.create false && poetry install --no-directory --no

# Now code
COPY ./app ./app
COPY ./unit_tests ./unit_tests
COPY ./integration_tests ./integration_tests
COPY ./tests ./tests

CMD python app/main.py
7 changes: 4 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ build_dev:
docker compose build

unit_test:
docker compose run --rm navigator-admin-backend pytest -vvv unit_tests
docker compose run --rm navigator-admin-backend pytest -vvv tests/unit_tests

integration_test:
docker compose run --rm navigator-admin-backend pytest -vvv integration_tests
docker compose run --rm navigator-admin-backend pytest -vvv tests/integration_tests

test: unit_test integration_test
test:
docker compose run --rm navigator-admin-backend pytest -vvv tests

run:
docker compose -f docker-compose.yml up -d --remove-orphans
Expand Down
5 changes: 5 additions & 0 deletions app/repository/protocols.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
class FamilyRepo(Protocol):
"""The interface definition for a FamilyRepo"""

# set some attributes for testing purposes...
return_empty: bool = False
throw_repository_error: bool = False
throw_timeout_error: bool = False

@staticmethod
def all(db: Session) -> list[FamilyReadDTO]:
"""Returns all the families"""
Expand Down
8 changes: 2 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ name = "admin_backend"
version = "0.0.1"
description = ""
authors = ["CPR-dev-team <[email protected]>"]
packages = [
{ include = "app" },
{ include = "integration_tests" },
{ include = "unit_tests" },
]
packages = [{ include = "app" }, { include = "tests" }]

[tool.poetry.dependencies]
python = "^3.9"
Expand Down Expand Up @@ -69,7 +65,7 @@ target-version = ["py39"]
exclude = "^/alembic/versions/"

[tool.pyright]
include = ["app", "unit_tests", "integration_tests"]
include = ["app", "tests"]
exclude = ["**/__pycache__"]
pythonVersion = "3.9"
venv = "admin-backend"
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 4 additions & 2 deletions unit_tests/helpers/document.py → tests/helpers/document.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from datetime import datetime
from typing import Optional, cast
from typing import Optional, Union, cast

from db_client.models.dfce.family import DocumentStatus
from pydantic import AnyHttpUrl
Expand All @@ -11,9 +11,11 @@ def create_document_create_dto(
family_import_id="test.family.1.0",
title: str = "title",
variant_name: Optional[str] = "Original Language",
source_url: Optional[AnyHttpUrl] = cast(AnyHttpUrl, "http://source"),
source_url: Optional[Union[str, AnyHttpUrl]] = "http://source",
user_language_name: Optional[str] = None,
) -> DocumentCreateDTO:
if source_url is not None:
source_url = cast(AnyHttpUrl, source_url)
return DocumentCreateDTO(
family_import_id=family_import_id,
variant_name=variant_name,
Expand Down
File renamed without changes.
12 changes: 8 additions & 4 deletions unit_tests/helpers/family.py → tests/helpers/family.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ def create_family_dto(
category: str = FamilyCategory.LEGISLATIVE.value,
metadata: Optional[dict] = None,
slug: str = "slug",
collections: Optional[list[str]] = ["x.y.z.1", "x.y.z.2"],
collections: Optional[list[str]] = None,
) -> FamilyReadDTO:
if metadata is None:
metadata = {}

if collections is None:
collections = []
collections = ["x.y.z.1", "x.y.z.2"]

return FamilyReadDTO(
import_id=import_id,
Expand Down Expand Up @@ -48,10 +48,12 @@ def create_family_create_dto(
geography: str = "CHN",
category: str = FamilyCategory.LEGISLATIVE.value,
metadata: Optional[dict] = None,
collections: list[str] = [],
collections: Optional[list[str]] = None,
) -> FamilyCreateDTO:
if metadata is None:
metadata = {}
if collections is None:
collections = []
return FamilyCreateDTO(
title=title,
summary=summary,
Expand All @@ -68,10 +70,12 @@ def create_family_write_dto(
geography: str = "CHN",
category: str = FamilyCategory.LEGISLATIVE.value,
metadata: Optional[dict] = None,
collections: list[str] = [],
collections: Optional[list[str]] = None,
) -> FamilyWriteDTO:
if metadata is None:
metadata = {}
if collections is None:
collections = []
return FamilyWriteDTO(
title=title,
summary=summary,
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from fastapi.testclient import TestClient
from sqlalchemy.orm import Session

from integration_tests.setup_db import (
from tests.integration_tests.setup_db import (
EXPECTED_ANALYTICS_SUMMARY,
EXPECTED_ANALYTICS_SUMMARY_KEYS,
setup_db,
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from fastapi.testclient import TestClient
from sqlalchemy.orm import Session

from integration_tests.setup_db import setup_db
from unit_tests.helpers.collection import create_collection_create_dto
from tests.helpers.collection import create_collection_create_dto
from tests.integration_tests.setup_db import setup_db


def test_create_collection(client: TestClient, data_db: Session, user_header_token):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from fastapi.testclient import TestClient
from sqlalchemy.orm import Session

from integration_tests.setup_db import EXPECTED_NUM_COLLECTIONS, setup_db
from tests.integration_tests.setup_db import EXPECTED_NUM_COLLECTIONS, setup_db


def test_delete_collection(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from fastapi.testclient import TestClient
from sqlalchemy.orm import Session

from integration_tests.setup_db import EXPECTED_COLLECTIONS, setup_db
from tests.integration_tests.setup_db import EXPECTED_COLLECTIONS, setup_db

# --- GET ALL

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from fastapi.testclient import TestClient
from sqlalchemy.orm import Session

from integration_tests.setup_db import setup_db
from tests.integration_tests.setup_db import setup_db


def test_search_collection(client: TestClient, data_db: Session, user_header_token):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from fastapi.testclient import TestClient
from sqlalchemy.orm import Session

from integration_tests.setup_db import EXPECTED_COLLECTIONS, setup_db
from unit_tests.helpers.collection import create_collection_write_dto
from tests.helpers.collection import create_collection_write_dto
from tests.integration_tests.setup_db import EXPECTED_COLLECTIONS, setup_db


def test_update_collection(client: TestClient, data_db: Session, user_header_token):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,30 @@
from app.config import SQLALCHEMY_DATABASE_URI
from app.main import app
from app.repository import collection_repo, document_repo, event_repo, family_repo
from integration_tests.mocks.bad_collection_repo import (
from tests.integration_tests.mocks.bad_collection_repo import (
mock_bad_collection_repo,
mock_collection_count_none,
)
from integration_tests.mocks.bad_document_repo import (
from tests.integration_tests.mocks.bad_document_repo import (
mock_bad_document_repo,
mock_document_count_none,
)
from integration_tests.mocks.bad_event_repo import (
from tests.integration_tests.mocks.bad_event_repo import (
mock_bad_event_repo,
mock_event_count_none,
)
from integration_tests.mocks.bad_family_repo import (
from tests.integration_tests.mocks.bad_family_repo import (
mock_bad_family_repo,
mock_family_count_none,
)
from integration_tests.mocks.rollback_collection_repo import (
from tests.integration_tests.mocks.rollback_collection_repo import (
mock_rollback_collection_repo,
)
from integration_tests.mocks.rollback_document_repo import mock_rollback_document_repo
from integration_tests.mocks.rollback_event_repo import mock_rollback_event_repo
from integration_tests.mocks.rollback_family_repo import mock_rollback_family_repo
from tests.integration_tests.mocks.rollback_document_repo import (
mock_rollback_document_repo,
)
from tests.integration_tests.mocks.rollback_event_repo import mock_rollback_event_repo
from tests.integration_tests.mocks.rollback_family_repo import mock_rollback_family_repo


def get_test_db_url() -> str:
Expand All @@ -53,6 +55,9 @@ def test_db(scope="function"):
if database_exists(test_db_url):
drop_database(test_db_url)
create_database(test_db_url)

test_session = None
connection = None
try:
test_engine = create_engine(test_db_url)
connection = test_engine.connect()
Expand All @@ -67,8 +72,11 @@ def test_db(scope="function"):
# Run the tests
yield test_session
finally:
test_session.close()
connection.close()
if test_session is not None:
test_session.close()

if connection is not None:
connection.close()
# Drop the test database
drop_database(test_db_url)

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
from fastapi.testclient import TestClient
from sqlalchemy.orm import Session

from integration_tests.setup_db import setup_db
from unit_tests.helpers.document import create_document_create_dto
from tests.helpers.document import create_document_create_dto
from tests.integration_tests.setup_db import setup_db


def test_create_document(client: TestClient, data_db: Session, user_header_token):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
from fastapi.testclient import TestClient
from sqlalchemy.orm import Session

import app.repository.document as document_repo
from integration_tests.setup_db import setup_db
from tests.integration_tests.setup_db import setup_db


def test_delete_document(client: TestClient, data_db: Session, admin_user_header_token):
Expand All @@ -25,11 +24,8 @@ def test_delete_document(client: TestClient, data_db: Session, admin_user_header
assert data_db.query(PhysicalDocument).count() == 2


def test_delete_document_when_not_authenticated(
client: TestClient, data_db: Session, mocker
):
def test_delete_document_when_not_authenticated(client: TestClient, data_db: Session):
setup_db(data_db)
mocker.spy(document_repo, "delete")
response = client.delete(
"/api/v1/documents/D.0.0.2",
)
Expand All @@ -42,7 +38,6 @@ def test_delete_document_when_not_authenticated(
== 0
)
assert data_db.query(PhysicalDocument).count() == 2
assert document_repo.delete.call_count == 0


def test_delete_document_rollback(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from fastapi.testclient import TestClient
from sqlalchemy.orm import Session

from integration_tests.setup_db import EXPECTED_DOCUMENTS, setup_db
from tests.integration_tests.setup_db import EXPECTED_DOCUMENTS, setup_db

# --- GET ALL

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from fastapi.testclient import TestClient
from sqlalchemy.orm import Session

from integration_tests.setup_db import setup_db
from tests.integration_tests.setup_db import setup_db


def test_search_document(client: TestClient, data_db: Session, user_header_token):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
from sqlalchemy.orm import Session

from app.model.document import DocumentWriteDTO
from integration_tests.setup_db import EXPECTED_DOCUMENTS, setup_db
from unit_tests.helpers.document import create_document_write_dto
from tests.helpers.document import create_document_write_dto
from tests.integration_tests.setup_db import EXPECTED_DOCUMENTS, setup_db


def _get_doc_tuple(
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
from fastapi.testclient import TestClient
from sqlalchemy.orm import Session

from integration_tests.setup_db import setup_db
from unit_tests.helpers.event import create_event_create_dto
from tests.helpers.event import create_event_create_dto
from tests.integration_tests.setup_db import setup_db


def test_create_event(client: TestClient, data_db: Session, user_header_token):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
from fastapi.testclient import TestClient
from sqlalchemy.orm import Session

import app.repository.event as event_repo
from integration_tests.setup_db import setup_db
from tests.integration_tests.setup_db import setup_db


def test_delete_event(client: TestClient, data_db: Session, admin_user_header_token):
Expand All @@ -18,17 +17,13 @@ def test_delete_event(client: TestClient, data_db: Session, admin_user_header_to
)


def test_delete_event_when_not_authenticated(
client: TestClient, data_db: Session, mocker
):
def test_delete_event_when_not_authenticated(client: TestClient, data_db: Session):
setup_db(data_db)
mocker.spy(event_repo, "delete")
response = client.delete(
"/api/v1/events/E.0.0.2",
)
assert response.status_code == status.HTTP_401_UNAUTHORIZED
assert data_db.query(FamilyEvent).count() == 3
assert event_repo.delete.call_count == 0


def test_delete_event_rollback(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from fastapi.testclient import TestClient
from sqlalchemy.orm import Session

from integration_tests.setup_db import EXPECTED_EVENTS, setup_db
from tests.integration_tests.setup_db import EXPECTED_EVENTS, setup_db

# --- GET ALL

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from fastapi.testclient import TestClient
from sqlalchemy.orm import Session

from integration_tests.setup_db import setup_db
from tests.integration_tests.setup_db import setup_db


def test_search_event(client: TestClient, data_db: Session, user_header_token):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from fastapi.testclient import TestClient
from sqlalchemy.orm import Session

from integration_tests.setup_db import EXPECTED_EVENTS, setup_db
from unit_tests.helpers.event import create_event_write_dto
from tests.helpers.event import create_event_write_dto
from tests.integration_tests.setup_db import EXPECTED_EVENTS, setup_db


def _get_event_tuple(test_db: Session, import_id: str) -> FamilyEvent:
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
from fastapi.testclient import TestClient
from sqlalchemy.orm import Session

from integration_tests.setup_db import setup_db
from unit_tests.helpers.family import create_family_create_dto
from tests.helpers.family import create_family_create_dto
from tests.integration_tests.setup_db import setup_db


def test_create_family(client: TestClient, data_db: Session, user_header_token):
Expand Down
Loading

0 comments on commit df91ea6

Please sign in to comment.