-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_main.py
174 lines (140 loc) · 5.1 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
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
import random
import string
from typing import Generator
import app
from fastapi.testclient import TestClient
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from model.base import Base
from utils.dependencies import get_db
SQLALCHEMY_DATABASE_URL = 'postgresql://postgres:postgres@localhost:5432/test_food'
engine = create_engine(
SQLALCHEMY_DATABASE_URL
)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base.metadata.create_all(bind=engine)
def random_lower_string() -> str:
return "".join(random.choices(string.ascii_lowercase, k=32))
username = random_lower_string()
def override_get_db() -> Generator:
db = SessionLocal()
try:
yield db
finally:
db.close()
app.app.dependency_overrides[get_db] = override_get_db
client = TestClient(app.app)
def test_register_user():
response = client.post('/register', json={"username": username, "password": "baston"})
assert response.status_code == 200
assert response.json()["token_type"] == "bearer"
def test_register_user_not_enough_fields():
response = client.post('/register', json={"password": "baston"})
assert response.status_code == 422
def test_predict_food():
filename = "example_food.jpg"
response = client.post(
"/uploadfile/", files={"file": ("filename", open(filename, "rb"), "image/jpeg")})
resp = response.json()
assert response.status_code == 200
assert resp["foodClass"] == 'Apple Pie'
def test_predict_non_food():
filename = "example_non_food.jpg"
response = client.post(
"/uploadfile/", files={"file": ("filename", open(filename, "rb"), "image/jpeg")})
assert response.status_code == 400
def test_register_already_existing_user():
response = client.post('/register', json={"username": username, "password": "baston"})
assert response.status_code == 401
def test_login_existing_user():
response = client.post('/login', json={"username": username, "password": "baston"})
assert response.status_code == 200
def test_login_existing_user_wrong_password():
response = client.post('/login', json={"username": username, "password": "error"})
assert response.status_code == 401
def test_login_unexisting_user():
response = client.post('/login', json={"username": "no_user", "password": "baston"})
assert response.status_code == 401
def test_login_insufficient_fields():
response = client.post('/login', json={"password": "baston"})
assert response.status_code == 422
def test_add_meal():
response = client.post('/login', json={"username": username, "password": "baston"})
token = response.json()['access_token']
response = client.post('/meal',
headers={"Authorization": f"Bearer {token}"},
json={
"id": -1,
"date": "2022-03-26T00:00:00",
"meal_type": "Breakfast",
"name": "Test food",
"kcal": 576,
"protein": 12.3,
"carbs": 12,
"fat": 14.2,
"user": {
"username": "",
"password": ""
},
"quantity": 1
},
)
assert response.status_code == 200
def test_add_meal_wrong_token():
response = client.post('/meal',
headers={"Authorization": f"Bearer "},
json={
"id": -1,
"date": "2022-03-26T00:00:00",
"meal_type": "Breakfast",
"name": "Test food",
"kcal": 576,
"protein": 12.3,
"carbs": 12,
"fat": 14.2,
"user": {
"username": "",
"password": ""
}
},
)
assert response.status_code == 401
def test_add_meal_wrong_fields():
response = client.post('/login', json={"username": username, "password": "baston"})
token = response.json()['access_token']
response = client.post('/meal',
headers={"Authorization": f"Bearer {token}"},
json={
"id": -1,
"date": "2022-03-26T00:00:00",
"meal_type": "Breakfast",
"name": "Test food",
"nutritional_values":{
"kcal": 576,
"protein": 12.3,
"carbs": 12,
"fats": 14.2
},
},
)
assert response.status_code == 422
def test_get_meal_date():
response = client.post('/login', json={"username": username, "password": "baston"})
token = response.json()['access_token']
response = client.get('/meal?datetime=2022-03-26T00:00:00',
headers={"Authorization": f"Bearer {token}"},
)
assert response.status_code == 200
assert len(response.json()) == 1
def test_get_meal_date_no_answer():
response = client.post('/login', json={"username": username, "password": "baston"})
token = response.json()['access_token']
response = client.get('/meal?datetime=2021-03-26T00:00:00',
headers={"Authorization": f"Bearer {token}"},
)
assert response.status_code == 200
assert len(response.json()) == 0
def test_get_meal_date_no_token():
response = client.get('/meal?datetime=2021-03-26T00:00:00',
)
assert response.status_code == 401