11import sys
22import types
3- import asyncio
43from typing import Any , Dict , List , Optional , Tuple
4+ from http import HTTPStatus
55
66import pytest
7- from fastapi import FastAPI
7+ from fastapi import FastAPI , HTTPException
88from fastapi .testclient import TestClient
99from pydantic import BaseModel
1010
@@ -116,6 +116,11 @@ async def get_index_tasks(self, index_name: str):
116116 raise RuntimeError ("oops" )
117117 return [{"id" : "x" }]
118118
119+ async def get_task_details (self , task_id : str ):
120+ if task_id == "missing" :
121+ return None
122+ return {"id" : task_id , "ok" : True }
123+
119124 async def filter_important_image (self , image_url : str , positive_prompt : str , negative_prompt : str ):
120125 if image_url == "err" :
121126 raise RuntimeError ("bad" )
@@ -212,7 +217,6 @@ def test_process_sync_endpoint_success():
212217def test_process_sync_endpoint_error (monkeypatch ):
213218 # Reconfigure tasks stub to raise when getting result
214219 from backend .apps import data_process_app as app_module
215- tasks_mod = sys .modules ["data_process.tasks" ]
216220
217221 class _ErrResult (_DummyResult ):
218222 def get (self , timeout = None ):
@@ -222,7 +226,7 @@ class _PSyncErr:
222226 def apply_async (self , ** kwargs ):
223227 return _ErrResult ("tid" )
224228
225- setattr (tasks_mod , "process_sync" , _PSyncErr ())
229+ monkeypatch . setattr (app_module , "process_sync" , _PSyncErr (), raising = True )
226230
227231 app = _build_app ()
228232 client = TestClient (app )
@@ -233,6 +237,25 @@ def apply_async(self, **kwargs):
233237 assert resp .status_code == 500
234238
235239
240+ def test_process_sync_endpoint_http_exception (monkeypatch ):
241+ from backend .apps import data_process_app as app_module
242+
243+ class _PSyncHTTP :
244+ def apply_async (self , ** kwargs ):
245+ raise HTTPException (
246+ status_code = HTTPStatus .BAD_REQUEST , detail = "bad req" )
247+
248+ monkeypatch .setattr (app_module , "process_sync" , _PSyncHTTP (), raising = True )
249+
250+ app = _build_app ()
251+ client = TestClient (app )
252+ resp = client .post (
253+ "/tasks/process" ,
254+ data = {"source" : "/tmp/a.txt" , "source_type" : "local" },
255+ )
256+ assert resp .status_code == HTTPStatus .BAD_REQUEST
257+
258+
236259def test_batch_tasks_success ():
237260 app = _build_app ()
238261 client = TestClient (app )
@@ -249,20 +272,37 @@ def test_batch_tasks_success():
249272
250273def test_batch_tasks_error (monkeypatch ):
251274 # Make service raise
252- svc_mod = sys .modules ["services.data_process_service" ]
253- service : _ServiceStub = svc_mod .get_data_process_service ()
275+ from backend .apps import data_process_app as app_module
254276
255277 async def err (* args , ** kwargs ):
256278 raise RuntimeError ("x" )
257279
258- service .create_batch_tasks_impl = err # type: ignore
280+ monkeypatch .setattr (app_module .service ,
281+ "create_batch_tasks_impl" , err , raising = True )
259282
260283 app = _build_app ()
261284 client = TestClient (app )
262285 resp = client .post ("/tasks/batch" , json = {"sources" : []}, headers = {"Authorization" : "Bearer t" })
263286 assert resp .status_code == 500
264287
265288
289+ def test_batch_tasks_http_exception (monkeypatch ):
290+ from backend .apps import data_process_app as app_module
291+
292+ async def err_http (* args , ** kwargs ):
293+ raise HTTPException (
294+ status_code = HTTPStatus .NOT_ACCEPTABLE , detail = "bad batch" )
295+
296+ monkeypatch .setattr (app_module .service ,
297+ "create_batch_tasks_impl" , err_http , raising = True )
298+
299+ app = _build_app ()
300+ client = TestClient (app )
301+ resp = client .post (
302+ "/tasks/batch" , json = {"sources" : []}, headers = {"Authorization" : "Bearer t" })
303+ assert resp .status_code == HTTPStatus .NOT_ACCEPTABLE
304+
305+
266306def test_load_image_success_and_not_found ():
267307 app = _build_app ()
268308 client = TestClient (app )
@@ -274,19 +314,37 @@ def test_load_image_success_and_not_found():
274314
275315
276316def test_load_image_internal_error (monkeypatch ):
277- svc_mod = sys .modules ["services.data_process_service" ]
278- service : _ServiceStub = svc_mod .get_data_process_service ()
317+ from backend .apps import data_process_app as app_module
279318
280319 async def err (url : str ):
281320 raise RuntimeError ("bad" )
282321
283- service . load_image = err # type: ignore
322+ monkeypatch . setattr ( app_module . service , " load_image" , err , raising = True )
284323 app = _build_app ()
285324 client = TestClient (app )
286325 resp = client .get ("/tasks/load_image" , params = {"url" : "x" })
287326 assert resp .status_code == 500
288327
289328
329+ def test_filter_important_image_http_exception (monkeypatch ):
330+ from backend .apps import data_process_app as app_module
331+
332+ async def err_http (* args , ** kwargs ):
333+ raise HTTPException (
334+ status_code = HTTPStatus .BAD_REQUEST , detail = "bad image" )
335+
336+ monkeypatch .setattr (app_module .service ,
337+ "filter_important_image" , err_http , raising = True )
338+
339+ app = _build_app ()
340+ client = TestClient (app )
341+ resp = client .post (
342+ "/tasks/filter_important_image" ,
343+ data = {"image_url" : "u" },
344+ )
345+ assert resp .status_code == HTTPStatus .BAD_REQUEST
346+
347+
290348def test_list_tasks ():
291349 app = _build_app ()
292350 client = TestClient (app )
@@ -342,19 +400,54 @@ def test_process_text_file_success_and_error(tmp_path):
342400 assert bad .status_code == 500
343401
344402
403+ def test_process_text_file_http_exception (monkeypatch ):
404+ from backend .apps import data_process_app as app_module
405+
406+ async def err_http (* args , ** kwargs ):
407+ raise HTTPException (
408+ status_code = HTTPStatus .UNPROCESSABLE_ENTITY , detail = "bad file" )
409+
410+ monkeypatch .setattr (app_module .service ,
411+ "process_uploaded_text_file" , err_http , raising = True )
412+
413+ app = _build_app ()
414+ client = TestClient (app )
415+ files = {"file" : ("x.txt" , b"hello" , "text/plain" )}
416+ resp = client .post ("/tasks/process_text_file" , files = files ,
417+ data = {"chunking_strategy" : "basic" })
418+ assert resp .status_code == HTTPStatus .UNPROCESSABLE_ENTITY
419+
420+
345421def test_convert_state_success_and_error (monkeypatch ):
346422 app = _build_app ()
347423 client = TestClient (app )
348424 ok = client .post ("/tasks/convert_state" , json = {"process_state" : "SUCCESS" , "forward_state" : "SUCCESS" })
349425 assert ok .status_code == 200 and ok .json ()["state" ] == "COMPLETED"
350426
351427 # Make service raise
352- svc_mod = sys .modules ["services.data_process_service" ]
353- service : _ServiceStub = svc_mod .get_data_process_service ()
428+ from backend .apps import data_process_app as app_module
354429 def raise_convert (* args , ** kwargs ):
355430 raise RuntimeError ("x" )
356- service .convert_celery_states_to_custom = raise_convert # type: ignore
431+ monkeypatch .setattr (
432+ app_module .service , "convert_celery_states_to_custom" , raise_convert , raising = True )
357433 err = client .post ("/tasks/convert_state" , json = {"process_state" : "PENDING" , "forward_state" : "" })
358434 assert err .status_code == 500
359435
360436
437+ def test_convert_state_http_exception (monkeypatch ):
438+ app = _build_app ()
439+ client = TestClient (app )
440+
441+ from backend .apps import data_process_app as app_module
442+
443+ def raise_convert_http (* args , ** kwargs ):
444+ raise HTTPException (
445+ status_code = HTTPStatus .NOT_ACCEPTABLE , detail = "bad convert" )
446+
447+ monkeypatch .setattr (
448+ app_module .service , "convert_celery_states_to_custom" , raise_convert_http , raising = True
449+ )
450+
451+ resp = client .post ("/tasks/convert_state" ,
452+ json = {"process_state" : "PENDING" , "forward_state" : "" })
453+ assert resp .status_code == HTTPStatus .NOT_ACCEPTABLE
0 commit comments