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
65 changes: 65 additions & 0 deletions async_api/tests/functional/src/test_person.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
from elasticsearch import AsyncElasticsearch
from elasticsearch.helpers import async_bulk
from httpx import AsyncClient
from tests.functional.settings import settings
from tests.functional.utils.factories import PersonIdNameFactory

from models.person import Person


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] = PersonIdNameFactory.batch(15)
a1d4r marked this conversation as resolved.
Show resolved Hide resolved
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


async def test_get_person_details(test_client: AsyncClient, es_client: AsyncElasticsearch):
# Arrange
person: Person = PersonIdNameFactory.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"] == [
{"uuid": str(film.id), "title": film.title} for film in person.films
]
a1d4r marked this conversation as resolved.
Show resolved Hide resolved
Loading