Consolidateion of document upload API (#14106)

### What problem does this PR solve?

Consolidation WEB API & HTTP API for document upload

Before consolidation
Web API: POST /v1/document/upload
Http API - POST /api/v1/datasets/<dataset_id>/documents

After consolidation, Restful API -- POST
/api/v1/datasets/<dataset_id>/documents

### Type of change

- [x] Refactoring
This commit is contained in:
Jack
2026-04-15 11:27:43 +08:00
committed by GitHub
parent e1dede1366
commit bc5f78996b
13 changed files with 339 additions and 319 deletions

View File

@@ -341,31 +341,6 @@ class TestDocRoutesUnit:
module.Chunk(positions=[[1, 2, 3, 4]])
assert "length of 5" in str(exc_info.value)
def test_upload_validation_and_upload_error(self, monkeypatch):
module = _load_doc_module(monkeypatch)
class _FileObj:
def __init__(self, name):
self.filename = name
monkeypatch.setattr(module, "request", SimpleNamespace(form=_AwaitableValue({}), files=_AwaitableValue(_DummyFiles({"file": [_FileObj("")]}))))
res = _run(module.upload.__wrapped__("ds-1", "tenant-1"))
assert res["code"] == module.RetCode.ARGUMENT_ERROR
assert res["message"] == "No file selected!"
long_name = "a" * (module.FILE_NAME_LEN_LIMIT + 1)
monkeypatch.setattr(module, "request", SimpleNamespace(form=_AwaitableValue({}), files=_AwaitableValue(_DummyFiles({"file": [_FileObj(long_name)]}))))
res = _run(module.upload.__wrapped__("ds-1", "tenant-1"))
assert res["code"] == module.RetCode.ARGUMENT_ERROR
assert "bytes or less" in res["message"]
monkeypatch.setattr(module, "request", SimpleNamespace(form=_AwaitableValue({}), files=_AwaitableValue(_DummyFiles({"file": [_FileObj("ok.txt")]}))))
monkeypatch.setattr(module.KnowledgebaseService, "get_by_id", lambda _id: (True, SimpleNamespace()))
monkeypatch.setattr(module.FileService, "upload_document", lambda *_args, **_kwargs: (["upload failed"], []))
res = _run(module.upload.__wrapped__("ds-1", "tenant-1"))
assert res["code"] == module.RetCode.SERVER_ERROR
assert res["message"] == "upload failed"
def test_download_and_download_doc_errors(self, monkeypatch):
module = _load_doc_module(monkeypatch)
_patch_send_file(monkeypatch, module)

View File

@@ -31,11 +31,11 @@ class TestAuthorization:
@pytest.mark.parametrize(
"invalid_auth, expected_code, expected_message",
[
(None, 0, "`Authorization` can't be empty"),
(None, 401, "<Unauthorized '401: Unauthorized'>"),
(
RAGFlowHttpApiAuth(INVALID_API_TOKEN),
109,
"Authentication error: API key is invalid!",
401,
"<Unauthorized '401: Unauthorized'>",
),
],
)
@@ -139,8 +139,8 @@ class TestDocumentsUpload:
def test_invalid_dataset_id(self, HttpApiAuth, tmp_path):
fp = create_txt_file(tmp_path / "ragflow_test.txt")
res = upload_documents(HttpApiAuth, "invalid_dataset_id", [fp])
assert res["code"] == 100
assert res["message"] == """LookupError("Can\'t find the dataset with ID invalid_dataset_id!")"""
assert res["code"] == 102
assert res["message"] == "Can\'t find the dataset with ID invalid_dataset_id!"
@pytest.mark.p2
def test_duplicate_files(self, HttpApiAuth, add_dataset_func, tmp_path):