Skip to content

Commit

Permalink
Optimize tests setup
Browse files Browse the repository at this point in the history
  • Loading branch information
spyker77 committed May 23, 2023
1 parent 6c71ae9 commit 5d49ca6
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 67 deletions.
68 changes: 34 additions & 34 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,67 +1,68 @@
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
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}") {{
Expand All @@ -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})
40 changes: 20 additions & 20 deletions tests/test_mutation.py
Original file line number Diff line number Diff line change
@@ -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}") {{
Expand All @@ -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"""
Expand All @@ -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"""
Expand All @@ -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()
Expand All @@ -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}") {{
Expand Down Expand Up @@ -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"""
Expand All @@ -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 {{
Expand All @@ -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()
Expand Down
21 changes: 8 additions & 13 deletions tests/test_query.py
Original file line number Diff line number Diff line change
@@ -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"""
{{
Expand All @@ -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}") {{
Expand Down Expand Up @@ -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"""
{{
Expand All @@ -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()
Expand All @@ -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"""
{{
Expand All @@ -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()
Expand Down

0 comments on commit 5d49ca6

Please sign in to comment.