File size: 1,589 Bytes
8ff45d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import unittest
from fastapi.testclient import TestClient
from app import app

class TestAnswerEndpoint(unittest.TestCase):
    def setUp(self):
        self.client = TestClient(app)

    def test_valid_question(self):
        response = self.client.post("/answer", json={"question": "What is an overactive bladder?"})
        self.assertEqual(response.status_code, 200)
        self.assertIn("answer", response.json())

    def test_empty_question(self):
        # Empty string fails Pydantic validation (constr(min_length=1))
        response = self.client.post("/answer", json={"question": ""})
        self.assertEqual(response.status_code, 422)  # Pydantic validation error
        self.assertIn("should have at least 1 character", response.json()["detail"][0]["msg"])

    def test_whitespace_question(self):
        # Whitespace-only string passes Pydantic but fails our custom validation
        response = self.client.post("/answer", json={"question": "   "})
        self.assertEqual(response.status_code, 500)  # Internal server error from our validation
        self.assertEqual(response.json()["detail"], "Error processing request")

    def test_missing_question_field(self):
        response = self.client.post("/answer", json={})
        self.assertEqual(response.status_code, 422)  # FastAPI validation error

    def test_invalid_json(self):
        response = self.client.post("/answer", data="invalid json")
        self.assertEqual(response.status_code, 422)  # FastAPI validation error

if __name__ == '__main__':
    unittest.main()