-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_app.py
More file actions
209 lines (158 loc) · 7.78 KB
/
Copy pathtest_app.py
File metadata and controls
209 lines (158 loc) · 7.78 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
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
209
"""Unit tests for the FastAPI application"""
import pytest
from fastapi.testclient import TestClient
from pydantic import ValidationError
from app import enqueue_query, queue
from app import UserInput, app
QUERY_VALIDATION_ERROR: str = "Query must contain words"
SUCCESS_RESPONSE_MESSAGE: str = "User query enqueued successfully!"
@pytest.fixture(scope="module")
def client():
"""Create a test client for synchronous requests"""
return TestClient(app)
class TestUserInputModel:
"""Test cases for UserInput Pydantic model"""
def test_valid_user_input(self):
"""Test creating UserInput with valid data"""
user_input = UserInput(query="Hello world", user_id="user123")
assert user_input.query == "Hello world"
assert user_input.user_id == "user123"
def test_missing_query(self):
"""Test UserInput with missing query field"""
with pytest.raises(ValidationError) as exc_info:
UserInput(user_id="user123")
assert "query" in str(exc_info.value)
def test_missing_user_id(self):
"""Test UserInput with missing user_id field"""
with pytest.raises(ValidationError) as exc_info:
UserInput(query="Hello world")
assert "user_id" in str(exc_info.value)
def test_empty_query(self):
"""Test UserInput with empty query"""
# Empty query should fail validation since it has no letters
with pytest.raises(ValidationError) as exc_info:
UserInput(query="", user_id="user123")
assert QUERY_VALIDATION_ERROR in str(exc_info.value)
def test_query_with_only_numbers(self):
"""Test UserInput with query containing only numbers"""
# Query with only numbers should fail validation
with pytest.raises(ValidationError) as exc_info:
UserInput(query="12345", user_id="user123")
assert QUERY_VALIDATION_ERROR in str(exc_info.value)
def test_query_with_only_symbols(self):
"""Test UserInput with query containing only symbols"""
# Query with only symbols should fail validation
with pytest.raises(ValidationError) as exc_info:
UserInput(query="!@#$%", user_id="user123")
assert QUERY_VALIDATION_ERROR in str(exc_info.value)
def test_query_with_only_whitespace(self):
"""Test UserInput with query containing only whitespace"""
# Query with only whitespace should fail validation
with pytest.raises(ValidationError) as exc_info:
UserInput(query=" \t\n", user_id="user123")
assert QUERY_VALIDATION_ERROR in str(exc_info.value)
def test_query_with_letters_and_numbers(self):
"""Test UserInput with query containing letters and numbers"""
# Query with letters and numbers should pass validation
user_input = UserInput(query="Hello123", user_id="user123")
assert user_input.query == "Hello123"
assert user_input.user_id == "user123"
def test_query_with_letters_and_symbols(self):
"""Test UserInput with query containing letters and symbols"""
# Query with letters and symbols should pass validation
user_input = UserInput(query="Hello!@#", user_id="user123")
assert user_input.query == "Hello!@#"
assert user_input.user_id == "user123"
def test_empty_user_id(self):
"""Test UserInput with empty user_id"""
user_input = UserInput(query="Hello world", user_id="")
assert user_input.query == "Hello world"
assert user_input.user_id == ""
def test_long_query(self):
"""Test UserInput with very long query"""
long_query = "A" * 10000
user_input = UserInput(query=long_query, user_id="user123")
assert user_input.query == long_query
assert len(user_input.query) == 10000
class TestEnqueueQuery:
"""Test cases for enqueue_query function"""
@pytest.mark.asyncio
async def test_enqueue_query_normal(self):
"""Test enqueuing normal user input"""
user_input = UserInput(query="Hello world", user_id="user123")
await enqueue_query(user_input)
# Check that the item was added to the queue
assert not queue.empty()
# Clean up the queue
await queue.get()
queue.task_done()
@pytest.mark.asyncio
async def test_enqueue_query_empty_query(self):
"""Test enqueuing user input with empty query - should fail validation"""
with pytest.raises(ValidationError) as exc_info:
UserInput(query="", user_id="user123")
assert QUERY_VALIDATION_ERROR in str(exc_info.value)
@pytest.mark.asyncio
async def test_enqueue_query_long_query(self):
"""Test enqueuing user input with very long query"""
long_query = "A" * 10000
user_input = UserInput(query=long_query, user_id="user123")
await enqueue_query(user_input)
# Check that the item was added to the queue
assert not queue.empty()
# Clean up the queue
await queue.get()
queue.task_done()
class TestQueryEndpoint:
"""Test cases for /query endpoint"""
def test_query_endpoint_success(self, client: TestClient):
"""Test successful POST request to /query endpoint"""
response = client.post("/query", json={"query": "Hello world", "user_id": "user123"})
assert response.status_code == 200
assert response.json() == {"message": SUCCESS_RESPONSE_MESSAGE}
def test_query_endpoint_missing_query(self, client: TestClient):
"""Test POST request with missing query field"""
response = client.post("/query", json={"user_id": "user123"})
assert response.status_code == 422 # Unprocessable Entity
error_detail = response.json()
assert "detail" in error_detail
def test_query_endpoint_missing_user_id(self, client: TestClient):
"""Test POST request with missing user_id field"""
response = client.post("/query", json={"query": "Hello world"})
assert response.status_code == 422 # Unprocessable Entity
error_detail = response.json()
assert "detail" in error_detail
def test_query_endpoint_empty_body(self, client: TestClient):
"""Test POST request with empty body"""
response = client.post("/query", json={})
assert response.status_code == 422 # Unprocessable Entity
def test_query_endpoint_with_client(self, client: TestClient):
"""Test POST request to /query endpoint using test client"""
response = client.post("/query", json={"query": "Hello world", "user_id": "user123"})
assert response.status_code == 200
assert response.json() == {"message": SUCCESS_RESPONSE_MESSAGE}
class TestErrorHandling:
"""Test cases for error handling"""
def test_internal_server_error(self, client: TestClient):
"""Test HTTP 500 error handling"""
# For now, just test that the endpoint handles errors properly
# We can't easily mock the background task without pytest-mock
response = client.post("/query", json={"query": "Hello world", "user_id": "user123"})
# This should succeed normally
assert response.status_code == 200
class TestIntegration:
"""Integration tests for the complete flow"""
@pytest.mark.asyncio
async def test_full_flow(self):
"""Test the complete request processing flow"""
from app import enqueue_query, queue
user_input = UserInput(query="Test integration", user_id="user123")
# Test that the input is valid
assert user_input.query == "Test integration"
assert user_input.user_id == "user123"
# Test that enqueuing works
await enqueue_query(user_input)
assert not queue.empty()
# Clean up the queue
await queue.get()
queue.task_done()