Skip to content

Commit bb21092

Browse files
committed
Validate squad numbers
1 parent 700194d commit bb21092

2 files changed

Lines changed: 18 additions & 5 deletions

File tree

models/player_model.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
- `PlayerRequestModel`: Represents player data for Create and Update operations.
66
- `PlayerResponseModel`: Represents player data including UUID for Retrieve operations.
77
8-
Design decision single request model vs split models:
8+
Design decision - single request model vs split models:
99
A single `PlayerRequestModel` is intentionally shared by both POST (Create)
1010
and PUT (Update). Per-operation differences are handled at the route layer
1111
rather than by duplicating the model:
@@ -19,7 +19,7 @@
1919

2020
from typing import Optional
2121
from uuid import UUID
22-
from pydantic import BaseModel, ConfigDict
22+
from pydantic import BaseModel, ConfigDict, Field
2323
from pydantic.alias_generators import to_camel
2424

2525

@@ -67,7 +67,7 @@ class PlayerRequestModel(MainModel):
6767
middle_name: Optional[str] = None
6868
last_name: str
6969
date_of_birth: Optional[str] = None
70-
squad_number: int
70+
squad_number: int = Field(gt=0)
7171
position: str
7272
abbr_position: Optional[str] = None
7373
team: Optional[str] = None

tests/test_main.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,19 @@ def test_request_post_player_body_existing_response_body_detail(client):
190190
)
191191

192192

193+
def test_request_post_player_body_zero_squad_number_response_status_unprocessable(
194+
client,
195+
):
196+
"""POST /players/ with squad number zero returns 422 Unprocessable Entity"""
197+
# Arrange
198+
player = nonexistent_player()
199+
payload = player.__dict__ | {"squad_number": 0}
200+
# Act
201+
response = client.post(PATH, json=payload)
202+
# Assert
203+
assert response.status_code == 422
204+
205+
193206
def test_request_post_player_body_nonexistent_response_status_created(client):
194207
"""POST /players/ with nonexistent player returns 201 Created with a valid UUID"""
195208
# Arrange
@@ -203,7 +216,7 @@ def test_request_post_player_body_nonexistent_response_status_created(client):
203216
assert "id" in body
204217
assert UUID(body["id"]).version == 4 # UUID v4 (API-created)
205218
finally:
206-
# Teardown remove the created player
219+
# Teardown - remove the created player
207220
client.delete(PATH + "squadnumber/" + str(player.squad_number))
208221

209222

@@ -250,7 +263,7 @@ def test_request_put_player_squadnumber_existing_response_status_no_content(clie
250263
# Assert
251264
assert response.status_code == 204
252265
finally:
253-
# Teardown restore Damián Martínez to its seeded state
266+
# Teardown - restore Damián Martínez to its seeded state
254267
seed = existing_player()
255268
client.put(PATH + "squadnumber/" + str(seed.squad_number), json=seed.__dict__)
256269

0 commit comments

Comments
 (0)