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
Show file tree
Hide file tree
Changes from all commits
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
86 changes: 86 additions & 0 deletions async_api/tests/functional/src/test_genre.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
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 GenreFactory


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] = GenreFactory.batch(15)
await insert_genres(es_client, genres)

# Act
response = await test_client.get("/v1/genres/", params={"page_number": 1, "page_size": 10})

# 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 = GenreFactory.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
assert response_genre["description"] == genre.description


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 = GenreFactory.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
14 changes: 14 additions & 0 deletions async_api/tests/functional/utils/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from polyfactory.factories.pydantic_factory import ModelFactory

from models.film import Film, GenreIdName, PersonIdName
from models.genre import Genre

GENRES = [
"Action",
Expand Down Expand Up @@ -37,6 +38,19 @@ def name(cls) -> str:
return cls.__random__.choice(GENRES)


class GenreFactory(ModelFactory[Genre]):
__random__ = Random()
__set_as_default_factory_for_type__ = True

@classmethod
def name(cls) -> str:
return cls.__random__.choice(GENRES)

@classmethod
def description(cls) -> str:
return cast(str, cls.__faker__.sentence(nb_words=10))


class FilmFactory(ModelFactory[Film]):
__faker__ = Faker()
__model__ = Film
Expand Down
Loading