@@ -48,7 +48,7 @@ async def test_upload_file_with_error_ext(monkeypatch):
4848 ("files" , ("test.exe" , BytesIO (b"test content" ), "text/plain" )), # Valid file
4949 ]
5050 response = await ac .post (
51- "/documents/upload_file ?token=test_token" ,
51+ "/documents?token=test_token" ,
5252 files = files , # Correct usage of file as a tuple
5353 )
5454 print ("Response:" , response .json ())
@@ -67,7 +67,7 @@ async def test_upload_file_with_error_mme(monkeypatch):
6767 ("files" , ("test.txt" , BytesIO (b"test content" ), "text/test" )), # Valid file
6868 ]
6969 response = await ac .post (
70- "/documents/upload_file ?token=test_token" ,
70+ "/documents?token=test_token" ,
7171 files = files , # Correct usage of file as a tuple
7272 )
7373 print ("Response:" , response .json ())
@@ -87,7 +87,7 @@ async def mock_add_document(_,__):
8787 ("files" , ("test.txt" , BytesIO (b"test content" ), "text/plain" )), # Valid file
8888 ]
8989 response = await ac .post (
90- "/documents/upload_file ?token=test_token" ,
90+ "/documents?token=test_token" ,
9191 files = files , # Correct usage of file as a tuple
9292 )
9393 print ("Response:" , response .json ())
@@ -108,7 +108,7 @@ async def mock_add_document(_,__):
108108 ]
109109
110110 response = await ac .post (
111- "/documents/upload_file ?token=test_token" ,
111+ "/documents?token=test_token" ,
112112 files = files , # Correct usage of file as a tuple
113113 )
114114 assert "EERROR" in response .json ()["detail" ], "EERROR should be in the error message"
@@ -127,7 +127,7 @@ async def mock_add_document(_,__):
127127 ]
128128
129129 response = await ac .post (
130- "/documents/upload_file ?token=test_token" ,
130+ "/documents?token=test_token" ,
131131 files = files , # Correct usage of file as a tuple
132132 )
133133 assert "EERROR" in response .json ()["detail" ], "EERROR should be in the error message"
@@ -146,7 +146,7 @@ async def mock_add_document(_,__):
146146 ("files" , ("test2.txt" , BytesIO (b"test content" ), "text/plain" )), # Valid file
147147 ]
148148 response = await ac .post (
149- "/documents/upload_file ?token=test_token" ,
149+ "/documents?token=test_token" ,
150150 files = files , # Correct usage of file as a tuple
151151 )
152152 print ("Response:" , response .json ())
@@ -167,7 +167,7 @@ async def mock_add_document(_,__):
167167 ("files" , ("test.exe" , BytesIO (b"test content" ), "text/plain" )), # Valid file
168168 ]
169169 response = await ac .post (
170- "/documents/upload_file ?token=test_token" ,
170+ "/documents?token=test_token" ,
171171 files = files , # Correct usage of file as a tuple
172172 )
173173 print ("Response:" , response .json ())
@@ -195,7 +195,7 @@ async def mock_delete_document(*args, **kwargs):
195195 }
196196
197197 async with AsyncClient (transport = transport , base_url = "http://test" ) as ac :
198- response = await ac .request ("DELETE" , "/documents/delete_file " , json = payload )
198+ response = await ac .request ("DELETE" , "/documents" , json = payload )
199199 assert response .status_code == 200
200200 assert response .json ()["message" ] == "File deleted successfully"
201201
@@ -211,7 +211,7 @@ async def test_delete_file_file_manager_not_found(monkeypatch):
211211 }
212212
213213 async with AsyncClient (transport = transport , base_url = "http://test" ) as ac :
214- response = await ac .request ("DELETE" , "/documents/delete_file " , json = payload )
214+ response = await ac .request ("DELETE" , "/documents" , json = payload )
215215 assert response .status_code == 400
216216 assert response .json ()["detail" ] == "File manager not found"
217217
@@ -234,7 +234,7 @@ async def mock_delete_document(*args, **kwargs):
234234 }
235235
236236 async with AsyncClient (transport = transport , base_url = "http://test" ) as ac :
237- response = await ac .request ("DELETE" , "/documents/delete_file " , json = payload )
237+ response = await ac .request ("DELETE" , "/documents" , json = payload )
238238 assert response .status_code == 404
239239 assert response .json ()["detail" ] == "Document not found"
240240
@@ -257,14 +257,14 @@ async def mock_delete_document(*args, **kwargs):
257257 }
258258
259259 async with AsyncClient (transport = transport , base_url = "http://test" ) as ac :
260- response = await ac .request ("DELETE" , "/documents/delete_file " , json = payload )
260+ response = await ac .request ("DELETE" , "/documents" , json = payload )
261261 assert response .status_code == 500
262262 assert response .json ()["detail" ] == "Error in deleting file"
263263
264264@pytest .mark .asyncio
265265async def test_delete_file_http_exception_default (monkeypatch ):
266266 async def mock_delete_document (* args , ** kwargs ):
267- raise HTTPException (status_code = 501 , detail = "Error in deleting file" )
267+ raise HTTPException (status_code = 500 , detail = "Error in deleting file" )
268268
269269 file_manager = MagicMock ()
270270 file_manager .get_full_path .return_value = "/data/documents/test.txt"
@@ -280,7 +280,7 @@ async def mock_delete_document(*args, **kwargs):
280280 }
281281
282282 async with AsyncClient (transport = transport , base_url = "http://test" ) as ac :
283- response = await ac .request ("DELETE" , "/documents/delete_file " , json = payload )
283+ response = await ac .request ("DELETE" , "/documents" , json = payload )
284284 assert response .status_code == 500
285285 assert response .json ()["detail" ] == "Error in deleting file"
286286
@@ -304,17 +304,30 @@ async def mock_delete_document(*args, **kwargs):
304304 }
305305
306306 async with AsyncClient (transport = transport , base_url = "http://test" ) as ac :
307- response = await ac .request ("DELETE" , "/documents/delete_file " , json = payload )
307+ response = await ac .request ("DELETE" , "/documents" , json = payload )
308308 assert response .status_code == 500
309309 assert "Error in deleting file" in response .json ()["detail" ]
310310
311311@pytest .mark .asyncio
312312async def test_get_documents (monkeypatch ):
313+ # Mock the file_manager and its methods that get_documents route will call
314+ mock_file_manager = MagicMock ()
315+ mock_file_manager .get_documents_number .return_value = 2
316+ mock_file_manager .get_documents .return_value = ["doc1.txt" , "doc2.pdf" ]
317+
318+ # Mock os.listdir as it's also called by the route
313319 monkeypatch .setattr (os , "listdir" , lambda _ : ["doc1.txt" , "doc2.pdf" ])
320+
321+ # Mock get_file_manager to return your mock_file_manager
322+ monkeypatch .setattr ("app.routes.documents.get_file_manager" , lambda : mock_file_manager )
323+
314324 async with AsyncClient (transport = transport , base_url = "http://test" ) as ac :
315- response = await ac .get ("/documents/get_documents " )
325+ response = await ac .get ("/documents" )
316326
317327 print ("Response:" , response .json ())
318328
319329 assert response .status_code == 200
320- assert response .json () == ["doc1.txt" , "doc2.pdf" ], "Should return the list of documents"
330+ # Adjust the expected response based on what your route actually returns:
331+ # The route returns a tuple: (number, list_of_docs_from_manager, list_of_docs_from_os_listdir)
332+ expected_response_data = [2 , ["doc1.txt" , "doc2.pdf" ], ["doc1.txt" , "doc2.pdf" ]]
333+ assert response .json () == expected_response_data , "Should return the document count and names"
0 commit comments