-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestapp.py
208 lines (172 loc) · 6.91 KB
/
testapp.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
from datetime import datetime
from fastapi.testclient import TestClient
from server import app, Reaction
client = TestClient(app)
def test_get_session():
response = client.get("/get-session")
assert response.status_code == 200
assert "session_id" in response.json()
assert "link" in response.json()
def test_add_and_get_reaction():
# Create a session first
response = client.get("/get-session")
assert response.status_code == 200
session_id = response.json()['session_id']
# Add a reaction
reaction_data = {
"reaction": 1,
"timeStamp": "2022-10-03T14:15:22Z",
"sessionId": session_id,
"userSessionId": "us12345"
}
response = client.post("/add-reaction/", json=reaction_data)
assert response.status_code == 200
assert response.json() == "Reaction added successfully"
# Get reactions
response = client.get(f"/get-reaction/{session_id}")
print(response.json())
assert response.status_code == 200
assert len(response.json()) == 1
assert response.json()[0]['reaction'] == reaction_data['reaction']
def test_add_multiple_reactions():
# Create a session first
response = client.get("/get-session")
assert response.status_code == 200
session_id = response.json()['session_id']
# Add multiple reactions
reactions = [
{
"reaction": 1,
"timeStamp": "2023-10-03T14:15:22Z",
"sessionId": session_id,
"userSessionId": "user1"
},
{
"reaction": 2,
"timeStamp": "2023-10-03T14:16:22Z",
"sessionId": session_id,
"userSessionId": "user2"
}
]
for reaction in reactions:
response = client.post("/add-reaction/", json=reaction)
assert response.status_code == 200
assert response.json() == "Reaction added successfully"
# Get reactions
response = client.get(f"/get-reaction/{session_id}")
print(response.json())
assert response.status_code == 200
assert len(response.json()) == 2
assert response.json()[0]['reaction'] == reactions[0]['reaction']
assert response.json()[1]['reaction'] == reactions[1]['reaction']
def test_create_new_session_and_get_empty_reaction():
# Create a new session
response = client.get("/get-session")
assert response.status_code == 200
session_id = response.json()['session_id']
# Get reactions for the newly created session
response = client.get(f"/get-reaction/{session_id}")
assert response.status_code == 200
assert response.json() == []
def test_session_and_reactions():
# Create a new session
response = client.get("/get-session")
assert response.status_code == 200
session_id = response.json()['session_id']
# Test 1: Get reactions for the newly created session (expecting an empty array)
response = client.get(f"/get-reaction/{session_id}")
assert response.status_code == 200
assert response.json() == []
# Test 2: Add one reaction
reaction_data = {
"reaction": 1,
"timeStamp": "2022-10-03T14:15:22Z",
"sessionId": session_id,
"userSessionId": "us12345"
}
response = client.post("/add-reaction/", json=reaction_data)
assert response.status_code == 200
assert response.json() == "Reaction added successfully"
# Test 3: Get reactions for the session and ensure the added reaction is correct
response = client.get(f"/get-reaction/{session_id}")
assert response.status_code == 200
assert len(response.json()) == 1
assert response.json()[0]["reaction"] == reaction_data["reaction"]
# Test 4: Add two more reactions
reactions = [
{
"reaction": 1,
"timeStamp": "2023-10-03T14:15:22Z",
"sessionId": session_id,
"userSessionId": "user1"
},
{
"reaction": 2,
"timeStamp": "2023-10-03T14:16:22Z",
"sessionId": session_id,
"userSessionId": "user2"
}
]
for reaction in reactions:
response = client.post("/add-reaction/", json=reaction)
assert response.status_code == 200
assert response.json() == "Reaction added successfully"
# Test 5: Get reactions for the session and ensure the added reactions are correct
response = client.get(f"/get-reaction/{session_id}")
assert response.status_code == 200
assert len(response.json()) == 2
assert response.json()[0]["reaction"] == reactions[0]["reaction"]
assert response.json()[1]["reaction"] == reactions[1]["reaction"]
# Test 6: End the session by sending the session_id as part of the URL
response = client.get(f"/end-session/{session_id}")
# Assert the response status code and content
assert response.status_code == 200
print(response.json())
assert response.json() == "Session ended, added data to firebase"
# Test 7: Attempt to add a reaction to the ended session
reaction_data = {
"reaction": 1,
"timeStamp": "2022-10-03T14:15:22Z",
"sessionId": session_id,
"userSessionId": "us12345"
}
response = client.post("/add-reaction/", json=reaction_data)
# Assert that the response status code is 200 and that the message indicates that the session ended
assert response.status_code == 200
assert response.json() == "session ended"
def test_end_session_that_does_not_exist():
# Attempt to end a session that doesn't exist
non_existing_session_id = "non_existing_session"
response = client.get(f"/end-session/{non_existing_session_id}")
# Assert the response status code and content
print(response.json())
assert response.status_code == 404
assert response.json() == {
"detail": f"Session {non_existing_session_id} not found"}
def test_get_reaction_for_session_that_does_not_exist():
# Attempt to get reactions for a session that doesn't exist
non_existing_session_id = "non_existing_session"
response = client.get(f"/get-reaction/{non_existing_session_id}")
# Assert the response status code and content
assert response.status_code == 404
assert response.json() == {
"detail": f"Reactions not found for session {non_existing_session_id}"}
def test_add_reaction_to_ended_session():
# Create a session first
response = client.get("/get-session")
assert response.status_code == 200
session_id = response.json()['session_id']
# End the session
end_session_response = client.get(f"/end-session/{session_id}")
assert end_session_response.status_code == 200
# Attempt to add a reaction to the ended session
reaction_data = {
"reaction": 1,
"timeStamp": "2022-10-03T14:15:22Z",
"sessionId": session_id,
"userSessionId": "us12345"
}
response = client.post("/add-reaction/", json=reaction_data)
# Assert the response status code and content
assert response.status_code == 200
assert response.json() == "session ended"