|
| 1 | +import pytest |
| 2 | +from fastapi import FastAPI, status |
| 3 | +from httpx import AsyncClient |
| 4 | +from pytest_asyncio import fixture |
| 5 | +from sqlalchemy.ext.asyncio import AsyncSession |
| 6 | + |
| 7 | +from tests.models import Task |
| 8 | +from tests.schemas import TaskBaseSchema |
| 9 | + |
| 10 | +pytestmark = pytest.mark.asyncio |
| 11 | + |
| 12 | + |
| 13 | +@fixture() |
| 14 | +async def task_with_none_ids( |
| 15 | + async_session: AsyncSession, |
| 16 | +) -> Task: |
| 17 | + task = Task(task_ids=None) |
| 18 | + async_session.add(task) |
| 19 | + await async_session.commit() |
| 20 | + |
| 21 | + yield task |
| 22 | + |
| 23 | + await async_session.delete(task) |
| 24 | + await async_session.commit() |
| 25 | + |
| 26 | + |
| 27 | +@pytest.fixture() |
| 28 | +def resource_type(): |
| 29 | + return "task" |
| 30 | + |
| 31 | + |
| 32 | +class TestTaskValidators: |
| 33 | + async def test_base_model_root_validator_get_one( |
| 34 | + self, |
| 35 | + app: FastAPI, |
| 36 | + client: AsyncClient, |
| 37 | + resource_type: str, |
| 38 | + task_with_none_ids: Task, |
| 39 | + ): |
| 40 | + url = app.url_path_for(f"get_{resource_type}_detail", obj_id=task_with_none_ids.id) |
| 41 | + res = await client.get(url) |
| 42 | + assert res.status_code == status.HTTP_200_OK, res.text |
| 43 | + response_data = res.json() |
| 44 | + attributes = response_data["data"].pop("attributes") |
| 45 | + assert response_data == { |
| 46 | + "data": { |
| 47 | + "id": str(task_with_none_ids.id), |
| 48 | + "type": resource_type, |
| 49 | + }, |
| 50 | + "jsonapi": {"version": "1.0"}, |
| 51 | + "meta": None, |
| 52 | + } |
| 53 | + assert attributes == { |
| 54 | + # not `None`! schema validator returns empty list `[]` |
| 55 | + # "task_ids": None, |
| 56 | + "task_ids": [], |
| 57 | + } |
| 58 | + assert attributes == TaskBaseSchema.from_orm(task_with_none_ids) |
| 59 | + |
| 60 | + async def test_base_model_root_validator_get_list( |
| 61 | + self, |
| 62 | + app: FastAPI, |
| 63 | + client: AsyncClient, |
| 64 | + resource_type: str, |
| 65 | + task_with_none_ids: Task, |
| 66 | + ): |
| 67 | + url = app.url_path_for(f"get_{resource_type}_list") |
| 68 | + res = await client.get(url) |
| 69 | + assert res.status_code == status.HTTP_200_OK, res.text |
| 70 | + response_data = res.json() |
| 71 | + assert response_data == { |
| 72 | + "data": [ |
| 73 | + { |
| 74 | + "id": str(task_with_none_ids.id), |
| 75 | + "type": resource_type, |
| 76 | + "attributes": { |
| 77 | + # not `None`! schema validator returns empty list `[]` |
| 78 | + # "task_ids": None, |
| 79 | + "task_ids": [], |
| 80 | + }, |
| 81 | + }, |
| 82 | + ], |
| 83 | + "jsonapi": { |
| 84 | + "version": "1.0", |
| 85 | + }, |
| 86 | + "meta": { |
| 87 | + "count": 1, |
| 88 | + "totalPages": 1, |
| 89 | + }, |
| 90 | + } |
| 91 | + |
| 92 | + async def test_base_model_root_validator_create( |
| 93 | + self, |
| 94 | + app: FastAPI, |
| 95 | + client: AsyncClient, |
| 96 | + resource_type: str, |
| 97 | + async_session: AsyncSession, |
| 98 | + ): |
| 99 | + task_data = { |
| 100 | + # should be converted to [] by schema on create |
| 101 | + "task_ids": None, |
| 102 | + } |
| 103 | + data_create = { |
| 104 | + "data": { |
| 105 | + "type": resource_type, |
| 106 | + "attributes": task_data, |
| 107 | + }, |
| 108 | + } |
| 109 | + url = app.url_path_for(f"create_{resource_type}_list") |
| 110 | + res = await client.post(url, json=data_create) |
| 111 | + assert res.status_code == status.HTTP_201_CREATED, res.text |
| 112 | + response_data: dict = res.json() |
| 113 | + task_id = response_data["data"].pop("id") |
| 114 | + task = await async_session.get(Task, int(task_id)) |
| 115 | + assert isinstance(task, Task) |
| 116 | + # we sent request with `None`, but value in db is `[]` |
| 117 | + # because validator converted data before object creation |
| 118 | + assert task.task_ids == [] |
| 119 | + assert response_data == { |
| 120 | + "data": { |
| 121 | + "type": resource_type, |
| 122 | + "attributes": { |
| 123 | + # should be empty list |
| 124 | + "task_ids": [], |
| 125 | + }, |
| 126 | + }, |
| 127 | + "jsonapi": {"version": "1.0"}, |
| 128 | + "meta": None, |
| 129 | + } |
0 commit comments