-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_http.py
More file actions
76 lines (57 loc) · 2.81 KB
/
test_http.py
File metadata and controls
76 lines (57 loc) · 2.81 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
from fastapi.testclient import TestClient
from main import app, DATA_FILE
def test_read_root():
"""Tests the root endpoint."""
with TestClient(app) as client:
response = client.get("/")
assert response.status_code == 200
assert response.json() == {"message": "Server is running"}
def test_read_status():
"""Tests the /status endpoint."""
with TestClient(app) as client:
response = client.get("/status")
assert response.status_code == 200
assert response.json() == {"status": "ok"}
def test_read_file():
"""Tests the static file serving endpoint."""
with TestClient(app) as client:
response = client.get("/static/data.txt")
assert response.status_code == 200
# The file content is read from the original source for the assertion
assert response.text == DATA_FILE.read_text()
def test_get_item_found():
"""Tests the successful case for finding an item."""
with TestClient(app) as client:
response = client.get("/items/1")
assert response.status_code == 200
assert response.json() == {"item_id": 1, "name": "The One Item"}
def test_get_item_not_found():
"""Tests the error case for not finding an item."""
with TestClient(app) as client:
response = client.get("/items/999")
assert response.status_code == 404
assert response.json() == {"detail": "Item with ID 999 not found."}
def test_create_item_success():
"""Tests the successful creation of an item via POST."""
with TestClient(app) as client:
item_payload = {"name": "Test Item", "is_offer": True}
response = client.post("/items", json=item_payload)
assert response.status_code == 201 # 201 Created
response_data = response.json()
assert response_data["message"] == "Item created successfully"
assert response_data["item_data"]["name"] == item_payload["name"]
assert response_data["item_data"]["is_offer"] == item_payload["is_offer"]
def test_create_item_validation_error_missing_field():
"""Tests for a validation error when a required field is missing."""
with TestClient(app) as client:
# The 'name' field is required by the Pydantic model, so this is invalid.
item_payload = {"is_offer": False}
response = client.post("/items", json=item_payload)
assert response.status_code == 422 # 422 Unprocessable Entity
def test_create_item_validation_error_wrong_type():
"""Tests for a validation error when a field has the wrong data type."""
with TestClient(app) as client:
# The 'name' field should be a string, not an integer.
item_payload = {"name": 123, "is_offer": False}
response = client.post("/items", json=item_payload)
assert response.status_code == 422 # 422 Unprocessable Entity