-
-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathtest_chatbot.py
More file actions
86 lines (55 loc) · 3.1 KB
/
test_chatbot.py
File metadata and controls
86 lines (55 loc) · 3.1 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
"""Unit Tests for FastAPI routes."""
from fastapi import FastAPI
from fastapi.testclient import TestClient
from api.routes.chatbot import router
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_start_chat_location_includes_router_prefix(mock_init_session):
"""Location header should include API prefix when router is mounted with one."""
mock_init_session.return_value = "test-session-id"
app = FastAPI()
app.include_router(router, prefix="/api/chatbot")
prefixed_client = TestClient(app)
response = prefixed_client.post("/api/chatbot/sessions")
assert response.status_code == 201
assert response.headers["location"] == "/api/chatbot/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", json=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", json=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", json=data)
errors = response.json()["detail"]
assert response.status_code == 422
assert "Message cannot be empty." in errors[0]["msg"]
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."}