-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_api_auto.py
105 lines (74 loc) · 3.38 KB
/
test_api_auto.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
from fastapi.testclient import TestClient
from main import app
import time
client = TestClient(app=app)
def test_index():
response = client.get("/")
assert response.status_code == 200
assert response.json() == {"length": 4}
def test_get_question():
response = client.get("/get_question/1")
print(response.json())
assert response.status_code == 200
assert response.json() == {"question": "What is your name?"}
def test_question_contains_attachment():
response = client.get("/get_question/4")
assert response.status_code == 200
assert '4_input' in str(response.json())
assert '4_starter' in str(response.json())
def test_submit_answer():
response = client.post("/submit_answer", json={"id": "1", "answer": "Sanjin", "team_name": "Mount Waverley"})
assert response.status_code == 200
assert "Correct" in response.json()["message"]
def test_submit_second_answer():
response = client.post("/submit_answer", json={"id": "2", "answer": "38", "team_name": "Mount Waverley"})
assert response.status_code == 200
assert "Correct" in response.json()["message"]
def test_submit_answer_again():
response = client.post("/submit_answer", json={"id": "1", "answer": "Sanjin", "team_name": "Mount Waverley"})
assert response.status_code == 200
assert "Already solved" in response.json()["message"]
def test_submit_answer_wrong():
response = client.post("/submit_answer", json={"id": "1", "answer": "Bob", "team_name": "Mount Waverley"})
assert response.status_code == 200
assert response.json() == {"message": "Incorrect"}
def test_submit_answer_nonexisting():
response = client.post("/submit_answer", json={"id": "10000", "answer": "Bob", "team_name": "Mount Waverley"})
assert response.status_code == 200
assert response.json() == {"message": "Question not found"}
def test_download_input_file():
response = client.get("/download_input_file/4")
assert response.status_code == 200
assert "tiptxlhgxngznvwy" in response.text
def test_download_starter_code():
response = client.get("/download_starter_code/4")
assert response.status_code == 200
assert "for s in strings:" in response.text
def test_valid_login():
response = client.post("/login", json={"name": "Mount Waverley", "password": "abc"})
assert response.status_code == 200
assert "Login successful" in response.json()["message"]
def test_invalid_login():
response = client.post("/login", json={"name": "Mount Waverley", "password": "xxxxx"})
print(response.json())
assert response.status_code == 200
assert "Login failed" in response.json()["message"]
def test_get_teams_table():
response = client.get("/get_teams_table")
assert response.status_code == 200
assert "Mount Waverley" in response.json()["teams"][0][0]
assert "Box Hill" in response.json()["teams"][1][0]
assert "Melbourne High" in response.json()["teams"][2][0]
assert "Wantirna" in response.json()["teams"][3][0]
assert response.json()["teams"][0][2] == 20
assert response.json()["teams"][1][2] == 0
assert response.json()["teams"][2][2] == 0
assert response.json()["teams"][3][2] == 0
assert response.json()["teams"][0][3] == '[1, 2]'
#this always needs to be the last test
def reset_db_just_run_file():
time.sleep(1)
with open("reset_db.py") as f:
exec(f.read())
time.sleep(1)
reset_db_just_run_file()