This commit is contained in:
@@ -30,6 +30,7 @@ class todocreate(BaseModel):
|
||||
|
||||
return value
|
||||
|
||||
|
||||
class todoupdate(BaseModel):
|
||||
completion: bool | None = None
|
||||
task: str | None = Field(
|
||||
@@ -52,12 +53,11 @@ class todoupdate(BaseModel):
|
||||
@model_validator(mode="after")
|
||||
def at_least_one_field_must_be_provided(self) -> Self:
|
||||
if self.task is None and self.completion is None:
|
||||
raise ValueError(
|
||||
"task or completion must be provided"
|
||||
)
|
||||
raise ValueError("task or completion must be provided")
|
||||
|
||||
return self
|
||||
|
||||
|
||||
class todoresponse(BaseModel):
|
||||
id: int
|
||||
task: str
|
||||
|
||||
@@ -10,6 +10,7 @@ class FakeScalarResult:
|
||||
def all(self) -> list[Todo]:
|
||||
return self.todos
|
||||
|
||||
|
||||
class FakeSession:
|
||||
def __init__(self) -> None:
|
||||
self.todo_to_return: Todo | None = None
|
||||
|
||||
@@ -8,19 +8,23 @@ def test_todocreate_requires_task() -> None:
|
||||
with pytest.raises(ValidationError):
|
||||
todocreate()
|
||||
|
||||
|
||||
def test_todocreate_accepts_valid_task() -> None:
|
||||
todo_data = todocreate(task="学习测试单元")
|
||||
|
||||
assert todo_data.task == "学习测试单元"
|
||||
|
||||
|
||||
def test_todocreate_rejects_empty_task() -> None:
|
||||
with pytest.raises(ValidationError):
|
||||
todocreate(task="")
|
||||
|
||||
|
||||
def test_todocreate_rejects_blank_task() -> None:
|
||||
with pytest.raises(ValidationError):
|
||||
todocreate(task=" ")
|
||||
|
||||
|
||||
def test_todocreate_rejects_task_longer_than_200_characters() -> None:
|
||||
long_task = "a" * 201
|
||||
|
||||
|
||||
@@ -25,6 +25,7 @@ def test_create_todo_rejects_invalid_task(
|
||||
|
||||
assert response.status_code == 422
|
||||
|
||||
|
||||
def test_create_todo_rejects_missing_task(
|
||||
client: TestClient,
|
||||
) -> None:
|
||||
@@ -40,6 +41,7 @@ def test_create_todo_rejects_missing_task(
|
||||
assert error["loc"] == ["body", "task"]
|
||||
assert error["type"] == "missing"
|
||||
|
||||
|
||||
def test_create_todo_success(
|
||||
client: TestClient,
|
||||
fake_session: FakeSession,
|
||||
@@ -58,6 +60,7 @@ def test_create_todo_success(
|
||||
assert fake_session.committed is True
|
||||
assert fake_session.added_todo.task == "学习单元测试"
|
||||
|
||||
|
||||
def test_patch_missing_todo_returns_404(
|
||||
client: TestClient,
|
||||
fake_session: FakeSession,
|
||||
@@ -76,6 +79,7 @@ def test_patch_missing_todo_returns_404(
|
||||
assert fake_session.committed is False
|
||||
assert fake_session.rolled_back is False
|
||||
|
||||
|
||||
def test_patch_existing_todo_updates_task(
|
||||
client: TestClient,
|
||||
fake_session: FakeSession,
|
||||
@@ -100,6 +104,7 @@ def test_patch_existing_todo_updates_task(
|
||||
assert fake_session.committed is True
|
||||
assert fake_session.todo_to_return.task == "新任务"
|
||||
|
||||
|
||||
def test_patch_existing_todo_updates_completion_only(
|
||||
client: TestClient,
|
||||
fake_session: FakeSession,
|
||||
@@ -125,6 +130,7 @@ def test_patch_existing_todo_updates_completion_only(
|
||||
assert fake_session.todo_to_return.task == "保留原任务"
|
||||
assert fake_session.todo_to_return.completion is True
|
||||
|
||||
|
||||
def test_delete_existing_todo_success(
|
||||
client: TestClient,
|
||||
fake_session: FakeSession,
|
||||
@@ -147,6 +153,7 @@ def test_delete_existing_todo_success(
|
||||
assert fake_session.committed is True
|
||||
assert fake_session.deleted_todo is existing_todo
|
||||
|
||||
|
||||
def test_delete_missing_todo_returns_404(
|
||||
client: TestClient,
|
||||
fake_session: FakeSession,
|
||||
@@ -161,6 +168,7 @@ def test_delete_missing_todo_returns_404(
|
||||
assert fake_session.deleted_todo is None
|
||||
assert fake_session.committed is False
|
||||
|
||||
|
||||
def test_create_todo_rolls_back_when_commit_fails(
|
||||
client: TestClient,
|
||||
fake_session: FakeSession,
|
||||
@@ -176,6 +184,7 @@ def test_create_todo_rolls_back_when_commit_fails(
|
||||
assert fake_session.rolled_back is True
|
||||
assert fake_session.committed is False
|
||||
|
||||
|
||||
def test_patch_todo_rolls_back_when_commit_fails(
|
||||
client: TestClient,
|
||||
fake_session: FakeSession,
|
||||
@@ -196,6 +205,7 @@ def test_patch_todo_rolls_back_when_commit_fails(
|
||||
assert fake_session.rolled_back is True
|
||||
assert fake_session.committed is False
|
||||
|
||||
|
||||
def test_delete_todo_rolls_back_when_commit_fails(
|
||||
client: TestClient,
|
||||
fake_session: FakeSession,
|
||||
@@ -215,6 +225,7 @@ def test_delete_todo_rolls_back_when_commit_fails(
|
||||
assert fake_session.rolled_back is True
|
||||
assert fake_session.committed is False
|
||||
|
||||
|
||||
def test_get_todos_returns_todo_list(
|
||||
client: TestClient,
|
||||
fake_session: FakeSession,
|
||||
@@ -248,6 +259,7 @@ def test_get_todos_returns_todo_list(
|
||||
},
|
||||
]
|
||||
|
||||
|
||||
def test_get_todos_returns_empty_list(
|
||||
client: TestClient,
|
||||
fake_session: FakeSession,
|
||||
@@ -259,6 +271,7 @@ def test_get_todos_returns_empty_list(
|
||||
assert response.status_code == 200
|
||||
assert response.json() == []
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"invalid_task",
|
||||
[
|
||||
@@ -285,6 +298,7 @@ def test_patch_todo_rejects_invalid_task(
|
||||
|
||||
assert response.status_code == 422
|
||||
|
||||
|
||||
def test_patch_todo_rejects_empty_body(
|
||||
client: TestClient,
|
||||
fake_session: FakeSession,
|
||||
@@ -303,6 +317,7 @@ def test_patch_todo_rejects_empty_body(
|
||||
assert response.status_code == 422
|
||||
assert fake_session.committed is False
|
||||
|
||||
|
||||
def test_patch_todo_accepts_completion_false(
|
||||
client: TestClient,
|
||||
fake_session: FakeSession,
|
||||
@@ -323,6 +338,7 @@ def test_patch_todo_accepts_completion_false(
|
||||
assert response.json()["completion"] is False
|
||||
assert fake_session.committed is True
|
||||
|
||||
|
||||
def test_patch_todo_updates_task_and_completion(
|
||||
client: TestClient,
|
||||
fake_session: FakeSession,
|
||||
|
||||
@@ -8,6 +8,7 @@ def test_todoupdate_rejects_empty_update() -> None:
|
||||
with pytest.raises(ValidationError):
|
||||
todoupdate()
|
||||
|
||||
|
||||
def test_todoupdate_accepts_provided_value() -> None:
|
||||
update_data = todoupdate(
|
||||
task="学习测试单元",
|
||||
@@ -16,6 +17,7 @@ def test_todoupdate_accepts_provided_value() -> None:
|
||||
|
||||
assert update_data.task == "学习测试单元"
|
||||
|
||||
|
||||
def test_todoupdate_accepts_task_only() -> None:
|
||||
update_data = todoupdate(task="修改后的任务")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user