From 5d49ca63f776ea117c776cda79957f0042e6acaa Mon Sep 17 00:00:00 2001 From: Evgeni Sautin Date: Tue, 23 May 2023 12:55:14 +0300 Subject: [PATCH] Optimize tests setup --- tests/conftest.py | 68 +++++++++++++++++++++--------------------- tests/test_mutation.py | 40 ++++++++++++------------- tests/test_query.py | 21 +++++-------- 3 files changed, 62 insertions(+), 67 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 9bb0cb7..37813af 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,57 +1,57 @@ -import asyncio - import pytest -from elasticsearch import NotFoundError +from elasticsearch import AsyncElasticsearch, NotFoundError from httpx import AsyncClient from motor.motor_asyncio import AsyncIOMotorClient, AsyncIOMotorDatabase -from app.config import Settings, get_settings from app.database.mongodb import get_db from app.main import create_application from app.models.creator import CreatorModel from app.search_engine.elasticsearch import get_es +from app.settings import get_settings pytestmark = pytest.mark.asyncio +settings = get_settings() -def get_settings_override() -> Settings: - return Settings(mongodb_database="test_db") +async def get_test_db() -> AsyncIOMotorDatabase: + mongo_client = AsyncIOMotorClient(settings.mongodb_url) + return mongo_client[settings.mongodb_database] -overridden_settings = get_settings_override() - -async def get_test_db() -> AsyncIOMotorDatabase: - client = AsyncIOMotorClient(overridden_settings.mongodb_url) - db = client[overridden_settings.mongodb_database] - return db +async def get_test_es() -> AsyncElasticsearch: + es_client = AsyncElasticsearch(hosts=[settings.elasticsearch_url]) + return es_client app = create_application() -app.dependency_overrides[get_settings] = get_settings_override app.dependency_overrides[get_db] = get_test_db +app.dependency_overrides[get_es] = get_test_es -@pytest.fixture -async def test_client() -> AsyncClient: - async with AsyncClient(app=app, base_url="http://localhost:8000") as ac: - yield ac - - -async def data_cleanup(): - async for es in get_es(): - db = await get_test_db() +async def data_cleanup(db: AsyncIOMotorDatabase, es: AsyncElasticsearch): + try: + await es.indices.delete(index="creator") + except NotFoundError: + pass + finally: await db["creators"].drop() - try: - await es.indices.delete(index="creator") - except NotFoundError: - pass @pytest.fixture(autouse=True) -def prepare_test_state(): - asyncio.run(data_cleanup()) +async def prepare_test_state(): + test_db = await get_test_db() + test_es = await get_test_es() + await data_cleanup(test_db, test_es) yield + await test_db.close() + await test_es.close() + + +@pytest.fixture +async def test_client() -> AsyncClient: + async with AsyncClient(app=app, base_url="http://localhost:8000") as ac: + yield ac @pytest.fixture @@ -59,9 +59,10 @@ def test_creator_data(faker) -> dict[str, str]: return CreatorModel(username=faker.user_name(), email=faker.email()).dict() -async def add_test_creator(test_creator, test_client): - test_email = test_creator["email"] - test_username = test_creator["username"] +@pytest.fixture +async def add_test_creator(test_creator_data, test_client): + test_email = test_creator_data["email"] + test_username = test_creator_data["username"] mutation = f""" mutation {{ addCreator(username: "{test_username}", email: "{test_email}") {{ @@ -73,6 +74,5 @@ async def add_test_creator(test_creator, test_client): }} }} """ - await test_client.post("/graphql", json={"query": mutation}) - async for es in get_es(): - await es.indices.refresh(index="_all") + async for client in test_client: + await client.post("/graphql", json={"query": mutation}) diff --git a/tests/test_mutation.py b/tests/test_mutation.py index 31b1085..138f615 100644 --- a/tests/test_mutation.py +++ b/tests/test_mutation.py @@ -1,13 +1,14 @@ -import pytest +from unittest.mock import AsyncMock, patch -from .conftest import add_test_creator +import pytest pytestmark = pytest.mark.asyncio -async def test_graphql_mutation_add_creator(test_creator_data, test_client): - test_email = test_creator_data["email"] - test_username = test_creator_data["username"] +@patch("app.worker.create_pool", new_callable=AsyncMock) +async def test_graphql_mutation_add_creator(mocked_create_pool, faker, test_client): + test_email = faker.email() + test_username = faker.user_name() add_creator = f""" mutation {{ addCreator(username: "{test_username}", email: "{test_email}") {{ @@ -26,7 +27,8 @@ async def test_graphql_mutation_add_creator(test_creator_data, test_client): assert response.json()["data"]["addCreator"]["email"] == test_email -async def test_graphql_mutation_add_existing_creator(test_creator_data, test_client): +async def test_graphql_mutation_add_existing_creator(add_test_creator, test_creator_data, test_client): + await add_test_creator test_email = test_creator_data["email"] test_username = test_creator_data["username"] add_creator = f""" @@ -41,15 +43,14 @@ async def test_graphql_mutation_add_existing_creator(test_creator_data, test_cli }} """ async for client in test_client: - await add_test_creator(test_creator_data, client) - response = await client.post("/graphql", json={"query": add_creator}) assert response.status_code == 200 assert "errors" in response.json() assert response.json()["errors"][0]["message"] == "Creator already exists." -async def test_graphql_mutation_add_asset_to_creator(test_creator_data, test_client): +async def test_graphql_mutation_add_asset_to_creator(add_test_creator, test_creator_data, test_client): + await add_test_creator test_email = test_creator_data["email"] test_type = "Test Type" add_asset_to_creator = f""" @@ -61,8 +62,6 @@ async def test_graphql_mutation_add_asset_to_creator(test_creator_data, test_cli }} """ async for client in test_client: - await add_test_creator(test_creator_data, client) - response = await client.post("/graphql", json={"query": add_asset_to_creator}) assert response.status_code == 200 assert "errors" not in response.json() @@ -87,12 +86,11 @@ async def test_graphql_mutation_add_asset_to_non_existing_creator(test_client): assert response.json()["errors"][0]["message"] == "Creator does not exist." -async def test_graphql_mutation_remove_asset_from_creator(test_creator_data, test_client): +async def test_graphql_mutation_remove_asset_from_creator(add_test_creator, test_creator_data, test_client): + await add_test_creator test_email = test_creator_data["email"] test_type = "Test Type" async for client in test_client: - await add_test_creator(test_creator_data, client) - add_asset_to_creator = f""" mutation {{ addAssetToCreator(type: "{test_type}", email: "{test_email}") {{ @@ -137,7 +135,12 @@ async def test_graphql_mutation_remove_asset_from_non_existing_creator(test_clie assert response.json()["errors"][0]["message"] == "Creator does not exist." -async def test_graphql_mutation_remove_non_existing_asset_from_creator(test_creator_data, test_client): +async def test_graphql_mutation_remove_non_existing_asset_from_creator( + add_test_creator, + test_creator_data, + test_client, +): + await add_test_creator test_email = test_creator_data["email"] test_type = "No Such Type" remove_asset_from_creator = f""" @@ -149,15 +152,14 @@ async def test_graphql_mutation_remove_non_existing_asset_from_creator(test_crea }} """ async for client in test_client: - await add_test_creator(test_creator_data, client) - response = await client.post("/graphql", json={"query": remove_asset_from_creator}) assert response.status_code == 200 assert "errors" in response.json() assert response.json()["errors"][0]["message"] == "Asset does not exist." -async def test_graphql_mutation_delete_creator(test_creator_data, test_client): +async def test_graphql_mutation_delete_creator(add_test_creator, test_creator_data, test_client): + await add_test_creator test_email = test_creator_data["email"] delete_creator = f""" mutation {{ @@ -167,8 +169,6 @@ async def test_graphql_mutation_delete_creator(test_creator_data, test_client): }} """ async for client in test_client: - await add_test_creator(test_creator_data, client) - response = await client.post("/graphql", json={"query": delete_creator}) assert response.status_code == 200 assert "errors" not in response.json() diff --git a/tests/test_query.py b/tests/test_query.py index 855215c..e5f4af8 100644 --- a/tests/test_query.py +++ b/tests/test_query.py @@ -1,11 +1,10 @@ import pytest -from .conftest import add_test_creator - pytestmark = pytest.mark.asyncio -async def test_graphql_query_get_creator(test_creator_data, test_client): +async def test_graphql_query_get_creator(add_test_creator, test_creator_data, test_client): + await add_test_creator test_email = test_creator_data["email"] get_creator = f""" {{ @@ -19,20 +18,17 @@ async def test_graphql_query_get_creator(test_creator_data, test_client): }} """ async for client in test_client: - await add_test_creator(test_creator_data, client) - response = await client.post("/graphql", json={"query": get_creator}) assert response.status_code == 200 assert "errors" not in response.json() assert response.json()["data"]["getCreator"]["email"] == test_email -async def test_graphql_query_get_creator_assets(test_creator_data, test_client): +async def test_graphql_query_get_creator_assets(add_test_creator, test_creator_data, test_client): + await add_test_creator test_email = test_creator_data["email"] test_type = "Test Type" async for client in test_client: - await add_test_creator(test_creator_data, client) - add_asset_to_creator = f""" mutation {{ addAssetToCreator(type: "{test_type}", email: "{test_email}") {{ @@ -81,7 +77,8 @@ async def test_graphql_query_get_non_existing_creator(test_client): assert response.json()["errors"][0]["message"] == "Creator does not exist." -async def test_graphql_query_search_creators(test_creator_data, test_client): +async def test_graphql_query_search_creators(add_test_creator, test_creator_data, test_client): + await add_test_creator test_email = test_creator_data["email"] search_creators = f""" {{ @@ -95,8 +92,6 @@ async def test_graphql_query_search_creators(test_creator_data, test_client): }} """ async for client in test_client: - await add_test_creator(test_creator_data, client) - response = await client.post("/graphql", json={"query": search_creators}) assert response.status_code == 200 assert "errors" not in response.json() @@ -112,12 +107,14 @@ async def test_graphql_query_search_creators(test_creator_data, test_client): ], ) async def test_graphql_query_search_creators_with_page_errors( + add_test_creator, test_creator_data, test_client, page, per_page, expected_error, ): + await add_test_creator test_email = test_creator_data["email"] search_creators = f""" {{ @@ -131,8 +128,6 @@ async def test_graphql_query_search_creators_with_page_errors( }} """ async for client in test_client: - await add_test_creator(test_creator_data, client) - response = await client.post("/graphql", json={"query": search_creators}) assert response.status_code == 200 assert "errors" in response.json()