Skip to content

Commit a581763

Browse files
committed
upgrade tests
1 parent ff9c9c4 commit a581763

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

tests/test_api/test_api_sqla_with_includes.py

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from copy import deepcopy
33
from itertools import chain, zip_longest
44
from json import dumps
5-
from typing import Dict, List, Type
5+
from typing import Dict, List, Optional, Type
66
from uuid import UUID, uuid4
77

88
from fastapi import FastAPI, status
@@ -2050,19 +2050,13 @@ def _refresh_caches(self) -> None:
20502050

20512051
RoutersJSONAPI.all_jsonapi_routers = all_jsonapi_routers
20522052

2053-
def _clear_cache(self):
2054-
SchemaBuilder.object_schemas_cache = {}
2055-
SchemaBuilder.relationship_schema_cache = {}
2056-
SchemaBuilder.base_jsonapi_object_schemas_cache = {}
2057-
RoutersJSONAPI.all_jsonapi_routers = {}
2058-
2059-
def build_app(self, schema) -> FastAPI:
2053+
def build_app(self, schema, resource_type: Optional[str] = None) -> FastAPI:
20602054
return build_app_custom(
20612055
model=User,
20622056
schema=schema,
2063-
schema_in_post=schema,
2064-
schema_in_patch=schema,
2065-
resource_type=self.resource_type,
2057+
# schema_in_post=schema,
2058+
# schema_in_patch=schema,
2059+
resource_type=resource_type or self.resource_type,
20662060
)
20672061

20682062
def inherit(self, schema: Type[BaseModel]) -> Type[BaseModel]:
@@ -2076,9 +2070,11 @@ async def execute_request_and_check_response(
20762070
app: FastAPI,
20772071
body: Dict,
20782072
expected_detail: str,
2073+
resource_type: Optional[str] = None,
20792074
):
2075+
resource_type = resource_type or self.resource_type
20802076
async with AsyncClient(app=app, base_url="http://test") as client:
2081-
url = app.url_path_for(f"get_{self.resource_type}_list")
2077+
url = app.url_path_for(f"get_{resource_type}_list")
20822078
res = await client.post(url, json=body)
20832079
assert res.status_code == status.HTTP_400_BAD_REQUEST, res.text
20842080
assert res.json() == {
@@ -2093,7 +2089,6 @@ async def execute_request_and_check_response(
20932089
],
20942090
},
20952091
}
2096-
self._clear_cache()
20972092

20982093
async def execute_request_twice_and_check_response(
20992094
self,
@@ -2104,15 +2099,17 @@ async def execute_request_twice_and_check_response(
21042099
"""
21052100
Makes two requests for check schema inheritance
21062101
"""
2107-
app_1 = self.build_app(schema)
2108-
self._clear_cache()
2109-
app_2 = self.build_app(self.inherit(schema))
2102+
resource_type_1 = self.resource_type + fake.word()
2103+
app_1 = self.build_app(schema, resource_type=resource_type_1)
2104+
resource_type_2 = self.resource_type + fake.word()
2105+
app_2 = self.build_app(self.inherit(schema), resource_type=resource_type_2)
21102106

2111-
for app in [app_1, app_2]:
2107+
for app, resource_type in [(app_1, resource_type_1), (app_2, resource_type_2)]:
21122108
await self.execute_request_and_check_response(
21132109
app=app,
21142110
body=body,
21152111
expected_detail=expected_detail,
2112+
resource_type=resource_type,
21162113
)
21172114

21182115
async def test_field_validator_call(self):

tests/test_api/test_validators.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ async def test_base_model_validator_pre_true_get_one(
3434
resource_type: str,
3535
task_with_none_ids: Task,
3636
):
37+
assert task_with_none_ids.task_ids is None
3738
url = app.url_path_for(f"get_{resource_type}_detail", obj_id=task_with_none_ids.id)
3839
res = await client.get(url)
3940
assert res.status_code == status.HTTP_200_OK, res.text
@@ -61,6 +62,7 @@ async def test_base_model_root_validator_get_list(
6162
resource_type: str,
6263
task_with_none_ids: Task,
6364
):
65+
assert task_with_none_ids.task_ids is None
6466
url = app.url_path_for(f"get_{resource_type}_list")
6567
res = await client.get(url)
6668
assert res.status_code == status.HTTP_200_OK, res.text
@@ -110,6 +112,7 @@ async def test_base_model_root_validator_create(
110112
task_id = response_data["data"].pop("id")
111113
task = await async_session.get(Task, int(task_id))
112114
assert isinstance(task, Task)
115+
assert task.task_ids == []
113116
# we sent request with `None`, but value in db is `[]`
114117
# because validator converted data before object creation
115118
assert task.task_ids == []

0 commit comments

Comments
 (0)