From 9d78d3ddb1286733869da5bfcc6e6e1e2ee890e7 Mon Sep 17 00:00:00 2001 From: Idriss Sbaaoui <112825897+6ba3i@users.noreply.github.com> Date: Mon, 2 Mar 2026 10:44:33 +0800 Subject: [PATCH] Tests: fix failling http in CI (#13301) ### What problem does this PR solve? test_doc_sdk_routes_unit had two flaky/incorrect branch assumptions: 1. parse/stop_parsing production logic gates on doc.run, but tests used progress, causing branch mismatch and unintended fallthrough into mutation/DB paths. 2. stop_parsing invalid-state test asserted an outdated message fragment, making the contract brittle. ### Type of change - [x] Bug Fix (non-breaking change which fixes an issue) --- api/apps/sdk/doc.py | 10 ++++++- .../test_doc_sdk_routes_unit.py | 27 +++++++++++++++---- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/api/apps/sdk/doc.py b/api/apps/sdk/doc.py index 3f0295e569..513a01c4c1 100644 --- a/api/apps/sdk/doc.py +++ b/api/apps/sdk/doc.py @@ -809,6 +809,10 @@ async def delete(tenant_id, dataset_id): return get_result() +DOC_STOP_PARSING_INVALID_STATE_MESSAGE = "Can't stop parsing document that has not started or already completed" +DOC_STOP_PARSING_INVALID_STATE_ERROR_CODE = "DOC_STOP_PARSING_INVALID_STATE" + + @manager.route("/datasets//chunks", methods=["POST"]) # noqa: F821 @token_required async def parse(tenant_id, dataset_id): @@ -947,7 +951,11 @@ async def stop_parsing(tenant_id, dataset_id): if not doc: return get_error_data_result(message=f"You don't own the document {id}.") if doc[0].run != TaskStatus.RUNNING.value : - return get_error_data_result("Can't stop parsing document that has not started or already completed") + return construct_json_result( + code=RetCode.DATA_ERROR, + message=DOC_STOP_PARSING_INVALID_STATE_MESSAGE, + data={"error_code": DOC_STOP_PARSING_INVALID_STATE_ERROR_CODE}, + ) # Send cancellation signal via Redis to stop background task cancel_all_task_of(id) info = {"run": "2", "progress": 0, "chunk_num": 0} diff --git a/test/testcases/test_http_api/test_file_management_within_dataset/test_doc_sdk_routes_unit.py b/test/testcases/test_http_api/test_file_management_within_dataset/test_doc_sdk_routes_unit.py index b1fe88f931..635d09b555 100644 --- a/test/testcases/test_http_api/test_file_management_within_dataset/test_doc_sdk_routes_unit.py +++ b/test/testcases/test_http_api/test_file_management_within_dataset/test_doc_sdk_routes_unit.py @@ -592,7 +592,12 @@ class TestDocRoutesUnit: res = _run(module.parse.__wrapped__("tenant-1", "ds-1")) assert "don't own the document" in res["message"] - monkeypatch.setattr(module.DocumentService, "query", lambda **_kwargs: [_DummyDoc(progress=0.5)]) + monkeypatch.setattr(module.DocumentService, "query", lambda **_kwargs: [_DummyDoc(run=module.TaskStatus.RUNNING.value)]) + monkeypatch.setattr( + module.DocumentService, + "update_by_id", + lambda *_args, **_kwargs: (_ for _ in ()).throw(AssertionError("update_by_id must not be called for running docs")), + ) res = _run(module.parse.__wrapped__("tenant-1", "ds-1")) assert "currently being processed" in res["message"] @@ -631,11 +636,23 @@ class TestDocRoutesUnit: res = _run(module.stop_parsing.__wrapped__("tenant-1", "ds-1")) assert "don't own the document" in res["message"] - monkeypatch.setattr(module.DocumentService, "query", lambda **_kwargs: [_DummyDoc(progress=1)]) + monkeypatch.setattr(module.DocumentService, "query", lambda **_kwargs: [_DummyDoc(run=module.TaskStatus.DONE.value)]) + monkeypatch.setattr( + module, + "cancel_all_task_of", + lambda *_args, **_kwargs: (_ for _ in ()).throw(AssertionError("cancel_all_task_of must not be called for non-running docs")), + ) + monkeypatch.setattr( + module.DocumentService, + "update_by_id", + lambda *_args, **_kwargs: (_ for _ in ()).throw(AssertionError("update_by_id must not be called for non-running docs")), + ) res = _run(module.stop_parsing.__wrapped__("tenant-1", "ds-1")) - assert "progress at 0 or 1" in res["message"] + assert res["code"] == module.RetCode.DATA_ERROR + assert res["data"]["error_code"] == module.DOC_STOP_PARSING_INVALID_STATE_ERROR_CODE + assert res["message"] == module.DOC_STOP_PARSING_INVALID_STATE_MESSAGE - monkeypatch.setattr(module.DocumentService, "query", lambda **_kwargs: [_DummyDoc(progress=0.3)]) + monkeypatch.setattr(module.DocumentService, "query", lambda **_kwargs: [_DummyDoc(run=module.TaskStatus.RUNNING.value)]) monkeypatch.setattr(module, "cancel_all_task_of", lambda *_args, **_kwargs: None) monkeypatch.setattr(module.DocumentService, "update_by_id", lambda *_args, **_kwargs: True) _patch_docstore(monkeypatch, module, delete=lambda *_args, **_kwargs: None) @@ -651,7 +668,7 @@ class TestDocRoutesUnit: assert "Duplicate document ids" in res["message"] monkeypatch.setattr(module, "check_duplicate_ids", lambda ids, _kind: (ids, [])) - monkeypatch.setattr(module.DocumentService, "query", lambda **_kwargs: [_DummyDoc(progress=0.3)]) + monkeypatch.setattr(module.DocumentService, "query", lambda **_kwargs: [_DummyDoc(run=module.TaskStatus.RUNNING.value)]) res = _run(module.stop_parsing.__wrapped__("tenant-1", "ds-1")) assert res["code"] == 0