3
3
4
4
5
5
def test_chain_response (test_client , mocker ):
6
+ """
7
+ Tests the chain response functionality of the server by simulating a POST
8
+ request to the `/stream_log` endpoint and verifying the streamed response.
9
+ Args:
10
+ test_client: A test client instance used to simulate HTTP requests.
11
+ mocker: A mocking library instance used to patch dependencies.
12
+ Mocks:
13
+ - `app.server.get_retriever`: Mocked to return `True`.
14
+ - `app.server.build_chain`: Mocked to return `True`.
15
+ - `app.server.process_query`: Mocked to return an iterator with values `["one", "two"]`.
16
+ Raises:
17
+ AssertionError: If any of the assertions fail.
18
+ """
6
19
7
20
payload = {"input" : "What is AI?" , "stream" : True }
8
21
22
+ mocker .patch ("app.server.get_retriever" , return_value = True )
23
+ mocker .patch ("app.server.build_chain" , return_value = True )
9
24
mocker .patch ("app.server.process_query" , return_value = iter (["one" , "two" ]))
10
25
11
26
response = test_client .post ("/stream_log" , json = payload )
@@ -22,6 +37,23 @@ def test_chain_response(test_client, mocker):
22
37
23
38
24
39
def test_success_upload_and_create_embedding (test_client , mocker ):
40
+ """
41
+ Tests the successful upload of a document and the creation of embeddings.
42
+ This test simulates the process of uploading a text file, validating the document,
43
+ saving it, and creating embeddings using a mocked FAISS vector database. It verifies
44
+ that the API endpoint responds with the correct status code and response JSON.
45
+ Args:
46
+ test_client: A test client instance used to simulate HTTP requests to the API.
47
+ mocker: A mocking library instance used to patch functions and simulate behavior.
48
+ Mocks:
49
+ - `app.server.validate_document`: Mocked to return `True`.
50
+ - `app.server.save_document`: Mocked to return the temporary file name and `None`.
51
+ - `app.server.create_faiss_vectordb`: Mocked to return `True`.
52
+ Assertions:
53
+ - The response status code is 200.
54
+ - The response JSON matches the expected success message and metadata.
55
+ """
56
+
25
57
with tempfile .NamedTemporaryFile (delete = False , suffix = ".txt" ) as tmp_file :
26
58
tmp_file .write (b"This is sample txt file." )
27
59
tmp_file .seek (0 )
@@ -44,6 +76,21 @@ def test_success_upload_and_create_embedding(test_client, mocker):
44
76
45
77
46
78
def test_success_get_documents (test_client , mocker ):
79
+ """
80
+ Test the successful retrieval of documents from the server.
81
+ This test verifies that the `/documents` endpoint returns a 200 status code
82
+ and the expected JSON response containing a list of documents.
83
+ Args:
84
+ test_client (TestClient): A test client instance for making HTTP requests.
85
+ mocker (MockerFixture): A mocker fixture for patching and mocking dependencies.
86
+ Mocks:
87
+ - `app.server.get_document_from_vectordb`: Mocked to return a list of documents.
88
+ Assertions:
89
+ - The response status code is 200.
90
+ - The response JSON contains a "status" key with the value "Success".
91
+ - The response JSON contains a "metadata" key with a "documents" list matching the mocked documents.
92
+ """
93
+
47
94
mock_documents = ["test1.txt" , "test2.pdf" ]
48
95
mocker .patch ('app.server.get_document_from_vectordb' , return_value = mock_documents )
49
96
@@ -57,6 +104,20 @@ def test_success_get_documents(test_client, mocker):
57
104
58
105
59
106
def test_delete_embedding_success (test_client , mocker ):
107
+ """
108
+ Test the successful deletion of an embedding from the vector database.
109
+ This test verifies that the `delete_embedding_from_vectordb` function is called
110
+ and the API endpoint for deleting a document responds with the expected status code.
111
+ Args:
112
+ test_client: A test client instance for simulating HTTP requests to the server.
113
+ mocker: A mocking library instance used to patch and mock dependencies.
114
+ Mocks:
115
+ - `app.server.delete_embedding_from_vectordb`: Mocked to return `True`.
116
+ Assertions:
117
+ - Ensures that the HTTP DELETE request to the "/documents" endpoint with
118
+ the specified parameters returns a status code of HTTPStatus.NO_CONTENT.
119
+ """
120
+
60
121
mocker .patch ('app.server.delete_embedding_from_vectordb' , return_value = True )
61
122
62
123
response = test_client .delete ("/documents" , params = {"document" : "test1.txt" })
@@ -65,6 +126,20 @@ def test_delete_embedding_success(test_client, mocker):
65
126
66
127
67
128
def test_delete_all_embedding_success (test_client , mocker ):
129
+ """
130
+ Test the successful deletion of all embeddings from the vector database.
131
+ This test verifies that the endpoint for deleting all documents functions
132
+ correctly by mocking the `delete_embedding_from_vectordb` function to
133
+ return `True` and asserting that the response status code is `HTTPStatus.NO_CONTENT`.
134
+ Args:
135
+ test_client: A test client instance used to simulate HTTP requests to the server.
136
+ mocker: A mocking utility used to patch the `delete_embedding_from_vectordb` function.
137
+ Mocks:
138
+ - `app.server.delete_embedding_from_vectordb`: Mocked to return `True`.
139
+ Assertions:
140
+ - The response status code is `HTTPStatus.NO_CONTENT` (204).
141
+ """
142
+
68
143
mocker .patch ('app.server.delete_embedding_from_vectordb' , return_value = True )
69
144
70
145
response = test_client .delete ("/documents" , params = {"delete_all" : True })
@@ -73,6 +148,17 @@ def test_delete_all_embedding_success(test_client, mocker):
73
148
74
149
75
150
def test_upload_unsupported_file (test_client ):
151
+ """
152
+ Tests the upload of an unsupported file format to the server.
153
+ This test verifies that the server returns a 400 status code and an appropriate
154
+ error message when a file with an unsupported format (e.g., .html) is uploaded.
155
+ Args:
156
+ test_client: A test client instance used to simulate HTTP requests to the server.
157
+ Raises:
158
+ AssertionError: If the response status code is not 400 or the error message
159
+ does not match the expected output.
160
+ """
161
+
76
162
with tempfile .NamedTemporaryFile (delete = True , suffix = ".html" ) as tmp_file :
77
163
tmp_file .write (b"This is sample html file." )
78
164
tmp_file .seek (0 )
@@ -86,6 +172,21 @@ def test_upload_unsupported_file(test_client):
86
172
87
173
88
174
def test_fail_get_documents (test_client , mocker ):
175
+ """
176
+ Test case for handling failure when retrieving documents from the vector database.
177
+ This test simulates an exception being raised during the retrieval of documents
178
+ from the vector database and verifies that the server responds with the appropriate
179
+ HTTP status code and error message.
180
+ Args:
181
+ test_client: A test client instance used to simulate HTTP requests to the server.
182
+ mocker: A mocking library instance used to patch and simulate behavior of dependencies.
183
+ Mocks:
184
+ - `app.server.get_document_from_vectordb`: Mocked to raise an exception with the message "Error getting documents."
185
+ Asserts:
186
+ - The HTTP response status code is 500 (Internal Server Error).
187
+ - The JSON response contains the expected error message.
188
+ """
189
+
89
190
mocker .patch ('app.server.get_document_from_vectordb' , side_effect = Exception ("Error getting documents." ))
90
191
91
192
response = test_client .get ("/documents" )
@@ -97,6 +198,21 @@ def test_fail_get_documents(test_client, mocker):
97
198
98
199
99
200
def test_delete_embedding_failure (test_client , mocker ):
201
+ """
202
+ Test case for handling failure during the deletion of embeddings from the vector database.
203
+ This test simulates a failure scenario where the `delete_embedding_from_vectordb` function
204
+ raises an exception. It verifies that the server responds with the appropriate HTTP status
205
+ code and error message.
206
+ Args:
207
+ test_client (TestClient): A test client instance for simulating HTTP requests to the server.
208
+ mocker (MockerFixture): A fixture for mocking dependencies and functions.
209
+ Mocks:
210
+ - `app.server.delete_embedding_from_vectordb`: Mocked to raise an exception with the message "Error deleting embeddings."
211
+ Asserts:
212
+ - The response status code is 500 (Internal Server Error).
213
+ - The response JSON contains the expected error detail message.
214
+ """
215
+
100
216
mocker .patch ('app.server.delete_embedding_from_vectordb' , side_effect = Exception ("Error deleting embeddings." ))
101
217
102
218
response = test_client .delete ("/documents" , params = {"document" : "test1.txt" })
0 commit comments