Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

test(sdk): audit document management tests (#21633)

Sbaaoui Idrissf467eb944ccd
diff --git a/test/testcases/test_sdk_api/test_file_management_within_dataset/README.md b/test/testcases/test_sdk_api/test_file_management_within_dataset/README.md
new file mode 100644
index 0000000000..93f53807a9
--- /dev/null
+++ b/test/testcases/test_sdk_api/test_file_management_within_dataset/README.md
@@ -0,0 +1,66 @@
+<!--
+
+ Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+
+-->
+
+# Document SDK tests
+
+This folder targets the current Python SDK and Go backend only. Its local pytest
+configuration prevents inherited legacy backend fixtures and marker filters from
+loading; it does not depend on the open dataset-audit PR or change other suites.
+Run it serially from the repository root, without selecting a parent pytest
+configuration or combining it with unaudited SDK folders.
+
+## Service-free contracts
+
+Install the SDK and its existing test dependencies into a Python 3.13 environment:
+
+```sh
+uv venv --python 3.13 /tmp/ragflow-sdk-document-tests
+uv pip install --python /tmp/ragflow-sdk-document-tests/bin/python -e sdk/python --group sdk/python/pyproject.toml:test
+/tmp/ragflow-sdk-document-tests/bin/python -m pytest test/testcases/test_sdk_api/test_file_management_within_dataset -m 'not integration'
+/tmp/ragflow-sdk-document-tests/bin/python -m pytest test/testcases/test_sdk_api/test_file_management_within_dataset --collect-only -q
+```
+
+Unmarked contract tests cannot open network connections. Fixtures, payload mapping,
+error propagation, download bytes and polling behavior are testable without Go,
+credentials, model setup or external storage.
+
+## Isolated Go integrations
+
+Provide a disposable Go deployment and a dedicated **empty** test tenant. The API,
+database, object store and configured search engine must work; parsing also needs
+the ingestor, queue and the tenant's configured embedding model/provider. Do not
+point these tests at a shared tenant or assume a running local service is isolated.
+
+```sh
+export RAGFLOW_SDK_ISOLATED=1
+export RAGFLOW_SDK_BASE_URL=http://isolated-go-host:9380
+export RAGFLOW_SDK_API_KEY=dedicated-test-tenant-key
+/tmp/ragflow-sdk-document-tests/bin/python -m pytest test/testcases/test_sdk_api/test_file_management_within_dataset -m integration
+```
+
+Missing prerequisites fail setup rather than being mislabeled as product skips.
+Before mutations, a read-only probe rejects a nonempty tenant. Each test creates
+fresh datasets, and teardown deletes only tracked dataset IDs, including their
+owned documents; no shared cleanup helper or tenant-wide deletion is used. HTTP
+calls have connect/read timeouts; parsing waits have explicit deadlines.
+
+`RAGFLOW_SDK_PARSE_TIMEOUT` optionally changes the parsing deadline (180 seconds by
+default). A timeout or unavailable model/service is an environment/test failure,
+not automatic evidence of a product defect. Targeted skips document independently
+confirmed implementation defects in the affected tests. Python SDK CI execution
+remains disabled; this PR does not change workflows or production code.
diff --git a/test/testcases/test_sdk_api/test_file_management_within_dataset/conftest.py b/test/testcases/test_sdk_api/test_file_management_within_dataset/conftest.py
index b60f5f2886..54950d439a 100644
--- a/test/testcases/test_sdk_api/test_file_management_within_dataset/conftest.py
+++ b/test/testcases/test_sdk_api/test_file_management_within_dataset/conftest.py
@@ -1,5 +1,5 @@
#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
+# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@@ -14,44 +14,161 @@
# limitations under the License.
#
+"""Document-only fixtures; no legacy backend imports or tenant-wide cleanup."""
+
+import os
+import re
+from uuid import uuid4
import pytest
-from common import bulk_upload_documents, delete_all_documents
-from pytest import FixtureRequest
-from ragflow_sdk import DataSet, Document
+import requests
+from ragflow_sdk import RAGFlow
+
+
[email protected](autouse=True)
+def network_boundary(request, monkeypatch):
+ original = requests.sessions.Session.request
+
+ def bounded_request(session, *args, **kwargs):
+ if request.node.get_closest_marker("integration") is None:
+ pytest.fail("Service-free document tests must not open a network connection")
+ kwargs.setdefault("timeout", (5, 60))
+ return original(session, *args, **kwargs)
+
+ monkeypatch.setattr(requests.sessions.Session, "request", bounded_request)
+
+
[email protected](scope="session")
+def integration_settings():
+ required = ("RAGFLOW_SDK_BASE_URL", "RAGFLOW_SDK_API_KEY")
+ if os.getenv("RAGFLOW_SDK_ISOLATED") != "1" or any(not os.getenv(key) for key in required):
+ pytest.fail(
+ "Integration environment required: set RAGFLOW_SDK_ISOLATED=1, "
+ "RAGFLOW_SDK_BASE_URL and RAGFLOW_SDK_API_KEY for a dedicated, empty Go test tenant. "
+ "Run service-free tests with -m 'not integration'."
+ )
+ settings = {
+ "base_url": os.environ["RAGFLOW_SDK_BASE_URL"],
+ "api_key": os.environ["RAGFLOW_SDK_API_KEY"],
+ "doc_engine": os.getenv("RAGFLOW_SDK_DOC_ENGINE"),
+ "parse_timeout": float(os.getenv("RAGFLOW_SDK_PARSE_TIMEOUT", "180")),
+ }
+ probe = RAGFlow(settings["api_key"], settings["base_url"])
+ # This read-only probe also has a timeout before any test may create resources.
+ original = requests.sessions.Session.request
+
+ def bounded_probe(session, *args, **kwargs):
+ kwargs.setdefault("timeout", (5, 60))
+ return original(session, *args, **kwargs)
+
+ with pytest.MonkeyPatch.context() as patch:
+ patch.setattr(requests.sessions.Session, "request", bounded_probe)
+ assert probe.list_datasets(page_size=1) == [], "Refusing integrations: test tenant is not empty"
+ return settings
+
+
+def client(integration_settings, monkeypatch):
+ rag = RAGFlow(integration_settings["api_key"], integration_settings["base_url"])
+ created = []
+ unresolved = set()
+ create = rag.create_dataset
+
+ def tracked_create(*args, **kwargs):
+ name = kwargs.get("name", args[0] if args else None)
+ # Recover only our UUID-named creations if persistence succeeds before the response fails.
+ if isinstance(name, str) and re.fullmatch(r"sdk-document-[0-9a-f]{32}", name):
+ unresolved.add(name)
+ dataset = create(*args, **kwargs)
+ created.append(dataset.id)
+ unresolved.discard(name)
+ return dataset
+
+ monkeypatch.setattr(rag, "create_dataset", tracked_create)
+ yield rag
+ for name in unresolved:
+ created.extend(dataset.id for dataset in rag.list_datasets(name=name, page_size=100) if dataset.name == name)
+ # A test can delete its own dataset early; never broaden cleanup to delete_all.
+ for offset in range(0, len(created), 100):
+ ids = created[offset : offset + 100]
+ remaining = rag.list_datasets(ids=ids, page_size=100)
+ if remaining:
+ rag.delete_datasets(ids=[dataset.id for dataset in remaining])
+ assert rag.list_datasets(ids=ids, page_size=100) == [], "Test dataset cleanup failed"
+
+
+def dataset_factory(client):
+ def create(name=None, **kwargs):
+ return client.create_dataset(name=name if name is not None else f"sdk-document-{uuid4().hex}", **kwargs)
+
+ return create
+
+
+def add_dataset_func(dataset_factory):
+ return dataset_factory()
+
+
+def add_dataset(add_dataset_func):
+ return add_dataset_func
+
+
+def document_factory():
+ def create(dataset, name=None, blob=b"RAGFlow document SDK integration test.\n"):
+ documents = dataset.upload_documents([{"display_name": name or f"sdk-{uuid4().hex}.txt", "blob": blob}])
+ assert len(documents) == 1
+ return documents[0]
+
+ return create
[email protected](scope="function")
-def add_document_func(request: FixtureRequest, add_dataset: DataSet, ragflow_tmp_dir) -> tuple[DataSet, Document]:
- dataset = add_dataset
- documents = bulk_upload_documents(dataset, 1, ragflow_tmp_dir)
+def upload_documents():
+ def upload(dataset, num):
+ payload = [{"display_name": f"ragflow_test_upload_{i}.txt", "blob": f"Document {i}: SDK integration test.\n".encode()} for i in range(num)]
+ documents = dataset.upload_documents(payload)
+ assert len(documents) == num
+ return documents
- def cleanup():
- delete_all_documents(dataset)
+ return upload
- request.addfinalizer(cleanup)
- return dataset, documents[0]
+def add_document_func(add_dataset_func, document_factory):
+ return add_dataset_func, document_factory(add_dataset_func, name="ragflow_test_upload_0.txt")
[email protected](scope="class")
-def add_documents(request: FixtureRequest, add_dataset: DataSet, ragflow_tmp_dir) -> tuple[DataSet, list[Document]]:
- dataset = add_dataset
- documents = bulk_upload_documents(dataset, 5, ragflow_tmp_dir)
- def cleanup():
- delete_all_documents(dataset)
+def add_documents_func(add_dataset_func, upload_documents):
+ return add_dataset_func, upload_documents(add_dataset_func, 3)
- request.addfinalizer(cleanup)
- return dataset, documents
+def add_documents(add_dataset_func, upload_documents):
+ return add_dataset_func, upload_documents(add_dataset_func, 5)
[email protected](scope="function")
-def add_documents_func(request: FixtureRequest, add_dataset_func: DataSet, ragflow_tmp_dir) -> tuple[DataSet, list[Document]]:
- dataset = add_dataset_func
- documents = bulk_upload_documents(dataset, 3, ragflow_tmp_dir)
- def cleanup():
- delete_all_documents(dataset)
+def generate_test_files(request, tmp_path):
+ # Import existing format generators only for live upload/download fixtures.
+ from utils import file_utils
- request.addfinalizer(cleanup)
- return dataset, documents
+ formats = {
+ "docx": ("docx", file_utils.create_docx_file),
+ "excel": ("xlsx", file_utils.create_excel_file),
+ "ppt": ("pptx", file_utils.create_ppt_file),
+ "image": ("png", file_utils.create_image_file),
+ "pdf": ("pdf", file_utils.create_pdf_file),
+ "txt": ("txt", file_utils.create_txt_file),
+ "md": ("md", file_utils.create_md_file),
+ "json": ("json", file_utils.create_json_file),
+ "eml": ("eml", file_utils.create_eml_file),
+ "html": ("html", file_utils.create_html_file),
+ }
+ extension, create = formats[request.param]
+ path = tmp_path / f"ragflow_test.{extension}"
+ create(path)
+ return {request.param: path}
diff --git a/test/testcases/test_sdk_api/test_file_management_within_dataset/pytest.ini b/test/testcases/test_sdk_api/test_file_management_within_dataset/pytest.ini
new file mode 100644
index 0000000000..0354b66c6d
--- /dev/null
+++ b/test/testcases/test_sdk_api/test_file_management_within_dataset/pytest.ini
@@ -0,0 +1,24 @@
+#
+# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+[pytest]
+pythonpath = ../../../../sdk/python ../..
+addopts = --strict-markers
+markers =
+ integration: requires an explicitly isolated Go backend and dedicated empty tenant
+ p1: high priority
+ p2: medium priority
+ p3: low priority
diff --git a/test/testcases/test_sdk_api/test_file_management_within_dataset/test_delete_documents.py b/test/testcases/test_sdk_api/test_file_management_within_dataset/test_delete_documents.py
index 8b059aa7d9..3e4ef84714 100644
--- a/test/testcases/test_sdk_api/test_file_management_within_dataset/test_delete_documents.py
+++ b/test/testcases/test_sdk_api/test_file_management_within_dataset/test_delete_documents.py
@@ -1,5 +1,5 @@
#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
+# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@@ -16,19 +16,19 @@
from concurrent.futures import ThreadPoolExecutor, as_completed
import pytest
-from common import bulk_upload_documents, list_all_documents
class TestDocumentsDeletion:
+ pytestmark = pytest.mark.integration
+
@pytest.mark.p3
@pytest.mark.parametrize(
"payload, expected_message, remaining",
[
- ({"ids": None}, "should either provide doc ids or set delete_all(true), dataset:", 3),
- ({"ids": []}, "should either provide doc ids or set delete_all(true), dataset:", 3),
+ ({"ids": None}, "should either provide doc ids or set delete_all(true)", 3),
+ ({"ids": []}, "should either provide doc ids or set delete_all(true)", 3),
({"ids": ["invalid_id"]}, "These documents do not belong to dataset", 3),
({"ids": ["\n!?。;!?\"'"]}, "These documents do not belong to dataset", 3),
- ("not json", "must be a mapping", 3),
(lambda r: {"ids": r[:1]}, "", 2),
(lambda r: {"ids": r}, "", 0),
],
@@ -41,6 +41,7 @@ class TestDocumentsDeletion:
remaining,
):
dataset, documents = add_documents_func
+ owned_ids = {document.id for document in documents}
if callable(payload):
payload = payload([document.id for document in documents])
@@ -51,8 +52,10 @@ class TestDocumentsDeletion:
else:
dataset.delete_documents(**payload)
- documents = dataset.list_documents()
- assert len(documents) == remaining, str(documents)
+ remaining_docs = dataset.list_documents()
+ assert len(remaining_docs) == remaining, str(remaining_docs)
+ expected_ids = owned_ids if expected_message else owned_ids - set(payload.get("ids") or [])
+ assert {document.id for document in remaining_docs} == expected_ids
@pytest.mark.p3
@pytest.mark.parametrize(
@@ -71,8 +74,8 @@ class TestDocumentsDeletion:
dataset.delete_documents(**payload)
assert "These documents do not belong to dataset" in str(exception_info.value), str(exception_info.value)
- documents = dataset.list_documents()
- assert len(documents) == 3, str(documents)
+ remaining = dataset.list_documents()
+ assert {document.id for document in remaining} == {document.id for document in documents}
@pytest.mark.p3
def test_repeated_deletion(self, add_documents_func):
@@ -82,6 +85,7 @@ class TestDocumentsDeletion:
with pytest.raises(Exception) as exception_info:
dataset.delete_documents(ids=document_ids)
assert "Document not found" in str(exception_info.value), str(exception_info.value)
+ assert dataset.list_documents() == []
@pytest.mark.p2
def test_duplicate_deletion(self, add_documents_func):
@@ -90,14 +94,15 @@ class TestDocumentsDeletion:
with pytest.raises(Exception) as exception_info:
dataset.delete_documents(ids=document_ids + document_ids)
assert "Field: <ids> - Message: <Duplicate ids:" in str(exception_info.value), str(exception_info.value)
- assert len(dataset.list_documents()) == 3, str(dataset.list_documents())
+ assert {doc.id for doc in dataset.list_documents()} == set(document_ids)
@pytest.mark.p3
-def test_concurrent_deletion(add_dataset, tmp_path):
+def test_concurrent_deletion(add_dataset_func, upload_documents):
count = 100
- dataset = add_dataset
- documents = bulk_upload_documents(dataset, count, tmp_path)
+ dataset = add_dataset_func
+ documents = upload_documents(dataset, count)
def delete_doc(doc_id):
dataset.delete_documents(ids=[doc_id])
@@ -105,16 +110,18 @@ def test_concurrent_deletion(add_dataset, tmp_path):
with ThreadPoolExecutor(max_workers=5) as executor:
futures = [executor.submit(delete_doc, doc.id) for doc in documents]
- responses = list(as_completed(futures))
- assert len(responses) == count, responses
+ for future in as_completed(futures):
+ future.result()
+
+ assert dataset.list_documents() == []
@pytest.mark.p3
-def test_delete_1k(add_dataset, tmp_path):
+def test_delete_1k(add_dataset_func, upload_documents):
count = 1_000
- dataset = add_dataset
- documents = bulk_upload_documents(dataset, count, tmp_path)
- assert len(list_all_documents(dataset, limit=count + 1)) == count
+ dataset = add_dataset_func
+ documents = upload_documents(dataset, count)
dataset.delete_documents(ids=[doc.id for doc in documents])
assert len(dataset.list_documents()) == 0
diff --git a/test/testcases/test_sdk_api/test_file_management_within_dataset/test_download_document.py b/test/testcases/test_sdk_api/test_file_management_within_dataset/test_download_document.py
index 821f35f958..13b5ecd39d 100644
--- a/test/testcases/test_sdk_api/test_file_management_within_dataset/test_download_document.py
+++ b/test/testcases/test_sdk_api/test_file_management_within_dataset/test_download_document.py
@@ -1,5 +1,5 @@
#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
+# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@@ -16,9 +16,7 @@
from concurrent.futures import ThreadPoolExecutor, as_completed
import pytest
-from common import bulk_upload_documents
from ragflow_sdk.modules.document import Document
-from utils import compare_by_hash
class _DownloadResponse:
@@ -105,6 +103,7 @@ def test_download_preserves_non_error_content_without_attachment(monkeypatch, co
@pytest.mark.p1
@pytest.mark.parametrize(
"generate_test_files",
[
@@ -121,65 +120,84 @@ def test_download_preserves_non_error_content_without_attachment(monkeypatch, co
],
indirect=True,
)
-def test_file_type_validation(add_dataset, generate_test_files, request):
- dataset = add_dataset
+def test_file_type_validation(add_dataset_func, generate_test_files, request):
+ dataset = add_dataset_func
fp = generate_test_files[request.node.callspec.params["generate_test_files"]]
with fp.open("rb") as f:
blob = f.read()
documents = dataset.upload_documents([{"display_name": fp.name, "blob": blob}])
- for document in documents:
- with fp.with_stem("ragflow_test_download").open("wb") as f:
- f.write(document.download())
+ assert len(documents) == 1, str(documents)
+ document = documents[0]
+ assert document.dataset_id == dataset.id
+ assert document.name == fp.name
+ with fp.with_stem("ragflow_test_download").open("wb") as f:
+ f.write(document.download())
- assert compare_by_hash(fp, fp.with_stem("ragflow_test_download"))
+ assert fp.read_bytes() == fp.with_stem("ragflow_test_download").read_bytes()
class TestDocumentDownload:
@pytest.mark.p3
- def test_same_file_repeat(self, add_documents, tmp_path, ragflow_tmp_dir):
+ @pytest.mark.skip(
+ reason=(
+ "Go DocumentService.DownloadDocument rejects zero-byte blobs in "
+ "internal/service/document/document_crud.go; upload is supported "
+ "by test/api/document_api_test.go::TestRESTDocumentUploadPersistenceAndNames."
+ )
+ )
+ def test_download_empty_uploaded_file(self, add_dataset_func):
+ dataset = add_dataset_func
+ documents = dataset.upload_documents([{"display_name": "empty.txt", "blob": b""}])
+
+ assert len(documents) == 1
+ assert documents[0].download() == b""
+
+ @pytest.mark.p3
+ def test_same_file_repeat(self, add_document_func):
num = 5
- _, documents = add_documents
+ _, document = add_document_func
- for i in range(num):
- download_path = tmp_path / f"ragflow_test_download_{i}.txt"
- with download_path.open("wb") as f:
- f.write(documents[0].download())
- assert compare_by_hash(ragflow_tmp_dir / "ragflow_test_upload_0.txt", download_path), f"Downloaded file {i} does not match original"
+ expected = b"RAGFlow document SDK integration test.\n"
+ for _ in range(num):
+ assert document.download() == expected
@pytest.mark.p3
- def test_download_error_json_raises(self, add_documents):
- dataset, documents = add_documents
- document = documents[0]
+ def test_download_error_json_raises(self, add_document_func):
+ dataset, document = add_document_func
invalid_document = document.__class__(
document.rag,
{"id": "missing-document-id-for-download", "dataset_id": dataset.id},
)
- with pytest.raises(Exception) as exception_info:
+ with pytest.raises(Exception, match="Resource not found"):
invalid_document.download()
- assert str(exception_info.value), exception_info
@pytest.mark.p3
-def test_concurrent_download(add_dataset, tmp_path):
+def test_concurrent_download(add_dataset_func, tmp_path):
count = 20
- dataset = add_dataset
- documents = bulk_upload_documents(dataset, count, tmp_path)
+ dataset = add_dataset_func
+ document_infos = []
+ for i in range(count):
+ fp = tmp_path / f"ragflow_test_upload_{i}.txt"
+ fp.write_text("This is the content of a test TXT file.", encoding="utf-8")
+ document_infos.append({"display_name": fp.name, "blob": fp.read_bytes()})
+ documents = dataset.upload_documents(document_infos)
+ assert len(documents) == count
def download_doc(document, i):
download_path = tmp_path / f"ragflow_test_download_{i}.txt"
with download_path.open("wb") as f:
f.write(document.download())
- # assert compare_by_hash(tmp_path / f"ragflow_test_upload_{i}.txt", download_path), str(download_path)
+ return download_path
with ThreadPoolExecutor(max_workers=5) as executor:
futures = [executor.submit(download_doc, documents[i], i) for i in range(count)]
- responses = list(as_completed(futures))
- assert len(responses) == count, responses
+ responses = [future.result() for future in as_completed(futures)]
+ assert len(responses) == count
for i in range(count):
- assert compare_by_hash(
- tmp_path / f"ragflow_test_upload_{i}.txt",
- tmp_path / f"ragflow_test_download_{i}.txt",
- )
+ assert (tmp_path / f"ragflow_test_upload_{i}.txt").read_bytes() == (tmp_path / f"ragflow_test_download_{i}.txt").read_bytes()
diff --git a/test/testcases/test_sdk_api/test_file_management_within_dataset/test_infrastructure.py b/test/testcases/test_sdk_api/test_file_management_within_dataset/test_infrastructure.py
new file mode 100644
index 0000000000..dc7f47b428
--- /dev/null
+++ b/test/testcases/test_sdk_api/test_file_management_within_dataset/test_infrastructure.py
@@ -0,0 +1,111 @@
+#
+# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+"""Service-free checks for the document suite's resource boundaries."""
+
+from types import SimpleNamespace
+
+import conftest as fixtures
+import pytest
+
+
[email protected]("isolated", [None, "0", "1"])
+def test_integration_requires_explicit_environment_before_network(monkeypatch, isolated):
+ monkeypatch.delenv("RAGFLOW_SDK_BASE_URL", raising=False)
+ monkeypatch.delenv("RAGFLOW_SDK_API_KEY", raising=False)
+ if isolated is None:
+ monkeypatch.delenv("RAGFLOW_SDK_ISOLATED", raising=False)
+ else:
+ monkeypatch.setenv("RAGFLOW_SDK_ISOLATED", isolated)
+
+ def no_network(*args, **kwargs):
+ pytest.fail("Missing integration prerequisites must not open a connection")
+
+ monkeypatch.setattr(fixtures, "RAGFlow", no_network)
+ with pytest.raises(pytest.fail.Exception, match="Integration environment required"):
+ fixtures.integration_settings.__wrapped__()
+
+
+def test_integration_refuses_nonempty_tenant(monkeypatch):
+ monkeypatch.setenv("RAGFLOW_SDK_ISOLATED", "1")
+ monkeypatch.setenv("RAGFLOW_SDK_BASE_URL", "http://example.invalid")
+ monkeypatch.setenv("RAGFLOW_SDK_API_KEY", "test-only-key")
+ monkeypatch.setattr(fixtures, "RAGFlow", lambda *args: SimpleNamespace(list_datasets=lambda **kwargs: [object()]))
+ with pytest.raises(AssertionError, match="test tenant is not empty"):
+ fixtures.integration_settings.__wrapped__()
+
+
[email protected]("persist_before_failure", [False, True])
+def test_cleanup_only_deletes_created_survivors_after_failure(monkeypatch, persist_before_failure):
+ datasets = {"unrelated": SimpleNamespace(id="unrelated")}
+ deleted = []
+ failed_name = "sdk-document-" + "a" * 32
+
+ class FakeClient:
+ def create_dataset(self, name):
+ if name == failed_name:
+ if persist_before_failure:
+ datasets[name] = SimpleNamespace(id=name, name=name)
+ raise RuntimeError("create failed")
+ datasets[name] = SimpleNamespace(id=name, name=name)
+ return datasets[name]
+
+ def list_datasets(self, *, ids=None, name=None, page_size):
+ assert page_size == 100
+ if name is not None:
+ return [dataset for dataset in datasets.values() if getattr(dataset, "name", None) == name]
+ return [datasets[key] for key in ids if key in datasets]
+
+ def delete_datasets(self, *, ids):
+ deleted.extend(ids)
+ for key in ids:
+ del datasets[key]
+
+ monkeypatch.setattr(fixtures, "RAGFlow", lambda *args: FakeClient())
+ fixture = fixtures.client.__wrapped__({"api_key": "test", "base_url": "http://example.invalid"}, monkeypatch)
+ client = next(fixture)
+ client.create_dataset("survivor")
+ client.create_dataset("already-deleted")
+ del datasets["already-deleted"]
+ with pytest.raises(RuntimeError, match="create failed"):
+ client.create_dataset(failed_name)
+ with pytest.raises(StopIteration):
+ next(fixture)
+ assert deleted == ["survivor"] + ([failed_name] if persist_before_failure else [])
+ assert set(datasets) == {"unrelated"}
+
+
[email protected]("timeout", [None, 12])
+def test_live_http_calls_have_bounded_timeouts(monkeypatch, timeout):
+ calls = []
+
+ def transport(session, *args, **kwargs):
+ calls.append(kwargs)
+ return object()
+
+ monkeypatch.setattr(fixtures.requests.sessions.Session, "request", transport)
+ request = SimpleNamespace(node=SimpleNamespace(get_closest_marker=lambda name: object()))
+ fixtures.network_boundary.__wrapped__(request, monkeypatch)
+ kwargs = {} if timeout is None else {"timeout": timeout}
+ fixtures.requests.Session().request("GET", "http://example.invalid", **kwargs)
+ assert calls == [{"timeout": (5, 60) if timeout is None else timeout}]
+
+
+def test_service_free_network_boundary_rejects_connections(monkeypatch):
+ request = SimpleNamespace(node=SimpleNamespace(get_closest_marker=lambda name: None))
+ fixtures.network_boundary.__wrapped__(request, monkeypatch)
+ with pytest.raises(pytest.fail.Exception, match="must not open a network connection"):
+ fixtures.requests.Session().request("GET", "http://example.invalid")
diff --git a/test/testcases/test_sdk_api/test_file_management_within_dataset/test_list_documents.py b/test/testcases/test_sdk_api/test_file_management_within_dataset/test_list_documents.py
index 7547228397..d81fa297d0 100644
--- a/test/testcases/test_sdk_api/test_file_management_within_dataset/test_list_documents.py
+++ b/test/testcases/test_sdk_api/test_file_management_within_dataset/test_list_documents.py
@@ -1,5 +1,5 @@
#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
+# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@@ -14,11 +14,26 @@
# limitations under the License.
#
from concurrent.futures import ThreadPoolExecutor, as_completed
+from unittest.mock import Mock
import pytest
+from beartype.roar import BeartypeCallHintParamViolation
+from ragflow_sdk import DataSet, Document
+
+
+def raw_document_values_by_id(dataset, field, desc):
+ response = dataset.rag.get(
+ f"/datasets/{dataset.id}/documents",
+ params={"page": 1, "page_size": 100, "orderby": field, "desc": desc},
+ )
+ result = response.json()
+ assert result["code"] == 0, result
+ return {document["id"]: document[field] for document in result["data"]["docs"]}
class TestDocumentsList:
+ pytestmark = pytest.mark.integration
+
@pytest.mark.p1
def test_default(self, add_documents):
dataset, _ = add_documents
@@ -27,113 +42,80 @@ class TestDocumentsList:
@pytest.mark.p1
@pytest.mark.parametrize(
- "params, expected_page_size, expected_message",
+ "params, expected_page_ids",
[
- ({"page": None, "page_size": 2}, 2, "not instance of"),
- ({"page": 1, "page_size": 2}, 2, ""),
- ({"page": 2, "page_size": 2}, 2, ""),
- ({"page": 3, "page_size": 2}, 1, ""),
- ({"page": "3", "page_size": 2}, 1, "not instance of"),
- pytest.param(
- {"page": -1, "page_size": 2},
- 0,
- "Invalid page number",
- marks=pytest.mark.skip(reason="issues/5851"),
- ),
- pytest.param(
- {"page": "a", "page_size": 2},
- 0,
- "Invalid page value",
- marks=pytest.mark.skip(reason="issues/5851"),
- ),
+ ({"page": 1, "page_size": 2, "orderby": "name", "desc": False}, slice(0, 2)),
+ ({"page": 2, "page_size": 2, "orderby": "name", "desc": False}, slice(2, 4)),
+ ({"page": 3, "page_size": 2, "orderby": "name", "desc": False}, slice(4, 6)),
+ ({"page": -1, "page_size": 2, "orderby": "name", "desc": False}, slice(0, 2)),
],
)
- def test_page(self, add_documents, params, expected_page_size, expected_message):
- dataset, _ = add_documents
- if expected_message:
- with pytest.raises(Exception) as exception_info:
- dataset.list_documents(**params)
- assert expected_message in str(exception_info.value), str(exception_info.value)
- else:
- documents = dataset.list_documents(**params)
- assert len(documents) == expected_page_size, str(documents)
+ def test_page(self, add_documents, params, expected_page_ids):
+ dataset, fixture_documents = add_documents
+ expected = sorted(fixture_documents, key=lambda document: document.name)
+ documents = dataset.list_documents(**params)
+ assert [document.id for document in documents] == [document.id for document in expected[expected_page_ids]]
@pytest.mark.p1
@pytest.mark.parametrize(
- "params, expected_page_size, expected_message",
+ "params, expected_size",
[
- ({"page_size": None}, 5, "not instance of"),
- ({"page_size": 2}, 2, ""),
- ({"page_size": 1}, 1, ""),
- ({"page_size": 6}, 5, ""),
- ({"page_size": "1"}, 1, "not instance of"),
- pytest.param(
- {"page_size": -1},
- 0,
- "Invalid page size",
- marks=pytest.mark.skip(reason="issues/5851"),
- ),
- pytest.param(
- {"page_size": "a"},
- 0,
- "Invalid page size value",
- marks=pytest.mark.skip(reason="issues/5851"),
- ),
+ ({"page_size": 2, "orderby": "name", "desc": False}, 2),
+ ({"page_size": 1, "orderby": "name", "desc": False}, 1),
+ ({"page_size": 6, "orderby": "name", "desc": False}, 5),
+ ({"page_size": -1, "orderby": "name", "desc": False}, 5),
],
)
- def test_page_size(self, add_documents, params, expected_page_size, expected_message):
- dataset, _ = add_documents
- if expected_message:
- with pytest.raises(Exception) as exception_info:
- dataset.list_documents(**params)
- assert expected_message in str(exception_info.value), str(exception_info.value)
- else:
- documents = dataset.list_documents(**params)
- assert len(documents) == expected_page_size, str(documents)
+ def test_page_size(self, add_documents, params, expected_size):
+ dataset, fixture_documents = add_documents
+ expected = sorted(fixture_documents, key=lambda document: document.name)
+ documents = dataset.list_documents(**params)
+ assert [document.id for document in documents] == [document.id for document in expected[:expected_size]]
@pytest.mark.p3
@pytest.mark.parametrize(
"params, expected_message",
[
- ({"orderby": None}, "not instance of"),
({"orderby": "create_time"}, ""),
({"orderby": "update_time"}, ""),
- pytest.param({"orderby": "name", "desc": "False"}, "", marks=pytest.mark.skip(reason="issues/5851")),
- pytest.param({"orderby": "unknown"}, "orderby should be create_time or update_time", marks=pytest.mark.skip(reason="issues/5851")),
+ ({"orderby": "name"}, ""),
+ ({"orderby": "unknown"}, "invalid orderby field: unknown"),
],
)
def test_orderby(self, add_documents, params, expected_message):
- dataset, _ = add_documents
+ dataset, fixture_documents = add_documents
if expected_message:
with pytest.raises(Exception) as exception_info:
dataset.list_documents(**params)
assert expected_message in str(exception_info.value), str(exception_info.value)
else:
- dataset.list_documents(**params)
+ field = params["orderby"]
+ documents = dataset.list_documents(**params)
+ assert {document.id for document in documents} == {doc.id for doc in fixture_documents}
+ raw_values = raw_document_values_by_id(dataset, field, desc=True)
+ assert set(raw_values) == {doc.id for doc in fixture_documents}
+ values = [raw_values[document.id] for document in documents]
+ assert values == sorted(values, reverse=True), values
@pytest.mark.p3
- @pytest.mark.parametrize(
- "params, expected_message",
- [
- ({"desc": None}, "not instance of"),
- ({"desc": "true"}, "not instance of"),
- ({"desc": "True"}, "not instance of"),
- ({"desc": True}, ""),
- pytest.param({"desc": "false"}, "", marks=pytest.mark.skip(reason="issues/5851")),
- ({"desc": "False"}, "not instance of"),
- ({"desc": False}, ""),
- ({"desc": "False", "orderby": "update_time"}, "not instance of"),
- pytest.param({"desc": "unknown"}, "desc should be true or false", marks=pytest.mark.skip(reason="issues/5851")),
- ],
- )
- def test_desc(self, add_documents, params, expected_message):
- dataset, _ = add_documents
- if expected_message:
- with pytest.raises(Exception) as exception_info:
- dataset.list_documents(**params)
- assert expected_message in str(exception_info.value), str(exception_info.value)
- else:
- dataset.list_documents(**params)
+ @pytest.mark.parametrize("desc", [True, False])
+ def test_desc(self, add_documents, desc):
+ dataset, fixture_documents = add_documents
+ documents = dataset.list_documents(desc=desc)
+ assert {document.id for document in documents} == {doc.id for doc in fixture_documents}
+ raw_values = raw_document_values_by_id(dataset, "create_time", desc)
+ assert set(raw_values) == {doc.id for doc in fixture_documents}
+ create_times = [raw_values[document.id] for document in documents]
+ assert create_times == sorted(create_times, reverse=desc), create_times
+
+ @pytest.mark.p2
+ @pytest.mark.parametrize("desc", [False, True])
+ def test_name_ordering(self, add_documents, desc):
+ dataset, fixture_documents = add_documents
+ documents = dataset.list_documents(orderby="name", desc=desc)
+ assert {document.id for document in documents} == {doc.id for doc in fixture_documents}
+ names = [document.name for document in documents]
+ assert names == sorted(names, reverse=desc), names
@pytest.mark.p2
@pytest.mark.parametrize(
@@ -267,9 +249,50 @@ class TestDocumentsList:
docs = future.result()
assert len(docs) == 5, str(docs)
- @pytest.mark.p3
- def test_invalid_params(self, add_documents):
- dataset, _ = add_documents
+
+class TestDocumentListSDKTypes:
+ def test_timestamp_ordering_uses_raw_rows_by_sdk_document_id(self):
+ rows = [
+ {"id": "second", "name": "second.txt", "create_time": 20},
+ {"id": "first", "name": "first.txt", "create_time": 10},
+ ]
+ rag = Mock()
+ rag.get.return_value.json.return_value = {"code": 0, "data": {"docs": rows}}
+ dataset = DataSet(rag, {"id": "dataset"})
+ documents = [Document(rag, row.copy()) for row in rows]
+ values = raw_document_values_by_id(dataset, "create_time", True)
+ assert [values[document.id] for document in documents] == [20, 10]
+ rag.get.assert_called_once_with(
+ "/datasets/dataset/documents",
+ params={"page": 1, "page_size": 100, "orderby": "create_time", "desc": True},
+ )
+
+ @pytest.mark.parametrize(
+ "params",
+ [
+ {"page": None},
+ {"page": "3"},
+ {"page": "a"},
+ {"page_size": None},
+ {"page_size": "1"},
+ {"page_size": "a"},
+ {"orderby": None},
+ {"desc": None},
+ {"desc": "true"},
+ {"desc": "True"},
+ {"desc": "false"},
+ {"desc": "False"},
+ {"desc": "False", "orderby": "update_time"},
+ {"desc": "unknown"},
+ ],
+ )
+ def test_invalid_parameter_types_are_rejected_before_http(self, params):
+ dataset = DataSet(None, {"id": "dataset-without-http-client"})
+ with pytest.raises(BeartypeCallHintParamViolation):
+ dataset.list_documents(**params)
+
+ def test_invalid_params(self):
+ dataset = DataSet(None, {"id": "dataset-without-http-client"})
params = {"a": "b"}
with pytest.raises(TypeError) as exception_info:
dataset.list_documents(**params)
diff --git a/test/testcases/test_sdk_api/test_file_management_within_dataset/test_parse_documents.py b/test/testcases/test_sdk_api/test_file_management_within_dataset/test_parse_documents.py
index c739b0b38f..01eb0ed9a5 100644
--- a/test/testcases/test_sdk_api/test_file_management_within_dataset/test_parse_documents.py
+++ b/test/testcases/test_sdk_api/test_file_management_within_dataset/test_parse_documents.py
@@ -1,5 +1,5 @@
#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
+# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@@ -13,121 +13,173 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
+
+import time
from concurrent.futures import ThreadPoolExecutor, as_completed
+
import pytest
-from common import bulk_upload_documents, list_all_documents
-from ragflow_sdk import DataSet
+from ragflow_sdk.modules.dataset import DataSet
from ragflow_sdk.modules.document import Document
-from utils import wait_for
-
-
-@wait_for(30, 1, "Document parsing timeout")
-def condition(_dataset: DataSet, _document_ids: list[str] = None):
- documents = _dataset.list_documents(page_size=100)
-
- if _document_ids is None:
- for document in documents:
- if document.run != "DONE":
- return False
- return True
- target_ids = set(_document_ids)
- for document in documents:
- if document.id in target_ids:
- if document.run != "DONE":
- return False
- return True
-
-def validate_document_details(dataset, document_ids):
+def _document_rows(dataset, document_ids):
+ rows = []
+ max_pages = len(document_ids) // 100 + 1
+ for page in range(1, max_pages + 1):
+ response = dataset.rag.get(
+ f"/datasets/{dataset.id}/documents",
+ params={"ids": document_ids, "page": page, "page_size": 100},
+ ).json()
+ if response.get("code") != 0:
+ raise AssertionError(response.get("message"))
+ batch = response["data"]["docs"]
+ rows.extend(batch)
+ if len(batch) < 100:
+ return rows
+ raise AssertionError(f"Document ID filter exceeded the expected {max_pages} pages")
+
+
+def _wait_for_terminal_statuses(dataset, document_ids, timeout, previous_event_ids=None):
+ deadline = time.monotonic() + timeout
target_ids = set(document_ids)
- found_ids = set()
- for document in list_all_documents(dataset):
- if document.id in target_ids:
- found_ids.add(document.id)
- assert document.run == "DONE"
- assert len(document.process_begin_at) > 0
- assert document.process_duration > 0
- assert document.progress > 0
- assert "Task done" in document.progress_msg
- assert found_ids == target_ids
-
-
+ terminal = {"COMPLETED", "STOPPED", "FAILED"}
+ rows = {}
+ while time.monotonic() < deadline:
+ rows = {row["id"]: row for row in _document_rows(dataset, document_ids)}
+ terminal_rows = rows.keys() == target_ids and all(row.get("ingestion_status") in terminal for row in rows.values())
+ new_events = previous_event_ids is None or all(
+ isinstance(row.get("latest_ingestion_event"), dict) and row["latest_ingestion_event"].get("id") != previous_event_ids[doc_id] for doc_id, row in rows.items()
+ )
+ if terminal_rows and new_events:
+ return rows
+ time.sleep(min(1, max(0, deadline - time.monotonic())))
+ statuses = {doc_id: row.get("ingestion_status") for doc_id, row in rows.items()}
+ raise AssertionError(f"Parsing did not reach a terminal state within {timeout}s: {statuses}")
+
+
+def _assert_completed(rows, document_ids):
+ assert set(rows) == set(document_ids)
+ for row in rows.values():
+ assert row["ingestion_status"] == "COMPLETED"
+ assert row["process_begin_at"]
+ assert row["process_duration"] > 0
+ assert row["progress"] >= 1
+ assert row["chunk_count"] >= 0
+ assert row["token_count"] >= 0
+
+
class TestDocumentsParse:
@pytest.mark.parametrize(
- "payload, expected_message",
+ "document_ids, expected_message",
[
- pytest.param(None, "AttributeError", marks=pytest.mark.skip),
- pytest.param({"document_ids": []}, "`document_ids` is required", marks=pytest.mark.p3),
- pytest.param({"document_ids": ["invalid_id"]}, "Documents not found: ['invalid_id']", marks=pytest.mark.p3),
- pytest.param({"document_ids": ["\n!?。;!?\"'"]}, "Documents not found: ['\\n!?。;!?\"\\'']", marks=pytest.mark.p3),
- pytest.param("not json", "AttributeError", marks=pytest.mark.skip),
- pytest.param(lambda r: {"document_ids": r[:1]}, "", marks=pytest.mark.p3),
- pytest.param(lambda r: {"document_ids": r}, "", marks=pytest.mark.p3),
+ (None, "`document_ids` is required"),
+ ([], "`document_ids` is required"),
+ (["invalid_id"], "documents not found: [invalid_id]"),
+ (["\n!?。;!?\"'"], "documents not found: ["),
+ ("not json", "Invalid request body"),
],
)
- def test_basic_scenarios(self, add_documents_func, payload, expected_message):
+ def test_invalid_document_ids(self, add_documents_func, document_ids, expected_message):
+ dataset, _documents = add_documents_func
+ with pytest.raises(Exception) as exc_info:
+ dataset.async_parse_documents(document_ids=document_ids)
+ assert expected_message in str(exc_info.value)
+
+ @pytest.mark.parametrize("selection", ["one", "all"])
+ def test_parse_valid_documents(self, add_documents_func, integration_settings, selection):
dataset, documents = add_documents_func
- if callable(payload):
- payload = payload([doc.id for doc in documents])
-
- if expected_message:
- with pytest.raises(Exception) as exception_info:
- dataset.async_parse_documents(**payload)
- assert expected_message in str(exception_info.value), str(exception_info.value)
- else:
- dataset.async_parse_documents(**payload)
- condition(dataset, payload["document_ids"])
- validate_document_details(dataset, payload["document_ids"])
+ document_ids = [document.id for document in documents]
+ if selection == "one":
+ document_ids = document_ids[:1]
+
+ assert dataset.async_parse_documents(document_ids=document_ids) is None
+ rows = _wait_for_terminal_statuses(dataset, document_ids, integration_settings["parse_timeout"])
+ _assert_completed(rows, document_ids)
@pytest.mark.parametrize(
- "payload",
- [
- pytest.param(lambda r: {"document_ids": ["invalid_id"] + r}, marks=pytest.mark.p3),
- pytest.param(lambda r: {"document_ids": r[:1] + ["invalid_id"] + r[1:3]}, marks=pytest.mark.p3),
- pytest.param(lambda r: {"document_ids": r + ["invalid_id"]}, marks=pytest.mark.p3),
- ],
+ "invalid_position",
+ ["first", "middle", "last"],
)
- def test_parse_partial_invalid_document_id(self, add_documents_func, payload):
+ def test_invalid_id_rejects_entire_parse_request(self, add_documents_func, invalid_position):
dataset, documents = add_documents_func
- document_ids = [doc.id for doc in documents]
- payload = payload(document_ids)
+ document_ids = [document.id for document in documents]
+ ids = list(document_ids)
+ index = {"first": 0, "middle": len(ids) // 2, "last": len(ids)}[invalid_position]
+ ids.insert(index, "invalid_id")
+ before = {row["id"]: row["ingestion_status"] for row in _document_rows(dataset, document_ids)}
- with pytest.raises(Exception) as exception_info:
- dataset.async_parse_documents(**payload)
- assert "Documents not found: ['invalid_id']" in str(exception_info.value), str(exception_info.value)
+ with pytest.raises(Exception, match="documents not found:"):
+ dataset.async_parse_documents(document_ids=ids)
- condition(dataset, document_ids)
- validate_document_details(dataset, document_ids)
+ after = {row["id"]: row["ingestion_status"] for row in _document_rows(dataset, document_ids)}
+ assert {doc_id: after[doc_id] for doc_id in document_ids} == {doc_id: before[doc_id] for doc_id in document_ids}
- @pytest.mark.p3
- def test_repeated_parse(self, add_documents_func):
+ def test_repeated_parse_reprocesses_completed_documents(self, add_documents_func, integration_settings):
dataset, documents = add_documents_func
- document_ids = [doc.id for doc in documents]
- dataset.async_parse_documents(document_ids=document_ids)
- condition(dataset, document_ids)
+ document_ids = [document.id for document in documents]
+
dataset.async_parse_documents(document_ids=document_ids)
+ first_rows = _wait_for_terminal_statuses(dataset, document_ids, integration_settings["parse_timeout"])
+ _assert_completed(first_rows, document_ids)
+ first_event_ids = {doc_id: row["latest_ingestion_event"]["id"] for doc_id, row in first_rows.items()}
- @pytest.mark.p3
- def test_duplicate_parse(self, add_documents_func):
+ dataset.async_parse_documents(document_ids=document_ids)
+ second_rows = _wait_for_terminal_statuses(
+ dataset,
+ document_ids,
+ integration_settings["parse_timeout"],
+ previous_event_ids=first_event_ids,
+ )
+ _assert_completed(second_rows, document_ids)
+
+ def test_duplicate_ids_are_parsed_once(self, add_documents_func, integration_settings):
dataset, documents = add_documents_func
- document_ids = [doc.id for doc in documents]
+ document_ids = [document.id for document in documents]
+
dataset.async_parse_documents(document_ids=document_ids + document_ids)
- condition(dataset, document_ids)
- validate_document_details(dataset, document_ids)
+ rows = _wait_for_terminal_statuses(dataset, document_ids, integration_settings["parse_timeout"])
+ _assert_completed(rows, document_ids)
+
+ @pytest.mark.p3
+ def test_parse_100_files(self, add_dataset_func, upload_documents, integration_settings):
+ dataset = add_dataset_func
+ documents = upload_documents(dataset, 100)
+ document_ids = [document.id for document in documents]
+ dataset.async_parse_documents(document_ids=document_ids)
+ rows = _wait_for_terminal_statuses(dataset, document_ids, integration_settings["parse_timeout"])
+ _assert_completed(rows, document_ids)
-def test_get_documents_status_handles_terminal_and_progress_paths(add_dataset_func, monkeypatch):
- """Collect terminal states and completed progress without another lookup."""
- dataset = add_dataset_func
- states = {"doc-done": ("DONE", 0.0), "doc-fail": ("FAIL", -1.0), "doc-cancel": ("CANCEL", 0.3), "doc-progress": ("RUNNING", 1.0)}
+ @pytest.mark.p3
+ def test_concurrent_parse(self, add_dataset_func, upload_documents, integration_settings):
+ dataset = add_dataset_func
+ documents = upload_documents(dataset, 100)
+
+ with ThreadPoolExecutor(max_workers=5) as executor:
+ futures = [executor.submit(dataset.async_parse_documents, [document.id]) for document in documents]
+ for future in as_completed(futures):
+ assert future.result() is None
+
+ document_ids = [document.id for document in documents]
+ rows = _wait_for_terminal_statuses(dataset, document_ids, integration_settings["parse_timeout"])
+ _assert_completed(rows, document_ids)
+
+
+def test_get_documents_status_handles_terminal_and_progress_paths(monkeypatch):
+ dataset = DataSet(None, {"id": "dataset-1"})
+ states = {
+ "doc-done": ("DONE", 0.0),
+ "doc-fail": ("FAIL", -1.0),
+ "doc-cancel": ("CANCEL", 0.3),
+ "doc-progress": ("RUNNING", 1.0),
+ }
calls = []
def _list_documents(id=None, **_kwargs):
calls.append(id)
run, progress = states[id]
- return [Document(dataset.rag, {"id": id, "run": run, "progress": progress, "chunk_count": 2, "token_count": 4})]
+ return [Document(None, {"id": id, "run": run, "progress": progress, "chunk_count": 2, "token_count": 4})]
monkeypatch.setattr(dataset, "list_documents", _list_documents)
monkeypatch.setattr("time.sleep", lambda *_args: pytest.fail("terminal documents must not be polled again"))
@@ -137,10 +189,8 @@ def test_get_documents_status_handles_terminal_and_progress_paths(add_dataset_fu
assert sorted(calls) == sorted(states)
-def test_get_documents_status_propagates_lookup_error(add_dataset_func, monkeypatch):
- """Surface the original lookup error instead of silently retrying."""
- dataset = add_dataset_func
+def test_get_documents_status_propagates_lookup_error(monkeypatch):
+ dataset = DataSet(None, {"id": "dataset-1"})
error = RuntimeError("temporary list failure")
calls = []
@@ -157,10 +207,8 @@ def test_get_documents_status_propagates_lookup_error(add_dataset_func, monkeypa
assert calls == ["doc-1"]
-def test_get_documents_status_raises_for_missing_document(add_dataset_func, monkeypatch):
- """Stop waiting when the requested document is no longer returned."""
- dataset = add_dataset_func
+def test_get_documents_status_raises_for_missing_document(monkeypatch):
+ dataset = DataSet(None, {"id": "dataset-1"})
calls = []
def _list_documents(id=None, **_kwargs):
@@ -175,11 +223,10 @@ def test_get_documents_status_raises_for_missing_document(add_dataset_func, monk
assert calls == ["doc-1"]
-def test_parse_documents_keyboard_interrupt_triggers_cancel_then_returns_status(add_dataset_func, monkeypatch):
- dataset = add_dataset_func
+def test_parse_documents_keyboard_interrupt_triggers_cancel_then_returns_status(monkeypatch):
+ dataset = DataSet(None, {"id": "dataset-1"})
state = {"cancel_calls": 0, "status_calls": 0}
- expected_status = [("doc-1", "DONE", 1, 2)]
+ expected_status = [("doc-1", "CANCEL", 1, 2)]
def _raise_keyboard_interrupt(_document_ids):
raise KeyboardInterrupt
@@ -197,16 +244,12 @@ def test_parse_documents_keyboard_interrupt_triggers_cancel_then_returns_status(
monkeypatch.setattr(dataset, "async_cancel_parse_documents", _cancel)
monkeypatch.setattr(dataset, "_get_documents_status", _status)
- status = dataset.parse_documents(["doc-1"])
- assert status == expected_status
- assert state["cancel_calls"] == 1
- assert state["status_calls"] == 1
+ assert dataset.parse_documents(["doc-1"]) == expected_status
+ assert state == {"cancel_calls": 1, "status_calls": 1}
-def test_parse_documents_returns_first_completed_status(add_dataset_func, monkeypatch):
- """Return the first wait result without polling the documents twice."""
- dataset = add_dataset_func
+def test_parse_documents_returns_first_completed_status(monkeypatch):
+ dataset = DataSet(None, {"id": "dataset-1"})
state = {"status_calls": 0}
def _noop_parse(_document_ids):
@@ -220,74 +263,108 @@ def test_parse_documents_returns_first_completed_status(add_dataset_func, monkey
monkeypatch.setattr(dataset, "async_parse_documents", _noop_parse)
monkeypatch.setattr(dataset, "_get_documents_status", _status)
- status = dataset.parse_documents(["doc-1"])
+ assert dataset.parse_documents(["doc-1"]) == [("doc-1", "DONE-1", 1, 2)]
assert state["status_calls"] == 1
- assert status == [("doc-1", "DONE-1", 1, 2)]
-def test_async_cancel_parse_documents_raises_on_nonzero_code(add_dataset_func, monkeypatch):
- dataset = add_dataset_func
+def test_async_parse_documents_sends_document_ids(monkeypatch):
+ dataset = DataSet(None, {"id": "dataset-1"})
+ calls = []
+
+ class _Response:
+ @staticmethod
+ def json():
+ return {"code": 0}
+
+ def _post(path, body):
+ calls.append((path, body))
+ return _Response()
+
+ monkeypatch.setattr(dataset, "post", _post)
+ assert dataset.async_parse_documents(["doc-1", "doc-2"]) is None
+ assert calls == [("/datasets/dataset-1/chunks", {"document_ids": ["doc-1", "doc-2"]})]
+
- class _Resp:
+def test_terminal_status_from_previous_parse_run_is_not_reused(monkeypatch):
+ dataset = DataSet(None, {"id": "dataset-1"})
+ row = {
+ "id": "doc-1",
+ "ingestion_status": "COMPLETED",
+ "latest_ingestion_event": {"id": 42},
+ }
+ calls = []
+
+ class _Response:
@staticmethod
def json():
- return {"code": 102, "message": "cancel failed"}
-
- monkeypatch.setattr(dataset, "rm", lambda *_args, **_kwargs: _Resp())
-
- with pytest.raises(Exception) as exc_info:
- dataset.async_cancel_parse_documents(["doc-1"])
- assert "cancel failed" in str(exc_info.value), str(exc_info.value)
-
-
-def test_parse_100_files(add_dataset_func, tmp_path):
- @wait_for(200, 1, "Document parsing timeout")
- def condition_inner(_dataset: DataSet, _count: int):
- docs = list_all_documents(_dataset, limit=_count)
- if len(docs) < _count:
- return False
- for document in docs:
- if document.run != "DONE":
- return False
- return True
-
- count = 100
- dataset = add_dataset_func
- documents = bulk_upload_documents(dataset, count, tmp_path)
- document_ids = [doc.id for doc in documents]
-
- dataset.async_parse_documents(document_ids=document_ids)
- condition_inner(dataset, count)
- validate_document_details(dataset, document_ids)
-
-
-def test_concurrent_parse(add_dataset_func, tmp_path):
- @wait_for(200, 1, "Document parsing timeout")
- def condition_inner(_dataset: DataSet, _count: int):
- docs = list_all_documents(_dataset, limit=_count)
- if len(docs) < _count:
- return False
- for document in docs:
- if document.run != "DONE":
- return False
- return True
-
- count = 100
- dataset = add_dataset_func
- documents = bulk_upload_documents(dataset, count, tmp_path)
- document_ids = [doc.id for doc in documents]
-
- def parse_doc(doc_id):
- dataset.async_parse_documents(document_ids=[doc_id])
-
- with ThreadPoolExecutor(max_workers=5) as executor:
- futures = [executor.submit(parse_doc, doc.id) for doc in documents]
-
- responses = list(as_completed(futures))
- assert len(responses) == count, responses
-
- condition_inner(dataset, count)
- validate_document_details(dataset, document_ids)
+ return {"code": 0, "data": {"docs": [row]}}
+
+ class _Rag:
+ @staticmethod
+ def get(_path, params):
+ calls.append(params)
+ return _Response()
+
+ dataset.rag = _Rag()
+
+ with pytest.raises(AssertionError, match="did not reach a terminal state"):
+ _wait_for_terminal_statuses(
+ dataset,
+ ["doc-1"],
+ timeout=0.02,
+ previous_event_ids={"doc-1": 42},
+ )
+ assert calls
+ assert all(call == {"ids": ["doc-1"], "page": 1, "page_size": 100} for call in calls)
+
+
+ "ingestion_status, progress, expected_status",
+ [
+ ("COMPLETED", 1.0, "DONE"),
+ pytest.param(
+ "FAILED",
+ -1.0,
+ "FAIL",
+ marks=pytest.mark.skip(
+ reason=(
+ "Go document-list handler internal/handler/document.go:mapDocumentListItem "
+ "returns ingestion_status, which sdk/python/ragflow_sdk/modules/document.py:Document.__init__ "
+ "drops; DataSet._get_documents_status then polls forever for failed tasks."
+ )
+ ),
+ ),
+ pytest.param(
+ "STOPPED",
+ 0.0,
+ "CANCEL",
+ marks=pytest.mark.skip(
+ reason=(
+ "Go document-list handler internal/handler/document.go:mapDocumentListItem "
+ "returns ingestion_status, which sdk/python/ragflow_sdk/modules/document.py:Document.__init__ "
+ "drops; DataSet._get_documents_status then polls forever for cancelled tasks."
+ )
+ ),
+ ),
+ ],
+)
+def test_parse_documents_returns_documented_terminal_status(monkeypatch, ingestion_status, progress, expected_status):
+ dataset = DataSet(None, {"id": "dataset-1"})
+
+ def _list_documents(**_kwargs):
+ return [
+ Document(
+ None,
+ {
+ "id": "doc-1",
+ "ingestion_status": ingestion_status,
+ "progress": progress,
+ "chunk_count": 2,
+ "token_count": 4,
+ },
+ )
+ ]
+
+ monkeypatch.setattr(dataset, "list_documents", _list_documents)
+ monkeypatch.setattr("time.sleep", lambda *_args: pytest.fail("terminal documents must not be polled again"))
+ assert dataset._get_documents_status(["doc-1"]) == [("doc-1", expected_status, 2, 4)]
diff --git a/test/testcases/test_sdk_api/test_file_management_within_dataset/test_stop_parse_documents.py b/test/testcases/test_sdk_api/test_file_management_within_dataset/test_stop_parse_documents.py
index 3cc4051fb3..bfa560c155 100644
--- a/test/testcases/test_sdk_api/test_file_management_within_dataset/test_stop_parse_documents.py
+++ b/test/testcases/test_sdk_api/test_file_management_within_dataset/test_stop_parse_documents.py
@@ -1,5 +1,5 @@
#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
+# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@@ -14,28 +14,78 @@
# limitations under the License.
#
+import time
+
import pytest
+from ragflow_sdk.modules.dataset import DataSet
+
+
+def test_async_cancel_parse_documents_sends_document_ids(monkeypatch):
+ dataset = DataSet(None, {"id": "dataset-1"})
+ calls = []
+
+ class _Response:
+ @staticmethod
+ def json():
+ return {"code": 0}
+
+ def _rm(path, body):
+ calls.append((path, body))
+ return _Response()
+
+ monkeypatch.setattr(dataset, "rm", _rm)
+ assert dataset.async_cancel_parse_documents(["doc-1", "doc-2"]) is None
+ assert calls == [("/datasets/dataset-1/chunks", {"document_ids": ["doc-1", "doc-2"]})]
+
+
+def test_async_cancel_parse_documents_surfaces_api_error(monkeypatch):
+ dataset = DataSet(None, {"id": "dataset-1"})
+
+ class _Response:
+ @staticmethod
+ def json():
+ return {"code": 102, "message": "cancel failed"}
+ monkeypatch.setattr(dataset, "rm", lambda *_args, **_kwargs: _Response())
+ with pytest.raises(Exception, match="cancel failed"):
+ dataset.async_cancel_parse_documents(["doc-1"])
-def validate_document_parse_done(dataset, document_ids):
- documents = dataset.list_documents(page_size=100)
- for document in documents:
- if document.id in document_ids:
- assert document.run == "DONE"
- assert len(document.process_begin_at) > 0
- assert document.process_duration > 0
- assert document.progress > 0
- assert "Task done" in document.progress_msg
+def test_async_cancel_stops_an_active_parse(add_dataset_func, document_factory, integration_settings):
+ dataset = add_dataset_func
+ document = document_factory(
+ dataset,
+ blob=(b"RAGFlow SDK cancellation test. " * 131072),
+ )
+ dataset.async_parse_documents([document.id])
-def validate_document_parse_cancel(dataset, document_ids):
- documents = dataset.list_documents(page_size=100)
- for document in documents:
- assert document.run == "CANCEL"
- assert len(document.process_begin_at) > 0
- assert document.progress == 0.0
+ def read_status():
+ response = dataset.rag.get(
+ f"/datasets/{dataset.id}/documents",
+ params={"id": document.id, "page": 1, "page_size": 1},
+ ).json()
+ assert response.get("code") == 0, response.get("message")
+ rows = response["data"]["docs"]
+ assert len(rows) == 1
+ return rows[0]["ingestion_status"]
+ def wait_for_status(statuses, *, terminal=None):
+ deadline = time.monotonic() + integration_settings["parse_timeout"]
+ status = None
+ while time.monotonic() < deadline:
+ status = read_status()
+ if status in statuses:
+ return status
+ if terminal and status in terminal:
+ pytest.fail(f"parse reached {status!r} before expected status {statuses}")
+ time.sleep(min(0.5, max(0, deadline - time.monotonic())))
+ pytest.fail(f"document did not reach {statuses}; latest status was {status!r}")
-class TestDocumentsParseStop:
- pass
+ wait_for_status({"CREATED", "SCHEDULED", "RUNNING", "STOPPING"})
+ dataset.async_cancel_parse_documents([document.id])
+ status = wait_for_status(
+ {"STOPPED", "COMPLETED", "FAILED"},
+ terminal={"COMPLETED", "FAILED"},
+ )
+ assert status == "STOPPED"
diff --git a/test/testcases/test_sdk_api/test_file_management_within_dataset/test_update_document.py b/test/testcases/test_sdk_api/test_file_management_within_dataset/test_update_document.py
index 78d5fbb217..eab263dd4f 100644
--- a/test/testcases/test_sdk_api/test_file_management_within_dataset/test_update_document.py
+++ b/test/testcases/test_sdk_api/test_file_management_within_dataset/test_update_document.py
@@ -1,5 +1,5 @@
#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
+# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@@ -14,24 +14,25 @@
# limitations under the License.
#
+from unittest.mock import Mock
+
import pytest
-from configs import DOCUMENT_NAME_LIMIT
-from ragflow_sdk import DataSet
-from configs import DEFAULT_PARSER_CONFIG
+from ragflow_sdk import Document
class TestDocumentsUpdated:
@pytest.mark.p1
@pytest.mark.parametrize(
"name, expected_message",
[
("new_name.txt", ""),
- (f"{'a' * (DOCUMENT_NAME_LIMIT - 4)}.txt", ""),
- (0, "AttributeError"),
- (None, "AttributeError"),
- ("", "The extension of file can't be changed"),
- ("ragflow_test_upload_0", "The extension of file can't be changed"),
- ("ragflow_test_upload_1.txt", "Duplicated document name in the same dataset"),
+ (f"{'a' * 251}.txt", ""),
+ (0, "Field: <name> - Message: <Input should be a valid string> - Value: <0>"),
+ (None, "Field: <name> - Message: <Input should be a valid string> - Value: <None>"),
+ ("", "the extension of file can't be changed"),
+ ("ragflow_test_upload_0", "the extension of file can't be changed"),
+ ("ragflow_test_upload_1.txt", "duplicated document name in the same dataset"),
("RAGFLOW_TEST_UPLOAD_1.TXT", ""),
],
)
@@ -40,81 +41,76 @@ class TestDocumentsUpdated:
document = documents[0]
if expected_message:
- if name is None or (isinstance(name, int) and name == 0):
- # Skip tests that don't raise exceptions as expected
- pytest.skip("This test case doesn't consistently raise an exception as expected")
- elif name == "":
- # Check if empty string raises an exception or not
- try:
- document.update({"name": name})
- # If no exception is raised, the test expectation might be wrong
- pytest.skip("Empty string name doesn't raise an exception as expected")
- except Exception as e:
- assert expected_message in str(e), str(e)
- elif name == "ragflow_test_upload_0":
- # Check if this case raises an exception or not
- try:
- document.update({"name": name})
- # If no exception is raised, the test expectation might be wrong
- pytest.skip("Name without extension doesn't raise an exception as expected")
- except Exception as e:
- assert expected_message in str(e), str(e)
- else:
- with pytest.raises(Exception) as exception_info:
- document.update({"name": name})
- assert expected_message in str(exception_info.value), str(exception_info.value)
- else:
- document.update({"name": name})
- docs = dataset.list_documents(id=document.id)
- updated_doc = [doc for doc in docs if doc.id == document.id][0]
- assert updated_doc.name == name, str(updated_doc)
+ with pytest.raises(Exception) as exception_info:
+ document.update({"name": name})
+ assert expected_message in str(exception_info.value), str(exception_info.value)
+ return
+
+ document.update({"name": name})
+ docs = dataset.list_documents(id=document.id)
+ updated_doc = next(doc for doc in docs if doc.id == document.id)
+ assert updated_doc.name == name, str(updated_doc)
@pytest.mark.p2
@pytest.mark.parametrize(
- "meta_fields, expected_message",
+ "meta_fields, expected_error",
[
- ({"test": "test"}, ""),
- ("test", "meta_fields must be a dictionary"),
+ ({"author": "Ada", "year": 2025, "tags": ["one", "two"]}, ""),
+ ({"nested": {"x": 1}}, "The type is not supported"),
+ ({"tags": [{"x": 1}]}, "The type is not supported in list"),
],
)
- def test_meta_fields(self, add_documents, meta_fields, expected_message):
- _, documents = add_documents
+ def test_meta_fields(self, add_documents, meta_fields, expected_error):
+ dataset, documents = add_documents
document = documents[0]
-
- if expected_message:
+ original = dataset.list_documents(id=document.id)[0].meta_fields.to_json()
+ if expected_error:
with pytest.raises(Exception) as exception_info:
document.update({"meta_fields": meta_fields})
- assert expected_message in str(exception_info.value), str(exception_info.value)
- else:
- document.update({"meta_fields": meta_fields})
+ assert expected_error in str(exception_info.value)
+ unchanged = dataset.list_documents(id=document.id)[0].meta_fields.to_json()
+ assert unchanged == original
+ return
- @pytest.mark.p2
- def test_meta_fields_invalid_type_guard_p2(self, add_documents):
- _, documents = add_documents
- document = documents[0]
- with pytest.raises(Exception) as exception_info:
- document.update({"meta_fields": "not-a-dict"})
- assert "meta_fields must be a dictionary" in str(exception_info.value), str(exception_info.value)
+ document.update({"meta_fields": meta_fields})
+ docs = dataset.list_documents(id=document.id)
+ updated_doc = next(doc for doc in docs if doc.id == document.id)
+ assert updated_doc.meta_fields.to_json() == meta_fields
+
+
+def test_meta_fields_rejects_non_dictionary_without_backend():
+ rag = Mock()
+ document = Document(rag, {"dataset_id": "dataset-1", "id": "document-1"})
+
+ with pytest.raises(Exception, match="meta_fields must be a dictionary"):
+ document.update({"meta_fields": "not-a-dict"})
+ rag.patch.assert_not_called()
+
+
+class TestDocumentParserSelection:
@pytest.mark.p2
@pytest.mark.parametrize(
"chunk_method, expected_message",
[
("naive", ""),
- ("manual", ""),
- ("qa", ""),
- ("table", ""),
- ("paper", ""),
+ ("general", ""),
("book", ""),
("laws", ""),
- ("presentation", ""),
- ("picture", ""),
("one", ""),
- ("knowledge_graph", ""),
- ("email", ""),
- ("tag", ""),
+ ("qa", ""),
+ ("table", ""),
+ ("manual", "selected pipeline does not support file type"),
+ ("paper", "selected pipeline does not support file type"),
+ ("presentation", "selected pipeline does not support file type"),
+ ("picture", "selected pipeline does not support file type"),
+ ("email", "selected pipeline does not support file type"),
+ ("knowledge_graph", "selected pipeline does not support file type"),
+ ("tag", "selected pipeline does not support file type"),
("", "`chunk_method` (empty string) is not valid"),
- ("other_chunk_method", "`chunk_method` other_chunk_method doesn't exist"),
+ ("other_chunk_method", "unknown builtin parser_id: other_chunk_method"),
],
)
def test_chunk_method(self, add_documents, chunk_method, expected_message):
@@ -122,125 +118,19 @@ class TestDocumentsUpdated:
document = documents[0]
if expected_message:
- if chunk_method == "":
- # Check if empty string raises an exception or not
- try:
- document.update({"chunk_method": chunk_method})
- # If no exception is raised, skip this test
- pytest.skip("Empty chunk_method doesn't raise an exception as expected")
- except Exception as e:
- assert expected_message in str(e), str(e)
- elif chunk_method == "other_chunk_method":
- with pytest.raises(Exception) as exception_info:
- document.update({"chunk_method": chunk_method})
- assert expected_message in str(exception_info.value), str(exception_info.value)
- else:
- with pytest.raises(Exception) as exception_info:
- document.update({"chunk_method": chunk_method})
- assert expected_message in str(exception_info.value), str(exception_info.value)
- else:
- document.update({"chunk_method": chunk_method})
- docs = dataset.list_documents()
- updated_doc = [doc for doc in docs if doc.id == document.id][0]
- assert updated_doc.chunk_method == chunk_method, str(updated_doc)
+ with pytest.raises(Exception) as exception_info:
+ document.update({"chunk_method": chunk_method})
+ assert expected_message in str(exception_info.value), str(exception_info.value)
+ return
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "payload, expected_message",
- [
- ({"chunk_count": 1}, "can't change `chunk_count`"),
- pytest.param(
- {"create_date": "Fri, 14 Mar 2025 16:53:42 GMT"},
- "The input parameters are invalid",
- marks=pytest.mark.skip(reason="issues/6104"),
- ),
- pytest.param(
- {"create_time": 1},
- "The input parameters are invalid",
- marks=pytest.mark.skip(reason="issues/6104"),
- ),
- pytest.param(
- {"created_by": "ragflow_test"},
- "The input parameters are invalid",
- marks=pytest.mark.skip(reason="issues/6104"),
- ),
- pytest.param(
- {"dataset_id": "ragflow_test"},
- "The input parameters are invalid",
- marks=pytest.mark.skip(reason="issues/6104"),
- ),
- pytest.param(
- {"id": "ragflow_test"},
- "The input parameters are invalid",
- marks=pytest.mark.skip(reason="issues/6104"),
- ),
- pytest.param(
- {"location": "ragflow_test.txt"},
- "The input parameters are invalid",
- marks=pytest.mark.skip(reason="issues/6104"),
- ),
- pytest.param(
- {"process_begin_at": 1},
- "The input parameters are invalid",
- marks=pytest.mark.skip(reason="issues/6104"),
- ),
- pytest.param(
- {"process_duration": 1.0},
- "The input parameters are invalid",
- marks=pytest.mark.skip(reason="issues/6104"),
- ),
- ({"progress": 1.0}, "can't change `progress`"),
- pytest.param(
- {"progress_msg": "ragflow_test"},
- "The input parameters are invalid",
- marks=pytest.mark.skip(reason="issues/6104"),
- ),
- pytest.param(
- {"run": "ragflow_test"},
- "The input parameters are invalid",
- marks=pytest.mark.skip(reason="issues/6104"),
- ),
- pytest.param(
- {"size": 1},
- "The input parameters are invalid",
- marks=pytest.mark.skip(reason="issues/6104"),
- ),
- pytest.param(
- {"source_type": "ragflow_test"},
- "The input parameters are invalid",
- marks=pytest.mark.skip(reason="issues/6104"),
- ),
- pytest.param(
- {"thumbnail": "ragflow_test"},
- "The input parameters are invalid",
- marks=pytest.mark.skip(reason="issues/6104"),
- ),
- ({"token_count": 1}, "can't change `token_count`"),
- pytest.param(
- {"type": "ragflow_test"},
- "The input parameters are invalid",
- marks=pytest.mark.skip(reason="issues/6104"),
- ),
- pytest.param(
- {"update_date": "Fri, 14 Mar 2025 16:33:17 GMT"},
- "The input parameters are invalid",
- marks=pytest.mark.skip(reason="issues/6104"),
- ),
- pytest.param(
- {"update_time": 1},
- "The input parameters are invalid",
- marks=pytest.mark.skip(reason="issues/6104"),
- ),
- ],
- )
- def test_invalid_field(self, add_documents, payload, expected_message):
- _, documents = add_documents
- document = documents[0]
+ document.update({"chunk_method": chunk_method})
+ docs = dataset.list_documents(id=document.id)
+ updated_doc = next(doc for doc in docs if doc.id == document.id)
+ assert updated_doc.chunk_method == chunk_method, str(updated_doc)
- with pytest.raises(Exception) as exception_info:
- document.update(payload)
- assert expected_message in str(exception_info.value), str(exception_info.value)
+class TestImmutableDocumentFields:
@pytest.mark.p2
@pytest.mark.parametrize(
"payload, expected_message",
@@ -250,265 +140,149 @@ class TestDocumentsUpdated:
)
def test_immutable_fields_chunk_count(self, add_documents, payload, expected_message):
_, documents = add_documents
- document = documents[0]
-
with pytest.raises(Exception) as exception_info:
- document.update(payload)
+ documents[0].update(payload)
assert expected_message in str(exception_info.value), str(exception_info.value)
@pytest.mark.p2
@pytest.mark.parametrize(
"payload, expected_message",
[
- ({"token_count": 9999}, "can't change `token_count`"), # Attempt to change immutable field
+ ({"token_count": 9999}, "can't change `token_count`"),
],
)
def test_immutable_fields_token_count(self, add_documents, payload, expected_message):
_, documents = add_documents
- document = documents[0]
-
with pytest.raises(Exception) as exception_info:
- document.update(payload)
+ documents[0].update(payload)
assert expected_message in str(exception_info.value), str(exception_info.value)
- @pytest.mark.p3
+ @pytest.mark.p2
@pytest.mark.parametrize(
"payload, expected_message",
[
- ({"progress": 0.5}, "can't change `progress`"), # Attempt to change immutable field
- ({"progress": 1.5}, "Field: <progress> - Message: <Input should be less than or equal to 1> - Value: <1.5>"), # Attempt to change immutable field
+ ({"progress": 0.5}, "can't change `progress`"),
+ ({"progress": 1.0}, "can't change `progress`"),
+ (
+ {"progress": 1.5},
+ "Field: <progress> - Message: <Input should be less than or equal to 1> - Value: <1.5>",
+ ),
],
)
def test_immutable_fields_progress(self, add_documents, payload, expected_message):
_, documents = add_documents
- document = documents[0]
-
with pytest.raises(Exception) as exception_info:
- document.update(payload)
+ documents[0].update(payload)
assert expected_message in str(exception_info.value), str(exception_info.value)
-DEFAULT_PARSER_CONFIG_FOR_TEST = {
- "layout_recognize": "DeepDOC",
- "chunk_token_num": 512,
- "delimiter": "\n",
- "auto_keywords": 0,
- "auto_questions": 0,
- "html4excel": False,
- "topn_tags": 3,
- "raptor": {
- "use_raptor": True,
- "prompt": "Summarize the paragraphs below without inventing facts or changing numbers.\nOutput exactly two parts in the same language as the source:\n1. First line: a concise title only.\n2. Following lines: a concise summary of the content.\nDo not output labels, Markdown headings, bullet points, or any other commentary.\n\nParagraphs:\n{cluster_content}",
- "max_token": 512,
- "clustering_threshold": 0.3,
- "clustering_ratio": 0.5,
- "max_cluster": 64,
- "random_seed": 0,
- },
- "graphrag": {
- "use_graphrag": True,
- "entity_types": [
- "organization",
- "person",
- "geo",
- "event",
- "category",
+class TestUnknownUpdateFields:
+ @pytest.mark.p3
+ @pytest.mark.parametrize(
+ "payload",
+ [
+ {"create_date": "Fri, 14 Mar 2025 16:53:42 GMT"},
+ {"create_time": 1},
+ {"created_by": "ragflow_test"},
+ {"dataset_id": "ragflow_test"},
+ {"id": "ragflow_test"},
+ {"location": "ragflow_test.txt"},
+ {"process_begin_at": 1},
+ {"process_duration": 1.0},
+ {"progress_msg": "ragflow_test"},
+ {"run": "ragflow_test"},
+ {"size": 1},
+ {"source_type": "ragflow_test"},
+ {"thumbnail": "ragflow_test"},
+ {"type": "ragflow_test"},
+ {"update_date": "Fri, 14 Mar 2025 16:33:17 GMT"},
+ {"update_time": 1},
],
- "method": "light",
- "batch_chunk_token_size": 4096,
- },
-}
+ )
+ def test_unknown_fields_are_ignored_without_mutation(self, add_documents, payload):
+ dataset, documents = add_documents
+ document = documents[0]
+ path = f"/datasets/{dataset.id}/documents"
+ before = dataset.rag.get(path, params={"id": document.id}).json()["data"]["docs"][0]
+
+ document.update(payload)
+ after = dataset.rag.get(path, params={"id": document.id}).json()["data"]["docs"][0]
+ for field in before.keys() - {"update_date", "update_time"}:
+ assert after[field] == before[field], f"{field} changed for {payload}"
+ for field in ("update_date", "update_time"):
+ if field in payload:
+ assert after[field] != payload[field], f"{field} was applied from {payload}"
class TestUpdateDocumentParserConfig:
- @pytest.mark.p3
+ @pytest.mark.p2
@pytest.mark.parametrize(
- "chunk_method, parser_config, expected_message",
+ "parser_config, saved_path, expected_value, expected_message",
[
- ("naive", {}, ""),
- pytest.param(
- "naive",
- DEFAULT_PARSER_CONFIG_FOR_TEST,
- "",
- marks=pytest.mark.skip(reason="DEFAULT_PARSER_CONFIG contains fields not allowed in document update API"),
- ),
- pytest.param(
- "naive",
- {"chunk_token_num": -1},
- "Field: <parser_config.chunk_token_num> - Message: <Input should be greater than or equal to 1> - Value: <-1>",
- ),
- (
- "naive",
- {"chunk_token_num": 0},
- "Input should be greater than or equal to 1",
- ),
- (
- "naive",
- {"chunk_token_num": 100000000},
- "Input should be less than or equal to 2048",
- ),
- (
- "naive",
- {"chunk_token_num": 3.14},
- "Input should be a valid integer",
- ),
- (
- "naive",
- {"chunk_token_num": "1024"},
- "Input should be a valid integer",
- ),
- ("naive", {"layout_recognize": "DeepDOC"}, ""),
- ("naive", {"layout_recognize": "Naive"}, ""),
- ("naive", {"html4excel": True}, ""),
- ("naive", {"html4excel": False}, ""),
- (
- "naive",
- {"html4excel": 1},
- "Input should be a valid boolean",
- ),
- ("naive", {"delimiter": ""}, "String should have at least 1 character"),
- ("naive", {"delimiter": "`##`"}, ""),
- (
- "naive",
- {"delimiter": 1},
- "Input should be a valid string",
- ),
- (
- "naive",
- {"task_page_size": -1},
- "Input should be greater than or equal to 1",
- ),
+ ({}, None, None, ""),
(
- "naive",
- {"task_page_size": 0},
- "Input should be greater than or equal to 1",
- ),
- pytest.param(
- "naive",
- {"task_page_size": 100000000},
+ {"GeneralChunker:SixApplesFall": {"chunk_token_size": 256}},
+ ("GeneralChunker:SixApplesFall", "chunk_token_size"),
+ 256,
"",
),
(
- "naive",
- {"task_page_size": 3.14},
- "Input should be a valid integer",
- ),
- (
- "naive",
- {"task_page_size": "1024"},
- "Input should be a valid integer",
- ),
- (
- "naive",
- {
- "raptor": {
- "use_raptor": True,
- "prompt": "Summarize the paragraphs below without inventing facts or changing numbers.\nOutput exactly two parts in the same language as the source:\n1. First line: a concise title only.\n2. Following lines: a concise summary of the content.\nDo not output labels, Markdown headings, bullet points, or any other commentary.\n\nParagraphs:\n{cluster_content}",
- "max_token": 512,
- "clustering_threshold": 0.3,
- "clustering_ratio": 0.5,
- "max_cluster": 64,
- "random_seed": 0,
- }
- },
+ {"GeneralChunker:SixApplesFall": {"delimiters": ["!"]}},
+ ("GeneralChunker:SixApplesFall", "delimiters"),
+ ["!"],
"",
),
- ("naive", {"raptor": {"use_raptor": False}}, ""),
(
- "naive",
- {"invalid_key": "invalid_value"},
+ {"Extractor:AutoExtractDefault": {"keywords": {"top_n": 5}}},
+ ("Extractor:AutoExtractDefault", "keywords", "top_n"),
+ 5,
"",
),
(
- "naive",
- {"auto_keywords": -1},
- "Input should be greater than or equal to 0",
- ),
- pytest.param(
- "naive",
- {"auto_keywords": 32},
+ {"Extractor:AutoExtractDefault": {"questions": {"top_n": 5}}},
+ ("Extractor:AutoExtractDefault", "questions", "top_n"),
+ 5,
"",
),
(
- "naive",
- {"auto_keywords": 3.14},
- "Input should be a valid integer",
- ),
- (
- "naive",
- {"auto_keywords": "1024"},
- "Input should be a valid integer",
- ),
- (
- "naive",
- {"auto_questions": -1},
- "Input should be greater than or equal to 0",
- ),
- pytest.param(
- "naive",
- {"auto_questions": 10},
- "",
- ),
- (
- "naive",
- {"auto_questions": 3.14},
- "Input should be a valid integer",
- ),
- (
- "naive",
- {"auto_questions": "1024"},
- "Input should be a valid integer",
- ),
- (
- "naive",
- {"topn_tags": -1},
- "Input should be greater than or equal to 1",
- ),
- pytest.param(
- "naive",
- {"topn_tags": 10},
+ {"Extractor:AutoExtractDefault": {"tags": {"top_n": 3}}},
+ ("Extractor:AutoExtractDefault", "tags", "top_n"),
+ 3,
"",
),
(
- "naive",
- {"topn_tags": 3.14},
- "Input should be a valid integer",
- ),
- (
- "naive",
- {"topn_tags": "1024"},
- "Input should be a valid integer",
+ {"GeneralChunker:SixApplesFall": {"delimiters": ["!"] * 33000}},
+ None,
+ None,
+ "exceeds size limit",
),
+ ([], None, None, "input should be a valid dictionary"),
+ ({"Parser:HipSignsRhyme": {"pdf": {"pages": [[0, 5]]}}}, None, None, "page"),
],
)
- def test_parser_config(self, client, add_documents, chunk_method, parser_config, expected_message):
+ def test_parser_config(self, add_documents, parser_config, saved_path, expected_value, expected_message):
dataset, documents = add_documents
document = documents[0]
- from operator import attrgetter
-
- update_data = {"chunk_method": chunk_method, "parser_config": parser_config}
+ original_config = dataset.list_documents(id=document.id)[0].parser_config.to_json()
if expected_message:
with pytest.raises(Exception) as exception_info:
- document.update(update_data)
- assert expected_message in str(exception_info.value), str(exception_info.value)
+ document.update({"parser_config": parser_config})
+ assert expected_message in str(exception_info.value).lower()
+ unchanged = dataset.list_documents(id=document.id)[0].parser_config.to_json()
+ assert unchanged == original_config
+ return
+
+ document.update({"parser_config": parser_config})
+ docs = dataset.list_documents(id=document.id)
+ updated_doc = next(doc for doc in docs if doc.id == document.id)
+ saved = updated_doc.parser_config.to_json()
+ if saved_path:
+ value = saved
+ for key in saved_path:
+ value = value[key]
+ assert value == expected_value
else:
- document.update(update_data)
- docs = dataset.list_documents(id=document.id)
- updated_doc = [doc for doc in docs if doc.id == document.id][0]
- if parser_config:
- for k, v in parser_config.items():
- if k in {"graphrag", "raptor"}:
- assert not hasattr(updated_doc.parser_config, k), str(updated_doc)
- continue
- if isinstance(v, dict):
- for kk, vv in v.items():
- assert attrgetter(f"{k}.{kk}")(updated_doc.parser_config) == vv, str(updated_doc)
- else:
- assert attrgetter(k)(updated_doc.parser_config) == v, str(updated_doc)
- else:
- expected_config = DataSet.ParserConfig(
- client,
- DEFAULT_PARSER_CONFIG,
- )
- assert str(updated_doc.parser_config) == str(expected_config), str(updated_doc)
+ assert saved == original_config
diff --git a/test/testcases/test_sdk_api/test_file_management_within_dataset/test_upload_documents.py b/test/testcases/test_sdk_api/test_file_management_within_dataset/test_upload_documents.py
index 4ceb31624c..7aef4f5ed3 100644
--- a/test/testcases/test_sdk_api/test_file_management_within_dataset/test_upload_documents.py
+++ b/test/testcases/test_sdk_api/test_file_management_within_dataset/test_upload_documents.py
@@ -1,5 +1,5 @@
#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
+# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@@ -15,24 +15,34 @@
#
import string
from concurrent.futures import ThreadPoolExecutor, as_completed
+from pathlib import Path
import pytest
-from configs import DOCUMENT_NAME_LIMIT
-from utils.file_utils import create_txt_file
+DOCUMENT_NAME_MAX_BYTES = 255
+TEXT_FILE_CONTENT = "This is the content of a test TXT file."
+
+def _create_text_file(path: Path) -> Path:
+ path.write_text(TEXT_FILE_CONTENT, encoding="utf-8")
+ return path
+
+
class TestDocumentsUpload:
@pytest.mark.p1
def test_valid_single_upload(self, add_dataset_func, tmp_path):
dataset = add_dataset_func
- fp = create_txt_file(tmp_path / "ragflow_test.txt")
+ fp = _create_text_file(tmp_path / "ragflow_test.txt")
with fp.open("rb") as f:
blob = f.read()
documents = dataset.upload_documents([{"display_name": fp.name, "blob": blob}])
- for document in documents:
- assert document.dataset_id == dataset.id, str(document)
- assert document.name == fp.name, str(document)
+ assert len(documents) == 1, str(documents)
+ document = documents[0]
+ assert document.dataset_id == dataset.id, str(document)
+ assert document.name == fp.name, str(document)
+ assert [item.id for item in dataset.list_documents(id=document.id)] == [document.id]
@pytest.mark.p1
@pytest.mark.parametrize(
@@ -59,9 +69,10 @@ class TestDocumentsUpload:
blob = f.read()
documents = dataset.upload_documents([{"display_name": fp.name, "blob": blob}])
- for document in documents:
- assert document.dataset_id == dataset.id, str(document)
- assert document.name == fp.name, str(document)
+ assert len(documents) == 1, str(documents)
+ document = documents[0]
+ assert document.dataset_id == dataset.id, str(document)
+ assert document.name == fp.name, str(document)
@pytest.mark.p3
@pytest.mark.parametrize(
@@ -97,38 +108,73 @@ class TestDocumentsUpload:
blob = f.read()
documents = dataset.upload_documents([{"display_name": fp.name, "blob": blob}])
- for document in documents:
- assert document.size == 0, str(document)
+ assert len(documents) == 1, str(documents)
+ document = documents[0]
+ assert document.size == 0, str(document)
+ assert [item.id for item in dataset.list_documents(id=document.id)] == [document.id]
@pytest.mark.p3
def test_filename_empty(self, add_dataset_func, tmp_path):
dataset = add_dataset_func
- fp = create_txt_file(tmp_path / "ragflow_test.txt")
+ fp = _create_text_file(tmp_path / "ragflow_test.txt")
with fp.open("rb") as f:
blob = f.read()
with pytest.raises(Exception) as exception_info:
dataset.upload_documents([{"display_name": "", "blob": blob}])
- assert str(exception_info.value) == "No file selected!", str(exception_info.value)
+ assert str(exception_info.value) == "No file part!", str(exception_info.value)
@pytest.mark.p2
def test_filename_max_length(self, add_dataset_func, tmp_path):
dataset = add_dataset_func
- fp = create_txt_file(tmp_path / f"{'a' * (DOCUMENT_NAME_LIMIT - 4)}.txt")
+ fp = _create_text_file(tmp_path / f"{'a' * (DOCUMENT_NAME_MAX_BYTES - len('.txt'))}.txt")
with fp.open("rb") as f:
blob = f.read()
documents = dataset.upload_documents([{"display_name": fp.name, "blob": blob}])
- for document in documents:
- assert document.dataset_id == dataset.id, str(document)
- assert document.name == fp.name, str(document)
+ assert len(documents) == 1, str(documents)
+ document = documents[0]
+ assert document.dataset_id == dataset.id, str(document)
+ assert document.name == fp.name, str(document)
+
+ @pytest.mark.p2
+ @pytest.mark.parametrize(
+ "display_name",
+ [
+ "a" * 252 + ".txt",
+ "界" * 84 + ".txt",
+ ],
+ )
+ def test_filename_over_limit(self, add_dataset_func, display_name):
+ assert len(display_name.encode("utf-8")) == DOCUMENT_NAME_MAX_BYTES + 1
+
+ with pytest.raises(Exception, match="File name must be 255 bytes or less\\."):
+ add_dataset_func.upload_documents([{"display_name": display_name, "blob": b"content"}])
+
+ @pytest.mark.p2
+ def test_partial_upload_persists_successful_file(self, add_dataset_func):
+ dataset = add_dataset_func
+ content = b"valid document in a mixed upload"
+
+ with pytest.raises(Exception, match="rejected\\.exe: This type of file has not been supported yet!"):
+ dataset.upload_documents(
+ [
+ {"display_name": "accepted.txt", "blob": content},
+ {"display_name": "rejected.exe", "blob": b"not an executable"},
+ ]
+ )
+
+ documents = dataset.list_documents(page_size=10)
+ assert len(documents) == 1
+ assert documents[0].name == "accepted.txt"
+ assert documents[0].download() == content
@pytest.mark.p2
def test_duplicate_files(self, add_dataset_func, tmp_path):
dataset = add_dataset_func
- fp = create_txt_file(tmp_path / "ragflow_test.txt")
+ fp = _create_text_file(tmp_path / "ragflow_test.txt")
with fp.open("rb") as f:
blob = f.read()
@@ -144,7 +190,7 @@ class TestDocumentsUpload:
@pytest.mark.p3
def test_same_file_repeat(self, add_dataset_func, tmp_path):
dataset = add_dataset_func
- fp = create_txt_file(tmp_path / "ragflow_test.txt")
+ fp = _create_text_file(tmp_path / "ragflow_test.txt")
with fp.open("rb") as f:
blob = f.read()
@@ -176,26 +222,26 @@ class TestDocumentsUpload:
assert document.name == fp.name, str(document)
@pytest.mark.p3
- def test_multiple_files(self, client, add_dataset_func, tmp_path):
+ def test_multiple_files(self, add_dataset_func, tmp_path):
dataset = add_dataset_func
expected_document_count = 20
document_infos = []
for i in range(expected_document_count):
- fp = create_txt_file(tmp_path / f"ragflow_test_upload_{i}.txt")
+ fp = _create_text_file(tmp_path / f"ragflow_test_upload_{i}.txt")
with fp.open("rb") as f:
blob = f.read()
document_infos.append({"display_name": fp.name, "blob": blob})
documents = dataset.upload_documents(document_infos)
assert len(documents) == expected_document_count, str(documents)
- retrieved_dataset = client.get_dataset(name=dataset.name)
- assert retrieved_dataset.document_count == expected_document_count, str(retrieved_dataset)
+ retrieved_documents = dataset.list_documents(page_size=expected_document_count)
+ assert {document.id for document in retrieved_documents} == {document.id for document in documents}
@pytest.mark.p3
- def test_concurrent_upload(self, client, add_dataset_func, tmp_path):
+ def test_concurrent_upload(self, add_dataset_func, tmp_path):
dataset = add_dataset_func
count = 20
- fps = [create_txt_file(tmp_path / f"ragflow_test_{i}.txt") for i in range(count)]
+ fps = [_create_text_file(tmp_path / f"ragflow_test_{i}.txt") for i in range(count)]
def upload_file(fp):
with fp.open("rb") as f:
@@ -204,8 +250,12 @@ class TestDocumentsUpload:
with ThreadPoolExecutor(max_workers=5) as executor:
futures = [executor.submit(upload_file, fp) for fp in fps]
- responses = list(as_completed(futures))
- assert len(responses) == count, responses
-
- retrieved_dataset = client.get_dataset(name=dataset.name)
- assert retrieved_dataset.document_count == count, str(retrieved_dataset)
+ uploaded = []
+ for future in as_completed(futures):
+ result = future.result()
+ assert len(result) == 1, result
+ uploaded.extend(result)
+
+ assert len(uploaded) == count
+ retrieved_documents = dataset.list_documents(page_size=count)
+ assert {document.id for document in retrieved_documents} == {document.id for document in uploaded}