-
Notifications
You must be signed in to change notification settings - Fork 1
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
Showing
6 changed files
with
185 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import httpx | ||
import pytest | ||
import pytest_asyncio | ||
from sqlalchemy.ext.asyncio import AsyncSession | ||
|
||
from src.config import Settings, settings as config_settings | ||
from src.main import app | ||
from src.repositories.event_groups import AbstractEventGroupRepository, SqlEventGroupRepository | ||
from src.repositories.events import AbstractEventRepository, SqlEventRepository | ||
from src.repositories.tags import AbstractTagRepository, SqlTagRepository | ||
from src.repositories.users import AbstractUserRepository, SqlUserRepository | ||
from src.storages.sql import AbstractSQLAlchemyStorage, SQLAlchemyStorage | ||
|
||
|
||
# --- Monkey Patching --- # | ||
@pytest.fixture(scope="session") | ||
def monkeysession(): | ||
from _pytest.monkeypatch import MonkeyPatch | ||
|
||
mpatch = MonkeyPatch() | ||
yield mpatch | ||
mpatch.undo() | ||
|
||
|
||
# --- Settings Fixtures --- # | ||
@pytest.fixture(scope="package") | ||
def settings() -> "Settings": | ||
return config_settings | ||
|
||
|
||
# --- Storage Fixtures --- # | ||
@pytest.fixture(scope="package") | ||
def storage(settings: "Settings") -> "AbstractSQLAlchemyStorage": | ||
_storage = SQLAlchemyStorage.from_url(settings.DB_URL.get_secret_value()) | ||
return _storage | ||
|
||
|
||
@pytest_asyncio.fixture(scope="function", autouse=True) | ||
async def setup_storage(storage: "AbstractSQLAlchemyStorage"): | ||
# Create the necessary tables before each test | ||
async with storage.create_session() as session: | ||
await _init_models(session) | ||
await session.commit() | ||
yield | ||
|
||
# Close the database connection after each test | ||
async with storage.create_session() as session: | ||
await _restore_session(session) | ||
await session.commit() | ||
await storage.close_connection() | ||
|
||
|
||
async def _init_models(session: AsyncSession): | ||
from src.storages.sql.models.base import Base | ||
|
||
async with session.bind.begin() as conn: | ||
await conn.run_sync(Base.metadata.drop_all) | ||
await conn.run_sync(Base.metadata.create_all) | ||
|
||
|
||
@pytest_asyncio.fixture() | ||
async def client(): | ||
async with httpx.AsyncClient(app=app, base_url="http://127.0.0.1:8000/") as client: | ||
yield client | ||
|
||
async def _restore_session(session: AsyncSession): | ||
from src.storages.sql.models.base import Base | ||
|
||
async with session.begin(): | ||
for table in reversed(Base.metadata.sorted_tables): | ||
await session.execute(table.delete()) | ||
|
||
|
||
# --- Repository Fixtures --- # | ||
|
||
|
||
@pytest.fixture(scope="package") | ||
def user_repository(storage) -> "AbstractUserRepository": | ||
return SqlUserRepository(storage) | ||
|
||
|
||
@pytest.fixture(scope="package") | ||
def event_group_repository(storage) -> "AbstractEventGroupRepository": | ||
return SqlEventGroupRepository(storage) | ||
|
||
|
||
@pytest.fixture(scope="package") | ||
def tag_repository(storage) -> "AbstractTagRepository": | ||
return SqlTagRepository(storage) | ||
|
||
|
||
@pytest.fixture(scope="package") | ||
def event_repository(storage) -> "AbstractEventRepository": | ||
return SqlEventRepository(storage) |
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,62 @@ | ||
import httpx | ||
import pytest | ||
|
||
from src.main import setup_repositories | ||
from tests.misc.test_nonobvious_schemas import get_fake_create_event_group | ||
|
||
|
||
async def create_event_group(event_group_repository): | ||
await setup_repositories() | ||
event_group_schema = get_fake_create_event_group() | ||
event_group = await event_group_repository.create_or_read(event_group_schema) | ||
return event_group | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_get_event_group(client: httpx.AsyncClient, event_group_repository): | ||
event_group = await create_event_group(event_group_repository) | ||
response = await client.get(f"event-groups/{event_group.id}") | ||
response_from_api = response.json() | ||
assert response.status_code == 200 | ||
assert event_group.alias in response_from_api["alias"] | ||
assert event_group.id == response_from_api["id"] | ||
assert event_group.path in response_from_api["path"] | ||
assert event_group.name in response_from_api["name"] | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_find_event_group_by_path(client: httpx.AsyncClient, event_group_repository): | ||
event_group = await create_event_group(event_group_repository) | ||
response = await client.get(f"event-groups/by-path?path={event_group.path}") | ||
assert response.status_code == 200 | ||
response_from_api = response.json() | ||
assert event_group.alias in response_from_api["alias"] | ||
assert event_group.id == response_from_api["id"] | ||
assert event_group.path in response_from_api["path"] | ||
assert event_group.name in response_from_api["name"] | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_not_find_event_group_by_path(client: httpx.AsyncClient, event_group_repository): | ||
await setup_repositories() | ||
response = await client.get(f"event-groups/by-path?path=nonexistingpath") | ||
assert response.status_code == 404 | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_list_event_groups(client: httpx.AsyncClient, event_group_repository): | ||
event_groups = [] | ||
event_groups_number = 10 | ||
for i in range(event_groups_number): | ||
event_group = await create_event_group(event_group_repository) | ||
event_groups.append(event_group) | ||
response = await client.get(f"event-groups/") | ||
response_from_api = response.json() | ||
assert response.status_code == 200 | ||
for i in range(event_groups_number): | ||
assert response_from_api["groups"][i]["alias"] == event_groups[i].alias | ||
assert response_from_api["groups"][i]["name"] == event_groups[i].name | ||
assert response_from_api["groups"][i]["path"] == event_groups[i].path | ||
assert response_from_api["groups"][i]["description"] == event_groups[i].description | ||
assert len(response_from_api["groups"][i]["ownerships"]) == 0 | ||
assert len(response_from_api["groups"][i]["tags"]) == 0 |
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
28f6541
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Coverage Report