Fixtures
This commit is contained in:
@ -3,14 +3,17 @@ import logging
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from typing import AsyncGenerator
|
from typing import AsyncGenerator
|
||||||
|
|
||||||
|
import pytest
|
||||||
import pytest_asyncio
|
import pytest_asyncio
|
||||||
from _pytest.config import Config
|
from _pytest.config import Config
|
||||||
|
from litestar.testing import AsyncTestClient
|
||||||
from sqlalchemy import event
|
from sqlalchemy import event
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
|
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
|
||||||
|
|
||||||
from app.lib import settings
|
from app.lib import settings
|
||||||
from app.lib.sqlalchemy_plugin import DBConnectionSettings, create_db_engine
|
from app.lib.sqlalchemy_plugin import DBConnectionSettings, create_db_engine
|
||||||
from app.lib.test_extras.db_setup import TestingDatabaseSetup
|
from app.lib.test_extras.db_setup import TestingDatabaseSetup
|
||||||
|
from main import app
|
||||||
|
|
||||||
# A Guide To Database Unit Testing with Pytest and SQLAlchemy
|
# A Guide To Database Unit Testing with Pytest and SQLAlchemy
|
||||||
# https://coderpad.io/blog/development/a-guide-to-database-unit-testing-with-pytest-and-sqlalchemy/
|
# https://coderpad.io/blog/development/a-guide-to-database-unit-testing-with-pytest-and-sqlalchemy/
|
||||||
@ -63,6 +66,11 @@ async def db_session() -> AsyncGenerator[AsyncSession, None]:
|
|||||||
pytest_plugins = ()
|
pytest_plugins = ()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(scope="function")
|
||||||
|
def async_client() -> AsyncTestClient:
|
||||||
|
return AsyncTestClient(app=app)
|
||||||
|
|
||||||
|
|
||||||
def pytest_configure(config: Config) -> None:
|
def pytest_configure(config: Config) -> None:
|
||||||
logging.info(f"Starting tests: {datetime.utcnow()}")
|
logging.info(f"Starting tests: {datetime.utcnow()}")
|
||||||
db_setup = TestingDatabaseSetup(options=settings.testing)
|
db_setup = TestingDatabaseSetup(options=settings.testing)
|
||||||
|
|||||||
@ -3,14 +3,14 @@ from litestar.status_codes import HTTP_200_OK
|
|||||||
from litestar.testing import AsyncTestClient
|
from litestar.testing import AsyncTestClient
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
|
||||||
from main import app
|
|
||||||
from tests.factories.city_factory import CityFactory
|
from tests.factories.city_factory import CityFactory
|
||||||
|
|
||||||
|
|
||||||
class TestCitiesEndpoints:
|
class TestCitiesEndpoints:
|
||||||
@pytest.fixture(scope="function", autouse=True)
|
@pytest.fixture(scope="function", autouse=True)
|
||||||
def setup_class(self, db_session: AsyncSession):
|
def setup_class(self, db_session: AsyncSession, async_client: AsyncTestClient):
|
||||||
self.db_session = db_session
|
self.db_session = db_session
|
||||||
|
self.async_client = async_client
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_get(self):
|
async def test_get(self):
|
||||||
@ -20,9 +20,9 @@ class TestCitiesEndpoints:
|
|||||||
city_4 = await CityFactory.create()
|
city_4 = await CityFactory.create()
|
||||||
city_5 = await CityFactory.create()
|
city_5 = await CityFactory.create()
|
||||||
|
|
||||||
async with AsyncTestClient(app=app) as client:
|
async with self.async_client as client:
|
||||||
response = await client.get("/v1/cities")
|
response = await client.get("/v1/cities")
|
||||||
print("#" * 100)
|
print("#" * 100)
|
||||||
print(response.content)
|
print(response.json())
|
||||||
print("#" * 100)
|
print("#" * 100)
|
||||||
assert response.status_code == HTTP_200_OK
|
assert response.status_code == HTTP_200_OK
|
||||||
Reference in New Issue
Block a user