-
-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathtest_chatbot.py
More file actions
65 lines (43 loc) · 2.5 KB
/
test_chatbot.py
File metadata and controls
65 lines (43 loc) · 2.5 KB
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
"""Unit Tests for FastAPI routes."""
def test_start_chat(client, mock_init_session):
"""Testing that creating a session returns session ID and location."""
mock_init_session.return_value = "test-session-id"
response = client.post("/sessions")
assert response.status_code == 201
assert response.json() == {"session_id": "test-session-id"}
assert response.headers["location"] == "/sessions/test-session-id/message"
def test_chatbot_reply_success(client, mock_session_exists, mock_get_chatbot_reply):
"""Testing that sending a valid message in a valid session returns a response."""
mock_session_exists.return_value = True
mock_get_chatbot_reply.return_value = {"reply": "This is a valid response"}
data = {"message": "This is a valid query"}
response = client.post("/sessions/test-session-id/message", data=data)
assert response.status_code == 200
assert response.json() == {"reply": "This is a valid response"}
def test_chatbot_reply_invalid_session(client, mock_session_exists):
"""Testing that sending a message to an invalid session returns 404."""
mock_session_exists.return_value = False
data = {"message": "This is a valid query"}
response = client.post("/sessions/invalid-session-id/message", data=data)
assert response.status_code == 404
assert response.json() == {"detail": "Session not found."}
def test_chatbot_reply_empty_message_returns_422(client, mock_session_exists):
"""Testing that if sending an empty message returns 422 validation error."""
mock_session_exists.return_value = True
data = {"message": " "}
response = client.post("/sessions/test-session-id/message", data=data)
detail = response.json()["detail"]
assert response.status_code == 422
assert detail == "Either a message or at least one file must be provided."
def test_delete_chat_success(client, mock_delete_session):
"""Testing that deleting an existing session returns confirmation."""
mock_delete_session.return_value = True
response = client.delete("/sessions/test-session-id")
assert response.status_code == 200
assert response.json() == {"message": "Session test-session-id deleted."}
def test_delete_chat_not_found(client, mock_delete_session):
"""Testing that deleting a session that does not exist returns 404."""
mock_delete_session.return_value = False
response = client.delete("/sessions/nonexistent-id")
assert response.status_code == 404
assert response.json() == {"detail": "Session not found."}