Repository navigation
Expand file tree
/
Copy pathtest_todo.py
More file actions
149 lines (89 loc) · 3.01 KB
/
Copy pathtest_todo.py
File metadata and controls
149 lines (89 loc) · 3.01 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
from todo import add_task, mark_done, delete_task, save_tasks, load_tasks
import json
import pytest
def test_add_task():
tasks = []
add_task(tasks, "Learn Python")
assert len(tasks) == 1
assert tasks[0] == {"title": "Learn Python", "done": False}
def test_mark_done():
tasks = [{"title": "Learn Python", "done": False}]
result = mark_done(tasks, 1)
assert result is True
assert tasks[0]["done"] is True
def test_mark_done_invalid_zero():
tasks = [{"title": "Learn Python", "done": False}]
result = mark_done(tasks, 0)
assert result is False
assert tasks[0]["done"] is False
def test_mark_done_invalid_number():
tasks = [{"title": "Learn Python", "done": False}]
result = mark_done(tasks, 5)
assert result is False
assert tasks[0]["done"] is False
def test_delete_task():
tasks = [
{"title": "Learn Python", "done": False},
{"title": "Learn Git", "done": False},
]
result = delete_task(tasks, 1)
assert result is True
assert len(tasks) == 1
assert tasks[0]["title"] == "Learn Git"
def test_delete_task_invalid_number():
tasks = [{"title": "Learn Python", "done": False}]
result = delete_task(tasks, 5)
assert result is False
assert len(tasks) == 1
def test_add_multiple_tasks():
tasks = []
add_task(tasks, "Learn Python")
add_task(tasks, "Learn Git")
assert len(tasks) == 2
assert tasks[0]["title"] == "Learn Python"
assert tasks[1]["title"] == "Learn Git"
def test_mark_done_empty_list():
tasks = []
result = mark_done(tasks, 1)
assert result is False
assert tasks == []
def test_delete_task_zero():
tasks = [{"title": "Learn Python", "done": False}]
result = delete_task(tasks, 0)
assert result is False
assert len(tasks) == 1
def test_delete_task_empty_list():
tasks = []
result = delete_task(tasks, 1)
assert result is False
assert tasks == []
def test_save_tasks(tmp_path, monkeypatch):
monkeypatch.chdir(tmp_path)
tasks = [
{"title": "Learn Python", "done": False},
{"title": "Learn Git", "done": True},
]
save_tasks(tasks)
with open("to_do.json", "r") as file:
saved_tasks = json.load(file)
assert saved_tasks == tasks
def test_load_tasks(tmp_path, monkeypatch):
monkeypatch.chdir(tmp_path)
expected_tasks = [
{"title": "Learn Python", "done": False},
{"title": "Learn Git", "done": True},
]
with open("to_do.json", "w") as file:
json.dump(expected_tasks, file)
loaded_tasks = load_tasks()
assert loaded_tasks == expected_tasks
def test_load_tasks_missing_file(tmp_path, monkeypatch):
monkeypatch.chdir(tmp_path)
tasks = load_tasks()
assert tasks == []
def test_load_tasks_corrupted_json(tmp_path, monkeypatch):
monkeypatch.chdir(tmp_path)
with open("to_do.json", "w") as file:
file.write('{"title": "Learn Python", "done": False')
with pytest.raises(json.JSONDecodeError):
load_tasks()