-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_main.py
36 lines (30 loc) · 1.11 KB
/
test_main.py
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
from fastapi.testclient import TestClient
from main import app
client = TestClient(app)
def test_read_main():
# Send a GET request to the root ("/") endpoint
response = client.get("/")
# Check if the response status code is 200 (OK)
# This ensures that the request was successful
assert response.status_code == 200
# Verify the response body
# It should be a JSON object with a "message" key
# The value of "message" should be exactly "Hello World"
assert response.json() == {"message": "Hello World"}
# Note: If any of these assertions fail, the test will fail,
# indicating that the API is not behaving as expected
def test_create_item():
item_data = {
"title": "Iphone 15",
"description": "128GB ROM, 8gb RAAM",
"at_sale": False,
"inventory": 4
}
response = client.post("/product", json=item_data)
data = response.json()
# Remove the "id" key from the data because it's auto-generated by the database
del data["id"]
if 'id' in data:
del data['id']
assert response.status_code == 200
assert data == item_data