Skip to content

Commit cb5ef3f

Browse files
committed
added validator test cases
1 parent b557bb8 commit cb5ef3f

File tree

2 files changed

+75
-4
lines changed

2 files changed

+75
-4
lines changed

tests/test_api/test_api_sqla_with_includes.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2293,6 +2293,38 @@ class Config:
22932293
expected_detail="Check validator",
22942294
)
22952295

2296+
async def test_field_validator_can_change_value(self):
2297+
class UserSchemaWithValidator(BaseModel):
2298+
name: str
2299+
2300+
@validator("name")
2301+
def fix_title(cls, v):
2302+
return v.title()
2303+
2304+
class Config:
2305+
orm_mode = True
2306+
2307+
attrs = {"name": "john doe"}
2308+
create_user_body = {"data": {"attributes": attrs}}
2309+
2310+
app = self.build_app(UserSchemaWithValidator)
2311+
async with AsyncClient(app=app, base_url="http://test") as client:
2312+
url = app.url_path_for(f"get_{self.resource_type}_list")
2313+
res = await client.post(url, json=create_user_body)
2314+
assert res.status_code == status.HTTP_201_CREATED, res.text
2315+
2316+
res_json = res.json()
2317+
assert res_json["data"]
2318+
assert res_json["data"].pop("id")
2319+
assert res_json == {
2320+
"data": {
2321+
"attributes": {"name": "John Doe"},
2322+
"type": "validator",
2323+
},
2324+
"jsonapi": {"version": "1.0"},
2325+
"meta": None,
2326+
}
2327+
22962328
@mark.parametrize(
22972329
("name", "expected_detail"),
22982330
[
@@ -2346,5 +2378,38 @@ class Config:
23462378
expected_detail=expected_detail,
23472379
)
23482380

2381+
async def test_root_validator_can_change_value(self):
2382+
class UserSchemaWithValidator(BaseModel):
2383+
name: str
2384+
2385+
@root_validator
2386+
def fix_title(cls, v):
2387+
v["name"] = v["name"].title()
2388+
return v
2389+
2390+
class Config:
2391+
orm_mode = True
2392+
2393+
attrs = {"name": "john doe"}
2394+
create_user_body = {"data": {"attributes": attrs}}
2395+
2396+
app = self.build_app(UserSchemaWithValidator)
2397+
async with AsyncClient(app=app, base_url="http://test") as client:
2398+
url = app.url_path_for(f"get_{self.resource_type}_list")
2399+
res = await client.post(url, json=create_user_body)
2400+
assert res.status_code == status.HTTP_201_CREATED, res.text
2401+
2402+
res_json = res.json()
2403+
assert res_json["data"]
2404+
assert res_json["data"].pop("id")
2405+
assert res_json == {
2406+
"data": {
2407+
"attributes": {"name": "John Doe"},
2408+
"type": "validator",
2409+
},
2410+
"jsonapi": {"version": "1.0"},
2411+
"meta": None,
2412+
}
2413+
23492414

23502415
# todo: test errors

tests/test_atomic/test_create_objects.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,17 @@
2121
UserBioAttributesBaseSchema,
2222
)
2323

24+
COLUMN_CHARACTERS_LIMIT = 50
25+
2426
pytestmark = mark.asyncio
2527

2628
logging.basicConfig(level=logging.DEBUG)
2729

2830

31+
def random_sentence() -> str:
32+
return fake.sentence()[:COLUMN_CHARACTERS_LIMIT]
33+
34+
2935
class TestAtomicCreateObjects:
3036
async def test_operations_empty_list(self, client: AsyncClient):
3137
data_atomic_request = {
@@ -216,7 +222,7 @@ async def test_create_bio_with_relationship_to_user_to_one(
216222
):
217223
user_bio = UserBioAttributesBaseSchema(
218224
birth_city=fake.city(),
219-
favourite_movies=fake.sentence(),
225+
favourite_movies=random_sentence(),
220226
)
221227
stmt_bio = select(UserBio).where(UserBio.user_id == user_1.id)
222228
res: Result = await async_session.execute(stmt_bio)
@@ -280,7 +286,7 @@ async def test_create_user_and_user_bio_with_local_id(
280286
)
281287
user_bio_data = UserBioAttributesBaseSchema(
282288
birth_city=fake.city(),
283-
favourite_movies=fake.sentence(),
289+
favourite_movies=random_sentence(),
284290
)
285291

286292
user_stmt = (
@@ -481,7 +487,7 @@ async def test_create_user_and_create_bio_and_computer_for_user(
481487
)
482488
user_bio_data = UserBioAttributesBaseSchema(
483489
birth_city=fake.city(),
484-
favourite_movies=fake.sentence(),
490+
favourite_movies=random_sentence(),
485491
)
486492
computer_data = ComputerAttributesBaseSchema(
487493
name=fake.word(),
@@ -795,7 +801,7 @@ async def test_create_and_associate_many_to_many(
795801
):
796802
parent_data = ParentAttributesSchema(name=fake.name())
797803
child_data = ChildAttributesSchema(name=fake.name())
798-
association_extra_data = fake.sentence()
804+
association_extra_data = random_sentence()
799805

800806
data_atomic_request = {
801807
"atomic:operations": [

0 commit comments

Comments
 (0)