2424async def task_with_none_ids (
2525 async_session : AsyncSession ,
2626) -> Task :
27- task = Task (task_ids = None )
27+ task = Task (
28+ task_ids_dict = None ,
29+ task_ids_list = None ,
30+ )
2831 async_session .add (task )
2932 await async_session .commit ()
3033
@@ -44,7 +47,8 @@ async def test_base_model_validator_pre_true_get_one(
4447 resource_type : str ,
4548 task_with_none_ids : Task ,
4649 ):
47- assert task_with_none_ids .task_ids is None
50+ assert task_with_none_ids .task_ids_dict is None
51+ assert task_with_none_ids .task_ids_list is None
4852 url = app .url_path_for (f"get_{ resource_type } _detail" , obj_id = task_with_none_ids .id )
4953 res = await client .get (url )
5054 assert res .status_code == status .HTTP_200_OK , res .text
@@ -59,20 +63,22 @@ async def test_base_model_validator_pre_true_get_one(
5963 "meta" : None ,
6064 }
6165 assert attributes == {
62- # not `None`! schema validator returns empty list `[]`
66+ # not `None`! schema validator returns empty dict `{}` and empty list `[]`
6367 # "task_ids": None,
64- "task_ids" : [],
68+ "task_ids_dict" : {},
69+ "task_ids_list" : [],
6570 }
6671 assert attributes == TaskBaseSchema .model_validate (task_with_none_ids ).model_dump ()
6772
68- async def test_base_model_model_validator_get_list (
73+ async def test_base_model_model_validator_get_list_and_dict (
6974 self ,
7075 app : FastAPI ,
7176 client : AsyncClient ,
7277 resource_type : str ,
7378 task_with_none_ids : Task ,
7479 ):
75- assert task_with_none_ids .task_ids is None
80+ assert task_with_none_ids .task_ids_dict is None
81+ assert task_with_none_ids .task_ids_list is None
7682 url = app .url_path_for (f"get_{ resource_type } _list" )
7783 res = await client .get (url )
7884 assert res .status_code == status .HTTP_200_OK , res .text
@@ -83,9 +89,10 @@ async def test_base_model_model_validator_get_list(
8389 "id" : f"{ task_with_none_ids .id } " ,
8490 "type" : resource_type ,
8591 "attributes" : {
86- # not `None`! schema validator returns empty list `[]`
92+ # not `None`! schema validator returns empty dict `{}` and empty list `[]`
8793 # "task_ids": None,
88- "task_ids" : [],
94+ "task_ids_dict" : {},
95+ "task_ids_list" : [],
8996 },
9097 },
9198 ],
@@ -109,8 +116,9 @@ async def test_base_model_model_validator_create(
109116 "data" : {
110117 "type" : resource_type ,
111118 "attributes" : {
112- # should be converted to [] by schema on create
113- "task_ids" : None ,
119+ # should be converted to [] and {} by schema on create
120+ "task_ids_dict" : None ,
121+ "task_ids_list" : None ,
114122 },
115123 },
116124 }
@@ -121,16 +129,17 @@ async def test_base_model_model_validator_create(
121129 task_id = response_data ["data" ].pop ("id" )
122130 task = await async_session .get (Task , int (task_id ))
123131 assert isinstance (task , Task )
124- assert task .task_ids == []
125- # we sent request with `None`, but value in db is `[]`
132+ # we sent request with `None`, but value in db is `[]` and `{}`
126133 # because validator converted data before object creation
127- assert task .task_ids == []
134+ assert task .task_ids_dict == {}
135+ assert task .task_ids_list == []
128136 assert response_data == {
129137 "data" : {
130138 "type" : resource_type ,
131139 "attributes" : {
132- # should be empty list
133- "task_ids" : [],
140+ # should be empty list and empty dict
141+ "task_ids_dict" : {},
142+ "task_ids_list" : [],
134143 },
135144 },
136145 "jsonapi" : {"version" : "1.0" },
0 commit comments