-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_all.py
More file actions
121 lines (111 loc) · 4.08 KB
/
Copy pathtest_all.py
File metadata and controls
121 lines (111 loc) · 4.08 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
# pyright: ignore [missing-import]
from fastapi.testclient import TestClient
from ex1.main import app as app1
from ex2.main import app as app2
from ex3.main import app as app3, students as students_ex3
from ex4.main import app as app4, books as books_ex4
client1 = TestClient(app1)
client2 = TestClient(app2)
client3 = TestClient(app3)
client4 = TestClient(app4)
# Test EX1
def test_ex1():
print("Testing EX1...")
res = client1.get("/students")
assert res.status_code == 200, f"Expected 200, got {res.status_code}"
# Verify it returns a standard JSON array directly, not a concatenated string
assert isinstance(res.json(), list), f"Expected list, got {type(res.json())}"
assert res.json() == ["An", "Binh", "Cuong"]
print("EX1 passed!")
# Test EX2
def test_ex2():
print("Testing EX2...")
# Call GET /students
res = client2.get("/students")
assert res.status_code == 200, f"Expected 200, got {res.status_code}"
assert isinstance(res.json(), list), f"Expected list, got {type(res.json())}"
assert len(res.json()) == 3, f"Expected 3 students, got {len(res.json())}"
assert res.json() == [
{"id": 1, "name": "An"},
{"id": 2, "name": "Binh"},
{"id": 3, "name": "Cuong"},
]
print("EX2 passed!")
# Test EX3
def test_ex3():
print("Testing EX3...")
# 1. Normal active student list retrieval
res = client3.get("/students/active")
assert res.status_code == 200, f"Expected 200, got {res.status_code}"
data = res.json()
assert data["message"] == "Danh sách sinh viên đang học"
assert len(data["data"]) == 2
assert data["data"] == [
{"id": 1, "name": "An", "status": "active"},
{"id": 3, "name": "Cuong", "status": "active"}
]
# 2. Trap check: Empty active list
# Temporarily modify students_ex3
original_students = students_ex3.copy()
try:
students_ex3[:] = [
{"id": 2, "name": "Binh", "status": "inactive"},
{"id": 4, "name": "Dung", "status": "pending"}
]
res_empty = client3.get("/students/active")
assert res_empty.status_code == 200
assert res_empty.json() == {
"message": "Không có sinh viên đang học",
"data": []
}
finally:
# Restore original students list
students_ex3[:] = original_students
print("EX3 passed!")
# Test EX4
def test_ex4():
print("Testing EX4...")
# 1. Normal low stock books list retrieval
res = client4.get("/books/low-stock")
assert res.status_code == 200, f"Expected 200, got {res.status_code}"
data = res.json()
assert data["message"] == "Danh sách sách sắp hết hàng"
# Ensure quantity <= 5 books are retrieved, and negative or missing quantity books are ignored
# Python Basic (12) -> no
# FastAPI Beginner (3) -> yes
# Clean Code (5) -> yes
# Database Design (0) -> yes
# Web API Design (20) -> no
# Java Basic (missing) -> ignore
# Spring Boot (-2) -> ignore
assert len(data["data"]) == 3
assert data["data"] == [
{"id": 2, "title": "FastAPI Beginner", "quantity": 3},
{"id": 3, "title": "Clean Code", "quantity": 5},
{"id": 4, "title": "Database Design", "quantity": 0}
]
# 2. Trap check: No low stock books
original_books = books_ex4.copy()
try:
books_ex4[:] = [
{"id": 1, "title": "Python Basic", "quantity": 12},
{"id": 5, "title": "Web API Design", "quantity": 20},
{"id": 6, "title": "Java Basic"}, # missing
{"id": 7, "title": "Spring Boot", "quantity": -2} # negative
]
res_empty = client4.get("/books/low-stock")
assert res_empty.status_code == 200
assert res_empty.json() == {
"message": "Không có sách nào sắp hết hàng",
"data": []
}
finally:
# Restore original books list
books_ex4[:] = original_books
print("EX4 passed!")
if __name__ == "__main__":
test_ex1()
test_ex2()
test_ex3()
test_ex4()
print("All tests passed successfully!")