-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_api.py
117 lines (81 loc) · 3.86 KB
/
test_api.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
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
""" Maria's Web Service Unit Tests (Databreathe Backend Coding Challange)
The unit testing of the API app for Maria's Web Service
"""
from fastapi.testclient import TestClient
from api import app
import re
# This is the test client object that will test the app
client = TestClient(app)
def test_ping() -> None:
""" Test the ping endpoint """
response = client.get("/ping")
assert response.status_code == 200
assert response.json() == {
"response": "pong"
}
def test_get_customers_birthday() -> None:
""" Test get customers birthdays endpoint """
response = client.get("/customers/birthday")
# Check for a valid response code
assert response.status_code == 200
# Convert the response to JSON and store in a variable for more assertions
response_json = response.json()
# Check if the main data key is available
assert "customers" in response_json
# If there is any data available, check for all keys availability, data types and values
for customer in response_json["customers"]:
assert "customer_id" in customer
assert isinstance(customer["customer_id"], int)
assert customer["customer_id"] > 0
assert "customer_first_name" in customer
assert isinstance(customer["customer_first_name"], str)
assert len(customer["customer_first_name"]) > 0
def test_get_top_selling_products() -> None:
""" Test get top selling products endpoint """
# First do some tests with empty data
response = client.get("/products/top-selling-products/2018")
# Check for a valid response code
assert response.status_code == 200
# Convert the response to JSON and store in a variable for more assertions
response_json = response.json()
# Check if the main data key is available
assert "products" in response_json
assert len(response_json["products"]) == 0
# Then tets with a year that we know we have data for
response = client.get("/products/top-selling-products/2019")
# Check for a valid response code
assert response.status_code == 200
# Convert the response to JSON and store in a variable for more assertions
response_json = response.json()
# Check if the main data key is available
assert "products" in response_json
assert len(response_json["products"]) > 0
# If there is any data available, check for all keys availability, data types and values
for product in response_json["products"]:
assert "total_sales" in product
assert isinstance(product["total_sales"], int)
assert product["total_sales"] > 0
assert "product_name" in product
assert isinstance(product["product_name"], str)
assert len(product["product_name"]) > 0
def test_get_last_order_per_customer() -> None:
""" Test get last order per custoemr endpoint """
response = client.get("/customers/last-order-per-customer")
# Check for a valid response code
assert response.status_code == 200
# Convert the response to JSON and store in a variable for more assertions
response_json = response.json()
# Check if the main data key is available and if there are custoemrs available
assert "customers" in response_json
assert len(response_json["customers"]) > 0
# If there is any data available, check for all keys availability, data types and values
for customer in response_json["customers"]:
assert "customer_id" in customer
assert isinstance(customer["customer_id"], int)
assert customer["customer_id"] > 0
assert "customer_email" in customer
assert isinstance(customer["customer_email"], str)
assert len(customer["customer_email"]) > 0
assert "last_order_date" in customer
assert isinstance(customer["last_order_date"], str)
assert re.search("^[1,2][0-9]{3}-[0,1][1-9]-[0,1,2,3][0-9]$", customer["last_order_date"])