-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_app.py
More file actions
57 lines (46 loc) · 1.62 KB
/
Copy pathtest_app.py
File metadata and controls
57 lines (46 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import pytest
from app import add_task, list_tasks, update_task, delete_task, clear_tasks
@pytest.fixture(autouse=True)
def setup_and_teardown():
clear_tasks()
yield
clear_tasks()
def test_add_task():
task = add_task("Buy groceries")
assert task["title"] == "Buy groceries"
assert task["id"] == 1
assert task["completed"] is False
assert len(list_tasks()) == 1
def test_add_task_empty_title():
with pytest.raises(ValueError, match="Title cannot be empty"):
add_task("")
with pytest.raises(ValueError, match="Title cannot be empty"):
add_task(" ")
def test_list_tasks():
add_task("Task 1")
add_task("Task 2")
tasks = list_tasks()
assert len(tasks) == 2
assert tasks[0]["title"] == "Task 1"
assert tasks[1]["title"] == "Task 2"
def test_update_task():
task = add_task("Original Title")
updated_task = update_task(task["id"], title="New Title", completed=True)
assert updated_task["title"] == "New Title"
assert updated_task["completed"] is True
tasks = list_tasks()
assert tasks[0]["title"] == "New Title"
assert tasks[0]["completed"] is True
def test_update_task_empty_title():
task = add_task("Task")
with pytest.raises(ValueError, match="Title cannot be empty"):
update_task(task["id"], title=" ")
def test_update_nonexistent_task():
result = update_task(999, title="New")
assert result is None
def test_delete_task():
task = add_task("To be deleted")
assert delete_task(task["id"]) is True
assert len(list_tasks()) == 0
def test_delete_nonexistent_task():
assert delete_task(999) is False