This commit is contained in:
+48
-32
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user