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 person test #49

Merged
merged 14 commits into from
Apr 2, 2024
64 changes: 64 additions & 0 deletions async_api/tests/functional/src/test_person.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
from elasticsearch import AsyncElasticsearch
from elasticsearch.helpers import async_bulk
from httpx import AsyncClient

from models.person import Person
from tests.functional.settings import settings
from tests.functional.utils.factories import PersonFactory


async def insert_persons(es_client: AsyncElasticsearch, persons: list[Person]):
documents = [
{
"_index": settings.es_persons_index,
"_id": str(person.id),
"_source": person.model_dump(mode="json"),
}
for person in persons
]
await async_bulk(es_client, documents, refresh="wait_for")


async def test_list_persons(test_client: AsyncClient, es_client: AsyncElasticsearch):
a1d4r marked this conversation as resolved.
Show resolved Hide resolved
# Arrange
persons: list[Person] = PersonFactory.batch(15)
await insert_persons(es_client, persons)

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

# Assert
assert response.status_code == 200

response_persons = response.json()
assert len(response_persons) == 10
assert {response_person["uuid"] for response_person in response_persons} <= {
str(person.id) for person in persons
}

some_person = response_persons[0]
person_in_es = next(
(person for person in persons if str(person.id) == some_person["uuid"]),
None,
)
assert person_in_es is not None
assert some_person["full_name"] == person_in_es.full_name
assert some_person["films"] == person_in_es.films


async def test_get_person_details(test_client: AsyncClient, es_client: AsyncElasticsearch):
# Arrange
person: Person = PersonFactory.build()
await insert_persons(es_client, [person])

# Act
response = await test_client.get(f"/v1/persons/{person.id}")

# Assert
assert response.status_code == 200
response_person = response.json()
assert response_person["full_name"] == person.full_name
assert response_person["films"] == person.films
49 changes: 49 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,8 @@
from polyfactory.factories.pydantic_factory import ModelFactory

from models.film import Film, GenreIdName, PersonIdName
from models.person import Person, PersonFilm
from models.value_objects import Roles

GENRES = [
"Action",
Expand All @@ -19,6 +21,53 @@
]


class RolesFactory(ModelFactory[Roles]):
a1d4r marked this conversation as resolved.
Show resolved Hide resolved
_faker__ = Faker()
__set_as_default_factory_for_type__ = True

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

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

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


class PersonFilmFactory(ModelFactory[PersonFilm]):
__faker__ = Faker()
__set_as_default_factory_for_type__ = True

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

@classmethod
def imdb_rating(cls):
return cast(str, cls.__faker__.random.uniform(1.0, 10.0))

@classmethod
def roles(cls) -> list[RolesFactory]:
return [RolesFactory() for _ in range(3)]


class PersonFactory(ModelFactory[Person]):
__faker__ = Faker()
__set_as_default_factory_for_type__ = True

@classmethod
def full_name(cls) -> str:
return cast(str, cls.__faker__.name())

@classmethod
def films(cls) -> list[PersonFilmFactory]:
return [PersonFilmFactory() for _ in range(3)]
a1d4r marked this conversation as resolved.
Show resolved Hide resolved


class PersonIdNameFactory(ModelFactory[PersonIdName]):
__faker__ = Faker()
__set_as_default_factory_for_type__ = True
Expand Down