From 99e42f8be44bb21144c7b59e17582527f763cf75 Mon Sep 17 00:00:00 2001 From: dengwenhao <1403595435@qq.com> Date: Fri, 31 Jul 2026 10:26:51 +0800 Subject: [PATCH] style: format Python files with Ruff --- main.py | 8 ++-- tests/fakes.py | 1 + tests/test_todocreate.py | 8 +++- tests/test_todos_api.py | 80 ++++++++++++++++++++++++---------------- tests/test_todoupdate.py | 8 ++-- 5 files changed, 64 insertions(+), 41 deletions(-) diff --git a/main.py b/main.py index 8b0309f..d36e5b1 100644 --- a/main.py +++ b/main.py @@ -30,6 +30,7 @@ class todocreate(BaseModel): return value + class todoupdate(BaseModel): completion: bool | None = None task: str | None = Field( @@ -48,16 +49,15 @@ class todoupdate(BaseModel): raise ValueError("task must not be blank") return value - + @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 diff --git a/tests/fakes.py b/tests/fakes.py index a39a25b..009ade8 100644 --- a/tests/fakes.py +++ b/tests/fakes.py @@ -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 diff --git a/tests/test_todocreate.py b/tests/test_todocreate.py index 13e868b..31b870a 100644 --- a/tests/test_todocreate.py +++ b/tests/test_todocreate.py @@ -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 = "学习测试单元") - + 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 diff --git a/tests/test_todos_api.py b/tests/test_todos_api.py index 38f7ba2..7608fdd 100644 --- a/tests/test_todos_api.py +++ b/tests/test_todos_api.py @@ -7,17 +7,17 @@ from tests.fakes import FakeSession @pytest.mark.parametrize( - "invalid_task", - [ - "", - " ", - "a" * 201, - ], + "invalid_task", + [ + "", + " ", + "a" * 201, + ], ) def test_create_todo_rejects_invalid_task( - client: TestClient, - invalid_task:str, - ) -> None: + client: TestClient, + invalid_task: str, +) -> None: response = client.post( "/todos", json={"task": invalid_task}, @@ -25,9 +25,10 @@ def test_create_todo_rejects_invalid_task( assert response.status_code == 422 + def test_create_todo_rejects_missing_task( - client: TestClient, - ) -> None: + client: TestClient, +) -> None: response = client.post( "/todos", json={}, @@ -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, @@ -51,13 +53,14 @@ def test_create_todo_success( assert response.status_code == 200 assert response.json() == { - "id": 1, - "task": "学习单元测试", - "completion": False, - } + "id": 1, + "task": "学习单元测试", + "completion": False, + } 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, @@ -93,13 +97,14 @@ def test_patch_existing_todo_updates_task( assert response.status_code == 200 assert response.json() == { - "id": 1, - "task": "新任务", - "completion": False, - } + "id": 1, + "task": "新任务", + "completion": False, + } 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, @@ -117,14 +122,15 @@ def test_patch_existing_todo_updates_completion_only( assert response.status_code == 200 assert response.json() == { - "id": 1, - "task": "保留原任务", - "completion": True, - } + "id": 1, + "task": "保留原任务", + "completion": True, + } assert fake_session.committed is True 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, @@ -140,13 +146,14 @@ def test_delete_existing_todo_success( assert response.status_code == 200 assert response.json() == { - "id": 1, - "task": "准备删除的任务", - "completion": False, - } + "id": 1, + "task": "准备删除的任务", + "completion": False, + } 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, @@ -344,10 +360,10 @@ def test_patch_todo_updates_task_and_completion( assert response.status_code == 200 assert response.json() == { - "id": 1, - "task": "修改后的任务", - "completion": True, - } + "id": 1, + "task": "修改后的任务", + "completion": True, + } assert existing_todo.task == "修改后的任务" assert existing_todo.completion is True assert fake_session.committed is True diff --git a/tests/test_todoupdate.py b/tests/test_todoupdate.py index b92af18..2035a2a 100644 --- a/tests/test_todoupdate.py +++ b/tests/test_todoupdate.py @@ -8,14 +8,16 @@ def test_todoupdate_rejects_empty_update() -> None: with pytest.raises(ValidationError): todoupdate() + def test_todoupdate_accepts_provided_value() -> None: update_data = todoupdate( - task = "学习测试单元", - completion = True, - ) + task="学习测试单元", + completion=True, + ) assert update_data.task == "学习测试单元" + def test_todoupdate_accepts_task_only() -> None: update_data = todoupdate(task="修改后的任务")