forked from TempeHS/TempeHS_Python-Flask_DevContainer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_chatbot.py
More file actions
60 lines (47 loc) · 2.2 KB
/
test_chatbot.py
File metadata and controls
60 lines (47 loc) · 2.2 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
# test_chatbot.py
"""Automated tests for the chatbot application."""
import pytest
# Import the Flask app and helper functions
from app import app, check_for_crisis
class TestCrisisDetection:
"""Tests for the crisis keyword detection feature."""
def test_crisis_keyword_detected(self):
"""TC-004: Crisis keywords should be detected."""
# Test various crisis phrases
assert check_for_crisis("I don't want to live anymore") == True
assert check_for_crisis("thinking about suicide") == True
assert check_for_crisis("want to die") == True
def test_normal_message_not_flagged(self):
"""Normal messages should NOT trigger crisis detection."""
assert check_for_crisis("Hello, how are you?") == False
assert check_for_crisis("What's the weather like?") == False
assert check_for_crisis("Tell me a joke") == False
def test_case_insensitive(self):
"""Crisis detection should work regardless of case."""
assert check_for_crisis("SUICIDE") == True
assert check_for_crisis("SuIcIdE") == True
class TestChatAPI:
"""Tests for the /chat API endpoint."""
@pytest.fixture
def client(self):
"""Create a test client for the Flask app."""
app.config["TESTING"] = True
with app.test_client() as client:
yield client
def test_empty_message_rejected(self, client):
"""TC-002: Empty messages should return an error."""
response = client.post("/chat", json={"message": ""})
data = response.get_json()
assert "Please enter a message" in data["response"]
def test_long_message_rejected(self, client):
"""TC-003: Messages over 500 chars should return an error."""
long_message = "a" * 501
response = client.post("/chat", json={"message": long_message})
data = response.get_json()
assert "too long" in data["response"].lower()
def test_normal_message_gets_response(self, client):
"""TC-001: Normal messages should get a bot response."""
response = client.post("/chat", json={"message": "Hello"})
data = response.get_json()
assert "response" in data
assert len(data["response"]) > 0