Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add genre functional tests #50

Merged
merged 3 commits into from
Mar 31, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions async_api/tests/functional/src/test_genre.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
from uuid import uuid4

from elasticsearch import AsyncElasticsearch
from elasticsearch.helpers import async_bulk
from httpx import AsyncClient

from models.genre import Genre
from tests.functional.settings import settings
from tests.functional.utils.factories import GenreIdNameFactory


async def insert_genres(es_client: AsyncElasticsearch, genres: list[Genre]):
documents = [
{
"_index": settings.es_genres_index,
"_id": str(genre.id),
"_source": genre.model_dump(mode="json"),
}
for genre in genres
]
await async_bulk(es_client, documents, refresh="wait_for")


async def test_list_genres(test_client: AsyncClient, es_client: AsyncElasticsearch):
# Arrange
genres: list[Genre] = GenreIdNameFactory.batch(15)
a1d4r marked this conversation as resolved.
Show resolved Hide resolved
await insert_genres(es_client, genres)

# Act
response = await test_client.get("/v1/genres/")

# Assert
assert response.status_code == 200

response_genres = response.json()
assert len(response_genres) == 10
a1d4r marked this conversation as resolved.
Show resolved Hide resolved
assert {response_genre["uuid"] for response_genre in response_genres} <= {
str(genre.id) for genre in genres
}

some_genre = response_genres[0]
genre_in_es = next((genre for genre in genres if str(genre.id) == some_genre["uuid"]), None)
assert genre_in_es is not None
assert some_genre["name"] == genre_in_es.name


async def test_get_genre_details(test_client: AsyncClient, es_client: AsyncElasticsearch):
genre: Genre = GenreIdNameFactory.build()
await insert_genres(es_client, [genre])

response = await test_client.get(f"/v1/genres/{genre.id}")

assert response.status_code == 200
response_genre = response.json()
assert response_genre["name"] == genre.name

a1d4r marked this conversation as resolved.
Show resolved Hide resolved

async def test_get_non_existent_genre_details(test_client: AsyncClient):
# Arrange
non_existent_genre_id = uuid4()

# Act
response = await test_client.get(f"/v1/genres/{non_existent_genre_id}")

# Assert
assert response.status_code == 404


async def test_get_genre_details_from_cache(
test_client: AsyncClient,
es_client: AsyncElasticsearch,
):
# Arrange
genre: Genre = GenreIdNameFactory.build()
await insert_genres(es_client, [genre])

# Act
await test_client.get(f"/v1/genres/{genre.id}")
await es_client.delete(index=settings.es_genres_index, id=str(genre.id), refresh="wait_for")
response = await test_client.get(f"/v1/genres/{genre.id}")

# Assert
assert response.status_code == 200
response_genre = response.json()
assert response_genre["name"] == genre.name
Loading