[TC-221]: init template-backend-service (#1)
https://eva.avroid.tech/desk/cards?obj=Task:TC-221 Начальная версия сервиса-шаблона Co-authored-by: Nadezhda <nadezhda.lavrentieva@avroid.team> Reviewed-on: https://git.avroid.tech/Templates/template-backend-service/pulls/1 Reviewed-by: Victor Stratov <victor.stratov@avroid.tech>
This commit is contained in:
0
tests/__init__.py
Normal file
0
tests/__init__.py
Normal file
98
tests/conftest.py
Normal file
98
tests/conftest.py
Normal file
@@ -0,0 +1,98 @@
|
||||
import warnings
|
||||
from collections.abc import AsyncGenerator
|
||||
|
||||
import pytest
|
||||
from aiopg.sa import Engine, SAConnection, create_engine
|
||||
from fastapi import FastAPI
|
||||
from httpx import AsyncClient
|
||||
from sqlalchemy import Table
|
||||
from sqlalchemy.dialects import postgresql
|
||||
from sqlalchemy.sql.ddl import CreateTable
|
||||
|
||||
from src.api_app import create_app
|
||||
from src.settings import WebAppSettings
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def test_settings() -> WebAppSettings:
|
||||
return WebAppSettings(
|
||||
postgres_user="postgres",
|
||||
postgres_password="postgres",
|
||||
postgres_host="localhost",
|
||||
postgres_db="postgres",
|
||||
postgres_port=5432,
|
||||
port=8000,
|
||||
scylladb_host="localhost",
|
||||
scylladb_port="9042",
|
||||
scylladb_user="test",
|
||||
scylladb_password="test",
|
||||
scylladb_keyspace="test",
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def test_app(test_settings: WebAppSettings) -> FastAPI:
|
||||
return create_app(settings=test_settings)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def test_client(test_app: FastAPI) -> AsyncGenerator[AsyncClient, None]:
|
||||
async with AsyncClient(app=test_app, base_url="http://test/api/v1") as client:
|
||||
yield client
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def sa_tables():
|
||||
"""
|
||||
Фикстура, с помощью которой можно локально переопределить перечень
|
||||
создаваемых таблиц.
|
||||
"""
|
||||
warnings.warn(
|
||||
"Please, override `sa_tables` fixture if you are using `db_engine`.",
|
||||
stacklevel=2,
|
||||
)
|
||||
return []
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def sa_enums():
|
||||
"""
|
||||
Фикстура, с помощью которой можно локально переопределить перечень
|
||||
создаваемых перечислений.
|
||||
"""
|
||||
return []
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
async def db_engine(test_settings: WebAppSettings, sa_tables, sa_enums) -> AsyncGenerator[Engine, None]:
|
||||
postgres_dsn = f"postgresql://{test_settings.postgres_user}:{test_settings.postgres_password}@{test_settings.postgres_host}:5432/{test_settings.postgres_db}"
|
||||
async with create_engine(postgres_dsn) as engine:
|
||||
async with engine.acquire() as connection:
|
||||
await drop_tables(connection)
|
||||
await create_enums(connection, sa_enums)
|
||||
await create_tables(connection, sa_tables)
|
||||
|
||||
yield engine
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def connection(db_engine: Engine) -> AsyncGenerator[SAConnection, None]:
|
||||
async with db_engine.acquire() as connection:
|
||||
yield connection
|
||||
|
||||
|
||||
async def drop_tables(connection: SAConnection):
|
||||
await connection.execute("DROP SCHEMA public CASCADE;")
|
||||
await connection.execute("CREATE SCHEMA public;")
|
||||
|
||||
|
||||
async def create_tables(connection: SAConnection, tables: list[Table]):
|
||||
for table in tables:
|
||||
ddl = str(CreateTable(table).compile(dialect=postgresql.dialect()))
|
||||
await connection.execute(ddl)
|
||||
|
||||
|
||||
async def create_enums(connection, enums):
|
||||
for enum in enums:
|
||||
ddl = str(postgresql.CreateEnumType(enum).compile(dialect=postgresql.dialect()))
|
||||
await connection.execute(ddl)
|
||||
9
tests/samples.py
Normal file
9
tests/samples.py
Normal file
@@ -0,0 +1,9 @@
|
||||
ACCOUNT_1 = {
|
||||
"country_id": 1,
|
||||
"iso_3166_code_alpha3": "RU",
|
||||
}
|
||||
|
||||
ACCOUNT_2 = {
|
||||
"country_id": 2,
|
||||
"iso_3166_code_alpha3": "EN",
|
||||
}
|
||||
23
tests/test_routes.py
Normal file
23
tests/test_routes.py
Normal file
@@ -0,0 +1,23 @@
|
||||
import pytest
|
||||
from aiopg.sa import SAConnection
|
||||
from httpx import AsyncClient
|
||||
|
||||
from src.repositories.tables import messenger_handbook_country_table
|
||||
from tests.samples import ACCOUNT_1, ACCOUNT_2
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def sa_tables():
|
||||
return [messenger_handbook_country_table]
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
async def _enter_data(connection: SAConnection):
|
||||
await connection.execute(messenger_handbook_country_table.insert().values([ACCOUNT_1, ACCOUNT_2]))
|
||||
|
||||
|
||||
async def test_get_info_from_postgresql(test_client: AsyncClient):
|
||||
response = await test_client.get("/test_psql/1")
|
||||
response_json = response.json()
|
||||
|
||||
assert (response.status_code, response_json) == (200, [ACCOUNT_1])
|
||||
Reference in New Issue
Block a user