|
| 1 | +""" |
| 2 | +Tests for custom id name. |
| 3 | +
|
| 4 | +Check cases when model is w/o `id` field |
| 5 | +""" |
| 6 | + |
| 7 | +from typing import Any |
| 8 | + |
| 9 | +from fastapi import FastAPI |
| 10 | +from httpx import AsyncClient |
| 11 | +from starlette import status |
| 12 | + |
| 13 | +from examples.api_for_sqlalchemy.models import AgeRating |
| 14 | +from examples.api_for_sqlalchemy.schemas import AgeRatingAttributesSchema, MovieAttributesSchema |
| 15 | +from tests.misc.utils import fake |
| 16 | + |
| 17 | + |
| 18 | +async def test_get_age_rating_list( |
| 19 | + app: FastAPI, |
| 20 | + client: AsyncClient, |
| 21 | + age_rating_g: AgeRating, |
| 22 | +): |
| 23 | + """ |
| 24 | + Get age rating list, no `id` field on model |
| 25 | + """ |
| 26 | + url = app.url_path_for("get_age-rating_list") |
| 27 | + response = await client.get(url) |
| 28 | + assert response.status_code == status.HTTP_200_OK, response.text |
| 29 | + response_data = response.json() |
| 30 | + assert "data" in response_data, response_data |
| 31 | + entities: list[dict[str, Any]] = response_data["data"] |
| 32 | + for entity in entities: |
| 33 | + assert entity["id"] == entity["attributes"]["name"] |
| 34 | + |
| 35 | + rating_g_data = next( |
| 36 | + filter(lambda e: e["id"] == age_rating_g.name, entities), |
| 37 | + ) |
| 38 | + expected_rating_g_data = AgeRatingAttributesSchema.model_validate(age_rating_g).model_dump() |
| 39 | + assert rating_g_data["attributes"] == expected_rating_g_data |
| 40 | + |
| 41 | + |
| 42 | +async def test_create_with_related_age_rating( |
| 43 | + app: FastAPI, |
| 44 | + client: AsyncClient, |
| 45 | + age_rating_g: AgeRating, |
| 46 | +): |
| 47 | + """ |
| 48 | + Create with related age rating, no `id` field on model. |
| 49 | +
|
| 50 | + Field `name` is used for `id`. |
| 51 | + Check relation is created, new related object is included. |
| 52 | + """ |
| 53 | + url = app.url_path_for("get_movie_list") |
| 54 | + url = f"{url}?include=age_rating_obj" |
| 55 | + |
| 56 | + movie_attributes_obj = MovieAttributesSchema( |
| 57 | + title=fake.name(), |
| 58 | + description=fake.sentence(), |
| 59 | + ) |
| 60 | + movie_attributes = movie_attributes_obj.model_dump(exclude_unset=True) |
| 61 | + assert "age_rating" not in movie_attributes, "don't provide this field, it should be a related obj" |
| 62 | + relationship_data = { |
| 63 | + "age_rating_obj": { |
| 64 | + "data": { |
| 65 | + "type": "age-rating", |
| 66 | + "id": age_rating_g.name, |
| 67 | + }, |
| 68 | + }, |
| 69 | + } |
| 70 | + movie_create = { |
| 71 | + "data": { |
| 72 | + "attributes": movie_attributes, |
| 73 | + "relationships": relationship_data, |
| 74 | + }, |
| 75 | + } |
| 76 | + response = await client.post(url, json=movie_create) |
| 77 | + assert response.status_code == status.HTTP_201_CREATED, response.text |
| 78 | + response_data = response.json() |
| 79 | + movie_data: dict = response_data["data"] |
| 80 | + assert movie_data.pop("id") |
| 81 | + movie_attributes_obj.age_rating = age_rating_g.name |
| 82 | + assert movie_data == { |
| 83 | + "type": "movie", |
| 84 | + "attributes": movie_attributes_obj.model_dump(), |
| 85 | + "relationships": relationship_data, |
| 86 | + } |
| 87 | + |
| 88 | + included = response_data["included"] |
| 89 | + assert included == [ |
| 90 | + { |
| 91 | + "id": age_rating_g.name, |
| 92 | + "type": "age-rating", |
| 93 | + "attributes": AgeRatingAttributesSchema.model_validate(age_rating_g).model_dump(exclude_unset=True), |
| 94 | + }, |
| 95 | + ] |
0 commit comments