forked from mf75/appointments
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_api.py
More file actions
313 lines (267 loc) · 14.9 KB
/
test_api.py
File metadata and controls
313 lines (267 loc) · 14.9 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import unittest
from datetime import datetime
from unittest.mock import patch
from api import app, appointments, extract_and_validate_data_fields, validate_category_types, CATEGORY_TYPES, \
validate_appointment
class TestApi(unittest.TestCase):
def setUp(self):
appointments.clear()
self.client = app.test_client()
def test_list_appointments_empty(self):
response = self.client.get("/appointments")
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json, [])
@patch("api.extract_and_validate_data_fields", wraps = extract_and_validate_data_fields)
def test_list_appointments_with_entries(self, mock_extract):
self.client.post("/appointments",
json = {"title": "Meeting", "start": "2025-09-26 10:00", "end": "2025-09-26 12:00",
"category": "general"})
self.client.post("/appointments",
json = {"title": "Meeting2", "start": "2025-09-26 13:00", "end": "2025-09-26 14:00",
"category": "general"})
response = self.client.get("/appointments")
self.assertEqual(response.status_code, 200)
self.assertEqual(len(appointments), 2)
mock_extract.assert_called()
def test_create_appointment(self):
response = self.client.post("/appointments",
json = {"title": "Meeting", "start": "2025-09-26 10:00",
"end": "2025-09-26 12:00", "category": "general"})
self.assertEqual(response.status_code, 201)
self.assertEqual(appointments[0]["title"], "Meeting")
self.assertEqual(appointments[0]["start"], datetime(2025, 9, 26, 10, 0))
self.assertEqual(appointments[0]["end"], datetime(2025, 9, 26, 12, 0))
self.assertEqual(appointments[0]["category"], "general")
def test_create_overlapping_appointment(self):
self.client.post("/appointments",
json = {"title": "Meeting", "start": "2025-09-26 10:00", "end": "2025-09-26 12:00",
"category": "general"})
response = self.client.post("/appointments",
json = {"title": "Meeting2", "start": "2025-09-26 09:00", "end": "2025-09-26 13:00",
"category": "general"})
self.assertEqual(response.status_code, 409)
self.assertIn("error", response.json)
self.assertEqual(response.json["error"], "Overlapping appointment")
@patch("api.extract_and_validate_data_fields", wraps = extract_and_validate_data_fields)
def test_update_appointment(self, mock_extract):
self.client.post("/appointments",
json = {"title": "Meeting", "start": "2025-09-26 10:00", "end": "2025-09-26 12:00",
"category": "general"})
appt_id = appointments[0]["id"]
response = self.client.put(f"/appointments/{appt_id}",
json = {"title": "Sommerfest Meeting", "start": "2025-09-26 13:00",
"end": "2025-09-26 15:00", "category": "general"})
self.assertEqual(response.status_code, 200)
self.assertEqual(appointments[0]["title"], "Sommerfest Meeting")
self.assertEqual(appointments[0]["start"], datetime(2025, 9, 26, 13, 0))
self.assertEqual(appointments[0]["end"], datetime(2025, 9, 26, 15, 0))
self.assertEqual(appointments[0]["category"], "general")
mock_extract.assert_called()
def test_update_overlapping_appointment(self):
self.client.post("/appointments",
json = {"title": "Meeting", "start": "2025-09-26 10:00", "end": "2025-09-26 12:00",
"category": "general"})
self.client.post("/appointments",
json = {"title": "Meeting2", "start": "2025-09-26 13:00", "end": "2025-09-26 15:00",
"category": "general"})
response = self.client.put("/appointments/2",
json = {"title": "Meeting2", "start": "2025-09-26 11:00", "end": "2025-09-26 14:00",
"category": "general"})
self.assertEqual(response.status_code, 409)
self.assertEqual(response.json["error"], "Overlapping appointment")
def test_update_overlapping_appointment_id_error(self):
self.client.post("/appointments",
json = {"title": "Meeting", "start": "2025-09-26 10:00", "end": "2025-09-26 12:00",
"category": "general"})
response = self.client.put("/appointments/2",
json = {"title": "Meeting", "start": "2025-09-26 13:00", "end": "2025-09-26 14:00",
"category": "general"})
self.assertEqual(response.status_code, 404)
self.assertEqual(response.json["error"], "Appointment not found")
def test_delete_appointment(self):
self.client.post("/appointments",
json = {"title": "Meeting", "start": "2025-09-26 10:00", "end": "2025-09-26 12:00",
"category": "general"})
appt_id = appointments[0]["id"]
response = self.client.delete(f"/appointments/{appt_id}")
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json["status"], "deleted")
self.assertEqual(len(appointments), 0)
def test_delete_appointment_not_found(self):
self.client.post("/appointments",
json = {"title": "Meeting", "start": "2025-09-26 10:00", "end": "2025-09-26 12:00",
"category": "general"})
response = self.client.delete("/appointments/8")
self.assertEqual(response.status_code, 404)
self.assertEqual(response.json["error"], "Appointment not found")
self.assertEqual(len(appointments), 1)
def test_invalid_appointments(self):
fehlerhafte_termine = [
{},
{"category": "1"},
{"title": "1"},
{"title": "1", "end": "1"},
{"start": "1", "end": "1"},
{"title": "1", "start": "1"},
{"title": "1", "start": "1", "category": "1"},
{"title": "1", "start": "1", "end": "1", "geheim": "1"},
{"title": "1", "start": "1", "end": "1", "geheim": "1", "category": "1"}
]
for json_data in fehlerhafte_termine:
with self.assertRaises(ValueError):
validate_appointment(json_data)
def test_valid_appointment(self):
json_data = {"title": "Meeting", "start": "2025-09-26 10:00", "end": "2025-09-26 12:00", "category": "general"}
validate_appointment(json_data)
def test_extract_fields_with_valid_appointment(self):
json_data = {"title": "Meeting", "start": "2025-09-26 10:00", "end": "2025-09-26 12:00", "category": "general"}
result = extract_and_validate_data_fields(json_data)
self.assertEqual(result, ("Meeting", datetime(2025, 9, 26, 10, 0), datetime(2025, 9, 26, 12, 0), "general"))
def test_extract_fields_with_invalid_datetime_format(self):
json_data = {
"title": "Meeting", "start": "2025-09-26 10:00", "end": "2025-0a-26 12:00", "category": "general"}
with self.assertRaises(ValueError):
extract_and_validate_data_fields(json_data)
def test_extract_fields_with_invalid_appointment(self):
with self.assertRaises(ValueError) as contextManager:
extract_and_validate_data_fields({})
self.assertEqual(contextManager.exception.args[0], "Invalid appointment: wrong or missing fields")
def test_check_appointment_list(self):
self.assertEqual(len(appointments), 0)
response = self.client.post("/appointments",
json = {"title": "Meeting", "start": "2025-09-26 10:00", "end": "2025-09-26 12:00",
"category": "general"})
self.assertEqual(response.status_code, 201)
self.assertEqual(len(appointments), 1)
self.assertEqual(appointments[0]["title"], "Meeting")
self.assertEqual(appointments[0]["start"], datetime(2025, 9, 26, 10, 0))
self.assertEqual(appointments[0]["end"], datetime(2025, 9, 26, 12, 0))
def test_list_appointments_with_category_filter(self):
appointments.append(
{"id": 1, "title": "Arzt", "start": datetime(2025, 9, 26, 13, 0), "end": datetime(2025, 9, 26, 14, 0),
"category": "health"})
appointments.append(
{"id": 2, "title": "Meeting", "start": datetime(2025, 9, 26, 15, 0), "end": datetime(2025, 9, 26, 16, 0),
"category": "work"})
appointments.append(
{"id": 3, "title": "Zahnarzt", "start": datetime(2025, 9, 26, 18, 0), "end": datetime(2025, 9, 26, 18, 30),
"category": "health"})
response = self.client.get("/appointments?category=health")
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.json), 2)
self.assertEqual(response.json[0]["category"], "health")
self.assertEqual(response.json[1]["category"], "health")
def test_list_appointments_with_invalid_category(self):
appointments.append({"title": "Meeting", "start": "10:00", "end": "12:00", "category": "doesnotexist"})
response = self.client.get("/appointments?category=doesnotexist")
self.assertIn("error", response.json)
self.assertEqual(response.json["error"],
"Invalid category. Must be one of ['health', 'general', 'work', 'social']")
def test_no_valid_category_type(self):
category = {"category": "abc"}
with self.assertRaises(ValueError) as contextManager:
validate_category_types(category)
self.assertEqual(contextManager.exception.args[0], f"Invalid category. Must be one of {CATEGORY_TYPES}")
def test_no_appointments_for_category(self):
appointments.append({"title": "Meeting", "start": "10:00", "end": "12:00", "category": "general"})
response = self.client.get("/appointments?category=health")
self.assertEqual(response.status_code, 404)
self.assertIn("error", response.json)
self.assertEqual(response.json["error"], "No appointments found for this category")
def test_shift_appointments_various_amounts(self):
test_cases = [
{"amount_start": 1, "amount_end": 1, "expected_start": datetime(2025, 1, 1, 10, 0),
"expected_end": datetime(2025, 1, 2, 11, 0)},
{"amount_start": -1.5, "amount_end": -1.5, "expected_start": datetime(2024, 12, 29, 22, 0),
"expected_end": datetime(2024, 12, 30, 23, 0)},
{"amount_start": 2.3, "amount_end": 2.3, "expected_start": datetime(2025, 1, 2, 17, 12),
"expected_end": datetime(2025, 1, 3, 18, 12)},
]
for case in test_cases:
appointments.clear()
appointments.append({
"id": 1,
"title": "Test Meeting",
"start": datetime(2024, 12, 31, 10, 0),
"end": datetime(2025, 1, 1, 11, 0),
"category": "work"
})
response = self.client.post(
f"/appointments/shift/1?amount_start={case['amount_start']}&amount_end={case['amount_end']}"
)
self.assertEqual(response.status_code, 200)
self.assertEqual(appointments[0]["id"], 1)
self.assertEqual(appointments[0]["start"], case["expected_start"])
self.assertEqual(appointments[0]["end"], case["expected_end"])
def test_shift_appointment_false_id(self):
response = self.client.post("/appointments/shift/0?amount=5")
self.assertEqual(response.status_code, 404)
self.assertIn("error", response.json)
self.assertEqual(response.json["error"], "Appointment not found")
def test_shift_appointment_false_amount(self):
appointments.append({
"id": 1,
"title": "Test Meeting",
"start": datetime(2024, 12, 31, 10, 0),
"end": datetime(2025, 1, 1, 11, 0),
"category": "work"
})
response = self.client.post("/appointments/shift/1?amount_start=xx")
self.assertIn("error", response.json)
self.assertEqual(response.json["error"], "Invalid amount. Must be a number.")
def test_end_shift(self):
appointments.append({
"id": 1,
"title": "Test Meeting",
"start": datetime(2024, 12, 31, 10, 0),
"end": datetime(2025, 1, 1, 11, 0),
"category": "work"
})
response = self.client.post("/appointments/shift/1?amount_end=3")
self.assertEqual(response.status_code, 200)
self.assertEqual(appointments[0]["id"], 1)
self.assertEqual(appointments[0]["start"], datetime(2024, 12, 31, 10, 0))
self.assertEqual(appointments[0]["end"], datetime(2025, 1, 4, 11, 0))
def test_start_and_end_shift(self):
appointments.append({
"id": 1,
"title": "Test Meeting",
"start": datetime(2024, 12, 31, 10, 0),
"end": datetime(2025, 1, 1, 11, 0),
"category": "work"
})
response = self.client.post("/appointments/shift/1?amount_start=2.5&amount_end=3")
self.assertEqual(response.status_code, 200)
self.assertEqual(appointments[0]["id"], 1)
self.assertEqual(appointments[0]["start"], datetime(2025, 1, 2, 22, 0))
self.assertEqual(appointments[0]["end"], datetime(2025, 1, 4, 11, 0))
def test_end_before_start(self):
appointments.append({
"id": 1,
"title": "Test Meeting",
"start": datetime(2023, 1, 1, 10, 0),
"end": datetime(2023, 1, 1, 11, 0),
"category": "work"
})
response = self.client.post("/appointments/shift/1?amount_start=5&amount_end=1")
self.assertEqual(response.status_code, 400)
self.assertIn("error", response.json)
self.assertEqual(response.json["error"], "Shift would result in start after end")
def test_shift_overlapping_appointment(self):
appointments.append({
"id": 1,
"title": "Meeting 1",
"start": datetime(2025, 9, 26, 10, 0),
"end": datetime(2025, 9, 26, 12, 0),
"category": "work"
})
appointments.append({
"id": 2,
"title": "Meeting 2",
"start": datetime(2025, 9, 26, 13, 0),
"end": datetime(2025, 9, 26, 15, 0),
"category": "work"
})
response = self.client.post("/appointments/shift/2?amount_start=-3&amount_end=0")
self.assertEqual(response.status_code, 409)
self.assertEqual(response.json["error"], "Shift would cause overlapping appointment")