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
1 changed file
with
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,29 @@ | ||
import asyncio | ||
import os | ||
from typing import Generator, Iterator | ||
|
||
import pytest | ||
from fastapi.testclient import TestClient | ||
from tortoise.contrib.test import finalizer, initializer | ||
|
||
from app.core.config import settings | ||
from typing import Generator | ||
from fastapi.testclient import TestClient | ||
from app.main import app | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def client() -> Generator: | ||
def event_loop() -> Iterator[asyncio.AbstractEventLoop]: | ||
loop = asyncio.get_event_loop_policy().new_event_loop() | ||
yield loop | ||
loop.close() | ||
|
||
|
||
# Test client | ||
@pytest.fixture(scope="module") | ||
def client(event_loop: asyncio.BaseEventLoop) -> Iterator[TestClient]: | ||
db_url = os.environ.get("TORTOISE_TEST_DB", "sqlite://:memory:") | ||
initializer(settings.TORTOISE_MODELS, db_url=db_url, app_label="models") | ||
initializer( | ||
settings.TORTOISE_MODELS, db_url=db_url, app_label="models", loop=event_loop | ||
) | ||
with TestClient(app) as c: | ||
yield c | ||
finalizer() | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def event_loop(client: TestClient) -> Generator: | ||
yield client.task.get_loop() |