Skip to content

Commit e18e96a

Browse files
committed
task ids validators updates
1 parent 7ac63c8 commit e18e96a

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

tests/schemas/task.py

+18-4
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,41 @@
11
from __future__ import annotations
22

3+
from typing import Annotated
4+
35
from pydantic import (
46
ConfigDict,
57
field_validator,
8+
BeforeValidator,
69
)
710

811
from fastapi_jsonapi.schema_base import BaseModel
912

1013

14+
def func_validator(value: list[str] | None) -> list[str]:
15+
"""
16+
return `[]`, if value is None both on get and on create
17+
18+
:param value:
19+
:return:
20+
"""
21+
return value or []
22+
23+
1124
class TaskBaseSchema(BaseModel):
1225
model_config = ConfigDict(from_attributes=True)
1326

1427
# TODO: check BeforeValidator annotated
1528
task_ids: list[str] | None = None
29+
another_task_ids: Annotated[list[str] | None, BeforeValidator(func_validator)]
1630

1731
# noinspection PyMethodParameters
18-
@field_validator("task_ids", mode="before", check_fields=False)
19-
@classmethod
20-
def task_ids_validator(cls, value: list[str] | None):
32+
@field_validator("task_ids", mode="before")
33+
@staticmethod
34+
def task_ids_validator(value: list[str] | None):
2135
"""
2236
return `[]`, if value is None both on get and on create
2337
"""
24-
return value or []
38+
return func_validator(value)
2539

2640

2741
class TaskPatchSchema(TaskBaseSchema):

tests/test_api/test_validators.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,6 @@ class UserAnnotatedSequenceNamesSchema(UserAttributesBaseSchema):
251251
assert detail["msg"].endswith(format_error(flag_name)), detail["msg"]
252252

253253

254-
@pytest.mark.xfail(reason="tasks ids passthrough not checked yet")
255254
@pytest.mark.usefixtures("refresh_db")
256255
class TestTaskValidators:
257256
async def test_base_model_validator_pre_true_get_one(
@@ -262,6 +261,7 @@ async def test_base_model_validator_pre_true_get_one(
262261
task_with_none_ids: Task,
263262
):
264263
assert task_with_none_ids.task_ids is None
264+
assert task_with_none_ids.another_task_ids is None
265265
url = app.url_path_for(f"get_{resource_type}_detail", obj_id=task_with_none_ids.id)
266266
res = await client.get(url)
267267
assert res.status_code == status.HTTP_200_OK, res.text
@@ -271,15 +271,17 @@ async def test_base_model_validator_pre_true_get_one(
271271
"data": {
272272
"id": ViewBase.get_db_item_id(task_with_none_ids),
273273
"type": resource_type,
274+
# dont' pass fields at all
274275
},
275276
"jsonapi": {"version": "1.0"},
276277
"meta": None,
277278
}
278279
assert attributes == {
279280
# not `None`! schema validator returns empty list `[]`
280281
"task_ids": [],
282+
"another_task_ids": [],
281283
}
282-
assert attributes == TaskBaseSchema.model_validate(task_with_none_ids)
284+
assert attributes == TaskBaseSchema.model_validate(task_with_none_ids).model_dump()
283285

284286
async def test_base_model_model_validator_get_list(
285287
self,
@@ -289,6 +291,7 @@ async def test_base_model_model_validator_get_list(
289291
task_with_none_ids: Task,
290292
):
291293
assert task_with_none_ids.task_ids is None
294+
assert task_with_none_ids.another_task_ids is None
292295
url = app.url_path_for(f"get_{resource_type}_list")
293296
res = await client.get(url)
294297
assert res.status_code == status.HTTP_200_OK, res.text
@@ -300,6 +303,7 @@ async def test_base_model_model_validator_get_list(
300303
"attributes": {
301304
# not `None`! schema validator returns empty list `[]`
302305
"task_ids": [],
306+
"another_task_ids": [],
303307
},
304308
},
305309
]
@@ -315,6 +319,7 @@ async def test_base_model_model_validator_create(
315319
task_data = {
316320
# should be converted to [] by schema on create
317321
"task_ids": None,
322+
"another_task_ids": None,
318323
}
319324
data_create = {
320325
"data": {
@@ -332,12 +337,14 @@ async def test_base_model_model_validator_create(
332337
# we sent request with `None`, but value in db is `[]`
333338
# because validator converted data before object creation
334339
assert task.task_ids == []
340+
assert task.another_task_ids == []
335341
assert response_data == {
336342
"data": {
337343
"type": resource_type,
338344
"attributes": {
339345
# should be empty list
340346
"task_ids": [],
347+
"another_task_ids": [],
341348
},
342349
},
343350
"jsonapi": {"version": "1.0"},

0 commit comments

Comments
 (0)