From 27a449793cab3193206d8dd4b464ec2e2e2e9ddc Mon Sep 17 00:00:00 2001 From: Eden Kirin Date: Thu, 21 Sep 2023 08:09:53 +0200 Subject: [PATCH] Fixtures --- tests/conftest.py | 8 ++++++++ tests/{endpoints => test_endpoints}/__init__.py | 0 tests/{ => test_endpoints}/test_cities.py | 8 ++++---- 3 files changed, 12 insertions(+), 4 deletions(-) rename tests/{endpoints => test_endpoints}/__init__.py (100%) rename tests/{ => test_endpoints}/test_cities.py (78%) diff --git a/tests/conftest.py b/tests/conftest.py index a03dae6..6cde9af 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -3,14 +3,17 @@ import logging from datetime import datetime from typing import AsyncGenerator +import pytest import pytest_asyncio from _pytest.config import Config +from litestar.testing import AsyncTestClient from sqlalchemy import event from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker from app.lib import settings from app.lib.sqlalchemy_plugin import DBConnectionSettings, create_db_engine from app.lib.test_extras.db_setup import TestingDatabaseSetup +from main import app # 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.fixture(scope="function") +def async_client() -> AsyncTestClient: + return AsyncTestClient(app=app) + + def pytest_configure(config: Config) -> None: logging.info(f"Starting tests: {datetime.utcnow()}") db_setup = TestingDatabaseSetup(options=settings.testing) diff --git a/tests/endpoints/__init__.py b/tests/test_endpoints/__init__.py similarity index 100% rename from tests/endpoints/__init__.py rename to tests/test_endpoints/__init__.py diff --git a/tests/test_cities.py b/tests/test_endpoints/test_cities.py similarity index 78% rename from tests/test_cities.py rename to tests/test_endpoints/test_cities.py index 5f3e928..185842b 100644 --- a/tests/test_cities.py +++ b/tests/test_endpoints/test_cities.py @@ -3,14 +3,14 @@ from litestar.status_codes import HTTP_200_OK from litestar.testing import AsyncTestClient from sqlalchemy.ext.asyncio import AsyncSession -from main import app from tests.factories.city_factory import CityFactory class TestCitiesEndpoints: @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.async_client = async_client @pytest.mark.asyncio async def test_get(self): @@ -20,9 +20,9 @@ class TestCitiesEndpoints: city_4 = 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") print("#" * 100) - print(response.content) + print(response.json()) print("#" * 100) assert response.status_code == HTTP_200_OK