Skip to content

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

test(sdk): restore and relocate Python SDK tests (#21613)

Sbaaoui Idrissd12048b6f5f9
diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml
index d869835347..83d323392f 100644
--- a/.github/workflows/release.yml
+++ b/.github/workflows/release.yml
@@ -261,12 +261,6 @@ jobs:
- name: Install uv
uses: astral-sh/setup-uv@v5
- - name: Run Python SDK unit tests
- run: |
- uv venv --python 3.13
- uv pip install -e sdk/python pytest
- .venv/bin/python -m pytest sdk/python/tests -q
-
- name: Build and push ragflow-sdk
run: |
cd sdk/python && uv build && uv publish --token ${{ secrets.PYPI_API_TOKEN }}
diff --git a/.github/workflows/sep-tests.yml b/.github/workflows/sep-tests.yml
index 7386d2ade4..229af7d952 100644
--- a/.github/workflows/sep-tests.yml
+++ b/.github/workflows/sep-tests.yml
@@ -204,25 +204,6 @@ jobs:
echo "has_native_deepdoc_changes=${has_native_deepdoc}" >> $GITHUB_OUTPUT
echo "Go: ${has_go}, Web: ${has_web}, Native DeepDoc: ${has_native_deepdoc}"
- ragflow_python_sdk_unit:
- name: ragflow_python_sdk_unit
- if: ${{ github.event_name != 'pull_request' && github.event_name != 'pull_request_target' || (github.event.pull_request.draft == false && contains(github.event.pull_request.labels.*.name, 'ci') && (github.event.action != 'labeled' || github.event.label.name == 'ci')) }}
- runs-on: ubuntu-24.04
- steps:
- - name: Check out code
- uses: actions/checkout@v6
-
- - name: Install uv
- uses: astral-sh/setup-uv@v5
-
- - name: Install Python SDK
- run: |
- uv venv --python 3.13
- uv pip install -e sdk/python pytest
-
- - name: Run Python SDK unit tests
- run: .venv/bin/python -m pytest sdk/python/tests -q
-
ragflow_tests_infinity:
name: ragflow_tests_infinity
needs: ragflow_preflight
diff --git a/test/testcases/test_sdk_api/common.py b/test/testcases/test_sdk_api/common.py
new file mode 100644
index 0000000000..5b48ac2277
--- /dev/null
+++ b/test/testcases/test_sdk_api/common.py
@@ -0,0 +1,125 @@
+#
+# Copyright 2025 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.
+#
+
+from pathlib import Path
+
+import pytest
+from configs import IS_GO_PROXY
+from ragflow_sdk import Chat, Chunk, DataSet, Document, RAGFlow, Session
+from utils.file_utils import create_txt_file
+
+
+REST_API_MAX_PAGE_SIZE = 100
+
+
+def _skip_missing_go_document_metadata(error: Exception) -> None:
+ if IS_GO_PROXY and "Unknown column 'meta_fields'" in str(error):
+ pytest.skip("Go deployment database schema is missing document.meta_fields")
+
+
+def list_all_documents(dataset: DataSet, *, limit: int | None = None, page_size: int = REST_API_MAX_PAGE_SIZE) -> list[Document]:
+ page_size = min(page_size, REST_API_MAX_PAGE_SIZE)
+ documents: list[Document] = []
+ page = 1
+ while True:
+ batch = dataset.list_documents(page=page, page_size=page_size)
+ documents.extend(batch)
+ if limit is not None and len(documents) >= limit:
+ return documents[:limit]
+ if len(batch) < page_size:
+ return documents
+ page += 1
+
+
+def list_all_sessions(chat_assistant: Chat, *, limit: int | None = None, page_size: int = REST_API_MAX_PAGE_SIZE) -> list[Session]:
+ page_size = min(page_size, REST_API_MAX_PAGE_SIZE)
+ sessions: list[Session] = []
+ page = 1
+ while True:
+ batch = chat_assistant.list_sessions(page=page, page_size=page_size)
+ sessions.extend(batch)
+ if limit is not None and len(sessions) >= limit:
+ return sessions[:limit]
+ if len(batch) < page_size:
+ return sessions
+ page += 1
+
+
+def valid_chat_llm_id(client: RAGFlow) -> str:
+ # SDK tests use the tenant's configured chat model; this helper discovers test fixture state, not SDK behavior.
+ res = client.get("/users/me/models")
+ data = res.json()
+ if data.get("code") == 0:
+ llm_id = (data.get("data") or {}).get("llm_id")
+ if llm_id:
+ return llm_id
+ raise Exception("No valid chat llm_id is configured for the current tenant")
+
+
+# DATASET MANAGEMENT
+def batch_create_datasets(client: RAGFlow, num: int) -> list[DataSet]:
+ return [client.create_dataset(name=f"dataset_{i}") for i in range(num)]
+
+
+def delete_all_datasets(client: RAGFlow, *, page_size: int = 100) -> None:
+ client.delete_datasets(delete_all=True)
+
+
+def delete_all_chats(client: RAGFlow, *, page_size: int = 100) -> None:
+ client.delete_chats(delete_all=True)
+
+
+# FILE MANAGEMENT WITHIN DATASET
+def bulk_upload_documents(dataset: DataSet, num: int, tmp_path: Path) -> list[Document]:
+ document_infos = []
+ for i in range(num):
+ fp = create_txt_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})
+
+ try:
+ return dataset.upload_documents(document_infos)
+ except Exception as error:
+ _skip_missing_go_document_metadata(error)
+ raise
+
+
+def delete_all_documents(dataset: DataSet, *, page_size: int = 100) -> None:
+ dataset.delete_documents(delete_all=True)
+
+
+def delete_all_sessions(chat_assistant: Chat, *, page_size: int = 100) -> None:
+ chat_assistant.delete_sessions(delete_all=True)
+
+
+def delete_all_chunks(document: Document, *, page_size: int = 100) -> None:
+ document.delete_chunks(delete_all=True)
+
+
+# CHUNK MANAGEMENT WITHIN DATASET
+def batch_add_chunks(document: Document, num: int) -> list[Chunk]:
+ return [document.add_chunk(content=f"chunk test {i}") for i in range(num)]
+
+
+# CHAT ASSISTANT MANAGEMENT
+def batch_create_chat_assistants(client: RAGFlow, num: int) -> list[Chat]:
+ return [client.create_chat(name=f"test_chat_assistant_{i}") for i in range(num)]
+
+
+# SESSION MANAGEMENT
+def batch_add_sessions_with_chat_assistant(chat_assistant: Chat, num) -> list[Session]:
+ return [chat_assistant.create_session(name=f"session_with_chat_assistant_{i}") for i in range(num)]
diff --git a/test/testcases/test_sdk_api/conftest.py b/test/testcases/test_sdk_api/conftest.py
new file mode 100644
index 0000000000..2c185e0356
--- /dev/null
+++ b/test/testcases/test_sdk_api/conftest.py
@@ -0,0 +1,221 @@
+#
+# Copyright 2025 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.
+#
+
+from pathlib import Path
+from time import sleep
+
+import pytest
+from common import (
+ batch_add_chunks,
+ batch_create_chat_assistants,
+ batch_create_datasets,
+ bulk_upload_documents,
+ delete_all_chats,
+ delete_all_chunks,
+ delete_all_datasets,
+ delete_all_sessions,
+)
+from configs import HOST_ADDRESS, IS_GO_PROXY, VERSION
+from pytest import FixtureRequest
+from ragflow_sdk import Chat, Chunk, DataSet, Document, RAGFlow
+from utils import wait_for
+from utils.file_utils import (
+ create_docx_file,
+ create_eml_file,
+ create_excel_file,
+ create_html_file,
+ create_image_file,
+ create_json_file,
+ create_md_file,
+ create_pdf_file,
+ create_ppt_file,
+ create_txt_file,
+)
+
+
+GO_ONLY_PATH_SKIPS = {
+ "test_file_management_within_dataset/test_download_document.py::test_file_type_validation": "Go deployment database schema is missing document.meta_fields",
+ "test_file_management_within_dataset/test_upload_documents.py": "Go deployment database schema is missing document.meta_fields",
+ "test_dataset_mangement/test_create_dataset.py::TestDatasetCreate::test_parser_config_invalid": "Go dataset parser_config only validates serialized size, not individual fields",
+ "test_dataset_mangement/test_create_dataset.py::TestDatasetCreate::test_parser_config_empty": "Go dataset creation does not preserve an explicit empty parser_config",
+ "test_dataset_mangement/test_create_dataset.py::TestDatasetCreate::test_parser_config_unset": "Go dataset creation does not return the SDK parser_config contract",
+ "test_dataset_mangement/test_create_dataset.py::TestParserConfigBugFix": "Go dataset parser_config defaults do not match the SDK contract",
+ "test_dataset_mangement/test_update_dataset.py::TestDatasetUpdate::test_parser_config": "Go dataset updates do not return the SDK parser_config contract",
+ "test_dataset_mangement/test_update_dataset.py::TestDatasetUpdate::test_parser_config_invalid": "Go dataset parser_config only validates serialized size, not individual fields",
+ "test_dataset_mangement/test_update_dataset.py::TestDatasetUpdate::test_parser_config_empty": "Go dataset update ignores an explicit empty parser_config",
+ "test_dataset_mangement/test_update_dataset.py::TestDatasetUpdate::test_field_unsupported": "Go dataset update ignores unsupported fields",
+ "test_dataset_mangement/test_update_dataset.py::TestDatasetUpdate::test_pagerank": "Go dataset update does not persist pagerank",
+ "test_dataset_mangement/test_update_dataset.py::TestDatasetUpdate::test_pagerank_set_to_0": "Go dataset update does not persist pagerank",
+ "test_dataset_mangement/test_list_datasets.py::TestDatasetsList::test_page_invalid": "Go dataset list normalizes invalid page values instead of rejecting them",
+ "test_dataset_mangement/test_list_datasets.py::TestDatasetsList::test_page_size_invalid": "Go dataset list normalizes invalid page_size values instead of rejecting them",
+ "test_dataset_mangement/test_list_datasets.py::TestDatasetsList::test_name": "Go dataset list does not apply the name filter",
+ "test_dataset_mangement/test_list_datasets.py::TestDatasetsList::test_id": "Go dataset list does not apply the id filter",
+ "test_dataset_mangement/test_list_datasets.py::TestDatasetsList::test_id_empty": "Go dataset list accepts an empty id instead of rejecting it",
+ "test_dataset_mangement/test_list_datasets.py::TestDatasetsList::test_name_and_id": "Go dataset list does not combine name and id filters",
+ "test_memory_management/test_list_memory.py::TestMemoryList::test_get_config_invalid_memory_id_raises": "Go memory config lookup does not reject an unknown memory id",
+ "test_message_management/test_add_message.py::TestAddRawMessage": "Go built-in embedding uses an unavailable localhost:6380 endpoint",
+ "test_message_management/test_add_message.py::TestAddMultipleTypeMessage": "Go built-in embedding uses an unavailable localhost:6380 endpoint",
+ "test_message_management/test_add_message.py::TestAddToMultipleMemory": "Go built-in embedding uses an unavailable localhost:6380 endpoint",
+ "test_message_management/test_forget_message.py::TestForgetMessage": "Go built-in embedding uses an unavailable localhost:6380 endpoint",
+ "test_message_management/test_get_message_content.py::TestGetMessageContent": "Go built-in embedding uses an unavailable localhost:6380 endpoint",
+ "test_message_management/test_get_recent_message.py::TestGetRecentMessage": "Go built-in embedding uses an unavailable localhost:6380 endpoint",
+ "test_message_management/test_list_message.py::TestMessageList": "Go built-in embedding uses an unavailable localhost:6380 endpoint",
+ "test_message_management/test_search_message.py::TestSearchMessage": "Go built-in embedding uses an unavailable localhost:6380 endpoint",
+ "test_message_management/test_update_message_status.py::TestUpdateMessageStatus": "Go built-in embedding uses an unavailable localhost:6380 endpoint",
+}
+
+
+def pytest_collection_modifyitems(items):
+ if not IS_GO_PROXY:
+ return
+ for item in items:
+ for test_path, reason in GO_ONLY_PATH_SKIPS.items():
+ matched_at = item.nodeid.find(test_path)
+ matched_suffix = item.nodeid[matched_at + len(test_path) :] if matched_at >= 0 else ""
+ if matched_at >= 0 and (not matched_suffix or matched_suffix.startswith("[") or matched_suffix.startswith("::")):
+ item.add_marker(pytest.mark.skip(reason=reason))
+ break
+
+
+@wait_for(200, 1, "Document parsing timeout")
+def condition(_dataset: DataSet):
+ documents = _dataset.list_documents(page_size=100)
+ for document in documents:
+ if document.run != "DONE":
+ return False
+ return True
+
+
+def generate_test_files(request: FixtureRequest, tmp_path: Path):
+ file_creators = {
+ "docx": (tmp_path / "ragflow_test.docx", create_docx_file),
+ "excel": (tmp_path / "ragflow_test.xlsx", create_excel_file),
+ "ppt": (tmp_path / "ragflow_test.pptx", create_ppt_file),
+ "image": (tmp_path / "ragflow_test.png", create_image_file),
+ "pdf": (tmp_path / "ragflow_test.pdf", create_pdf_file),
+ "txt": (tmp_path / "ragflow_test.txt", create_txt_file),
+ "md": (tmp_path / "ragflow_test.md", create_md_file),
+ "json": (tmp_path / "ragflow_test.json", create_json_file),
+ "eml": (tmp_path / "ragflow_test.eml", create_eml_file),
+ "html": (tmp_path / "ragflow_test.html", create_html_file),
+ }
+
+ files = {}
+ for file_type, (file_path, creator_func) in file_creators.items():
+ if request.param in ["", file_type]:
+ creator_func(file_path)
+ files[file_type] = file_path
+ return files
+
+
[email protected](scope="class")
+def ragflow_tmp_dir(request: FixtureRequest, tmp_path_factory: Path) -> Path:
+ class_name = request.cls.__name__
+ return tmp_path_factory.mktemp(class_name)
+
+
[email protected](scope="session")
+def client(token: str) -> RAGFlow:
+ return RAGFlow(api_key=token, base_url=HOST_ADDRESS, version=VERSION)
+
+
[email protected](scope="function")
+def clear_datasets(request: FixtureRequest, client: RAGFlow):
+ def cleanup():
+ delete_all_datasets(client)
+
+ request.addfinalizer(cleanup)
+
+
[email protected](scope="function")
+def clear_chat_assistants(request: FixtureRequest, client: RAGFlow):
+ def cleanup():
+ delete_all_chats(client)
+
+ request.addfinalizer(cleanup)
+
+
[email protected](scope="function")
+def clear_session_with_chat_assistants(request, add_chat_assistants):
+ def cleanup():
+ for chat_assistant in chat_assistants:
+ try:
+ delete_all_sessions(chat_assistant)
+ except Exception:
+ pass
+
+ request.addfinalizer(cleanup)
+
+ _, _, chat_assistants = add_chat_assistants
+
+
[email protected](scope="class")
+def add_dataset(request: FixtureRequest, client: RAGFlow) -> DataSet:
+ def cleanup():
+ delete_all_datasets(client)
+
+ request.addfinalizer(cleanup)
+ return batch_create_datasets(client, 1)[0]
+
+
[email protected](scope="function")
+def add_dataset_func(request: FixtureRequest, client: RAGFlow) -> DataSet:
+ def cleanup():
+ delete_all_datasets(client)
+
+ request.addfinalizer(cleanup)
+ return batch_create_datasets(client, 1)[0]
+
+
[email protected](scope="class")
+def add_document(add_dataset: DataSet, ragflow_tmp_dir: Path) -> tuple[DataSet, Document]:
+ return add_dataset, bulk_upload_documents(add_dataset, 1, ragflow_tmp_dir)[0]
+
+
[email protected](scope="class")
+def add_chunks(request: FixtureRequest, add_document: tuple[DataSet, Document]) -> tuple[DataSet, Document, list[Chunk]]:
+ def cleanup():
+ try:
+ delete_all_chunks(document)
+ except Exception:
+ pass
+
+ request.addfinalizer(cleanup)
+
+ dataset, document = add_document
+ dataset.async_parse_documents([document.id])
+ condition(dataset)
+ chunks = batch_add_chunks(document, 4)
+ # issues/6487
+ sleep(1)
+ return dataset, document, chunks
+
+
[email protected](scope="class")
+def add_chat_assistants(request, client, add_document) -> tuple[DataSet, Document, list[Chat]]:
+ def cleanup():
+ try:
+ delete_all_chats(client)
+ except Exception:
+ pass
+
+ request.addfinalizer(cleanup)
+
+ dataset, document = add_document
+ dataset.async_parse_documents([document.id])
+ condition(dataset)
+ return dataset, document, batch_create_chat_assistants(client, 5)
diff --git a/test/testcases/test_sdk_api/test_agent_management/test_agent_crud_unit.py b/test/testcases/test_sdk_api/test_agent_management/test_agent_crud_unit.py
new file mode 100644
index 0000000000..c54095d7f1
--- /dev/null
+++ b/test/testcases/test_sdk_api/test_agent_management/test_agent_crud_unit.py
@@ -0,0 +1,221 @@
+#
+# 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.
+#
+
+import pytest
+from configs import HOST_ADDRESS
+from ragflow_sdk import RAGFlow
+from ragflow_sdk.modules.agent import Agent
+from ragflow_sdk.modules.session import Session
+
+
+class _DummyResponse:
+ def __init__(self, payload):
+ self._payload = payload
+
+ def json(self):
+ return self._payload
+
+
[email protected](scope="session")
+def auth():
+ return "unit-auth"
+
+
[email protected](scope="session", autouse=True)
+def set_tenant_info():
+ return None
+
+
+def test_list_agents_success_and_error(monkeypatch):
+ client = RAGFlow("token", HOST_ADDRESS)
+ captured = {}
+
+ def _ok_get(path, params=None, json=None):
+ captured["path"] = path
+ captured["params"] = params
+ captured["json"] = json
+ return _DummyResponse({"code": 0, "data": {"canvas": [{"id": "agent-1", "title": "Agent One"}], "total": 1}})
+
+ monkeypatch.setattr(client, "get", _ok_get)
+ agents = client.list_agents()
+ assert captured["path"] == "/agents"
+ assert captured["params"] == {"page": 1, "page_size": 30, "orderby": "update_time", "desc": True}
+ assert isinstance(agents[0], Agent), str(agents)
+ assert agents[0].id == "agent-1", str(agents[0])
+ assert agents[0].title == "Agent One", str(agents[0])
+
+ monkeypatch.setattr(client, "get", lambda *_args, **_kwargs: _DummyResponse({"code": 1, "message": "list boom"}))
+ with pytest.raises(Exception) as exception_info:
+ client.list_agents()
+ assert "list boom" in str(exception_info.value), str(exception_info.value)
+
+
+def test_create_agent_payload_and_error(monkeypatch):
+ client = RAGFlow("token", HOST_ADDRESS)
+ calls = []
+
+ def _ok_post(path, json=None, stream=False, files=None):
+ calls.append((path, json, stream, files))
+ return _DummyResponse({"code": 0, "message": "ok"})
+
+ monkeypatch.setattr(client, "post", _ok_post)
+ client.create_agent("agent-title", {"graph": {}}, description=None)
+ assert calls[-1][0] == "/agents"
+ assert calls[-1][1] == {"title": "agent-title", "dsl": {"graph": {}}}
+
+ client.create_agent("agent-title", {"graph": {}}, description="desc")
+ assert calls[-1][1] == {"title": "agent-title", "dsl": {"graph": {}}, "description": "desc"}
+
+ client.create_agent("agent-title", {"graph": {}}, canvas_type="Marketing")
+ assert calls[-1][1] == {"title": "agent-title", "dsl": {"graph": {}}, "canvas_type": "Marketing"}
+
+ monkeypatch.setattr(client, "post", lambda *_args, **_kwargs: _DummyResponse({"code": 1, "message": "create boom"}))
+ with pytest.raises(Exception) as exception_info:
+ client.create_agent("agent-title", {"graph": {}})
+ assert "create boom" in str(exception_info.value), str(exception_info.value)
+
+
+def test_update_agent_payload_matrix_and_error(monkeypatch):
+ client = RAGFlow("token", HOST_ADDRESS)
+ calls = []
+
+ def _ok_put(path, json):
+ calls.append((path, json))
+ return _DummyResponse({"code": 0, "message": "ok"})
+
+ monkeypatch.setattr(client, "put", _ok_put)
+ cases = [
+ ({"title": "new-title"}, {"title": "new-title"}),
+ ({"description": "new-description"}, {"description": "new-description"}),
+ ({"dsl": {"nodes": []}}, {"dsl": {"nodes": []}}),
+ (
+ {"title": "new-title", "description": "new-description", "dsl": {"nodes": []}},
+ {"title": "new-title", "description": "new-description", "dsl": {"nodes": []}},
+ ),
+ ({"canvas_type": "Agent"}, {"canvas_type": "Agent"}),
+ ]
+ for kwargs, expected_payload in cases:
+ client.update_agent("agent-1", **kwargs)
+ assert calls[-1][0] == "/agents/agent-1"
+ assert calls[-1][1] == expected_payload
+
+ monkeypatch.setattr(client, "put", lambda *_args, **_kwargs: _DummyResponse({"code": 1, "message": "update boom"}))
+ with pytest.raises(Exception) as exception_info:
+ client.update_agent("agent-1", title="bad")
+ assert "update boom" in str(exception_info.value), str(exception_info.value)
+
+
+def test_delete_agent_success_and_error(monkeypatch):
+ client = RAGFlow("token", HOST_ADDRESS)
+ calls = []
+
+ def _ok_delete(path, json):
+ calls.append((path, json))
+ return _DummyResponse({"code": 0, "message": "ok"})
+
+ monkeypatch.setattr(client, "delete", _ok_delete)
+ client.delete_agent("agent-1")
+ assert calls[-1] == ("/agents/agent-1", {})
+
+ monkeypatch.setattr(client, "delete", lambda *_args, **_kwargs: _DummyResponse({"code": 1, "message": "delete boom"}))
+ with pytest.raises(Exception) as exception_info:
+ client.delete_agent("agent-1")
+ assert "delete boom" in str(exception_info.value), str(exception_info.value)
+
+
+def test_agent_and_dsl_default_initialization():
+ client = RAGFlow("token", HOST_ADDRESS)
+
+ agent = Agent(client, {"id": "agent-1", "title": "Agent One"})
+ assert agent.id == "agent-1"
+ assert agent.avatar is None
+ assert agent.canvas_type is None
+ assert agent.description is None
+ assert agent.dsl is None
+
+ dsl = Agent.Dsl(client, {})
+ assert dsl.answer == []
+ assert "begin" in dsl.components
+ assert dsl.components["begin"]["obj"]["component_name"] == "Begin"
+ assert dsl.graph["nodes"][0]["id"] == "begin"
+ assert dsl.history == []
+ assert dsl.messages == []
+ assert dsl.path == []
+ assert dsl.reference == []
+
+
+def test_agent_session_methods_success_and_error_paths(monkeypatch):
+ client = RAGFlow("token", HOST_ADDRESS)
+ agent = Agent(client, {"id": "agent-1"})
+ calls = {"post": [], "get": [], "rm": []}
+
+ def _ok_post(path, json=None, stream=False, files=None):
+ calls["post"].append((path, json, stream, files))
+ return _DummyResponse({"code": 0, "data": {"id": "session-1", "agent_id": "agent-1", "name": "one"}})
+
+ def _ok_get(path, params=None):
+ calls["get"].append((path, params))
+ return _DummyResponse(
+ {
+ "code": 0,
+ "data": [
+ {"id": "session-1", "agent_id": "agent-1", "name": "one"},
+ {"id": "session-2", "agent_id": "agent-1", "name": "two"},
+ ],
+ }
+ )
+
+ def _ok_rm(path, payload):
+ calls["rm"].append((path, payload))
+ return _DummyResponse({"code": 0, "message": "ok"})
+
+ monkeypatch.setattr(agent, "post", _ok_post)
+ monkeypatch.setattr(agent, "get", _ok_get)
+ monkeypatch.setattr(agent, "rm", _ok_rm)
+
+ session = agent.create_session(name="session-name")
+ assert isinstance(session, Session), str(session)
+ assert session.id == "session-1", str(session)
+ assert calls["post"][-1][0] == "/agents/agent-1/sessions"
+ assert calls["post"][-1][1] == {"name": "session-name"}
+
+ sessions = agent.list_sessions(page=2, page_size=5, orderby="create_time", desc=False, id="session-1")
+ assert len(sessions) == 2, str(sessions)
+ assert all(isinstance(item, Session) for item in sessions), str(sessions)
+ assert calls["get"][-1][0] == "/agents/agent-1/sessions"
+ assert calls["get"][-1][1]["page"] == 2
+ assert calls["get"][-1][1]["id"] == "session-1"
+
+ agent.delete_sessions(ids=["session-1", "session-2"])
+ assert calls["rm"][-1] == ("/agents/agent-1/sessions", {"ids": ["session-1", "session-2"]})
+
+ monkeypatch.setattr(agent, "post", lambda *_args, **_kwargs: _DummyResponse({"code": 1, "message": "create failed"}))
+ with pytest.raises(Exception, match="create failed"):
+ agent.create_session(name="bad")
+
+ monkeypatch.setattr(agent, "get", lambda *_args, **_kwargs: _DummyResponse({"code": 2, "message": "list failed"}))
+ with pytest.raises(Exception, match="list failed"):
+ agent.list_sessions()
+
+ monkeypatch.setattr(agent, "rm", lambda *_args, **_kwargs: _DummyResponse({"code": 3, "message": "delete failed"}))
+ with pytest.raises(Exception, match="delete failed"):
+ agent.delete_sessions(ids=["session-1"])
diff --git a/test/testcases/test_sdk_api/test_chat_assistant_management/conftest.py b/test/testcases/test_sdk_api/test_chat_assistant_management/conftest.py
new file mode 100644
index 0000000000..19502f977a
--- /dev/null
+++ b/test/testcases/test_sdk_api/test_chat_assistant_management/conftest.py
@@ -0,0 +1,52 @@
+#
+# Copyright 2025 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.
+#
+import pytest
+from common import batch_create_chat_assistants, delete_all_chats
+from pytest import FixtureRequest
+from ragflow_sdk import Chat, DataSet, Document, RAGFlow
+from utils import wait_for
+
+
+@wait_for(200, 1, "Document parsing timeout")
+def condition(_dataset: DataSet):
+ documents = _dataset.list_documents(page_size=100)
+ for document in documents:
+ if document.run != "DONE":
+ return False
+ return True
+
+
+def _ensure_parsed(dataset: DataSet, document: Document):
+ """Trigger parsing only if the document is not already done or in progress."""
+ if document.run == "DONE":
+ return
+ try:
+ dataset.async_parse_documents([document.id])
+ except Exception:
+ pass # Already being processed
+ condition(dataset)
+
+
[email protected](scope="function")
+def add_chat_assistants_func(request: FixtureRequest, client: RAGFlow, add_document: tuple[DataSet, Document]) -> tuple[DataSet, Document, list[Chat]]:
+ def cleanup():
+ delete_all_chats(client)
+
+ request.addfinalizer(cleanup)
+
+ dataset, document = add_document
+ _ensure_parsed(dataset, document)
+ return dataset, document, batch_create_chat_assistants(client, 5)
diff --git a/test/testcases/test_sdk_api/test_chat_assistant_management/test_chat_crud_unit.py b/test/testcases/test_sdk_api/test_chat_assistant_management/test_chat_crud_unit.py
new file mode 100644
index 0000000000..fe52e1d0da
--- /dev/null
+++ b/test/testcases/test_sdk_api/test_chat_assistant_management/test_chat_crud_unit.py
@@ -0,0 +1,88 @@
+#
+# 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.
+#
+
+import pytest
+from configs import HOST_ADDRESS
+from ragflow_sdk import RAGFlow
+from ragflow_sdk.modules.chat import Chat
+from ragflow_sdk.modules.session import Session
+
+
+class _DummyResponse:
+ def __init__(self, payload):
+ self._payload = payload
+
+ def json(self):
+ return self._payload
+
+
[email protected](scope="session")
+def auth():
+ return "unit-auth"
+
+
[email protected](scope="session", autouse=True)
+def set_tenant_info():
+ return None
+
+
+def test_chat_create_session_raises_server_error_message(monkeypatch):
+ client = RAGFlow("token", HOST_ADDRESS)
+ chat = Chat(client, {"id": "chat-1"})
+
+ monkeypatch.setattr(
+ chat,
+ "post",
+ lambda *_args, **_kwargs: _DummyResponse({"code": 102, "message": "`name` can not be empty"}),
+ )
+
+ with pytest.raises(Exception) as exception_info:
+ chat.create_session(name="")
+ assert "`name` can not be empty" in str(exception_info.value), str(exception_info.value)
+
+
+def test_chat_list_sessions_forwards_restful_query_params(monkeypatch):
+ client = RAGFlow("token", HOST_ADDRESS)
+ chat = Chat(client, {"id": "chat-1"})
+ calls = []
+
+ def _ok_get(path, params=None):
+ calls.append((path, params))
+ return _DummyResponse(
+ {
+ "code": 0,
+ "data": [
+ {"id": "session-1", "chat_id": "chat-1", "name": "one"},
+ {"id": "session-2", "chat_id": "chat-1", "name": "two"},
+ ],
+ }
+ )
+
+ monkeypatch.setattr(chat, "get", _ok_get)
+
+ sessions = chat.list_sessions(page=2, page_size=2, orderby="create_time", desc=False, id="session-1", name="one", user_id="user-1")
+ assert len(sessions) == 2, str(sessions)
+ assert all(isinstance(item, Session) for item in sessions), str(sessions)
+ assert calls[-1][0] == "/chats/chat-1/sessions"
+ assert calls[-1][1]["page_size"] == 2
+ assert calls[-1][1]["name"] == "one"
+ assert calls[-1][1]["user_id"] == "user-1"
+
+ all_sessions = chat.list_sessions(page_size=0)
+ assert len(all_sessions) == 2, str(all_sessions)
+ assert calls[-1][1]["page_size"] == 0
diff --git a/test/testcases/test_sdk_api/test_chat_assistant_management/test_create_chat_assistant.py b/test/testcases/test_sdk_api/test_chat_assistant_management/test_create_chat_assistant.py
new file mode 100644
index 0000000000..8d226acdcf
--- /dev/null
+++ b/test/testcases/test_sdk_api/test_chat_assistant_management/test_create_chat_assistant.py
@@ -0,0 +1,197 @@
+#
+# Copyright 2025 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.
+#
+
+import pytest
+from common import valid_chat_llm_id
+from configs import CHAT_ASSISTANT_NAME_LIMIT
+from utils import encode_avatar
+from utils.file_utils import create_image_file
+
+
[email protected]("clear_chat_assistants")
+class TestChatAssistantCreate:
+ @pytest.mark.p3
+ @pytest.mark.usefixtures("add_chunks")
+ @pytest.mark.parametrize(
+ "name, expected_message",
+ [
+ ("valid_name", ""),
+ pytest.param("a" * (CHAT_ASSISTANT_NAME_LIMIT + 1), "", marks=pytest.mark.skip(reason="issues/")),
+ pytest.param(1, "", marks=pytest.mark.skip(reason="issues/")),
+ ("", "`name` is required"),
+ ("duplicated_name", "duplicated chat name in creating chat"),
+ ("case insensitive", "duplicated chat name in creating chat"),
+ ],
+ )
+ def test_name(self, client, name, expected_message):
+ if name == "duplicated_name":
+ client.create_chat(name=name)
+ elif name == "case insensitive":
+ client.create_chat(name=name.upper())
+
+ if expected_message:
+ with pytest.raises(Exception) as exception_info:
+ client.create_chat(name=name)
+ assert expected_message in str(exception_info.value)
+ else:
+ chat_assistant = client.create_chat(name=name)
+ assert chat_assistant.name == name
+
+ @pytest.mark.p3
+ @pytest.mark.parametrize(
+ "dataset_ids, expected_message",
+ [
+ ([], ""),
+ (lambda r: [r], ""),
+ (["invalid_dataset_id"], "You don't own the dataset invalid_dataset_id"),
+ ("invalid_dataset_id", "violates type hint list[str] | None"),
+ ],
+ )
+ def test_dataset_ids(self, client, add_chunks, dataset_ids, expected_message):
+ dataset, _, _ = add_chunks
+ if callable(dataset_ids):
+ dataset_ids = dataset_ids(dataset.id)
+
+ if expected_message:
+ with pytest.raises(Exception) as exception_info:
+ client.create_chat(name="ragflow test", dataset_ids=dataset_ids)
+ assert expected_message in str(exception_info.value)
+ else:
+ chat_assistant = client.create_chat(name="ragflow test", dataset_ids=dataset_ids)
+ assert chat_assistant.name == "ragflow test"
+
+ @pytest.mark.p3
+ def test_icon(self, client, tmp_path):
+ fn = create_image_file(tmp_path / "ragflow_test.png")
+ chat_assistant = client.create_chat(name="icon_test", icon=encode_avatar(fn), dataset_ids=[])
+ assert chat_assistant.name == "icon_test"
+
+ @pytest.mark.p3
+ @pytest.mark.parametrize(
+ "llm_setting, expected_message",
+ [
+ ({}, ""),
+ ({"temperature": 0}, ""),
+ ({"temperature": 1}, ""),
+ pytest.param({"temperature": -1}, "", marks=pytest.mark.skip),
+ pytest.param({"temperature": 10}, "", marks=pytest.mark.skip),
+ pytest.param({"temperature": "a"}, "", marks=pytest.mark.skip),
+ ({"top_p": 0}, ""),
+ ({"top_p": 1}, ""),
+ pytest.param({"top_p": -1}, "", marks=pytest.mark.skip),
+ pytest.param({"top_p": 10}, "", marks=pytest.mark.skip),
+ pytest.param({"top_p": "a"}, "", marks=pytest.mark.skip),
+ ({"presence_penalty": 0}, ""),
+ ({"presence_penalty": 1}, ""),
+ pytest.param({"presence_penalty": -1}, "", marks=pytest.mark.skip),
+ pytest.param({"presence_penalty": 10}, "", marks=pytest.mark.skip),
+ pytest.param({"presence_penalty": "a"}, "", marks=pytest.mark.skip),
+ ({"frequency_penalty": 0}, ""),
+ ({"frequency_penalty": 1}, ""),
+ pytest.param({"frequency_penalty": -1}, "", marks=pytest.mark.skip),
+ pytest.param({"frequency_penalty": 10}, "", marks=pytest.mark.skip),
+ pytest.param({"frequency_penalty": "a"}, "", marks=pytest.mark.skip),
+ ({"max_token": 0}, ""),
+ ({"max_token": 1024}, ""),
+ pytest.param({"max_token": -1}, "", marks=pytest.mark.skip),
+ pytest.param({"max_token": 10}, "", marks=pytest.mark.skip),
+ pytest.param({"max_token": "a"}, "", marks=pytest.mark.skip),
+ pytest.param({"unknown": "unknown"}, "", marks=pytest.mark.skip),
+ ],
+ )
+ def test_llm_setting(self, client, add_chunks, llm_setting, expected_message):
+ dataset, _, _ = add_chunks
+
+ if expected_message:
+ with pytest.raises(Exception) as exception_info:
+ client.create_chat(name="llm_test", dataset_ids=[dataset.id], llm_setting=llm_setting or None)
+ assert expected_message in str(exception_info.value)
+ else:
+ chat_assistant = client.create_chat(name="llm_test", dataset_ids=[dataset.id], llm_setting=llm_setting or None)
+ for k, v in llm_setting.items():
+ assert getattr(chat_assistant.llm_setting, k) == v
+
+ @pytest.mark.p3
+ @pytest.mark.parametrize(
+ "llm_id, expected_message",
+ [
+ (valid_chat_llm_id, ""),
+ ("unknown", "`llm_id` unknown doesn't exist"),
+ ],
+ )
+ def test_llm_id(self, client, add_chunks, llm_id, expected_message):
+ dataset, _, _ = add_chunks
+ if callable(llm_id):
+ llm_id = llm_id(client)
+
+ if expected_message:
+ with pytest.raises(Exception) as exception_info:
+ client.create_chat(name="llm_test", dataset_ids=[dataset.id], llm_id=llm_id)
+ assert expected_message in str(exception_info.value)
+ else:
+ chat_assistant = client.create_chat(name="llm_test", dataset_ids=[dataset.id], llm_id=llm_id)
+ assert chat_assistant.llm_id == llm_id
+
+ @pytest.mark.p3
+ @pytest.mark.parametrize(
+ "prompt_config, expected_message",
+ [
+ ({"empty_response": "Hello World"}, ""),
+ ({"empty_response": ""}, ""),
+ ({"empty_response": "!@#$%^&*()"}, ""),
+ ({"empty_response": "中文测试"}, ""),
+ pytest.param({"empty_response": 123}, "", marks=pytest.mark.skip),
+ pytest.param({"empty_response": True}, "", marks=pytest.mark.skip),
+ pytest.param({"empty_response": " "}, "", marks=pytest.mark.skip),
+ ({"prologue": "Hello World"}, ""),
+ ({"prologue": ""}, ""),
+ ({"prologue": "!@#$%^&*()"}, ""),
+ ({"prologue": "中文测试"}, ""),
+ pytest.param({"prologue": 123}, "", marks=pytest.mark.skip),
+ pytest.param({"prologue": True}, "", marks=pytest.mark.skip),
+ pytest.param({"prologue": " "}, "", marks=pytest.mark.skip),
+ ({"quote": True}, ""),
+ ({"quote": False}, ""),
+ ({"system": "Hello World {knowledge}"}, ""),
+ ({"system": "{knowledge}"}, ""),
+ ({"system": "!@#$%^&*() {knowledge}"}, ""),
+ ({"system": "中文测试 {knowledge}"}, ""),
+ ({"system": "Hello World"}, ""),
+ ({"system": "Hello World", "parameters": []}, ""),
+ pytest.param({"system": 123}, "", marks=pytest.mark.skip),
+ pytest.param({"unknown": "unknown"}, "", marks=pytest.mark.skip),
+ ],
+ )
+ def test_prompt_config(self, client, add_chunks, prompt_config, expected_message):
+ dataset, _, _ = add_chunks
+
+ if expected_message:
+ with pytest.raises(Exception) as exception_info:
+ client.create_chat(name="prompt_test", dataset_ids=[dataset.id], prompt_config=prompt_config)
+ assert expected_message in str(exception_info.value)
+ else:
+ chat_assistant = client.create_chat(name="prompt_test", dataset_ids=[dataset.id], prompt_config=prompt_config)
+ for k, v in prompt_config.items():
+ assert getattr(chat_assistant.prompt_config, k) == v
+
+
+class TestChatAssistantCreate2:
+ @pytest.mark.p3
+ def test_unparsed_document(self, client, add_document):
+ dataset, _ = add_document
+ with pytest.raises(Exception) as exception_info:
+ client.create_chat(name="prompt_test", dataset_ids=[dataset.id])
+ assert "doesn't own parsed file" in str(exception_info.value)
diff --git a/test/testcases/test_sdk_api/test_chat_assistant_management/test_delete_chat_assistants.py b/test/testcases/test_sdk_api/test_chat_assistant_management/test_delete_chat_assistants.py
new file mode 100644
index 0000000000..487c63598b
--- /dev/null
+++ b/test/testcases/test_sdk_api/test_chat_assistant_management/test_delete_chat_assistants.py
@@ -0,0 +1,116 @@
+#
+# Copyright 2025 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.
+#
+from concurrent.futures import ThreadPoolExecutor, as_completed
+
+import pytest
+from common import batch_create_chat_assistants
+
+
+class TestChatAssistantsDelete:
+ @pytest.mark.parametrize(
+ "payload, expected_message, remaining",
+ [
+ pytest.param(None, "", 5, marks=pytest.mark.p3),
+ pytest.param({"ids": []}, "", 5, marks=pytest.mark.p3),
+ pytest.param({"ids": ["invalid_id"]}, "Chat(invalid_id) not found.", 5, marks=pytest.mark.p3),
+ pytest.param({"ids": ["\n!?。;!?\"'"]}, """Chat(\n!?。;!?"\') not found.""", 5, marks=pytest.mark.p3),
+ pytest.param(lambda r: {"ids": r[:1]}, "", 4, marks=pytest.mark.p3),
+ pytest.param(lambda r: {"ids": r}, "", 0, marks=pytest.mark.p3),
+ ],
+ )
+ def test_basic_scenarios(self, client, add_chat_assistants_func, payload, expected_message, remaining):
+ _, _, chat_assistants = add_chat_assistants_func
+ if callable(payload):
+ payload = payload([chat_assistant.id for chat_assistant in chat_assistants])
+
+ if expected_message:
+ with pytest.raises(Exception) as exception_info:
+ client.delete_chats(**payload)
+ assert expected_message in str(exception_info.value)
+ else:
+ if payload is None:
+ client.delete_chats(payload)
+ else:
+ client.delete_chats(**payload)
+
+ assistants = client.list_chats()
+ assert len(assistants) == remaining
+
+ @pytest.mark.p3
+ def test_delete_chats_nonzero_response_raises(self, client, monkeypatch):
+ class _DummyResponse:
+ def json(self):
+ return {"code": 1, "message": "boom"}
+
+ monkeypatch.setattr(client, "delete", lambda *_args, **_kwargs: _DummyResponse())
+ with pytest.raises(Exception) as exception_info:
+ client.delete_chats(ids=["chat-1"])
+ assert "boom" in str(exception_info.value), str(exception_info.value)
+
+ @pytest.mark.parametrize(
+ "payload",
+ [
+ pytest.param(lambda r: {"ids": ["invalid_id"] + r}, marks=pytest.mark.p3),
+ pytest.param(lambda r: {"ids": r[:1] + ["invalid_id"] + r[1:5]}, marks=pytest.mark.p3),
+ pytest.param(lambda r: {"ids": r + ["invalid_id"]}, marks=pytest.mark.p3),
+ ],
+ )
+ def test_delete_partial_invalid_id(self, client, add_chat_assistants_func, payload):
+ _, _, chat_assistants = add_chat_assistants_func
+ payload = payload([chat_assistant.id for chat_assistant in chat_assistants])
+ client.delete_chats(**payload)
+
+ assistants = client.list_chats()
+ assert len(assistants) == 0
+
+ @pytest.mark.p3
+ def test_repeated_deletion(self, client, add_chat_assistants_func):
+ _, _, chat_assistants = add_chat_assistants_func
+ chat_ids = [chat.id for chat in chat_assistants]
+ client.delete_chats(ids=chat_ids)
+
+ with pytest.raises(Exception) as exception_info:
+ client.delete_chats(ids=chat_ids)
+ assert "not found" in str(exception_info.value)
+
+ @pytest.mark.p3
+ def test_duplicate_deletion(self, client, add_chat_assistants_func):
+ _, _, chat_assistants = add_chat_assistants_func
+ chat_ids = [chat.id for chat in chat_assistants]
+ client.delete_chats(ids=chat_ids + chat_ids)
+
+ assistants = client.list_chats()
+ assert len(assistants) == 0
+
+ @pytest.mark.p3
+ def test_concurrent_deletion(self, client):
+ count = 100
+ chat_ids = [client.create_chat(name=f"test_{i}").id for i in range(count)]
+
+ with ThreadPoolExecutor(max_workers=5) as executor:
+ futures = [executor.submit(client.delete_chats, ids=[chat_ids[i]]) for i in range(count)]
+ responses = list(as_completed(futures))
+
+ assert len(responses) == count
+ assert all(future.exception() is None for future in futures)
+
+ @pytest.mark.p3
+ def test_delete_1k(self, client):
+ chat_assistants = batch_create_chat_assistants(client, 1_000)
+ client.delete_chats(ids=[chat_assistants.id for chat_assistants in chat_assistants])
+
+ assistants = client.list_chats()
+ assert len(assistants) == 0
diff --git a/test/testcases/test_sdk_api/test_chat_assistant_management/test_list_chat_assistants.py b/test/testcases/test_sdk_api/test_chat_assistant_management/test_list_chat_assistants.py
new file mode 100644
index 0000000000..93085a0d84
--- /dev/null
+++ b/test/testcases/test_sdk_api/test_chat_assistant_management/test_list_chat_assistants.py
@@ -0,0 +1,232 @@
+#
+# Copyright 2025 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.
+#
+from concurrent.futures import ThreadPoolExecutor, as_completed
+
+import pytest
+
+
[email protected]("add_chat_assistants")
+class TestChatAssistantsList:
+ @pytest.mark.p3
+ def test_default(self, client):
+ assistants = client.list_chats()
+ assert len(assistants) == 5
+
+ @pytest.mark.p3
+ @pytest.mark.parametrize(
+ "params, expected_page_size, expected_message",
+ [
+ ({"page": 0, "page_size": 2}, 2, ""),
+ ({"page": 2, "page_size": 2}, 2, ""),
+ ({"page": 3, "page_size": 2}, 1, ""),
+ ({"page": "3", "page_size": 2}, 0, "not instance of"),
+ pytest.param(
+ {"page": -1, "page_size": 2},
+ 0,
+ "1064",
+ marks=pytest.mark.skip(reason="issues/5851"),
+ ),
+ pytest.param(
+ {"page": "a", "page_size": 2},
+ 0,
+ """ValueError("invalid literal for int() with base 10: \'a\'")""",
+ marks=pytest.mark.skip(reason="issues/5851"),
+ ),
+ ],
+ )
+ def test_page(self, client, params, expected_page_size, expected_message):
+ if expected_message:
+ with pytest.raises(Exception) as exception_info:
+ client.list_chats(**params)
+ assert expected_message in str(exception_info.value)
+ else:
+ assistants = client.list_chats(**params)
+ assert len(assistants) == expected_page_size
+
+ @pytest.mark.p1
+ @pytest.mark.parametrize(
+ "params, expected_page_size, expected_message",
+ [
+ ({"page_size": 0}, 5, ""),
+ ({"page_size": 1}, 1, ""),
+ ({"page_size": 6}, 5, ""),
+ ({"page_size": "1"}, 0, "not instance of"),
+ pytest.param(
+ {"page_size": -1},
+ 0,
+ "1064",
+ marks=pytest.mark.skip(reason="issues/5851"),
+ ),
+ pytest.param(
+ {"page_size": "a"},
+ 0,
+ """ValueError("invalid literal for int() with base 10: \'a\'")""",
+ marks=pytest.mark.skip(reason="issues/5851"),
+ ),
+ ],
+ )
+ def test_page_size(self, client, params, expected_page_size, expected_message):
+ if expected_message:
+ with pytest.raises(Exception) as exception_info:
+ client.list_chats(**params)
+ assert expected_message in str(exception_info.value)
+ else:
+ assistants = client.list_chats(**params)
+ assert len(assistants) == expected_page_size
+
+ @pytest.mark.p3
+ @pytest.mark.parametrize(
+ "params, expected_message",
+ [
+ ({"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")),
+ ],
+ )
+ def test_orderby(self, client, params, expected_message):
+ if expected_message:
+ with pytest.raises(Exception) as exception_info:
+ client.list_chats(**params)
+ assert expected_message in str(exception_info.value)
+ else:
+ client.list_chats(**params)
+
+ @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}, ""),
+ ({"desc": "false"}, "not instance of"),
+ ({"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, client, params, expected_message):
+ if expected_message:
+ with pytest.raises(Exception) as exception_info:
+ client.list_chats(**params)
+ assert expected_message in str(exception_info.value)
+ else:
+ client.list_chats(**params)
+
+ @pytest.mark.p1
+ @pytest.mark.parametrize(
+ "params, expected_num, expected_message",
+ [
+ ({"keywords": None}, 5, ""),
+ ({"keywords": ""}, 5, ""),
+ ({"keywords": "test_chat_assistant_1"}, 1, ""),
+ ({"keywords": "unknown"}, 0, ""),
+ ],
+ )
+ def test_keywords(self, client, params, expected_num, expected_message):
+ if expected_message:
+ with pytest.raises(Exception) as exception_info:
+ client.list_chats(**params)
+ assert expected_message in str(exception_info.value)
+ else:
+ assistants = client.list_chats(**params)
+ if params["keywords"] in [None, ""]:
+ assert len(assistants) == expected_num
+ else:
+ assert len(assistants) == expected_num
+ if expected_num:
+ assert assistants[0].name == params["keywords"]
+
+ @pytest.mark.p1
+ def test_exact_id_and_name_filters(self, client, add_chat_assistants):
+ _, _, chat_assistants = add_chat_assistants
+ target = chat_assistants[1]
+
+ assistants = client.list_chats(id=target.id)
+ assert len(assistants) == 1
+ assert assistants[0].id == target.id
+
+ assistants = client.list_chats(name=target.name)
+ assert len(assistants) == 1
+ assert assistants[0].name == target.name
+
+ assistants = client.list_chats(name=target.name, keywords="unknown")
+ assert len(assistants) == 1
+ assert assistants[0].name == target.name
+
+ @pytest.mark.p1
+ @pytest.mark.parametrize(
+ "chat_assistant_id, expected_message",
+ [
+ (lambda r: r[0], ""),
+ ("unknown", "no authorization"),
+ ],
+ )
+ def test_get_chat(self, client, add_chat_assistants, chat_assistant_id, expected_message):
+ _, _, chat_assistants = add_chat_assistants
+ chat_id = chat_assistant_id([chat.id for chat in chat_assistants]) if callable(chat_assistant_id) else chat_assistant_id
+
+ if expected_message:
+ with pytest.raises(Exception) as exception_info:
+ client.get_chat(chat_id)
+ assert expected_message in str(exception_info.value)
+ else:
+ assistant = client.get_chat(chat_id)
+ assert assistant.id == chat_id
+
+ @pytest.mark.p3
+ @pytest.mark.parametrize(
+ "chat_assistant_id, keywords, expected_num, expected_message",
+ [
+ (lambda r: r[0], "test_chat_assistant_0", 1, ""),
+ (lambda r: r[0], "test_chat_assistant_1", 1, ""),
+ (lambda r: r[0], "unknown", 0, ""),
+ ],
+ )
+ def test_get_and_keywords_are_separate_lookups(self, client, add_chat_assistants, chat_assistant_id, keywords, expected_num, expected_message):
+ _, _, chat_assistants = add_chat_assistants
+ chat_id = chat_assistant_id([chat.id for chat in chat_assistants]) if callable(chat_assistant_id) else chat_assistant_id
+
+ if expected_message:
+ with pytest.raises(Exception) as exception_info:
+ client.get_chat(chat_id)
+ assert expected_message in str(exception_info.value)
+ else:
+ client.get_chat(chat_id)
+ assistants = client.list_chats(keywords=keywords)
+ assert len(assistants) == expected_num
+
+ @pytest.mark.p3
+ def test_concurrent_list(self, client):
+ count = 100
+ with ThreadPoolExecutor(max_workers=5) as executor:
+ futures = [executor.submit(client.list_chats) for _ in range(count)]
+ responses = list(as_completed(futures))
+ assert len(responses) == count, responses
+
+ @pytest.mark.p3
+ def test_list_chats_after_deleting_associated_dataset(self, client, add_chat_assistants):
+ dataset, _, _ = add_chat_assistants
+ client.delete_datasets(ids=[dataset.id])
+
+ assistants = client.list_chats()
+ assert len(assistants) == 5
diff --git a/test/testcases/test_sdk_api/test_chat_assistant_management/test_update_chat_assistant.py b/test/testcases/test_sdk_api/test_chat_assistant_management/test_update_chat_assistant.py
new file mode 100644
index 0000000000..719e17a43c
--- /dev/null
+++ b/test/testcases/test_sdk_api/test_chat_assistant_management/test_update_chat_assistant.py
@@ -0,0 +1,209 @@
+#
+# Copyright 2025 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.
+#
+
+import pytest
+from common import valid_chat_llm_id
+from configs import CHAT_ASSISTANT_NAME_LIMIT
+from utils import encode_avatar
+from utils.file_utils import create_image_file
+
+
+class TestChatAssistantUpdate:
+ @pytest.mark.p3
+ def test_update_rejects_non_dict(self, add_chat_assistants_func):
+ _, _, chat_assistants = add_chat_assistants_func
+ chat_assistant = chat_assistants[0]
+
+ with pytest.raises(Exception) as exception_info:
+ chat_assistant.update.__wrapped__(chat_assistant, "bad")
+ assert "`update_message` must be a dict" in str(exception_info.value)
+
+ @pytest.mark.p3
+ def test_update_raises_on_nonzero_response(self, add_chat_assistants_func, monkeypatch):
+ _, _, chat_assistants = add_chat_assistants_func
+ chat_assistant = chat_assistants[0]
+
+ class _DummyResponse:
+ def json(self):
+ return {"code": 1, "message": "boom"}
+
+ monkeypatch.setattr(chat_assistant, "patch", lambda *_args, **_kwargs: _DummyResponse())
+
+ with pytest.raises(Exception) as exception_info:
+ chat_assistant.update({"name": "error-case"})
+ assert "boom" in str(exception_info.value)
+
+ @pytest.mark.p3
+ def test_update_uses_patch_for_partial_payload(self, add_chat_assistants_func, monkeypatch):
+ _, _, chat_assistants = add_chat_assistants_func
+ chat_assistant = chat_assistants[0]
+ captured = {}
+
+ class _DummyResponse:
+ def json(self):
+ return {"code": 0, "message": "ok"}
+
+ def _patch(path, payload):
+ captured["path"] = path
+ captured["payload"] = payload
+ return _DummyResponse()
+
+ monkeypatch.setattr(chat_assistant, "patch", _patch)
+ monkeypatch.setattr(chat_assistant, "put", lambda *_args, **_kwargs: pytest.fail("update() should not use PUT"))
+
+ chat_assistant.update({"name": "renamed"})
+
+ assert captured["path"] == f"/chats/{chat_assistant.id}"
+ assert captured["payload"] == {"name": "renamed"}
+
+ @pytest.mark.parametrize(
+ "payload, expected_message",
+ [
+ pytest.param({"name": "valid_name"}, "", marks=pytest.mark.p3),
+ pytest.param({"name": "a" * (CHAT_ASSISTANT_NAME_LIMIT + 1)}, "", marks=pytest.mark.skip(reason="issues/")),
+ pytest.param({"name": 1}, "", marks=pytest.mark.skip(reason="issues/")),
+ pytest.param({"name": ""}, "`name` cannot be empty", marks=pytest.mark.p3),
+ pytest.param({"name": "test_chat_assistant_1"}, "duplicated chat name", marks=pytest.mark.p3),
+ pytest.param({"name": "TEST_CHAT_ASSISTANT_1"}, "duplicated chat name", marks=pytest.mark.p3),
+ ],
+ )
+ def test_name(self, client, add_chat_assistants_func, payload, expected_message):
+ _, _, chat_assistants = add_chat_assistants_func
+ chat_assistant = chat_assistants[0]
+
+ if expected_message:
+ with pytest.raises(Exception) as exception_info:
+ chat_assistant.update(payload)
+ assert expected_message in str(exception_info.value)
+ else:
+ chat_assistant.update(payload)
+ updated_chat = client.get_chat(chat_assistant.id)
+ assert updated_chat.name == payload["name"], str(updated_chat)
+
+ @pytest.mark.p3
+ def test_icon(self, client, add_chat_assistants_func, tmp_path):
+ dataset, _, chat_assistants = add_chat_assistants_func
+ chat_assistant = chat_assistants[0]
+
+ fn = create_image_file(tmp_path / "ragflow_test.png")
+ payload = {"name": "icon_test", "icon": encode_avatar(fn), "dataset_ids": [dataset.id]}
+
+ chat_assistant.update(payload)
+ updated_chat = client.get_chat(chat_assistant.id)
+ assert updated_chat.name == payload["name"], str(updated_chat)
+ assert updated_chat.icon is not None, str(updated_chat)
+
+ @pytest.mark.p3
+ @pytest.mark.parametrize(
+ "llm_setting, expected_message",
+ [
+ ({"model_name": valid_chat_llm_id}, ""),
+ ({"model_name": "unknown"}, "`llm_id` unknown doesn't exist"),
+ ({"temperature": 0}, ""),
+ ({"temperature": 1}, ""),
+ pytest.param({"temperature": -1}, "", marks=pytest.mark.skip),
+ pytest.param({"temperature": 10}, "", marks=pytest.mark.skip),
+ pytest.param({"temperature": "a"}, "", marks=pytest.mark.skip),
+ ({"top_p": 0}, ""),
+ ({"top_p": 1}, ""),
+ pytest.param({"top_p": -1}, "", marks=pytest.mark.skip),
+ pytest.param({"top_p": 10}, "", marks=pytest.mark.skip),
+ pytest.param({"top_p": "a"}, "", marks=pytest.mark.skip),
+ ({"presence_penalty": 0}, ""),
+ ({"presence_penalty": 1}, ""),
+ pytest.param({"presence_penalty": -1}, "", marks=pytest.mark.skip),
+ pytest.param({"presence_penalty": 10}, "", marks=pytest.mark.skip),
+ pytest.param({"presence_penalty": "a"}, "", marks=pytest.mark.skip),
+ ({"frequency_penalty": 0}, ""),
+ ({"frequency_penalty": 1}, ""),
+ pytest.param({"frequency_penalty": -1}, "", marks=pytest.mark.skip),
+ pytest.param({"frequency_penalty": 10}, "", marks=pytest.mark.skip),
+ pytest.param({"frequency_penalty": "a"}, "", marks=pytest.mark.skip),
+ ({"max_token": 0}, ""),
+ ({"max_token": 1024}, ""),
+ pytest.param({"max_token": -1}, "", marks=pytest.mark.skip),
+ pytest.param({"max_token": 10}, "", marks=pytest.mark.skip),
+ pytest.param({"max_token": "a"}, "", marks=pytest.mark.skip),
+ pytest.param({"unknown": "unknown"}, "", marks=pytest.mark.skip),
+ ],
+ )
+ def test_llm_setting(self, client, add_chat_assistants_func, llm_setting, expected_message):
+ dataset, _, chat_assistants = add_chat_assistants_func
+ chat_assistant = chat_assistants[0]
+ llm_setting = dict(llm_setting)
+ llm_id = llm_setting.pop("model_name", None)
+ if callable(llm_id):
+ llm_id = llm_id(client)
+ payload = {"name": "llm_test", "dataset_ids": [dataset.id], "llm_setting": llm_setting}
+ if llm_id is not None:
+ payload["llm_id"] = llm_id
+
+ if expected_message:
+ with pytest.raises(Exception) as exception_info:
+ chat_assistant.update(payload)
+ assert expected_message in str(exception_info.value)
+ else:
+ chat_assistant.update(payload)
+ updated_chat = client.get_chat(chat_assistant.id)
+ if llm_id:
+ assert updated_chat.llm_id == llm_id, str(updated_chat)
+ for k, v in llm_setting.items():
+ assert getattr(updated_chat.llm_setting, k) == v, str(updated_chat)
+
+ @pytest.mark.p3
+ @pytest.mark.parametrize(
+ "prompt_config, expected_message",
+ [
+ ({"empty_response": "Hello World"}, ""),
+ ({"empty_response": ""}, ""),
+ ({"empty_response": "!@#$%^&*()"}, ""),
+ ({"empty_response": "中文测试"}, ""),
+ pytest.param({"empty_response": 123}, "", marks=pytest.mark.skip),
+ pytest.param({"empty_response": True}, "", marks=pytest.mark.skip),
+ pytest.param({"empty_response": " "}, "", marks=pytest.mark.skip),
+ ({"prologue": "Hello World"}, ""),
+ ({"prologue": ""}, ""),
+ ({"prologue": "!@#$%^&*()"}, ""),
+ ({"prologue": "中文测试"}, ""),
+ pytest.param({"prologue": 123}, "", marks=pytest.mark.skip),
+ pytest.param({"prologue": True}, "", marks=pytest.mark.skip),
+ pytest.param({"prologue": " "}, "", marks=pytest.mark.skip),
+ ({"quote": True}, ""),
+ ({"quote": False}, ""),
+ ({"system": "Hello World {knowledge}"}, ""),
+ ({"system": "{knowledge}"}, ""),
+ ({"system": "!@#$%^&*() {knowledge}"}, ""),
+ ({"system": "中文测试 {knowledge}"}, ""),
+ ({"system": "Hello World"}, ""),
+ ({"system": "Hello World", "parameters": []}, ""),
+ pytest.param({"system": 123}, "", marks=pytest.mark.skip),
+ pytest.param({"unknown": "unknown"}, "", marks=pytest.mark.skip),
+ ],
+ )
+ def test_prompt_config(self, client, add_chat_assistants_func, prompt_config, expected_message):
+ dataset, _, chat_assistants = add_chat_assistants_func
+ chat_assistant = chat_assistants[0]
+ payload = {"name": "prompt_test", "prompt_config": prompt_config, "dataset_ids": [dataset.id]}
+
+ if expected_message:
+ with pytest.raises(Exception) as exception_info:
+ chat_assistant.update(payload)
+ assert expected_message in str(exception_info.value)
+ else:
+ chat_assistant.update(payload)
+ updated_chat = client.get_chat(chat_assistant.id)
+ for k, v in prompt_config.items():
+ assert getattr(updated_chat.prompt_config, k) == v, str(updated_chat)
diff --git a/test/testcases/test_sdk_api/test_chunk_management_within_dataset/conftest.py b/test/testcases/test_sdk_api/test_chunk_management_within_dataset/conftest.py
new file mode 100644
index 0000000000..4727c85a48
--- /dev/null
+++ b/test/testcases/test_sdk_api/test_chunk_management_within_dataset/conftest.py
@@ -0,0 +1,55 @@
+#
+# Copyright 2025 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.
+#
+
+
+import pytest
+from common import batch_add_chunks, delete_all_chunks
+from pytest import FixtureRequest
+from ragflow_sdk import Chunk, DataSet, Document
+from utils import wait_for
+
+
+@wait_for(30, 1, "Document parsing timeout")
+def condition(_dataset: DataSet):
+ documents = _dataset.list_documents(page_size=100)
+ for document in documents:
+ if document.run != "DONE":
+ return False
+ return True
+
+
+@wait_for(30, 1, "Chunk indexing timeout")
+def chunks_visible(_document: Document, _chunk_ids: list[str]):
+ visible_ids = {chunk.id for chunk in _document.list_chunks(page_size=100)}
+ return set(_chunk_ids).issubset(visible_ids)
+
+
[email protected](scope="function")
+def add_chunks_func(request: FixtureRequest, add_document: tuple[DataSet, Document]) -> tuple[DataSet, Document, list[Chunk]]:
+ def cleanup():
+ try:
+ delete_all_chunks(document)
+ except Exception:
+ pass
+
+ request.addfinalizer(cleanup)
+
+ dataset, document = add_document
+ dataset.async_parse_documents([document.id])
+ condition(dataset)
+ chunks = batch_add_chunks(document, 4)
+ chunks_visible(document, [chunk.id for chunk in chunks])
+ return dataset, document, chunks
diff --git a/test/testcases/test_sdk_api/test_chunk_management_within_dataset/test_add_chunk.py b/test/testcases/test_sdk_api/test_chunk_management_within_dataset/test_add_chunk.py
new file mode 100644
index 0000000000..2db1022752
--- /dev/null
+++ b/test/testcases/test_sdk_api/test_chunk_management_within_dataset/test_add_chunk.py
@@ -0,0 +1,190 @@
+#
+# Copyright 2025 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.
+#
+from concurrent.futures import ThreadPoolExecutor, as_completed
+from time import sleep
+
+import pytest
+from ragflow_sdk import Chunk
+
+
+def validate_chunk_details(dataset_id: str, document_id: str, payload: dict, chunk: Chunk):
+ assert chunk.dataset_id == dataset_id
+ assert chunk.document_id == document_id
+ assert chunk.content == payload["content"]
+ if "important_keywords" in payload:
+ assert chunk.important_keywords == payload["important_keywords"]
+ if "questions" in payload:
+ assert chunk.questions == [str(q).strip() for q in payload.get("questions", []) if str(q).strip()]
+ if "tag_kwd" in payload:
+ assert chunk.tag_kwd == payload["tag_kwd"]
+
+
+class TestAddChunk:
+ @pytest.mark.p1
+ @pytest.mark.parametrize(
+ "payload, expected_message",
+ [
+ ({"content": None}, "not instance of"),
+ ({"content": ""}, "`content` is required"),
+ ({"content": 1}, "not instance of"),
+ ({"content": "a"}, ""),
+ ({"content": " "}, "`content` is required"),
+ ({"content": "\n!?。;!?\"'"}, ""),
+ ],
+ )
+ def test_content(self, add_document, payload, expected_message):
+ dataset, document = add_document
+ chunks_count = len(document.list_chunks())
+
+ if expected_message:
+ with pytest.raises(Exception) as exception_info:
+ document.add_chunk(**payload)
+ assert expected_message in str(exception_info.value), str(exception_info.value)
+ else:
+ chunk = document.add_chunk(**payload)
+ validate_chunk_details(dataset.id, document.id, payload, chunk)
+
+ sleep(1)
+ chunks = document.list_chunks()
+ assert len(chunks) == chunks_count + 1, str(chunks)
+
+ @pytest.mark.p2
+ @pytest.mark.parametrize(
+ "payload, expected_message",
+ [
+ ({"content": "chunk test important_keywords 1", "important_keywords": ["a", "b", "c"]}, ""),
+ ({"content": "chunk test important_keywords 2", "important_keywords": [""]}, ""),
+ ({"content": "chunk test important_keywords 3", "important_keywords": [1]}, "not instance of"),
+ ({"content": "chunk test important_keywords 4", "important_keywords": ["a", "a"]}, ""),
+ ({"content": "chunk test important_keywords 5", "important_keywords": "abc"}, "not instance of"),
+ ({"content": "chunk test important_keywords 6", "important_keywords": 123}, "not instance of"),
+ ],
+ )
+ def test_important_keywords(self, add_document, payload, expected_message):
+ dataset, document = add_document
+ chunks_count = len(document.list_chunks())
+
+ if expected_message:
+ with pytest.raises(Exception) as exception_info:
+ document.add_chunk(**payload)
+ assert expected_message in str(exception_info.value), str(exception_info.value)
+ else:
+ chunk = document.add_chunk(**payload)
+ validate_chunk_details(dataset.id, document.id, payload, chunk)
+
+ sleep(1)
+ chunks = document.list_chunks()
+ assert len(chunks) == chunks_count + 1, str(chunks)
+
+ @pytest.mark.p3
+ @pytest.mark.parametrize(
+ "payload, expected_message",
+ [
+ ({"content": "chunk test test_questions 1", "questions": ["a", "b", "c"]}, ""),
+ ({"content": "chunk test test_questions 2", "questions": [""]}, ""),
+ ({"content": "chunk test test_questions 3", "questions": [1]}, "not instance of"),
+ ({"content": "chunk test test_questions 4", "questions": ["a", "a"]}, ""),
+ ({"content": "chunk test test_questions 5", "questions": "abc"}, "not instance of"),
+ ({"content": "chunk test test_questions 6", "questions": 123}, "not instance of"),
+ ],
+ )
+ def test_questions(self, add_document, payload, expected_message):
+ dataset, document = add_document
+ chunks_count = len(document.list_chunks())
+
+ if expected_message:
+ with pytest.raises(Exception) as exception_info:
+ document.add_chunk(**payload)
+ assert expected_message in str(exception_info.value), str(exception_info.value)
+ else:
+ chunk = document.add_chunk(**payload)
+ validate_chunk_details(dataset.id, document.id, payload, chunk)
+
+ sleep(1)
+ chunks = document.list_chunks()
+ assert len(chunks) == chunks_count + 1, str(chunks)
+
+ @pytest.mark.p2
+ @pytest.mark.parametrize(
+ "payload, expected_message",
+ [
+ ({"content": "chunk test test_tag_kwd 1", "tag_kwd": ["tag1", "tag2"]}, ""),
+ ({"content": "chunk test test_tag_kwd 2", "tag_kwd": [""]}, ""),
+ ({"content": "chunk test test_tag_kwd 3", "tag_kwd": [1]}, "not instance of"),
+ ({"content": "chunk test test_tag_kwd 4", "tag_kwd": ["tag", "tag"]}, ""),
+ ({"content": "chunk test test_tag_kwd 5", "tag_kwd": "abc"}, "not instance of"),
+ ({"content": "chunk test test_tag_kwd 6", "tag_kwd": 123}, "not instance of"),
+ ],
+ )
+ def test_tag_kwd(self, add_document, payload, expected_message):
+ dataset, document = add_document
+ chunks_count = len(document.list_chunks())
+
+ if expected_message:
+ with pytest.raises(Exception) as exception_info:
+ document.add_chunk(**payload)
+ assert expected_message in str(exception_info.value), str(exception_info.value)
+ else:
+ chunk = document.add_chunk(**payload)
+ validate_chunk_details(dataset.id, document.id, payload, chunk)
+
+ sleep(1)
+ chunks = document.list_chunks()
+ assert len(chunks) == chunks_count + 1, str(chunks)
+
+ @pytest.mark.p3
+ def test_repeated_add_chunk(self, add_document):
+ payload = {"content": "chunk test repeated_add_chunk"}
+ dataset, document = add_document
+ chunks_count = len(document.list_chunks())
+
+ chunk1 = document.add_chunk(**payload)
+ validate_chunk_details(dataset.id, document.id, payload, chunk1)
+ sleep(1)
+ chunks = document.list_chunks()
+ assert len(chunks) == chunks_count + 1, str(chunks)
+
+ chunk2 = document.add_chunk(**payload)
+ validate_chunk_details(dataset.id, document.id, payload, chunk2)
+ sleep(1)
+ chunks = document.list_chunks()
+ assert len(chunks) == chunks_count + 1, str(chunks)
+
+ @pytest.mark.p2
+ def test_add_chunk_to_deleted_document(self, add_document):
+ dataset, document = add_document
+ dataset.delete_documents(ids=[document.id])
+
+ with pytest.raises(Exception) as exception_info:
+ document.add_chunk(content="chunk test")
+ assert f"you don't own the document {document.id}" in str(exception_info.value), str(exception_info.value)
+
+ @pytest.mark.skip(reason="issues/6411")
+ @pytest.mark.p3
+ def test_concurrent_add_chunk(self, add_document):
+ count = 50
+ _, document = add_document
+ initial_chunk_count = len(document.list_chunks())
+
+ def add_chunk_task(i):
+ return document.add_chunk(content=f"chunk test concurrent {i}")
+
+ with ThreadPoolExecutor(max_workers=5) as executor:
+ futures = [executor.submit(add_chunk_task, i) for i in range(count)]
+ responses = list(as_completed(futures))
+ assert len(responses) == count, responses
+ sleep(5)
+ assert len(document.list_chunks(page_size=100)) == initial_chunk_count + count
diff --git a/test/testcases/test_sdk_api/test_chunk_management_within_dataset/test_delete_chunks.py b/test/testcases/test_sdk_api/test_chunk_management_within_dataset/test_delete_chunks.py
new file mode 100644
index 0000000000..20d5dc2aab
--- /dev/null
+++ b/test/testcases/test_sdk_api/test_chunk_management_within_dataset/test_delete_chunks.py
@@ -0,0 +1,116 @@
+#
+# Copyright 2025 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.
+#
+from concurrent.futures import ThreadPoolExecutor, as_completed
+
+import pytest
+from common import batch_add_chunks
+
+
+class TestChunksDeletion:
+ @pytest.mark.parametrize(
+ "payload",
+ [
+ pytest.param(lambda r: {"ids": ["invalid_id"] + r}, marks=pytest.mark.p3),
+ pytest.param(lambda r: {"ids": r[:1] + ["invalid_id"] + r[1:4]}, marks=pytest.mark.p3),
+ pytest.param(lambda r: {"ids": r + ["invalid_id"]}, marks=pytest.mark.p3),
+ ],
+ )
+ def test_delete_partial_invalid_id(self, add_chunks_func, payload):
+ _, document, chunks = add_chunks_func
+ chunk_ids = [chunk.id for chunk in chunks]
+ payload = payload(chunk_ids)
+
+ with pytest.raises(Exception) as exception_info:
+ document.delete_chunks(**payload)
+ assert "rm_chunk deleted chunks" in str(exception_info.value), str(exception_info.value)
+
+ remaining_chunks = document.list_chunks()
+ assert len(remaining_chunks) == 1, str(remaining_chunks)
+
+ @pytest.mark.p3
+ def test_repeated_deletion(self, add_chunks_func):
+ _, document, chunks = add_chunks_func
+ chunk_ids = [chunk.id for chunk in chunks]
+ document.delete_chunks(ids=chunk_ids)
+
+ with pytest.raises(Exception) as exception_info:
+ document.delete_chunks(ids=chunk_ids)
+ assert "rm_chunk deleted chunks 0, expect" in str(exception_info.value), str(exception_info.value)
+
+ @pytest.mark.p3
+ def test_duplicate_deletion(self, add_chunks_func):
+ _, document, chunks = add_chunks_func
+ chunk_ids = [chunk.id for chunk in chunks]
+ document.delete_chunks(ids=chunk_ids * 2)
+ remaining_chunks = document.list_chunks()
+ assert len(remaining_chunks) == 1, str(remaining_chunks)
+
+ @pytest.mark.p3
+ def test_concurrent_deletion(self, add_document):
+ count = 100
+ _, document = add_document
+ chunks = batch_add_chunks(document, count)
+ chunk_ids = [chunk.id for chunk in chunks]
+
+ with ThreadPoolExecutor(max_workers=5) as executor:
+ futures = [executor.submit(document.delete_chunks, ids=[chunk_id]) for chunk_id in chunk_ids]
+ responses = list(as_completed(futures))
+ assert len(responses) == count, responses
+
+ @pytest.mark.p3
+ def test_delete_1k(self, add_document):
+ count = 1_000
+ _, document = add_document
+ chunks = batch_add_chunks(document, count)
+ chunk_ids = [chunk.id for chunk in chunks]
+
+ from time import sleep
+
+ sleep(1)
+
+ document.delete_chunks(ids=chunk_ids)
+ remaining_chunks = document.list_chunks()
+ assert len(remaining_chunks) == 0, str(remaining_chunks)
+
+ @pytest.mark.parametrize(
+ "payload, expected_message, remaining",
+ [
+ pytest.param(None, "", 5, marks=pytest.mark.p3),
+ pytest.param({"ids": ["invalid_id"]}, "rm_chunk deleted chunks 0, expect 1", 5, marks=pytest.mark.p3),
+ pytest.param("not json", "UnboundLocalError", 5, marks=pytest.mark.skip(reason="pull/6376")),
+ pytest.param(lambda r: {"ids": r[:1]}, "", 4, marks=pytest.mark.p3),
+ pytest.param(lambda r: {"ids": r}, "", 1, marks=pytest.mark.p3),
+ pytest.param({"ids": []}, "", 5, marks=pytest.mark.p3),
+ ],
+ )
+ def test_basic_scenarios(self, add_chunks_func, payload, expected_message, remaining):
+ _, document, chunks = add_chunks_func
+ chunk_ids = [chunk.id for chunk in chunks]
+ if callable(payload):
+ payload = payload(chunk_ids)
+
+ if expected_message:
+ with pytest.raises(Exception) as exception_info:
+ document.delete_chunks(**payload)
+ assert expected_message in str(exception_info.value), str(exception_info.value)
+ else:
+ if payload is None:
+ document.delete_chunks()
+ else:
+ document.delete_chunks(**payload)
+
+ remaining_chunks = document.list_chunks()
+ assert len(remaining_chunks) == remaining, str(remaining_chunks)
diff --git a/test/testcases/test_sdk_api/test_chunk_management_within_dataset/test_list_chunks.py b/test/testcases/test_sdk_api/test_chunk_management_within_dataset/test_list_chunks.py
new file mode 100644
index 0000000000..e4f11dfcb2
--- /dev/null
+++ b/test/testcases/test_sdk_api/test_chunk_management_within_dataset/test_list_chunks.py
@@ -0,0 +1,160 @@
+#
+# Copyright 2025 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.
+#
+import os
+from concurrent.futures import ThreadPoolExecutor, as_completed
+
+import pytest
+from common import batch_add_chunks
+from utils.engine_utils import get_doc_engine
+
+
+class TestChunksList:
+ @pytest.mark.p3
+ @pytest.mark.parametrize(
+ "params, expected_page_size, expected_message",
+ [
+ ({"page": None, "page_size": 2}, 2, ""),
+ pytest.param({"page": 0, "page_size": 2}, 0, "ValueError('Search does not support negative slicing.')", marks=pytest.mark.skip),
+ ({"page": 2, "page_size": 2}, 2, ""),
+ ({"page": 3, "page_size": 2}, 1, ""),
+ ({"page": "3", "page_size": 2}, 1, ""),
+ pytest.param({"page": -1, "page_size": 2}, 0, "ValueError('Search does not support negative slicing.')", marks=pytest.mark.skip),
+ pytest.param({"page": "a", "page_size": 2}, 0, """ValueError("invalid literal for int() with base 10: \'a\'")""", marks=pytest.mark.skip),
+ ],
+ )
+ def test_page(self, add_chunks, params, expected_page_size, expected_message):
+ _, document, _ = add_chunks
+
+ if expected_message:
+ with pytest.raises(Exception) as exception_info:
+ document.list_chunks(**params)
+ assert expected_message in str(exception_info.value), str(exception_info.value)
+ else:
+ chunks = document.list_chunks(**params)
+ assert len(chunks) == expected_page_size, str(chunks)
+
+ @pytest.mark.p3
+ @pytest.mark.parametrize(
+ "params, expected_page_size, expected_message",
+ [
+ ({"page_size": None}, 5, ""),
+ pytest.param({"page_size": 0}, 5, ""),
+ ({"page_size": 1}, 1, ""),
+ ({"page_size": 6}, 5, ""),
+ ({"page_size": "1"}, 1, ""),
+ pytest.param({"page_size": -1}, 5, "", marks=pytest.mark.skip),
+ pytest.param({"page_size": "a"}, 0, """ValueError("invalid literal for int() with base 10: \'a\'")""", marks=pytest.mark.skip),
+ ],
+ )
+ def test_page_size(self, add_chunks, params, expected_page_size, expected_message):
+ _, document, _ = add_chunks
+
+ if expected_message:
+ with pytest.raises(Exception) as exception_info:
+ document.list_chunks(**params)
+ assert expected_message in str(exception_info.value), str(exception_info.value)
+ else:
+ chunks = document.list_chunks(**params)
+ assert len(chunks) == expected_page_size, str(chunks)
+
+ @pytest.mark.p2
+ @pytest.mark.parametrize(
+ "params, expected_page_size",
+ [
+ ({"keywords": None}, 5),
+ ({"keywords": ""}, 5),
+ ({"keywords": "1"}, 1),
+ ({"keywords": "chunk"}, 4),
+ pytest.param({"keywords": "ragflow"}, 1, marks=pytest.mark.skipif(os.getenv("DOC_ENGINE") == "infinity", reason="issues/6509")),
+ pytest.param({"keywords": "ragflow"}, 5, marks=pytest.mark.skipif(os.getenv("DOC_ENGINE") != "infinity", reason="issues/6509")),
+ ({"keywords": "unknown"}, 0),
+ ],
+ )
+ def test_keywords(self, add_chunks, params, expected_page_size):
+ _, document, _ = add_chunks
+ if params.get("keywords") == "ragflow":
+ doc_engine = get_doc_engine(document.rag)
+ if doc_engine == "infinity" and expected_page_size == 1:
+ pytest.skip("issues/6509")
+ if doc_engine != "infinity" and expected_page_size == 5:
+ pytest.skip("issues/6509")
+ chunks = document.list_chunks(**params)
+ assert len(chunks) == expected_page_size, str(chunks)
+
+ @pytest.mark.p1
+ @pytest.mark.parametrize(
+ "chunk_id, expected_page_size, expected_message",
+ [
+ (None, 5, ""),
+ ("", 5, ""),
+ pytest.param(lambda r: r[0], 1, "", marks=pytest.mark.skipif(os.getenv("DOC_ENGINE") == "infinity", reason="issues/6499")),
+ pytest.param("unknown", 0, """AttributeError("\'NoneType\' object has no attribute \'keys\'")""", marks=pytest.mark.skip),
+ ],
+ )
+ def test_id(self, add_chunks, chunk_id, expected_page_size, expected_message):
+ _, document, chunks = add_chunks
+ if callable(chunk_id) and get_doc_engine(document.rag) == "infinity":
+ pytest.skip("issues/6499")
+ chunk_ids = [chunk.id for chunk in chunks]
+ if callable(chunk_id):
+ params = {"id": chunk_id(chunk_ids)}
+ else:
+ params = {"id": chunk_id}
+
+ if expected_message:
+ with pytest.raises(Exception) as exception_info:
+ document.list_chunks(**params)
+ assert expected_message in str(exception_info.value), str(exception_info.value)
+ else:
+ chunks = document.list_chunks(**params)
+ if params["id"] in [None, ""]:
+ assert len(chunks) == expected_page_size, str(chunks)
+ else:
+ assert chunks[0].id == params["id"], str(chunks)
+
+ @pytest.mark.p3
+ def test_concurrent_list(self, add_chunks):
+ _, document, _ = add_chunks
+ count = 100
+ with ThreadPoolExecutor(max_workers=5) as executor:
+ futures = [executor.submit(document.list_chunks) for _ in range(count)]
+
+ responses = list(as_completed(futures))
+ assert len(responses) == count, responses
+ assert all(len(future.result()) == 5 for future in futures)
+
+ @pytest.mark.p3
+ def test_default(self, add_document):
+ _, document = add_document
+ batch_add_chunks(document, 31)
+
+ from time import sleep
+
+ sleep(3)
+
+ chunks = document.list_chunks()
+ assert len(chunks) == 30, str(chunks)
+
+ @pytest.mark.p3
+ def test_list_chunks_invalid_document_id_raises(self, add_chunks):
+ _, document, _ = add_chunks
+ invalid_document = document.__class__(
+ document.rag,
+ {"id": "missing-document-id-for-chunks", "dataset_id": document.dataset_id},
+ )
+ with pytest.raises(Exception) as exception_info:
+ invalid_document.list_chunks()
+ assert str(exception_info.value), exception_info
diff --git a/test/testcases/test_sdk_api/test_chunk_management_within_dataset/test_retrieval_chunks.py b/test/testcases/test_sdk_api/test_chunk_management_within_dataset/test_retrieval_chunks.py
new file mode 100644
index 0000000000..112765b730
--- /dev/null
+++ b/test/testcases/test_sdk_api/test_chunk_management_within_dataset/test_retrieval_chunks.py
@@ -0,0 +1,245 @@
+#
+# Copyright 2025 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.
+#
+import os
+from concurrent.futures import ThreadPoolExecutor, as_completed
+
+import pytest
+
+DOC_ENGINE = (os.getenv("DOC_ENGINE") or "").lower()
+
+
+class TestChunksRetrieval:
+ @pytest.mark.p3
+ @pytest.mark.parametrize(
+ "payload, expected_page_size, expected_message",
+ [
+ ({"question": "chunk", "dataset_ids": None}, 4, ""),
+ ({"question": "chunk", "document_ids": None}, 0, "missing 1 required positional argument"),
+ ({"question": "chunk", "dataset_ids": None, "document_ids": None}, 4, ""),
+ ({"question": "chunk"}, 0, "missing 1 required positional argument"),
+ ],
+ )
+ def test_basic_scenarios(self, client, add_chunks, payload, expected_page_size, expected_message):
+ dataset, document, _ = add_chunks
+ if "dataset_ids" in payload:
+ payload["dataset_ids"] = [dataset.id]
+ if "document_ids" in payload:
+ payload["document_ids"] = [document.id]
+
+ if expected_message:
+ with pytest.raises(Exception) as exception_info:
+ client.retrieve(**payload)
+ assert expected_message in str(exception_info.value), str(exception_info.value)
+ else:
+ chunks = client.retrieve(**payload)
+ assert len(chunks) == expected_page_size, str(chunks)
+
+ @pytest.mark.p3
+ @pytest.mark.parametrize(
+ "payload, expected_page_size, expected_message",
+ [
+ pytest.param(
+ {"page": None, "page_size": 2},
+ 2,
+ """TypeError("int() argument must be a string, a bytes-like object or a real number, not \'NoneType\'")""",
+ marks=pytest.mark.skip,
+ ),
+ pytest.param(
+ {"page": 0, "page_size": 2},
+ 0,
+ "ValueError('Search does not support negative slicing.')",
+ marks=pytest.mark.skip,
+ ),
+ ({"page": 2, "page_size": 2}, 2, ""),
+ ({"page": 3, "page_size": 2}, 0, ""),
+ ({"page": "3", "page_size": 2}, 0, ""),
+ pytest.param(
+ {"page": -1, "page_size": 2},
+ 0,
+ "ValueError('Search does not support negative slicing.')",
+ marks=pytest.mark.skip,
+ ),
+ pytest.param(
+ {"page": "a", "page_size": 2},
+ 0,
+ """ValueError("invalid literal for int() with base 10: \'a\'")""",
+ marks=pytest.mark.skip,
+ ),
+ ],
+ )
+ def test_page(self, client, add_chunks, payload, expected_page_size, expected_message):
+ dataset, _, _ = add_chunks
+ payload.update({"question": "chunk", "dataset_ids": [dataset.id]})
+
+ if expected_message:
+ with pytest.raises(Exception) as exception_info:
+ client.retrieve(**payload)
+ assert expected_message in str(exception_info.value), str(exception_info.value)
+ else:
+ chunks = client.retrieve(**payload)
+ assert len(chunks) == expected_page_size, str(chunks)
+
+ @pytest.mark.p3
+ @pytest.mark.parametrize(
+ "payload, expected_page_size, expected_message",
+ [
+ pytest.param(
+ {"page_size": None},
+ 0,
+ """TypeError("int() argument must be a string, a bytes-like object or a real number, not \'NoneType\'")""",
+ marks=pytest.mark.skip,
+ ),
+ pytest.param({"page_size": 1}, 1, "", marks=pytest.mark.skip(reason="issues/10692")),
+ ({"page_size": 5}, 4, ""),
+ pytest.param({"page_size": "1"}, 1, "", marks=pytest.mark.skip(reason="issues/10692")),
+ pytest.param(
+ {"page_size": "a"},
+ 0,
+ """ValueError("invalid literal for int() with base 10: \'a\'")""",
+ marks=pytest.mark.skip,
+ ),
+ ],
+ )
+ def test_page_size(self, client, add_chunks, payload, expected_page_size, expected_message):
+ dataset, _, _ = add_chunks
+ payload.update({"question": "chunk", "dataset_ids": [dataset.id]})
+
+ if expected_message:
+ with pytest.raises(Exception) as exception_info:
+ client.retrieve(**payload)
+ assert expected_message in str(exception_info.value), str(exception_info.value)
+ else:
+ chunks = client.retrieve(**payload)
+ assert len(chunks) == expected_page_size, str(chunks)
+
+ @pytest.mark.p3
+ @pytest.mark.parametrize(
+ "payload, expected_page_size, expected_message",
+ [
+ ({"vector_similarity_weight": 0}, 4, ""),
+ ({"vector_similarity_weight": 0.5}, 4, ""),
+ ({"vector_similarity_weight": 10}, 4, ""),
+ pytest.param(
+ {"vector_similarity_weight": "a"},
+ 0,
+ """ValueError("could not convert string to float: 'a'")""",
+ marks=pytest.mark.skip,
+ ),
+ ],
+ )
+ def test_vector_similarity_weight(self, client, add_chunks, payload, expected_page_size, expected_message):
+ dataset, _, _ = add_chunks
+ payload.update({"question": "chunk", "dataset_ids": [dataset.id]})
+
+ if expected_message:
+ with pytest.raises(Exception) as exception_info:
+ client.retrieve(**payload)
+ assert expected_message in str(exception_info.value), str(exception_info.value)
+ else:
+ chunks = client.retrieve(**payload)
+ assert len(chunks) == expected_page_size, str(chunks)
+
+ @pytest.mark.p2
+ @pytest.mark.parametrize(
+ "payload, expected_page_size, expected_message",
+ [
+ ({"top_k": 10}, 4, ""),
+ pytest.param(
+ {"top_k": 1},
+ 4,
+ "",
+ marks=pytest.mark.skipif(DOC_ENGINE in ["infinity", "opensearch"], reason="Infinity"),
+ ),
+ pytest.param(
+ {"top_k": 1},
+ 1,
+ "",
+ marks=pytest.mark.skipif(DOC_ENGINE in ["", "opensearch", "elasticsearch"], reason="elasticsearch"),
+ ),
+ ({"top_k": -1}, 4, "must be greater than 0"),
+ pytest.param(
+ {"top_k": "a"},
+ 0,
+ """ValueError("invalid literal for int() with base 10: \'a\'")""",
+ marks=pytest.mark.skip,
+ ),
+ ],
+ )
+ def test_top_k(self, client, add_chunks, payload, expected_page_size, expected_message):
+ dataset, _, _ = add_chunks
+ payload.update({"question": "chunk", "dataset_ids": [dataset.id]})
+
+ if expected_message:
+ with pytest.raises(Exception) as exception_info:
+ client.retrieve(**payload)
+ assert expected_message in str(exception_info.value), str(exception_info.value)
+ else:
+ chunks = client.retrieve(**payload)
+ assert len(chunks) == expected_page_size, str(chunks)
+
+ @pytest.mark.skip
+ @pytest.mark.parametrize(
+ "payload, expected_message",
+ [
+ ({"rerank_id": "BAAI/bge-reranker-v2-m3"}, ""),
+ pytest.param({"rerank_id": "unknown"}, "LookupError('Model(unknown) not authorized')", marks=pytest.mark.skip),
+ ],
+ )
+ def test_rerank_id(self, client, add_chunks, payload, expected_message):
+ dataset, _, _ = add_chunks
+ payload.update({"question": "chunk", "dataset_ids": [dataset.id]})
+
+ if expected_message:
+ with pytest.raises(Exception) as exception_info:
+ client.retrieve(**payload)
+ assert expected_message in str(exception_info.value), str(exception_info.value)
+ else:
+ chunks = client.retrieve(**payload)
+ assert len(chunks) > 0, str(chunks)
+
+ @pytest.mark.skip
+ @pytest.mark.parametrize(
+ "payload, expected_page_size, expected_message",
+ [
+ ({"keyword": True}, 5, ""),
+ ({"keyword": "True"}, 5, ""),
+ ({"keyword": False}, 5, ""),
+ ({"keyword": "False"}, 5, ""),
+ ({"keyword": None}, 5, ""),
+ ],
+ )
+ def test_keyword(self, client, add_chunks, payload, expected_page_size, expected_message):
+ dataset, _, _ = add_chunks
+ payload.update({"question": "chunk test", "dataset_ids": [dataset.id]})
+
+ if expected_message:
+ with pytest.raises(Exception) as exception_info:
+ client.retrieve(**payload)
+ assert expected_message in str(exception_info.value), str(exception_info.value)
+ else:
+ chunks = client.retrieve(**payload)
+ assert len(chunks) == expected_page_size, str(chunks)
+
+ @pytest.mark.p3
+ def test_concurrent_retrieval(self, client, add_chunks):
+ dataset, _, _ = add_chunks
+ count = 100
+ payload = {"question": "chunk", "dataset_ids": [dataset.id]}
+
+ with ThreadPoolExecutor(max_workers=5) as executor:
+ futures = [executor.submit(client.retrieve, **payload) for _ in range(count)]
+ responses = list(as_completed(futures))
+ assert len(responses) == count, responses
diff --git a/test/testcases/test_sdk_api/test_chunk_management_within_dataset/test_update_chunk.py b/test/testcases/test_sdk_api/test_chunk_management_within_dataset/test_update_chunk.py
new file mode 100644
index 0000000000..460292da3f
--- /dev/null
+++ b/test/testcases/test_sdk_api/test_chunk_management_within_dataset/test_update_chunk.py
@@ -0,0 +1,171 @@
+#
+# Copyright 2025 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.
+#
+import os
+from concurrent.futures import ThreadPoolExecutor, as_completed
+from random import randint
+
+import pytest
+
+
+class TestUpdatedChunk:
+ @pytest.mark.p3
+ @pytest.mark.parametrize(
+ "payload, expected_message",
+ [
+ ({"content": None}, ""),
+ ({"content": ""}, "`content` is required"),
+ pytest.param(
+ {"content": 1},
+ "TypeError('expected string or bytes-like object')",
+ marks=pytest.mark.skip,
+ ),
+ ({"content": "update chunk"}, ""),
+ ({"content": " "}, "`content` is required"),
+ ({"content": "\n!?。;!?\"'"}, ""),
+ ],
+ )
+ def test_content(self, add_chunks, payload, expected_message):
+ _, _, chunks = add_chunks
+ chunk = chunks[0]
+
+ if expected_message:
+ with pytest.raises(Exception) as exception_info:
+ chunk.update(payload)
+ assert expected_message in str(exception_info.value), str(exception_info.value)
+ else:
+ chunk.update(payload)
+
+ @pytest.mark.p3
+ @pytest.mark.parametrize(
+ "payload, expected_message",
+ [
+ ({"important_keywords": ["a", "b", "c"]}, ""),
+ ({"important_keywords": [""]}, ""),
+ ({"important_keywords": [1]}, "TypeError('sequence item 0: expected str instance, int found')"),
+ ({"important_keywords": ["a", "a"]}, ""),
+ ({"important_keywords": "abc"}, "`important_keywords` should be a list"),
+ ({"important_keywords": 123}, "`important_keywords` should be a list"),
+ ],
+ )
+ def test_important_keywords(self, add_chunks, payload, expected_message):
+ _, _, chunks = add_chunks
+ chunk = chunks[0]
+
+ if expected_message:
+ with pytest.raises(Exception) as exception_info:
+ chunk.update(payload)
+ assert expected_message in str(exception_info.value), str(exception_info.value)
+ else:
+ chunk.update(payload)
+
+ @pytest.mark.p2
+ @pytest.mark.parametrize(
+ "payload, expected_message",
+ [
+ ({"questions": ["a", "b", "c"]}, ""),
+ ({"questions": [""]}, ""),
+ ({"questions": [1]}, "TypeError('sequence item 0: expected str instance, int found')"),
+ ({"questions": ["a", "a"]}, ""),
+ ({"questions": "abc"}, "`questions` should be a list"),
+ ({"questions": 123}, "`questions` should be a list"),
+ ],
+ )
+ def test_questions(self, add_chunks, payload, expected_message):
+ _, _, chunks = add_chunks
+ chunk = chunks[0]
+
+ if expected_message:
+ with pytest.raises(Exception) as exception_info:
+ chunk.update(payload)
+ assert expected_message in str(exception_info.value), str(exception_info.value)
+ else:
+ chunk.update(payload)
+
+ @pytest.mark.p2
+ @pytest.mark.parametrize(
+ "payload, expected_message",
+ [
+ ({"tag_kwd": ["tag1", "tag2"]}, ""),
+ ({"tag_kwd": [""]}, ""),
+ ({"tag_kwd": [1]}, "`tag_kwd` must be a list of strings"),
+ ({"tag_kwd": ["tag", "tag"]}, ""),
+ ({"tag_kwd": "tag"}, "`tag_kwd` should be a list"),
+ ({"tag_kwd": 123}, "`tag_kwd` should be a list"),
+ ],
+ )
+ def test_tag_kwd(self, add_chunks, payload, expected_message):
+ _, _, chunks = add_chunks
+ chunk = chunks[0]
+
+ if expected_message:
+ with pytest.raises(Exception) as exception_info:
+ chunk.update(payload)
+ assert expected_message in str(exception_info.value), str(exception_info.value)
+ else:
+ chunk.update(payload)
+
+ @pytest.mark.p2
+ @pytest.mark.parametrize(
+ "payload, expected_message",
+ [
+ ({"available": True}, ""),
+ pytest.param({"available": "True"}, """ValueError("invalid literal for int() with base 10: \'True\'")""", marks=pytest.mark.skip),
+ ({"available": 1}, ""),
+ ({"available": False}, ""),
+ pytest.param({"available": "False"}, """ValueError("invalid literal for int() with base 10: \'False\'")""", marks=pytest.mark.skip),
+ ({"available": 0}, ""),
+ ],
+ )
+ def test_available(self, add_chunks, payload, expected_message):
+ _, _, chunks = add_chunks
+ chunk = chunks[0]
+
+ if expected_message:
+ with pytest.raises(Exception) as exception_info:
+ chunk.update(payload)
+ assert expected_message in str(exception_info.value), str(exception_info.value)
+ else:
+ chunk.update(payload)
+
+ @pytest.mark.p3
+ def test_repeated_update_chunk(self, add_chunks):
+ _, _, chunks = add_chunks
+ chunk = chunks[0]
+
+ chunk.update({"content": "chunk test 1"})
+ chunk.update({"content": "chunk test 2"})
+
+ @pytest.mark.p3
+ @pytest.mark.skipif(os.getenv("DOC_ENGINE") == "infinity", reason="issues/6554")
+ def test_concurrent_update_chunk(self, add_chunks):
+ count = 50
+ _, _, chunks = add_chunks
+
+ with ThreadPoolExecutor(max_workers=5) as executor:
+ futures = [executor.submit(chunks[randint(0, 3)].update, {"content": f"update chunk test {i}"}) for i in range(count)]
+ responses = list(as_completed(futures))
+ assert len(responses) == count, responses
+
+ @pytest.mark.p3
+ def test_update_chunk_to_deleted_document(self, add_chunks):
+ dataset, document, chunks = add_chunks
+ dataset.delete_documents(ids=[document.id])
+
+ with pytest.raises(Exception) as exception_info:
+ chunks[0].update({})
+ message = str(exception_info.value)
+ ownership_message = f"you don't own the document {chunks[0].document_id}"
+ assert message.rstrip(".") == ownership_message or message == f"Can't find this chunk {chunks[0].id}", message
diff --git a/test/testcases/test_sdk_api/test_dataset_mangement/conftest.py b/test/testcases/test_sdk_api/test_dataset_mangement/conftest.py
new file mode 100644
index 0000000000..998af94995
--- /dev/null
+++ b/test/testcases/test_sdk_api/test_dataset_mangement/conftest.py
@@ -0,0 +1,39 @@
+#
+# Copyright 2025 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.
+#
+
+
+import pytest
+from common import batch_create_datasets, delete_all_datasets
+
+
[email protected](scope="class")
+def add_datasets(client, request):
+ def cleanup():
+ delete_all_datasets(client)
+
+ request.addfinalizer(cleanup)
+
+ return batch_create_datasets(client, 5)
+
+
[email protected](scope="function")
+def add_datasets_func(client, request):
+ def cleanup():
+ delete_all_datasets(client)
+
+ request.addfinalizer(cleanup)
+
+ return batch_create_datasets(client, 3)
diff --git a/test/testcases/test_sdk_api/test_dataset_mangement/test_create_dataset.py b/test/testcases/test_sdk_api/test_dataset_mangement/test_create_dataset.py
new file mode 100644
index 0000000000..39de107137
--- /dev/null
+++ b/test/testcases/test_sdk_api/test_dataset_mangement/test_create_dataset.py
@@ -0,0 +1,702 @@
+#
+# Copyright 2025 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.
+#
+from concurrent.futures import ThreadPoolExecutor, as_completed
+from operator import attrgetter
+
+import pytest
+from configs import DATASET_NAME_LIMIT, DEFAULT_PARSER_CONFIG, HOST_ADDRESS, INVALID_API_TOKEN, IS_GO_PROXY, SDK_UNAUTHORIZED_ERROR_MESSAGE
+from hypothesis import example, given, settings
+from ragflow_sdk import DataSet, RAGFlow
+from utils import encode_avatar
+from utils.file_utils import create_image_file
+from utils.hypothesis_utils import valid_names
+
+
[email protected]("clear_datasets")
+class TestAuthorization:
+ @pytest.mark.p2
+ @pytest.mark.parametrize(
+ "invalid_auth, expected_message",
+ [
+ (None, SDK_UNAUTHORIZED_ERROR_MESSAGE),
+ (INVALID_API_TOKEN, SDK_UNAUTHORIZED_ERROR_MESSAGE),
+ ],
+ ids=["empty_auth", "invalid_api_token"],
+ )
+ def test_auth_invalid(self, invalid_auth, expected_message):
+ client = RAGFlow(invalid_auth, HOST_ADDRESS)
+ with pytest.raises(Exception) as exception_info:
+ client.create_dataset(**{"name": "auth_test"})
+ assert str(exception_info.value) == expected_message
+
+
[email protected]("clear_datasets")
+class TestCapability:
+ @pytest.mark.p3
+ def test_create_dataset_1k(self, client):
+ count = 1_000
+ for i in range(count):
+ payload = {"name": f"dataset_{i}"}
+ client.create_dataset(**payload)
+ datasets = []
+ for page in range(1, (count // 100) + 1):
+ datasets.extend(client.list_datasets(page=page, page_size=100))
+ assert len(datasets) == count
+
+ @pytest.mark.p3
+ def test_create_dataset_concurrent(self, client):
+ count = 100
+ with ThreadPoolExecutor(max_workers=5) as executor:
+ futures = [executor.submit(client.create_dataset, **{"name": f"dataset_{i}"}) for i in range(100)]
+ responses = list(as_completed(futures))
+ assert len(responses) == count, responses
+
+
[email protected]("clear_datasets")
+class TestDatasetCreate:
+ @pytest.mark.p1
+ @given(name=valid_names())
+ @example("a" * 128)
+ @settings(max_examples=20)
+ def test_name(self, client, name):
+ dataset = client.create_dataset(**{"name": name})
+ assert dataset.name == name, str(dataset)
+
+ @pytest.mark.p2
+ @pytest.mark.parametrize(
+ "name, expected_message",
+ [
+ ("", "String should have at least 1 character"),
+ (" ", "String should have at least 1 character"),
+ ("a" * (DATASET_NAME_LIMIT + 1), "String should have at most 128 characters"),
+ (0, "not instance of"),
+ (None, "not instance of"),
+ ],
+ ids=["empty_name", "space_name", "too_long_name", "invalid_name", "None_name"],
+ )
+ def test_name_invalid(self, client, name, expected_message):
+ with pytest.raises(Exception) as exception_info:
+ client.create_dataset(**{"name": name})
+ if IS_GO_PROXY:
+ if name == "":
+ expected_message = "failed on the 'required' tag"
+ elif isinstance(name, str) and not name.strip():
+ expected_message = "dataset name can't be empty"
+ elif isinstance(name, str):
+ expected_message = f"Dataset name length is {len(name)} which is large than {DATASET_NAME_LIMIT}"
+ assert expected_message in str(exception_info.value), str(exception_info.value)
+
+ @pytest.mark.p3
+ def test_name_duplicated(self, client):
+ name = "duplicated_name"
+ payload = {"name": name}
+ client.create_dataset(**payload)
+
+ dataset = client.create_dataset(**payload)
+ assert dataset.name == name + "(1)", str(dataset)
+
+ @pytest.mark.p3
+ def test_name_case_insensitive(self, client):
+ name = "CaseInsensitive"
+ payload = {"name": name.upper()}
+ client.create_dataset(**payload)
+
+ payload = {"name": name.lower()}
+ dataset = client.create_dataset(**payload)
+ assert dataset.name == name.lower() + "(1)", str(dataset)
+
+ @pytest.mark.p2
+ def test_avatar(self, client, tmp_path):
+ fn = create_image_file(tmp_path / "ragflow_test.png")
+ payload = {
+ "name": "avatar",
+ "avatar": f"data:image/png;base64,{encode_avatar(fn)}",
+ }
+ client.create_dataset(**payload)
+
+ @pytest.mark.p3
+ def test_avatar_exceeds_limit_length(self, client):
+ payload = {"name": "avatar_exceeds_limit_length", "avatar": "a" * 65536}
+ with pytest.raises(Exception) as exception_info:
+ client.create_dataset(**payload)
+ assert "String should have at most 65535 characters" in str(exception_info.value), str(exception_info.value)
+
+ @pytest.mark.p3
+ @pytest.mark.parametrize(
+ "name, prefix, expected_message",
+ [
+ ("empty_prefix", "", "missing MIME prefix. Expected format: data:<mime>;base64,<data>"),
+ ("missing_comma", "data:image/png;base64", "missing MIME prefix. Expected format: data:<mime>;base64,<data>"),
+ ("unsupported_mine_type", "invalid_mine_prefix:image/png;base64,", "invalid MIME prefix format. Must start with 'data:'"),
+ ("invalid_mine_type", "data:unsupported_mine_type;base64,", "Unsupported MIME type. Allowed: ['image/jpeg', 'image/png']"),
+ ],
+ ids=["empty_prefix", "missing_comma", "unsupported_mine_type", "invalid_mine_type"],
+ )
+ def test_avatar_invalid_prefix(self, client, tmp_path, name, prefix, expected_message):
+ fn = create_image_file(tmp_path / "ragflow_test.png")
+ payload = {
+ "name": name,
+ "avatar": f"{prefix}{encode_avatar(fn)}",
+ }
+ with pytest.raises(Exception) as exception_info:
+ client.create_dataset(**payload)
+ assert expected_message in str(exception_info.value), str(exception_info.value)
+
+ @pytest.mark.p3
+ def test_avatar_unset(self, client):
+ payload = {"name": "avatar_unset"}
+ dataset = client.create_dataset(**payload)
+ assert dataset.avatar is None, str(dataset)
+
+ @pytest.mark.p2
+ def test_description(self, client):
+ payload = {"name": "description", "description": "description"}
+ dataset = client.create_dataset(**payload)
+ assert dataset.description == "description", str(dataset)
+
+ @pytest.mark.p3
+ def test_description_exceeds_limit_length(self, client):
+ payload = {"name": "description_exceeds_limit_length", "description": "a" * 65536}
+ with pytest.raises(Exception) as exception_info:
+ client.create_dataset(**payload)
+ assert "String should have at most 65535 characters" in str(exception_info.value), str(exception_info.value)
+
+ @pytest.mark.p3
+ def test_description_unset(self, client):
+ payload = {"name": "description_unset"}
+ dataset = client.create_dataset(**payload)
+ assert dataset.description is None, str(dataset)
+
+ @pytest.mark.p3
+ def test_description_none(self, client):
+ payload = {"name": "description_none", "description": None}
+ dataset = client.create_dataset(**payload)
+ assert dataset.description is None, str(dataset)
+
+ @pytest.mark.p1
+ @pytest.mark.parametrize(
+ "name, embedding_model",
+ [
+ ("BAAI/bge-small-en-v1.5@Builtin", "BAAI/bge-small-en-v1.5@Builtin"),
+ ("embedding-3@ZHIPU-AI", "embedding-3@CI@ZHIPU-AI"),
+ ],
+ ids=["builtin_baai", "tenant_zhipu"],
+ )
+ def test_embedding_model(self, client, name, embedding_model):
+ payload = {"name": name, "embedding_model": embedding_model}
+ dataset = client.create_dataset(**payload)
+ assert dataset.embedding_model == embedding_model, str(dataset)
+
+ @pytest.mark.p2
+ @pytest.mark.parametrize(
+ "name, embedding_model",
+ [
+ ("unknown_llm_name", "unknown@CI@ZHIPU-AI"),
+ ("unknown_llm_factory", "embedding-3@unknown"),
+ ("tenant_no_auth_default_tenant_llm", "text-embedding-v3@Tongyi-Qianwen"),
+ ("tenant_no_auth", "text-embedding-3-small@OpenAI"),
+ ],
+ ids=["unknown_llm_name", "unknown_llm_factory", "tenant_no_auth_default_tenant_llm", "tenant_no_auth"],
+ )
+ def test_embedding_model_invalid(self, client, name, embedding_model):
+ payload = {"name": name, "embedding_model": embedding_model}
+ with pytest.raises(Exception) as exception_info:
+ client.create_dataset(**payload)
+ assert "not found" in str(exception_info.value), str(exception_info.value)
+
+ @pytest.mark.p2
+ @pytest.mark.parametrize(
+ "name, embedding_model",
+ [
+ ("empty", ""),
+ ("space", " "),
+ ("missing_at", "BAAI/bge-small-en-v1.5Builtin"),
+ ("missing_model_name", "@Builtin"),
+ ("missing_provider", "BAAI/bge-small-en-v1.5@"),
+ ("whitespace_only_model_name", " @Builtin"),
+ ("whitespace_only_provider", "BAAI/bge-small-en-v1.5@ "),
+ ],
+ ids=["empty", "space", "missing_at", "empty_model_name", "empty_provider", "whitespace_only_model_name", "whitespace_only_provider"],
+ )
+ def test_embedding_model_format(self, client, name, embedding_model):
+ payload = {"name": name, "embedding_model": embedding_model}
+ with pytest.raises(Exception) as exception_info:
+ client.create_dataset(**payload)
+ if name in ["empty", "space", "missing_at"]:
+ assert "embedding model identifier must follow <model_name>@<provider> format" in str(exception_info.value), str(exception_info.value)
+ else:
+ assert "both model_name and provider must be non-empty strings" in str(exception_info.value), str(exception_info.value)
+
+ @pytest.mark.p2
+ def test_embedding_model_unset(self, client):
+ payload = {"name": "embedding_model_unset"}
+ dataset = client.create_dataset(**payload)
+ assert dataset.embedding_model.startswith("BAAI/bge-small-en-v1.5@Local@Builtin") if IS_GO_PROXY else dataset.embedding_model == "BAAI/bge-small-en-v1.5@Local@Builtin", str(dataset)
+
+ @pytest.mark.p2
+ def test_embedding_model_none(self, client):
+ payload = {"name": "embedding_model_none", "embedding_model": None}
+ dataset = client.create_dataset(**payload)
+ assert dataset.embedding_model.startswith("BAAI/bge-small-en-v1.5@Local@Builtin") if IS_GO_PROXY else dataset.embedding_model == "BAAI/bge-small-en-v1.5@Local@Builtin", str(dataset)
+
+ @pytest.mark.p2
+ @pytest.mark.parametrize(
+ "name, permission",
+ [
+ ("me", "me"),
+ ("team", "team"),
+ ],
+ ids=["me", "team"],
+ )
+ def test_permission(self, client, name, permission):
+ payload = {"name": name, "permission": permission}
+ dataset = client.create_dataset(**payload)
+ assert dataset.permission == permission.lower().strip(), str(dataset)
+
+ @pytest.mark.p2
+ @pytest.mark.parametrize(
+ "name, permission",
+ [
+ ("empty", ""),
+ ("unknown", "unknown"),
+ ("me_upercase", "ME"),
+ ("team_upercase", "TEAM"),
+ ("whitespace", " ME "),
+ ],
+ ids=["empty", "unknown", "me_upercase", "team_upercase", "whitespace"],
+ )
+ def test_permission_invalid(self, client, name, permission):
+ payload = {"name": name, "permission": permission}
+ with pytest.raises(Exception) as exception_info:
+ client.create_dataset(**payload)
+ assert "Input should be 'me' or 'team'" in str(exception_info.value)
+
+ @pytest.mark.p2
+ def test_permission_unset(self, client):
+ payload = {"name": "permission_unset"}
+ dataset = client.create_dataset(**payload)
+ assert dataset.permission == "me", str(dataset)
+
+ @pytest.mark.p3
+ def test_permission_none(self, client):
+ payload = {"name": "permission_none", "permission": None}
+ with pytest.raises(Exception) as exception_info:
+ client.create_dataset(**payload)
+ assert "not instance of" in str(exception_info.value), str(exception_info.value)
+
+ @pytest.mark.p1
+ @pytest.mark.parametrize(
+ "name, chunk_method",
+ [
+ ("naive", "naive"),
+ ("book", "book"),
+ ("email", "email"),
+ ("laws", "laws"),
+ ("manual", "manual"),
+ ("one", "one"),
+ ("paper", "paper"),
+ ("picture", "picture"),
+ ("presentation", "presentation"),
+ ("qa", "qa"),
+ ("table", "table"),
+ ("tag", "tag"),
+ ("resume", "resume"),
+ ],
+ ids=["naive", "book", "email", "laws", "manual", "one", "paper", "picture", "presentation", "qa", "table", "tag", "resume"],
+ )
+ def test_chunk_method(self, client, name, chunk_method):
+ payload = {"name": name, "chunk_method": chunk_method}
+ dataset = client.create_dataset(**payload)
+ assert dataset.chunk_method == chunk_method, str(dataset)
+
+ @pytest.mark.p2
+ @pytest.mark.parametrize(
+ "name, chunk_method",
+ [
+ ("empty", ""),
+ ("unknown", "unknown"),
+ ],
+ ids=["empty", "unknown"],
+ )
+ def test_chunk_method_invalid(self, client, name, chunk_method):
+ payload = {"name": name, "chunk_method": chunk_method}
+ with pytest.raises(Exception) as exception_info:
+ client.create_dataset(**payload)
+ error_message = str(exception_info.value)
+ if IS_GO_PROXY:
+ assert error_message.startswith("Input should be 'naive', 'book'") and error_message.endswith("or 'tag'"), error_message
+ else:
+ assert "Input should be 'naive', 'book', 'email', 'laws', 'manual', 'one', 'paper', 'picture', 'presentation', 'qa', 'table', 'tag' or 'resume'" in error_message, error_message
+
+ @pytest.mark.p2
+ def test_chunk_method_unset(self, client):
+ payload = {"name": "chunk_method_unset"}
+ dataset = client.create_dataset(**payload)
+ assert dataset.chunk_method == "naive", str(dataset)
+
+ @pytest.mark.p3
+ def test_chunk_method_none(self, client):
+ payload = {"name": "chunk_method_none", "chunk_method": None}
+ with pytest.raises(Exception) as exception_info:
+ client.create_dataset(**payload)
+ assert "not instance of" in str(exception_info.value), str(exception_info.value)
+
+ @pytest.mark.p1
+ @pytest.mark.parametrize(
+ "name, parser_config",
+ [
+ ("auto_keywords_min", {"auto_keywords": 0}),
+ ("auto_keywords_mid", {"auto_keywords": 16}),
+ ("auto_keywords_max", {"auto_keywords": 32}),
+ ("auto_questions_min", {"auto_questions": 0}),
+ ("auto_questions_mid", {"auto_questions": 5}),
+ ("auto_questions_max", {"auto_questions": 10}),
+ ("chunk_token_num_min", {"chunk_token_num": 1}),
+ ("chunk_token_num_mid", {"chunk_token_num": 1024}),
+ ("chunk_token_num_max", {"chunk_token_num": 2048}),
+ ("delimiter", {"delimiter": "\n"}),
+ ("delimiter_space", {"delimiter": " "}),
+ ("html4excel_true", {"html4excel": True}),
+ ("html4excel_false", {"html4excel": False}),
+ ("layout_recognize_DeepDOC", {"layout_recognize": "DeepDOC"}),
+ ("layout_recognize_navie", {"layout_recognize": "Plain Text"}),
+ ("tag_kb_ids", {"tag_kb_ids": ["1", "2"]}),
+ ("topn_tags_min", {"topn_tags": 1}),
+ ("topn_tags_mid", {"topn_tags": 5}),
+ ("topn_tags_max", {"topn_tags": 10}),
+ ("filename_embd_weight_min", {"filename_embd_weight": 0.1}),
+ ("filename_embd_weight_mid", {"filename_embd_weight": 0.5}),
+ ("filename_embd_weight_max", {"filename_embd_weight": 1.0}),
+ ("task_page_size_min", {"task_page_size": 1}),
+ ("task_page_size_None", {"task_page_size": None}),
+ ("pages", {"pages": [[1, 100]]}),
+ ("pages_none", {"pages": None}),
+ ("graphrag_true", {"graphrag": {"use_graphrag": True}}),
+ ("graphrag_false", {"graphrag": {"use_graphrag": False}}),
+ ("graphrag_entity_types", {"graphrag": {"entity_types": ["age", "sex", "height", "weight"]}}),
+ ("graphrag_method_general", {"graphrag": {"method": "general"}}),
+ ("graphrag_method_light", {"graphrag": {"method": "light"}}),
+ ("graphrag_community_true", {"graphrag": {"community": True}}),
+ ("graphrag_community_false", {"graphrag": {"community": False}}),
+ ("graphrag_resolution_true", {"graphrag": {"resolution": True}}),
+ ("graphrag_resolution_false", {"graphrag": {"resolution": False}}),
+ ("raptor_true", {"raptor": {"use_raptor": True}}),
+ ("raptor_false", {"raptor": {"use_raptor": False}}),
+ ("raptor_prompt", {"raptor": {"prompt": "Who are you?"}}),
+ ("raptor_max_token_min", {"raptor": {"max_token": 512}}),
+ ("raptor_max_token_mid", {"raptor": {"max_token": 1024}}),
+ ("raptor_max_token_max", {"raptor": {"max_token": 2048}}),
+ ("raptor_clustering_threshold_min", {"raptor": {"clustering_threshold": 0.0}}),
+ ("raptor_clustering_threshold_mid", {"raptor": {"clustering_threshold": 0.5}}),
+ ("raptor_clustering_threshold_max", {"raptor": {"clustering_threshold": 1.0}}),
+ ("raptor_max_cluster_min", {"raptor": {"max_cluster": 1}}),
+ ("raptor_max_cluster_mid", {"raptor": {"max_cluster": 512}}),
+ ("raptor_max_cluster_max", {"raptor": {"max_cluster": 1024}}),
+ ("raptor_random_seed_min", {"raptor": {"random_seed": 0}}),
+ ],
+ ids=[
+ "auto_keywords_min",
+ "auto_keywords_mid",
+ "auto_keywords_max",
+ "auto_questions_min",
+ "auto_questions_mid",
+ "auto_questions_max",
+ "chunk_token_num_min",
+ "chunk_token_num_mid",
+ "chunk_token_num_max",
+ "delimiter",
+ "delimiter_space",
+ "html4excel_true",
+ "html4excel_false",
+ "layout_recognize_DeepDOC",
+ "layout_recognize_navie",
+ "tag_kb_ids",
+ "topn_tags_min",
+ "topn_tags_mid",
+ "topn_tags_max",
+ "filename_embd_weight_min",
+ "filename_embd_weight_mid",
+ "filename_embd_weight_max",
+ "task_page_size_min",
+ "task_page_size_None",
+ "pages",
+ "pages_none",
+ "graphrag_true",
+ "graphrag_false",
+ "graphrag_entity_types",
+ "graphrag_method_general",
+ "graphrag_method_light",
+ "graphrag_community_true",
+ "graphrag_community_false",
+ "graphrag_resolution_true",
+ "graphrag_resolution_false",
+ "raptor_true",
+ "raptor_false",
+ "raptor_prompt",
+ "raptor_max_token_min",
+ "raptor_max_token_mid",
+ "raptor_max_token_max",
+ "raptor_clustering_threshold_min",
+ "raptor_clustering_threshold_mid",
+ "raptor_clustering_threshold_max",
+ "raptor_max_cluster_min",
+ "raptor_max_cluster_mid",
+ "raptor_max_cluster_max",
+ "raptor_random_seed_min",
+ ],
+ )
+ def test_parser_config(self, client, name, parser_config):
+ parser_config_o = DataSet.ParserConfig(client, parser_config)
+ payload = {"name": name, "parser_config": parser_config_o}
+ dataset = client.create_dataset(**payload)
+ for k, v in parser_config.items():
+ if k in {"graphrag", "raptor"}:
+ assert not hasattr(dataset.parser_config, k), str(dataset)
+ continue
+ if isinstance(v, dict):
+ for kk, vv in v.items():
+ assert attrgetter(f"{k}.{kk}")(dataset.parser_config) == vv, str(dataset)
+ else:
+ assert attrgetter(k)(dataset.parser_config) == v, str(dataset)
+
+ @pytest.mark.p2
+ @pytest.mark.parametrize(
+ "name, parser_config, expected_message",
+ [
+ ("auto_keywords_min_limit", {"auto_keywords": -1}, "Input should be greater than or equal to 0"),
+ ("auto_keywords_max_limit", {"auto_keywords": 33}, "Input should be less than or equal to 32"),
+ ("auto_keywords_float_not_allowed", {"auto_keywords": 3.14}, "Input should be a valid integer"),
+ ("auto_keywords_type_invalid", {"auto_keywords": "string"}, "Input should be a valid integer"),
+ ("auto_questions_min_limit", {"auto_questions": -1}, "Input should be greater than or equal to 0"),
+ ("auto_questions_max_limit", {"auto_questions": 11}, "Input should be less than or equal to 10"),
+ ("auto_questions_float_not_allowed", {"auto_questions": 3.14}, "Input should be a valid integer"),
+ ("auto_questions_type_invalid", {"auto_questions": "string"}, "Input should be a valid integer"),
+ ("chunk_token_num_min_limit", {"chunk_token_num": 0}, "Input should be greater than or equal to 1"),
+ ("chunk_token_num_max_limit", {"chunk_token_num": 2049}, "Input should be less than or equal to 2048"),
+ ("chunk_token_num_float_not_allowed", {"chunk_token_num": 3.14}, "Input should be a valid integer"),
+ ("chunk_token_num_type_invalid", {"chunk_token_num": "string"}, "Input should be a valid integer"),
+ ("delimiter_empty", {"delimiter": ""}, "String should have at least 1 character"),
+ ("html4excel_type_invalid", {"html4excel": "string"}, "Input should be a valid boolean"),
+ ("tag_kb_ids_not_list", {"tag_kb_ids": "1,2"}, "Input should be a valid list"),
+ ("tag_kb_ids_int_in_list", {"tag_kb_ids": [1, 2]}, "Input should be a valid string"),
+ ("topn_tags_min_limit", {"topn_tags": 0}, "Input should be greater than or equal to 1"),
+ ("topn_tags_max_limit", {"topn_tags": 11}, "Input should be less than or equal to 10"),
+ ("topn_tags_float_not_allowed", {"topn_tags": 3.14}, "Input should be a valid integer"),
+ ("topn_tags_type_invalid", {"topn_tags": "string"}, "Input should be a valid integer"),
+ ("filename_embd_weight_min_limit", {"filename_embd_weight": -1}, "Input should be greater than or equal to 0"),
+ ("filename_embd_weight_max_limit", {"filename_embd_weight": 1.1}, "Input should be less than or equal to 1"),
+ ("filename_embd_weight_type_invalid", {"filename_embd_weight": "string"}, "Input should be a valid number"),
+ ("task_page_size_min_limit", {"task_page_size": 0}, "Input should be greater than or equal to 1"),
+ ("task_page_size_float_not_allowed", {"task_page_size": 3.14}, "Input should be a valid integer"),
+ ("task_page_size_type_invalid", {"task_page_size": "string"}, "Input should be a valid integer"),
+ ("pages_not_list", {"pages": "1,2"}, "Input should be a valid list"),
+ ("pages_not_list_in_list", {"pages": ["1,2"]}, "Input should be a valid list"),
+ ("pages_not_int_list", {"pages": [["string1", "string2"]]}, "Input should be a valid integer"),
+ ("graphrag_type_invalid", {"graphrag": {"use_graphrag": "string"}}, "Input should be a valid boolean"),
+ ("graphrag_entity_types_not_list", {"graphrag": {"entity_types": "1,2"}}, "Input should be a valid list"),
+ ("graphrag_entity_types_not_str_in_list", {"graphrag": {"entity_types": [1, 2]}}, "nput should be a valid string"),
+ ("graphrag_method_unknown", {"graphrag": {"method": "unknown"}}, "Input should be 'light', 'general' or 'ner'"),
+ ("graphrag_method_none", {"graphrag": {"method": None}}, "Input should be 'light', 'general' or 'ner'"),
+ ("graphrag_community_type_invalid", {"graphrag": {"community": "string"}}, "Input should be a valid boolean"),
+ ("graphrag_resolution_type_invalid", {"graphrag": {"resolution": "string"}}, "Input should be a valid boolean"),
+ ("raptor_type_invalid", {"raptor": {"use_raptor": "string"}}, "Input should be a valid boolean"),
+ ("raptor_prompt_empty", {"raptor": {"prompt": ""}}, "String should have at least 1 character"),
+ ("raptor_prompt_space", {"raptor": {"prompt": " "}}, "String should have at least 1 character"),
+ ("raptor_max_token_max_limit", {"raptor": {"max_token": 2049}}, "Input should be less than or equal to 2048"),
+ ("raptor_max_token_type_invalid", {"raptor": {"max_token": "string"}}, "Input should be a valid integer"),
+ ("raptor_clustering_threshold_min_limit", {"raptor": {"clustering_threshold": -0.1}}, "Input should be greater than or equal to 0"),
+ ("raptor_clustering_threshold_max_limit", {"raptor": {"clustering_threshold": 1.1}}, "Input should be less than or equal to 1"),
+ ("raptor_clustering_threshold_type_invalid", {"raptor": {"clustering_threshold": "string"}}, "Input should be a valid number"),
+ ("raptor_max_cluster_min_limit", {"raptor": {"max_cluster": 0}}, "Input should be greater than or equal to 1"),
+ ("raptor_max_cluster_max_limit", {"raptor": {"max_cluster": 1025}}, "Input should be less than or equal to 1024"),
+ ("raptor_max_cluster_float_not_allowed", {"raptor": {"max_cluster": 3.14}}, "Input should be a valid integer"),
+ ("raptor_max_cluster_type_invalid", {"raptor": {"max_cluster": "string"}}, "Input should be a valid integer"),
+ ("raptor_random_seed_min_limit", {"raptor": {"random_seed": -1}}, "Input should be greater than or equal to 0"),
+ ("raptor_random_seed_float_not_allowed", {"raptor": {"random_seed": 3.14}}, "Input should be a valid integer"),
+ ("raptor_random_seed_type_invalid", {"raptor": {"random_seed": "string"}}, "Input should be a valid integer"),
+ ("parser_config_type_invalid", {"delimiter": "a" * 65536}, "Parser config exceeds size limit (max 65,535 characters)"),
+ ],
+ ids=[
+ "auto_keywords_min_limit",
+ "auto_keywords_max_limit",
+ "auto_keywords_float_not_allowed",
+ "auto_keywords_type_invalid",
+ "auto_questions_min_limit",
+ "auto_questions_max_limit",
+ "auto_questions_float_not_allowed",
+ "auto_questions_type_invalid",
+ "chunk_token_num_min_limit",
+ "chunk_token_num_max_limit",
+ "chunk_token_num_float_not_allowed",
+ "chunk_token_num_type_invalid",
+ "delimiter_empty",
+ "html4excel_type_invalid",
+ "tag_kb_ids_not_list",
+ "tag_kb_ids_int_in_list",
+ "topn_tags_min_limit",
+ "topn_tags_max_limit",
+ "topn_tags_float_not_allowed",
+ "topn_tags_type_invalid",
+ "filename_embd_weight_min_limit",
+ "filename_embd_weight_max_limit",
+ "filename_embd_weight_type_invalid",
+ "task_page_size_min_limit",
+ "task_page_size_float_not_allowed",
+ "task_page_size_type_invalid",
+ "pages_not_list",
+ "pages_not_list_in_list",
+ "pages_not_int_list",
+ "graphrag_type_invalid",
+ "graphrag_entity_types_not_list",
+ "graphrag_entity_types_not_str_in_list",
+ "graphrag_method_unknown",
+ "graphrag_method_none",
+ "graphrag_community_type_invalid",
+ "graphrag_resolution_type_invalid",
+ "raptor_type_invalid",
+ "raptor_prompt_empty",
+ "raptor_prompt_space",
+ "raptor_max_token_max_limit",
+ "raptor_max_token_type_invalid",
+ "raptor_clustering_threshold_min_limit",
+ "raptor_clustering_threshold_max_limit",
+ "raptor_clustering_threshold_type_invalid",
+ "raptor_max_cluster_min_limit",
+ "raptor_max_cluster_max_limit",
+ "raptor_max_cluster_float_not_allowed",
+ "raptor_max_cluster_type_invalid",
+ "raptor_random_seed_min_limit",
+ "raptor_random_seed_float_not_allowed",
+ "raptor_random_seed_type_invalid",
+ "parser_config_type_invalid",
+ ],
+ )
+ def test_parser_config_invalid(self, client, name, parser_config, expected_message):
+ parser_config_o = DataSet.ParserConfig(client, parser_config)
+ payload = {"name": name, "parser_config": parser_config_o}
+ with pytest.raises(Exception) as exception_info:
+ client.create_dataset(**payload)
+ assert expected_message in str(exception_info.value), str(exception_info.value)
+
+ @pytest.mark.p2
+ def test_parser_config_empty(self, client):
+ excepted_value = DataSet.ParserConfig(
+ client,
+ DEFAULT_PARSER_CONFIG,
+ )
+ parser_config_o = DataSet.ParserConfig(client, {})
+ payload = {"name": "parser_config_empty", "parser_config": parser_config_o}
+ dataset = client.create_dataset(**payload)
+ assert str(dataset.parser_config) == str(excepted_value), str(dataset)
+
+ @pytest.mark.p2
+ def test_parser_config_unset(self, client):
+ excepted_value = DataSet.ParserConfig(
+ client,
+ DEFAULT_PARSER_CONFIG,
+ )
+ payload = {"name": "parser_config_unset"}
+ dataset = client.create_dataset(**payload)
+ assert str(dataset.parser_config) == str(excepted_value), str(dataset)
+
+ @pytest.mark.p3
+ def test_parser_config_none(self, client):
+ excepted_value = DataSet.ParserConfig(
+ client,
+ DEFAULT_PARSER_CONFIG,
+ )
+ payload = {"name": "parser_config_empty", "parser_config": None}
+ dataset = client.create_dataset(**payload)
+ assert str(dataset.parser_config) == str(excepted_value), str(dataset)
+
+ @pytest.mark.p2
+ @pytest.mark.parametrize(
+ "payload",
+ [
+ {"name": "id", "id": "id"},
+ {"name": "tenant_id", "tenant_id": "e57c1966f99211efb41e9e45646e0111"},
+ {"name": "created_by", "created_by": "created_by"},
+ {"name": "create_date", "create_date": "Tue, 11 Mar 2025 13:37:23 GMT"},
+ {"name": "create_time", "create_time": 1741671443322},
+ {"name": "update_date", "update_date": "Tue, 11 Mar 2025 13:37:23 GMT"},
+ {"name": "update_time", "update_time": 1741671443339},
+ {"name": "document_count", "document_count": 1},
+ {"name": "chunk_count", "chunk_count": 1},
+ {"name": "token_num", "token_num": 1},
+ {"name": "status", "status": "1"},
+ {"name": "pagerank", "pagerank": 50},
+ {"name": "unknown_field", "unknown_field": "unknown_field"},
+ ],
+ )
+ def test_unsupported_field(self, client, payload):
+ with pytest.raises(Exception) as exception_info:
+ client.create_dataset(**payload)
+ assert "got an unexpected keyword argument" in str(exception_info.value), str(exception_info.value)
+
+
[email protected]("clear_datasets")
+class TestParserConfigBugFix:
+ @pytest.mark.p1
+ def test_parser_config_missing_raptor_and_graphrag(self, client):
+ parser_config = DataSet.ParserConfig(client, {"chunk_token_num": 1024})
+ payload = {"name": "test_parser_config_missing_fields_sdk", "parser_config": parser_config}
+ dataset = client.create_dataset(**payload)
+
+ config = dataset.parser_config
+ assert not hasattr(config, "raptor"), "raptor field should not be exposed"
+ assert not hasattr(config, "graphrag"), "graphrag field should not be exposed"
+ assert config.chunk_token_num == 1024, "User-provided chunk_token_num should be preserved"
+
+ @pytest.mark.p1
+ def test_parser_config_with_only_raptor(self, client):
+ parser_config = DataSet.ParserConfig(client, {"chunk_token_num": 1024, "raptor": {"use_raptor": True}})
+ payload = {"name": "test_parser_config_only_raptor_sdk", "parser_config": parser_config}
+ dataset = client.create_dataset(**payload)
+
+ config = dataset.parser_config
+ assert not hasattr(config, "raptor"), "raptor field should not be exposed"
+ assert not hasattr(config, "graphrag"), "graphrag field should not be exposed"
+
+ @pytest.mark.p1
+ def test_parser_config_with_only_graphrag(self, client):
+ parser_config = DataSet.ParserConfig(client, {"chunk_token_num": 1024, "graphrag": {"use_graphrag": True}})
+ payload = {"name": "test_parser_config_only_graphrag_sdk", "parser_config": parser_config}
+ dataset = client.create_dataset(**payload)
+
+ config = dataset.parser_config
+ assert not hasattr(config, "raptor"), "raptor field should not be exposed"
+ assert not hasattr(config, "graphrag"), "graphrag field should not be exposed"
+
+ @pytest.mark.p1
+ def test_parser_config_with_both_fields(self, client):
+ parser_config = DataSet.ParserConfig(client, {"chunk_token_num": 1024, "raptor": {"use_raptor": True}, "graphrag": {"use_graphrag": True}})
+ payload = {"name": "test_parser_config_both_fields_sdk", "parser_config": parser_config}
+ dataset = client.create_dataset(**payload)
+
+ config = dataset.parser_config
+ assert not hasattr(config, "raptor"), "raptor field should not be exposed"
+ assert not hasattr(config, "graphrag"), "graphrag field should not be exposed"
+
+ @pytest.mark.p2
+ @pytest.mark.parametrize("chunk_method", ["qa", "manual", "paper", "book", "laws", "presentation"])
+ def test_parser_config_different_chunk_methods(self, client, chunk_method):
+ parser_config = DataSet.ParserConfig(client, {"chunk_token_num": 512})
+ payload = {"name": f"test_parser_config_{chunk_method}_sdk", "chunk_method": chunk_method, "parser_config": parser_config}
+ dataset = client.create_dataset(**payload)
+
+ config = dataset.parser_config
+ assert not hasattr(config, "raptor"), f"raptor field should not be exposed for {chunk_method}"
+ assert not hasattr(config, "graphrag"), f"graphrag field should not be exposed for {chunk_method}"
diff --git a/sdk/python/tests/test_dataset_sdk.py b/test/testcases/test_sdk_api/test_dataset_mangement/test_dataset_sdk.py
similarity index 100%
rename from sdk/python/tests/test_dataset_sdk.py
rename to test/testcases/test_sdk_api/test_dataset_mangement/test_dataset_sdk.py
diff --git a/test/testcases/test_sdk_api/test_dataset_mangement/test_delete_datasets.py b/test/testcases/test_sdk_api/test_dataset_mangement/test_delete_datasets.py
new file mode 100644
index 0000000000..cf7488fa45
--- /dev/null
+++ b/test/testcases/test_sdk_api/test_dataset_mangement/test_delete_datasets.py
@@ -0,0 +1,179 @@
+#
+# Copyright 2025 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.
+#
+import uuid
+from concurrent.futures import ThreadPoolExecutor, as_completed
+
+import pytest
+from common import batch_create_datasets
+from configs import HOST_ADDRESS, INVALID_API_TOKEN, SDK_UNAUTHORIZED_ERROR_MESSAGE
+from ragflow_sdk import RAGFlow
+
+
+class TestAuthorization:
+ @pytest.mark.p2
+ @pytest.mark.parametrize(
+ "invalid_auth, expected_message",
+ [
+ (None, SDK_UNAUTHORIZED_ERROR_MESSAGE),
+ (INVALID_API_TOKEN, SDK_UNAUTHORIZED_ERROR_MESSAGE),
+ ],
+ )
+ def test_auth_invalid(self, invalid_auth, expected_message):
+ client = RAGFlow(invalid_auth, HOST_ADDRESS)
+ with pytest.raises(Exception) as exception_info:
+ client.delete_datasets()
+ assert str(exception_info.value) == expected_message
+
+
+class TestCapability:
+ @pytest.mark.p3
+ def test_delete_dataset_1k(self, client):
+ datasets = batch_create_datasets(client, 1_000)
+ client.delete_datasets(**{"ids": [dataset.id for dataset in datasets]})
+
+ datasets = client.list_datasets()
+ assert len(datasets) == 0, datasets
+
+ @pytest.mark.p3
+ def test_concurrent_deletion(self, client):
+ count = 1_000
+ datasets = batch_create_datasets(client, count)
+ with ThreadPoolExecutor(max_workers=5) as executor:
+ futures = [executor.submit(client.delete_datasets, **{"ids": [dataset.id for dataset in datasets][i : i + 1]}) for i in range(count)]
+ responses = list(as_completed(futures))
+ assert len(responses) == count, responses
+
+ datasets = client.list_datasets()
+ assert len(datasets) == 0, datasets
+
+
+class TestDatasetsDelete:
+ @pytest.mark.p1
+ @pytest.mark.parametrize(
+ "func, remaining",
+ [
+ (lambda r: {"ids": r[:1]}, 2),
+ (lambda r: {"ids": r}, 0),
+ ],
+ ids=["single_dataset", "multiple_datasets"],
+ )
+ def test_ids(self, client, add_datasets_func, func, remaining):
+ payload = None
+ if callable(func):
+ payload = func([dataset.id for dataset in add_datasets_func])
+ client.delete_datasets(**payload)
+
+ datasets = client.list_datasets()
+ assert len(datasets) == remaining, str(datasets)
+
+ @pytest.mark.p2
+ @pytest.mark.usefixtures("add_dataset_func")
+ def test_ids_empty(self, client):
+ payload = {"ids": []}
+ client.delete_datasets(**payload)
+
+ datasets = client.list_datasets()
+ assert len(datasets) == 1, str(datasets)
+
+ @pytest.mark.p3
+ @pytest.mark.usefixtures("add_datasets_func")
+ def test_ids_none(self, client):
+ payload = {"ids": None}
+ client.delete_datasets(**payload)
+
+ datasets = client.list_datasets()
+ assert len(datasets) == 3, str(datasets)
+
+ @pytest.mark.p2
+ @pytest.mark.usefixtures("add_dataset_func")
+ def test_id_not_uuid(self, client):
+ payload = {"ids": ["not_uuid"]}
+ with pytest.raises(Exception) as exception_info:
+ client.delete_datasets(**payload)
+ assert "Invalid UUID format" in str(exception_info.value), str(exception_info.value)
+
+ datasets = client.list_datasets()
+ assert len(datasets) == 1, str(datasets)
+
+ @pytest.mark.p3
+ @pytest.mark.usefixtures("add_dataset_func")
+ def test_id_not_uuid1(self, client):
+ payload = {"ids": [uuid.uuid4().hex]}
+ with pytest.raises(Exception) as exception_info:
+ client.delete_datasets(**payload)
+ assert "lacks permission for dataset" in str(exception_info.value), str(exception_info.value)
+
+ @pytest.mark.p2
+ @pytest.mark.usefixtures("add_dataset_func")
+ def test_id_wrong_uuid(self, client):
+ payload = {"ids": ["d94a8dc02c9711f0930f7fbc369eab6d"]}
+ with pytest.raises(Exception) as exception_info:
+ client.delete_datasets(**payload)
+ assert "lacks permission for dataset" in str(exception_info.value), str(exception_info.value)
+
+ datasets = client.list_datasets()
+ assert len(datasets) == 1, str(datasets)
+
+ @pytest.mark.p2
+ @pytest.mark.parametrize(
+ "func",
+ [
+ lambda r: {"ids": ["d94a8dc02c9711f0930f7fbc369eab6d"] + r},
+ lambda r: {"ids": r[:1] + ["d94a8dc02c9711f0930f7fbc369eab6d"] + r[1:3]},
+ lambda r: {"ids": r + ["d94a8dc02c9711f0930f7fbc369eab6d"]},
+ ],
+ )
+ def test_ids_partial_invalid(self, client, add_datasets_func, func):
+ if callable(func):
+ payload = func([dataset.id for dataset in add_datasets_func])
+ with pytest.raises(Exception) as exception_info:
+ client.delete_datasets(**payload)
+ assert "lacks permission for dataset" in str(exception_info.value), str(exception_info.value)
+
+ datasets = client.list_datasets()
+ assert len(datasets) == 3, str(datasets)
+
+ @pytest.mark.p2
+ def test_ids_duplicate(self, client, add_datasets_func):
+ dataset_ids = [dataset.id for dataset in add_datasets_func]
+ payload = {"ids": dataset_ids + dataset_ids}
+ with pytest.raises(Exception) as exception_info:
+ client.delete_datasets(**payload)
+ assert "Duplicate ids:" in str(exception_info.value), str(exception_info.value)
+
+ datasets = client.list_datasets()
+ assert len(datasets) == 3, str(datasets)
+
+ @pytest.mark.p2
+ def test_repeated_delete(self, client, add_datasets_func):
+ dataset_ids = [dataset.id for dataset in add_datasets_func]
+ payload = {"ids": dataset_ids}
+ client.delete_datasets(**payload)
+
+ with pytest.raises(Exception) as exception_info:
+ client.delete_datasets(**payload)
+ assert "lacks permission for dataset" in str(exception_info.value), str(exception_info.value)
+
+ @pytest.mark.p3
+ @pytest.mark.usefixtures("add_dataset_func")
+ def test_field_unsupported(self, client):
+ payload = {"unknown_field": "unknown_field"}
+ with pytest.raises(Exception) as exception_info:
+ client.delete_datasets(**payload)
+ assert "got an unexpected keyword argument 'unknown_field'" in str(exception_info.value), str(exception_info.value)
+
+ datasets = client.list_datasets()
+ assert len(datasets) == 1, str(datasets)
diff --git a/test/testcases/test_sdk_api/test_dataset_mangement/test_list_datasets.py b/test/testcases/test_sdk_api/test_dataset_mangement/test_list_datasets.py
new file mode 100644
index 0000000000..60e7e405d3
--- /dev/null
+++ b/test/testcases/test_sdk_api/test_dataset_mangement/test_list_datasets.py
@@ -0,0 +1,370 @@
+#
+# Copyright 2025 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.
+#
+import uuid
+from concurrent.futures import ThreadPoolExecutor, as_completed
+
+import pytest
+from configs import HOST_ADDRESS, INVALID_API_TOKEN, SDK_UNAUTHORIZED_ERROR_MESSAGE
+from ragflow_sdk import RAGFlow
+
+
+class _ListDatasetsResponse:
+ def json(self):
+ return {"code": 0, "data": []}
+
+
+class TestListDatasetsArgumentValidation:
+ @pytest.fixture(autouse=True)
+ def set_tenant_info(self):
+ return None
+
+ @pytest.mark.p2
+ @pytest.mark.parametrize(
+ "params",
+ [{"id": "dataset-id", "ids": ["dataset-id"]}, {"id": "dataset-id", "ids": [""]}],
+ )
+ def test_rejects_conflicting_id_and_ids_before_get(self, monkeypatch, params):
+ client = RAGFlow("token", HOST_ADDRESS)
+ get_calls = []
+ monkeypatch.setattr(client, "get", lambda *_args, **_kwargs: get_calls.append((_args, _kwargs)))
+
+ with pytest.raises(ValueError, match="^Cannot use both 'id' and 'ids' parameters at the same time\\.$"):
+ client.list_datasets(**params)
+
+ assert get_calls == []
+
+ @pytest.mark.p2
+ @pytest.mark.parametrize(
+ "params",
+ [{"id": "dataset-id"}, {"ids": ["dataset-id"]}, {"id": None, "ids": None}, {"id": "dataset-id", "ids": []}, {"id": "", "ids": ["dataset-id"]}],
+ )
+ def test_preserves_nonconflicting_id_and_ids(self, monkeypatch, params):
+ client = RAGFlow("token", HOST_ADDRESS)
+ get_calls = []
+
+ def _get(path, params=None, json=None):
+ get_calls.append((path, params, json))
+ return _ListDatasetsResponse()
+
+ monkeypatch.setattr(client, "get", _get)
+ assert client.list_datasets(**params) == []
+ assert get_calls == [
+ (
+ "/datasets",
+ {"page": 1, "page_size": 30, "orderby": "create_time", "desc": True, "id": params.get("id"), "ids": params.get("ids"), "name": None},
+ None,
+ )
+ ]
+
+
+class TestAuthorization:
+ @pytest.mark.p2
+ @pytest.mark.parametrize(
+ "invalid_auth, expected_message",
+ [
+ (None, SDK_UNAUTHORIZED_ERROR_MESSAGE),
+ (INVALID_API_TOKEN, SDK_UNAUTHORIZED_ERROR_MESSAGE),
+ ],
+ )
+ def test_auth_invalid(self, invalid_auth, expected_message):
+ client = RAGFlow(invalid_auth, HOST_ADDRESS)
+ with pytest.raises(Exception) as exception_info:
+ client.list_datasets()
+ assert expected_message in str(exception_info.value)
+
+
+class TestCapability:
+ @pytest.mark.p3
+ def test_concurrent_list(self, client):
+ count = 100
+ with ThreadPoolExecutor(max_workers=5) as executor:
+ futures = [
+ executor.submit(
+ client.list_datasets,
+ )
+ for _ in range(count)
+ ]
+ responses = list(as_completed(futures))
+ assert len(responses) == count, responses
+
+
[email protected]("add_datasets")
+class TestDatasetsList:
+ @pytest.mark.p2
+ def test_params_unset(self, client):
+ datasets = client.list_datasets()
+ assert len(datasets) == 5, str(datasets)
+
+ @pytest.mark.p2
+ def test_params_empty(self, client):
+ datasets = client.list_datasets(**{})
+ assert len(datasets) == 5, str(datasets)
+
+ @pytest.mark.p1
+ @pytest.mark.parametrize(
+ "params, expected_page_size",
+ [
+ ({"page": 2, "page_size": 2}, 2),
+ ({"page": 3, "page_size": 2}, 1),
+ ({"page": 4, "page_size": 2}, 0),
+ ({"page": 1, "page_size": 10}, 5),
+ ],
+ ids=["normal_middle_page", "normal_last_partial_page", "beyond_max_page", "full_data_single_page"],
+ )
+ def test_page(self, client, params, expected_page_size):
+ datasets = client.list_datasets(**params)
+ assert len(datasets) == expected_page_size, str(datasets)
+
+ @pytest.mark.p2
+ @pytest.mark.parametrize(
+ "params, expected_message",
+ [
+ ({"page": 0}, "Input should be greater than or equal to 1"),
+ ({"page": "a"}, "not instance of"),
+ ],
+ ids=["page_0", "page_a"],
+ )
+ def test_page_invalid(self, client, params, expected_message):
+ with pytest.raises(Exception) as exception_info:
+ client.list_datasets(**params)
+ assert expected_message in str(exception_info.value), str(exception_info.value)
+
+ @pytest.mark.p2
+ def test_page_none(self, client):
+ params = {"page": None}
+ with pytest.raises(Exception) as exception_info:
+ client.list_datasets(**params)
+ assert "not instance of" in str(exception_info.value), str(exception_info.value)
+
+ @pytest.mark.p1
+ @pytest.mark.parametrize(
+ "params, expected_page_size",
+ [
+ ({"page_size": 1}, 1),
+ ({"page_size": 3}, 3),
+ ({"page_size": 5}, 5),
+ ({"page_size": 6}, 5),
+ ],
+ ids=["min_valid_page_size", "medium_page_size", "page_size_equals_total", "page_size_exceeds_total"],
+ )
+ def test_page_size(self, client, params, expected_page_size):
+ datasets = client.list_datasets(**params)
+ assert len(datasets) == expected_page_size, str(datasets)
+
+ @pytest.mark.p2
+ @pytest.mark.parametrize(
+ "params, expected_message",
+ [
+ ({"page_size": 0}, "Input should be greater than or equal to 1"),
+ ({"page_size": "a"}, "not instance of"),
+ ],
+ )
+ def test_page_size_invalid(self, client, params, expected_message):
+ with pytest.raises(Exception) as exception_info:
+ client.list_datasets(**params)
+ assert expected_message in str(exception_info.value), str(exception_info.value)
+
+ @pytest.mark.p2
+ def test_page_size_none(self, client):
+ params = {"page_size": None}
+ with pytest.raises(Exception) as exception_info:
+ client.list_datasets(**params)
+ assert "not instance of" in str(exception_info.value), str(exception_info.value)
+
+ @pytest.mark.p3
+ @pytest.mark.parametrize(
+ "params",
+ [
+ {"orderby": "create_time"},
+ {"orderby": "update_time"},
+ ],
+ ids=["orderby_create_time", "orderby_update_time"],
+ )
+ def test_orderby(self, client, params):
+ client.list_datasets(**params)
+
+ @pytest.mark.p3
+ @pytest.mark.parametrize(
+ "params",
+ [
+ {"orderby": ""},
+ {"orderby": "unknown"},
+ {"orderby": "CREATE_TIME"},
+ {"orderby": "UPDATE_TIME"},
+ {"orderby": " create_time "},
+ ],
+ ids=["empty", "unknown", "orderby_create_time_upper", "orderby_update_time_upper", "whitespace"],
+ )
+ def test_orderby_invalid(self, client, params):
+ with pytest.raises(Exception) as exception_info:
+ client.list_datasets(**params)
+ assert "Input should be 'create_time' or 'update_time'" in str(exception_info.value), str(exception_info.value)
+
+ @pytest.mark.p3
+ def test_orderby_none(self, client):
+ params = {"orderby": None}
+ with pytest.raises(Exception) as exception_info:
+ client.list_datasets(**params)
+ assert "not instance of" in str(exception_info.value), str(exception_info.value)
+
+ @pytest.mark.p3
+ @pytest.mark.parametrize(
+ "params",
+ [
+ {"desc": True},
+ {"desc": False},
+ ],
+ ids=["desc=True", "desc=False"],
+ )
+ def test_desc(self, client, params):
+ client.list_datasets(**params)
+
+ @pytest.mark.p3
+ @pytest.mark.parametrize(
+ "params",
+ [
+ {"desc": 3.14},
+ {"desc": "unknown"},
+ ],
+ ids=["float_value", "invalid_string"],
+ )
+ def test_desc_invalid(self, client, params):
+ with pytest.raises(Exception) as exception_info:
+ client.list_datasets(**params)
+ assert "not instance of" in str(exception_info.value), str(exception_info.value)
+
+ @pytest.mark.p3
+ def test_desc_none(self, client):
+ params = {"desc": None}
+ with pytest.raises(Exception) as exception_info:
+ client.list_datasets(**params)
+ assert "not instance of" in str(exception_info.value), str(exception_info.value)
+
+ @pytest.mark.p1
+ def test_name(self, client):
+ params = {"name": "dataset_1"}
+ datasets = client.list_datasets(**params)
+ assert len(datasets) == 1, str(datasets)
+ assert datasets[0].name == "dataset_1", str(datasets)
+
+ @pytest.mark.p2
+ def test_name_wrong(self, client):
+ params = {"name": "wrong name"}
+ with pytest.raises(Exception) as exception_info:
+ client.list_datasets(**params)
+ assert "lacks permission for dataset" in str(exception_info.value), str(exception_info.value)
+
+ @pytest.mark.p2
+ def test_get_dataset_not_found_raises(self, client, monkeypatch):
+ monkeypatch.setattr(client, "list_datasets", lambda **_: [])
+ with pytest.raises(Exception) as exception_info:
+ client.get_dataset(name="missing-name-for-coverage")
+ assert "Dataset missing-name-for-coverage not found" in str(exception_info.value), str(exception_info.value)
+
+ @pytest.mark.p2
+ def test_name_empty(self, client):
+ params = {"name": ""}
+ datasets = client.list_datasets(**params)
+ assert len(datasets) == 5, str(datasets)
+
+ @pytest.mark.p2
+ def test_name_none(self, client):
+ params = {"name": None}
+ datasets = client.list_datasets(**params)
+ assert len(datasets) == 5, str(datasets)
+
+ @pytest.mark.p1
+ def test_id(self, client, add_datasets):
+ dataset_ids = [dataset.id for dataset in add_datasets]
+ params = {"id": dataset_ids[0]}
+ datasets = client.list_datasets(**params)
+ assert len(datasets) == 1, str(datasets)
+ assert datasets[0].id == dataset_ids[0], str(datasets)
+
+ @pytest.mark.p2
+ def test_id_not_uuid(self, client):
+ params = {"id": "not_uuid"}
+ with pytest.raises(Exception) as exception_info:
+ client.list_datasets(**params)
+ assert "Invalid UUID format" in str(exception_info.value), str(exception_info.value)
+
+ @pytest.mark.p2
+ def test_id_not_uuid1(self, client):
+ params = {"id": uuid.uuid4().hex}
+ with pytest.raises(Exception) as exception_info:
+ client.list_datasets(**params)
+ assert "lacks permission for dataset" in str(exception_info.value), str(exception_info.value)
+
+ @pytest.mark.p2
+ def test_id_wrong_uuid(self, client):
+ params = {"id": "d94a8dc02c9711f0930f7fbc369eab6d"}
+ with pytest.raises(Exception) as exception_info:
+ client.list_datasets(**params)
+ assert "lacks permission for dataset" in str(exception_info.value), str(exception_info.value)
+
+ @pytest.mark.p2
+ def test_id_empty(self, client):
+ params = {"id": ""}
+ with pytest.raises(Exception) as exception_info:
+ client.list_datasets(**params)
+ assert "Invalid UUID format" in str(exception_info.value), str(exception_info.value)
+
+ @pytest.mark.p2
+ def test_id_none(self, client):
+ params = {"id": None}
+ datasets = client.list_datasets(**params)
+ assert len(datasets) == 5, str(datasets)
+
+ @pytest.mark.p2
+ @pytest.mark.parametrize(
+ "func, name, expected_num",
+ [
+ (lambda r: r[0].id, "dataset_0", 1),
+ (lambda r: r[0].id, "dataset_1", 0),
+ ],
+ ids=["name_and_id_match", "name_and_id_mismatch"],
+ )
+ def test_name_and_id(self, client, add_datasets, func, name, expected_num):
+ params = None
+ if callable(func):
+ params = {"id": func(add_datasets), "name": name}
+ datasets = client.list_datasets(**params)
+ assert len(datasets) == expected_num, str(datasets)
+
+ @pytest.mark.p3
+ @pytest.mark.parametrize(
+ "dataset_id, name",
+ [
+ (lambda r: r[0].id, "wrong_name"),
+ (uuid.uuid1().hex, "dataset_0"),
+ ],
+ ids=["name", "id"],
+ )
+ def test_name_and_id_wrong(self, client, add_datasets, dataset_id, name):
+ if callable(dataset_id):
+ params = {"id": dataset_id(add_datasets), "name": name}
+ else:
+ params = {"id": dataset_id, "name": name}
+ with pytest.raises(Exception) as exception_info:
+ client.list_datasets(**params)
+ assert "lacks permission for dataset" in str(exception_info.value), str(exception_info.value)
+
+ @pytest.mark.p3
+ def test_field_unsupported(self, client):
+ params = {"unknown_field": "unknown_field"}
+ with pytest.raises(Exception) as exception_info:
+ client.list_datasets(**params)
+ assert "got an unexpected keyword argument" in str(exception_info.value), str(exception_info.value)
diff --git a/test/testcases/test_sdk_api/test_dataset_mangement/test_update_dataset.py b/test/testcases/test_sdk_api/test_dataset_mangement/test_update_dataset.py
new file mode 100644
index 0000000000..543245e0dd
--- /dev/null
+++ b/test/testcases/test_sdk_api/test_dataset_mangement/test_update_dataset.py
@@ -0,0 +1,777 @@
+#
+# Copyright 2025 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.
+#
+import os
+from concurrent.futures import ThreadPoolExecutor, as_completed
+from operator import attrgetter
+
+import pytest
+from configs import DATASET_NAME_LIMIT, IS_GO_PROXY
+from hypothesis import HealthCheck, example, given, settings
+from ragflow_sdk import DataSet
+from utils import encode_avatar
+from utils.file_utils import create_image_file
+from utils.hypothesis_utils import valid_names
+from configs import DEFAULT_PARSER_CONFIG
+from utils.engine_utils import get_doc_engine
+
+
+class TestRquest:
+ @pytest.mark.p2
+ def test_payload_empty(self, add_dataset_func):
+ dataset = add_dataset_func
+ with pytest.raises(Exception) as exception_info:
+ dataset.update({})
+ assert "no properties were modified" in str(exception_info.value), str(exception_info.value)
+
+
+class TestCapability:
+ @pytest.mark.p3
+ def test_update_dateset_concurrent(self, add_dataset_func):
+ dataset = add_dataset_func
+ count = 100
+ with ThreadPoolExecutor(max_workers=5) as executor:
+ futures = [executor.submit(dataset.update, {"name": f"dataset_{i}"}) for i in range(count)]
+ responses = list(as_completed(futures))
+ assert len(responses) == count, responses
+
+
+class TestDatasetUpdate:
+ @pytest.mark.p1
+ @given(name=valid_names())
+ @example("a" * 128)
+ @settings(max_examples=20, suppress_health_check=[HealthCheck.function_scoped_fixture])
+ def test_name(self, client, add_dataset_func, name):
+ dataset = add_dataset_func
+ payload = {"name": name}
+ dataset.update(payload)
+ assert dataset.name == name, str(dataset)
+
+ retrieved_dataset = client.get_dataset(name=dataset.name)
+ assert retrieved_dataset.name == name, str(retrieved_dataset)
+
+ @pytest.mark.p2
+ @pytest.mark.parametrize(
+ "name, expected_message",
+ [
+ ("", "String should have at least 1 character"),
+ (" ", "String should have at least 1 character"),
+ ("a" * (DATASET_NAME_LIMIT + 1), "String should have at most 128 characters"),
+ (0, "Input should be a valid string"),
+ (None, "Input should be a valid string"),
+ ],
+ ids=["empty_name", "space_name", "too_long_name", "invalid_name", "None_name"],
+ )
+ def test_name_invalid(self, add_dataset_func, name, expected_message):
+ dataset = add_dataset_func
+ with pytest.raises(Exception) as exception_info:
+ dataset.update({"name": name})
+ error_message = str(exception_info.value)
+ if IS_GO_PROXY:
+ if name is None:
+ pytest.skip("Go dataset update ignores an explicit null name")
+ if not isinstance(name, str):
+ assert "cannot unmarshal" in error_message and ".name" in error_message, error_message
+ return
+ assert expected_message in error_message, error_message
+
+ @pytest.mark.p3
+ def test_name_duplicated(self, add_datasets_func):
+ datasets = add_datasets_func
+ name = "dataset_1"
+ with pytest.raises(Exception) as exception_info:
+ datasets[0].update({"name": name})
+ assert f"Dataset name '{name}' already exists" in str(exception_info.value), str(exception_info.value)
+
+ @pytest.mark.p3
+ def test_name_case_insensitive(self, add_datasets_func):
+ dataset = add_datasets_func[0]
+ name = "DATASET_1"
+ with pytest.raises(Exception) as exception_info:
+ dataset.update({"name": name})
+ assert f"Dataset name '{name}' already exists" in str(exception_info.value), str(exception_info.value)
+
+ @pytest.mark.p2
+ def test_avatar(self, client, add_dataset_func, tmp_path):
+ dataset = add_dataset_func
+ fn = create_image_file(tmp_path / "ragflow_test.png")
+ avatar_data = f"data:image/png;base64,{encode_avatar(fn)}"
+ dataset.update({"avatar": avatar_data})
+ assert dataset.avatar == avatar_data, str(dataset)
+
+ retrieved_dataset = client.get_dataset(name=dataset.name)
+ assert retrieved_dataset.avatar == avatar_data, str(retrieved_dataset)
+
+ @pytest.mark.p3
+ def test_avatar_exceeds_limit_length(self, add_dataset_func):
+ dataset = add_dataset_func
+ with pytest.raises(Exception) as exception_info:
+ dataset.update({"avatar": "a" * 65536})
+ assert "String should have at most 65535 characters" in str(exception_info.value), str(exception_info.value)
+
+ @pytest.mark.p3
+ @pytest.mark.parametrize(
+ "avatar_prefix, expected_message",
+ [
+ ("", "missing MIME prefix. Expected format: data:<mime>;base64,<data>"),
+ ("data:image/png;base64", "missing MIME prefix. Expected format: data:<mime>;base64,<data>"),
+ ("invalid_mine_prefix:image/png;base64,", "invalid MIME prefix format. Must start with 'data:'"),
+ ("data:unsupported_mine_type;base64,", "Unsupported MIME type. Allowed: ['image/jpeg', 'image/png']"),
+ ],
+ ids=["empty_prefix", "missing_comma", "unsupported_mine_type", "invalid_mine_type"],
+ )
+ def test_avatar_invalid_prefix(self, add_dataset_func, tmp_path, avatar_prefix, expected_message):
+ dataset = add_dataset_func
+ fn = create_image_file(tmp_path / "ragflow_test.png")
+ with pytest.raises(Exception) as exception_info:
+ dataset.update({"avatar": f"{avatar_prefix}{encode_avatar(fn)}"})
+ assert expected_message in str(exception_info.value), str(exception_info.value)
+
+ @pytest.mark.p3
+ def test_avatar_none(self, client, add_dataset_func):
+ dataset = add_dataset_func
+ dataset.update({"avatar": None})
+ assert dataset.avatar is None, str(dataset)
+
+ retrieved_dataset = client.get_dataset(name=dataset.name)
+ assert retrieved_dataset.avatar is None, str(retrieved_dataset)
+
+ @pytest.mark.p2
+ def test_description(self, client, add_dataset_func):
+ dataset = add_dataset_func
+ dataset.update({"description": "description"})
+ assert dataset.description == "description", str(dataset)
+
+ retrieved_dataset = client.get_dataset(name=dataset.name)
+ assert retrieved_dataset.description == "description", str(retrieved_dataset)
+
+ @pytest.mark.p3
+ def test_description_exceeds_limit_length(self, add_dataset_func):
+ dataset = add_dataset_func
+ with pytest.raises(Exception) as exception_info:
+ dataset.update({"description": "a" * 65536})
+ assert "String should have at most 65535 characters" in str(exception_info.value), str(exception_info.value)
+
+ @pytest.mark.p3
+ def test_description_none(self, client, add_dataset_func):
+ dataset = add_dataset_func
+ dataset.update({"description": None})
+ assert dataset.description is None, str(dataset)
+
+ retrieved_dataset = client.get_dataset(name=dataset.name)
+ assert retrieved_dataset.description is None, str(retrieved_dataset)
+
+ @pytest.mark.p1
+ @pytest.mark.parametrize(
+ "embedding_model",
+ [
+ "BAAI/bge-small-en-v1.5@Builtin",
+ "embedding-3@CI@ZHIPU-AI",
+ ],
+ ids=["builtin_baai", "tenant_zhipu"],
+ )
+ def test_embedding_model(self, client, add_dataset_func, embedding_model):
+ dataset = add_dataset_func
+ dataset.update({"embedding_model": embedding_model})
+ assert dataset.embedding_model == embedding_model, str(dataset)
+
+ retrieved_dataset = client.get_dataset(name=dataset.name)
+ assert retrieved_dataset.embedding_model == embedding_model, str(retrieved_dataset)
+
+ @pytest.mark.p2
+ @pytest.mark.parametrize(
+ "name, embedding_model",
+ [
+ ("unknown_llm_name", "unknown@ZHIPU-AI"),
+ ("unknown_llm_factory", "embedding-3@unknown"),
+ ("tenant_no_auth_default_tenant_llm", "text-embedding-v3@Tongyi-Qianwen"),
+ ("tenant_no_auth", "text-embedding-3-small@OpenAI"),
+ ],
+ ids=["unknown_llm_name", "unknown_llm_factory", "tenant_no_auth_default_tenant_llm", "tenant_no_auth"],
+ )
+ def test_embedding_model_invalid(self, add_dataset_func, name, embedding_model):
+ dataset = add_dataset_func
+ with pytest.raises(Exception) as exception_info:
+ dataset.update({"name": name, "embedding_model": embedding_model})
+ error_msg = str(exception_info.value)
+ assert "not found" in error_msg, error_msg
+
+ @pytest.mark.p2
+ @pytest.mark.parametrize(
+ "name, embedding_model",
+ [
+ ("empty", ""),
+ ("space", " "),
+ ("missing_at", "BAAI/bge-small-en-v1.5Builtin"),
+ ("missing_model_name", "@Builtin"),
+ ("missing_provider", "BAAI/bge-small-en-v1.5@"),
+ ("whitespace_only_model_name", " @Builtin"),
+ ("whitespace_only_provider", "BAAI/bge-small-en-v1.5@ "),
+ ],
+ ids=["empty", "space", "missing_at", "empty_model_name", "empty_provider", "whitespace_only_model_name", "whitespace_only_provider"],
+ )
+ def test_embedding_model_format(self, add_dataset_func, name, embedding_model):
+ dataset = add_dataset_func
+ with pytest.raises(Exception) as exception_info:
+ dataset.update({"name": name, "embedding_model": embedding_model})
+ error_msg = str(exception_info.value)
+ if IS_GO_PROXY and name in ["empty", "space"]:
+ assert "lookup failed: record not found" in error_msg, error_msg
+ elif name in ["empty", "space", "missing_at"]:
+ assert "embedding model identifier must follow <model_name>@<provider> format" in error_msg, error_msg
+ else:
+ assert "both model_name and provider must be non-empty strings" in error_msg, error_msg
+
+ @pytest.mark.p2
+ def test_embedding_model_none(self, client, add_dataset_func):
+ dataset = add_dataset_func
+ if IS_GO_PROXY:
+ pytest.skip("Go dataset update ignores an explicit null embedding_model")
+ dataset.update({"embedding_model": None})
+ assert dataset.embedding_model.split("@", 1)[0] == "BAAI/bge-small-en-v1.5", str(dataset)
+
+ retrieved_dataset = client.get_dataset(name=dataset.name)
+ assert retrieved_dataset.embedding_model.split("@", 1)[0] == "BAAI/bge-small-en-v1.5", str(retrieved_dataset)
+
+ @pytest.mark.p2
+ @pytest.mark.parametrize(
+ "permission",
+ [
+ "me",
+ "team",
+ ],
+ ids=["me", "team"],
+ )
+ def test_permission(self, client, add_dataset_func, permission):
+ dataset = add_dataset_func
+ dataset.update({"permission": permission})
+ assert dataset.permission == permission.lower().strip(), str(dataset)
+
+ retrieved_dataset = client.get_dataset(name=dataset.name)
+ assert retrieved_dataset.permission == permission.lower().strip(), str(retrieved_dataset)
+
+ @pytest.mark.p2
+ @pytest.mark.parametrize(
+ "permission",
+ [
+ "",
+ "unknown",
+ list(),
+ "ME",
+ "TEAM",
+ " ME ",
+ ],
+ ids=["empty", "unknown", "type_error", "me_upercase", "team_upercase", "whitespace"],
+ )
+ def test_permission_invalid(self, add_dataset_func, permission):
+ dataset = add_dataset_func
+ with pytest.raises(Exception) as exception_info:
+ dataset.update({"permission": permission})
+ error_message = str(exception_info.value)
+ if IS_GO_PROXY and not isinstance(permission, str):
+ assert "cannot unmarshal" in error_message and ".permission" in error_message, error_message
+ else:
+ assert "Input should be 'me' or 'team'" in error_message, error_message
+
+ @pytest.mark.p3
+ def test_permission_none(self, add_dataset_func):
+ dataset = add_dataset_func
+ with pytest.raises(Exception) as exception_info:
+ dataset.update({"permission": None})
+ assert "Input should be 'me' or 'team'" in str(exception_info.value), str(exception_info.value)
+
+ @pytest.mark.p1
+ @pytest.mark.parametrize(
+ "chunk_method",
+ [
+ "naive",
+ "book",
+ "email",
+ "laws",
+ "manual",
+ "one",
+ "paper",
+ "picture",
+ "presentation",
+ "qa",
+ "table",
+ "tag",
+ ],
+ ids=["naive", "book", "email", "laws", "manual", "one", "paper", "picture", "presentation", "qa", "table", "tag"],
+ )
+ def test_chunk_method(self, client, add_dataset_func, chunk_method):
+ dataset = add_dataset_func
+ dataset.update({"chunk_method": chunk_method})
+ assert dataset.chunk_method == chunk_method, str(dataset)
+
+ retrieved_dataset = client.get_dataset(name=dataset.name)
+ assert retrieved_dataset.chunk_method == chunk_method, str(retrieved_dataset)
+
+ @pytest.mark.p2
+ @pytest.mark.parametrize(
+ "chunk_method",
+ [
+ "",
+ "unknown",
+ list(),
+ ],
+ ids=["empty", "unknown", "type_error"],
+ )
+ def test_chunk_method_invalid(self, add_dataset_func, chunk_method):
+ dataset = add_dataset_func
+ with pytest.raises(Exception) as exception_info:
+ dataset.update({"chunk_method": chunk_method})
+ error_message = str(exception_info.value)
+ if IS_GO_PROXY and not isinstance(chunk_method, str):
+ assert "cannot unmarshal" in error_message and ".chunk_method" in error_message, error_message
+ elif IS_GO_PROXY:
+ assert error_message.startswith("Input should be 'naive', 'book'") and error_message.endswith("or 'tag'"), error_message
+ else:
+ assert "Input should be 'naive', 'book', 'email', 'laws', 'manual', 'one', 'paper', 'picture', 'presentation', 'qa', 'table', 'tag' or 'resume'" in error_message, error_message
+
+ @pytest.mark.p3
+ def test_chunk_method_none(self, add_dataset_func):
+ dataset = add_dataset_func
+ with pytest.raises(Exception) as exception_info:
+ dataset.update({"chunk_method": None})
+ assert "Input should be 'naive', 'book', 'email', 'laws', 'manual', 'one', 'paper', 'picture', 'presentation', 'qa', 'table', 'tag' or 'resume'" in str(exception_info.value), str(
+ exception_info.value
+ )
+
+ @pytest.mark.skipif(os.getenv("DOC_ENGINE") == "infinity", reason="#8208")
+ @pytest.mark.p2
+ @pytest.mark.parametrize("pagerank", [0, 50, 100], ids=["min", "mid", "max"])
+ def test_pagerank(self, client, add_dataset_func, pagerank):
+ if get_doc_engine(client) == "infinity":
+ pytest.skip("#8208")
+ dataset = add_dataset_func
+ dataset.update({"pagerank": pagerank})
+ assert dataset.pagerank == pagerank, str(dataset)
+
+ retrieved_dataset = client.get_dataset(name=dataset.name)
+ assert retrieved_dataset.pagerank == pagerank, str(retrieved_dataset)
+
+ @pytest.mark.skipif(os.getenv("DOC_ENGINE") == "infinity", reason="#8208")
+ @pytest.mark.p2
+ def test_pagerank_set_to_0(self, client, add_dataset_func):
+ if get_doc_engine(client) == "infinity":
+ pytest.skip("#8208")
+ dataset = add_dataset_func
+ dataset.update({"pagerank": 50})
+ assert dataset.pagerank == 50, str(dataset)
+
+ retrieved_dataset = client.get_dataset(name=dataset.name)
+ assert retrieved_dataset.pagerank == 50, str(retrieved_dataset)
+
+ dataset.update({"pagerank": 0})
+ assert dataset.pagerank == 0, str(dataset)
+
+ retrieved_dataset = client.get_dataset(name=dataset.name)
+ assert retrieved_dataset.pagerank == 0, str(retrieved_dataset)
+
+ @pytest.mark.skipif(os.getenv("DOC_ENGINE") != "infinity", reason="#8208")
+ @pytest.mark.p2
+ def test_pagerank_infinity(self, client, add_dataset_func):
+ if get_doc_engine(client) != "infinity":
+ pytest.skip("#8208")
+ dataset = add_dataset_func
+ with pytest.raises(Exception) as exception_info:
+ dataset.update({"pagerank": 50})
+ assert "'pagerank' can only be set when doc_engine is elasticsearch" in str(exception_info.value), str(exception_info.value)
+
+ @pytest.mark.p2
+ @pytest.mark.parametrize(
+ "pagerank, expected_message",
+ [
+ (-1, "Input should be greater than or equal to 0"),
+ (101, "Input should be less than or equal to 100"),
+ ],
+ ids=["min_limit", "max_limit"],
+ )
+ def test_pagerank_invalid(self, add_dataset_func, pagerank, expected_message):
+ dataset = add_dataset_func
+ with pytest.raises(Exception) as exception_info:
+ dataset.update({"pagerank": pagerank})
+ error_message = str(exception_info.value)
+ if IS_GO_PROXY and pagerank == -1 and "less than or equal to 100" in error_message:
+ pytest.skip("Go dataset update applies the wrong pagerank bound error for negative values")
+ assert expected_message in error_message, error_message
+
+ @pytest.mark.p3
+ def test_pagerank_none(self, add_dataset_func):
+ dataset = add_dataset_func
+ with pytest.raises(Exception) as exception_info:
+ dataset.update({"pagerank": None})
+ assert "Input should be a valid integer" in str(exception_info.value), str(exception_info.value)
+
+ @pytest.mark.p1
+ @pytest.mark.parametrize(
+ "parser_config",
+ [
+ {"auto_keywords": 0},
+ {"auto_keywords": 16},
+ {"auto_keywords": 32},
+ {"auto_questions": 0},
+ {"auto_questions": 5},
+ {"auto_questions": 10},
+ {"chunk_token_num": 1},
+ {"chunk_token_num": 1024},
+ {"chunk_token_num": 2048},
+ {"delimiter": "\n"},
+ {"delimiter": " "},
+ {"html4excel": True},
+ {"html4excel": False},
+ {"layout_recognize": "DeepDOC"},
+ {"layout_recognize": "Plain Text"},
+ {"tag_kb_ids": ["1", "2"]},
+ {"topn_tags": 1},
+ {"topn_tags": 5},
+ {"topn_tags": 10},
+ {"filename_embd_weight": 0.1},
+ {"filename_embd_weight": 0.5},
+ {"filename_embd_weight": 1.0},
+ {"task_page_size": 1},
+ {"task_page_size": None},
+ {"pages": [[1, 100]]},
+ {"pages": None},
+ {"graphrag": {"use_graphrag": True}},
+ {"graphrag": {"use_graphrag": False}},
+ {"graphrag": {"entity_types": ["age", "sex", "height", "weight"]}},
+ {"graphrag": {"method": "general"}},
+ {"graphrag": {"method": "light"}},
+ {"graphrag": {"community": True}},
+ {"graphrag": {"community": False}},
+ {"graphrag": {"resolution": True}},
+ {"graphrag": {"resolution": False}},
+ {"raptor": {"use_raptor": True}},
+ {"raptor": {"use_raptor": False}},
+ {"raptor": {"prompt": "Who are you?"}},
+ {"raptor": {"max_token": 512}},
+ {"raptor": {"max_token": 1024}},
+ {"raptor": {"max_token": 2048}},
+ {"raptor": {"clustering_threshold": 0.0}},
+ {"raptor": {"clustering_threshold": 0.5}},
+ {"raptor": {"clustering_threshold": 1.0}},
+ {"raptor": {"max_cluster": 1}},
+ {"raptor": {"max_cluster": 512}},
+ {"raptor": {"max_cluster": 1024}},
+ {"raptor": {"random_seed": 0}},
+ ],
+ ids=[
+ "auto_keywords_min",
+ "auto_keywords_mid",
+ "auto_keywords_max",
+ "auto_questions_min",
+ "auto_questions_mid",
+ "auto_questions_max",
+ "chunk_token_num_min",
+ "chunk_token_num_mid",
+ "chunk_token_num_max",
+ "delimiter",
+ "delimiter_space",
+ "html4excel_true",
+ "html4excel_false",
+ "layout_recognize_DeepDOC",
+ "layout_recognize_navie",
+ "tag_kb_ids",
+ "topn_tags_min",
+ "topn_tags_mid",
+ "topn_tags_max",
+ "filename_embd_weight_min",
+ "filename_embd_weight_mid",
+ "filename_embd_weight_max",
+ "task_page_size_min",
+ "task_page_size_None",
+ "pages",
+ "pages_none",
+ "graphrag_true",
+ "graphrag_false",
+ "graphrag_entity_types",
+ "graphrag_method_general",
+ "graphrag_method_light",
+ "graphrag_community_true",
+ "graphrag_community_false",
+ "graphrag_resolution_true",
+ "graphrag_resolution_false",
+ "raptor_true",
+ "raptor_false",
+ "raptor_prompt",
+ "raptor_max_token_min",
+ "raptor_max_token_mid",
+ "raptor_max_token_max",
+ "raptor_clustering_threshold_min",
+ "raptor_clustering_threshold_mid",
+ "raptor_clustering_threshold_max",
+ "raptor_max_cluster_min",
+ "raptor_max_cluster_mid",
+ "raptor_max_cluster_max",
+ "raptor_random_seed_min",
+ ],
+ )
+ def test_parser_config(self, client, add_dataset_func, parser_config):
+ dataset = add_dataset_func
+ dataset.update({"parser_config": parser_config})
+ for k, v in parser_config.items():
+ if k in {"graphrag", "raptor"}:
+ assert not hasattr(dataset.parser_config, k), str(dataset)
+ continue
+ if isinstance(v, dict):
+ for kk, vv in v.items():
+ assert attrgetter(f"{k}.{kk}")(dataset.parser_config) == vv, str(dataset)
+ else:
+ assert attrgetter(k)(dataset.parser_config) == v, str(dataset)
+
+ retrieved_dataset = client.get_dataset(name=dataset.name)
+ for k, v in parser_config.items():
+ if k in {"graphrag", "raptor"}:
+ assert not hasattr(retrieved_dataset.parser_config, k), str(retrieved_dataset)
+ continue
+ if isinstance(v, dict):
+ for kk, vv in v.items():
+ assert attrgetter(f"{k}.{kk}")(retrieved_dataset.parser_config) == vv, str(retrieved_dataset)
+ else:
+ assert attrgetter(k)(retrieved_dataset.parser_config) == v, str(retrieved_dataset)
+
+ @pytest.mark.p2
+ @pytest.mark.parametrize(
+ "parser_config, expected_message",
+ [
+ ({"auto_keywords": -1}, "Input should be greater than or equal to 0"),
+ ({"auto_keywords": 33}, "Input should be less than or equal to 32"),
+ ({"auto_keywords": 3.14}, "Input should be a valid integer"),
+ ({"auto_keywords": "string"}, "Input should be a valid integer"),
+ ({"auto_questions": -1}, "Input should be greater than or equal to 0"),
+ ({"auto_questions": 11}, "Input should be less than or equal to 10"),
+ ({"auto_questions": 3.14}, "Input should be a valid integer"),
+ ({"auto_questions": "string"}, "Input should be a valid integer"),
+ ({"chunk_token_num": 0}, "Input should be greater than or equal to 1"),
+ ({"chunk_token_num": 2049}, "Input should be less than or equal to 2048"),
+ ({"chunk_token_num": 3.14}, "Input should be a valid integer"),
+ ({"chunk_token_num": "string"}, "Input should be a valid integer"),
+ ({"delimiter": ""}, "String should have at least 1 character"),
+ ({"html4excel": "string"}, "Input should be a valid boolean"),
+ ({"tag_kb_ids": "1,2"}, "Input should be a valid list"),
+ ({"tag_kb_ids": [1, 2]}, "Input should be a valid string"),
+ ({"topn_tags": 0}, "Input should be greater than or equal to 1"),
+ ({"topn_tags": 11}, "Input should be less than or equal to 10"),
+ ({"topn_tags": 3.14}, "Input should be a valid integer"),
+ ({"topn_tags": "string"}, "Input should be a valid integer"),
+ ({"filename_embd_weight": -1}, "Input should be greater than or equal to 0"),
+ ({"filename_embd_weight": 1.1}, "Input should be less than or equal to 1"),
+ ({"filename_embd_weight": "string"}, "Input should be a valid number"),
+ ({"task_page_size": 0}, "Input should be greater than or equal to 1"),
+ ({"task_page_size": 3.14}, "Input should be a valid integer"),
+ ({"task_page_size": "string"}, "Input should be a valid integer"),
+ ({"pages": "1,2"}, "Input should be a valid list"),
+ ({"pages": ["1,2"]}, "Input should be a valid list"),
+ ({"pages": [["string1", "string2"]]}, "Input should be a valid integer"),
+ ({"graphrag": {"use_graphrag": "string"}}, "Input should be a valid boolean"),
+ ({"graphrag": {"entity_types": "1,2"}}, "Input should be a valid list"),
+ ({"graphrag": {"entity_types": [1, 2]}}, "nput should be a valid string"),
+ ({"graphrag": {"method": "unknown"}}, "Input should be 'light', 'general' or 'ner'"),
+ ({"graphrag": {"method": None}}, "Input should be 'light', 'general' or 'ner'"),
+ ({"graphrag": {"community": "string"}}, "Input should be a valid boolean"),
+ ({"graphrag": {"resolution": "string"}}, "Input should be a valid boolean"),
+ ({"raptor": {"use_raptor": "string"}}, "Input should be a valid boolean"),
+ ({"raptor": {"prompt": ""}}, "String should have at least 1 character"),
+ ({"raptor": {"prompt": " "}}, "String should have at least 1 character"),
+ ({"raptor": {"max_token": 2049}}, "Input should be less than or equal to 2048"),
+ ({"raptor": {"max_token": "string"}}, "Input should be a valid integer"),
+ ({"raptor": {"clustering_threshold": -0.1}}, "Input should be greater than or equal to 0"),
+ ({"raptor": {"clustering_threshold": 1.1}}, "Input should be less than or equal to 1"),
+ ({"raptor": {"clustering_threshold": "string"}}, "Input should be a valid number"),
+ ({"raptor": {"max_cluster": 0}}, "Input should be greater than or equal to 1"),
+ ({"raptor": {"max_cluster": 1025}}, "Input should be less than or equal to 1024"),
+ ({"raptor": {"max_cluster": 3.14}}, "Input should be a valid integer"),
+ ({"raptor": {"max_cluster": "string"}}, "Input should be a valid integer"),
+ ({"raptor": {"random_seed": -1}}, "Input should be greater than or equal to 0"),
+ ({"raptor": {"random_seed": 3.14}}, "Input should be a valid integer"),
+ ({"raptor": {"random_seed": "string"}}, "Input should be a valid integer"),
+ ({"delimiter": "a" * 65536}, "Parser config exceeds size limit (max 65,535 characters)"),
+ ],
+ ids=[
+ "auto_keywords_min_limit",
+ "auto_keywords_max_limit",
+ "auto_keywords_float_not_allowed",
+ "auto_keywords_type_invalid",
+ "auto_questions_min_limit",
+ "auto_questions_max_limit",
+ "auto_questions_float_not_allowed",
+ "auto_questions_type_invalid",
+ "chunk_token_num_min_limit",
+ "chunk_token_num_max_limit",
+ "chunk_token_num_float_not_allowed",
+ "chunk_token_num_type_invalid",
+ "delimiter_empty",
+ "html4excel_type_invalid",
+ "tag_kb_ids_not_list",
+ "tag_kb_ids_int_in_list",
+ "topn_tags_min_limit",
+ "topn_tags_max_limit",
+ "topn_tags_float_not_allowed",
+ "topn_tags_type_invalid",
+ "filename_embd_weight_min_limit",
+ "filename_embd_weight_max_limit",
+ "filename_embd_weight_type_invalid",
+ "task_page_size_min_limit",
+ "task_page_size_float_not_allowed",
+ "task_page_size_type_invalid",
+ "pages_not_list",
+ "pages_not_list_in_list",
+ "pages_not_int_list",
+ "graphrag_type_invalid",
+ "graphrag_entity_types_not_list",
+ "graphrag_entity_types_not_str_in_list",
+ "graphrag_method_unknown",
+ "graphrag_method_none",
+ "graphrag_community_type_invalid",
+ "graphrag_resolution_type_invalid",
+ "raptor_type_invalid",
+ "raptor_prompt_empty",
+ "raptor_prompt_space",
+ "raptor_max_token_max_limit",
+ "raptor_max_token_type_invalid",
+ "raptor_clustering_threshold_min_limit",
+ "raptor_clustering_threshold_max_limit",
+ "raptor_clustering_threshold_type_invalid",
+ "raptor_max_cluster_min_limit",
+ "raptor_max_cluster_max_limit",
+ "raptor_max_cluster_float_not_allowed",
+ "raptor_max_cluster_type_invalid",
+ "raptor_random_seed_min_limit",
+ "raptor_random_seed_float_not_allowed",
+ "raptor_random_seed_type_invalid",
+ "parser_config_type_invalid",
+ ],
+ )
+ def test_parser_config_invalid(self, add_dataset_func, parser_config, expected_message):
+ dataset = add_dataset_func
+ with pytest.raises(Exception) as exception_info:
+ dataset.update({"parser_config": parser_config})
+ assert expected_message in str(exception_info.value), str(exception_info.value)
+
+ @pytest.mark.p2
+ def test_parser_config_empty(self, client, add_dataset_func):
+ dataset = add_dataset_func
+ expected_config = DataSet.ParserConfig(
+ client,
+ DEFAULT_PARSER_CONFIG,
+ )
+ dataset.update({"parser_config": {}})
+ assert str(dataset.parser_config) == str(expected_config), str(dataset)
+
+ retrieved_dataset = client.get_dataset(name=dataset.name)
+ assert str(retrieved_dataset.parser_config) == str(expected_config), str(retrieved_dataset)
+
+ @pytest.mark.p3
+ def test_parser_config_none(self, client, add_dataset_func):
+ dataset = add_dataset_func
+ expected_config = DataSet.ParserConfig(
+ client,
+ DEFAULT_PARSER_CONFIG,
+ )
+ dataset.update({"parser_config": None})
+ assert str(dataset.parser_config) == str(expected_config), str(dataset)
+
+ retrieved_dataset = client.get_dataset(name=dataset.name)
+ assert str(retrieved_dataset.parser_config) == str(expected_config), str(retrieved_dataset)
+
+ @pytest.mark.p3
+ def test_parser_config_empty_with_chunk_method_change(self, client, add_dataset_func):
+ dataset = add_dataset_func
+ expected_config = DataSet.ParserConfig(
+ client,
+ {
+ "image_context_size": 0,
+ "table_context_size": 0,
+ },
+ )
+ dataset.update({"chunk_method": "qa", "parser_config": {}})
+ assert str(dataset.parser_config) == str(expected_config), str(dataset)
+
+ retrieved_dataset = client.get_dataset(name=dataset.name)
+ assert str(retrieved_dataset.parser_config) == str(expected_config), str(retrieved_dataset)
+
+ @pytest.mark.p3
+ def test_parser_config_unset_with_chunk_method_change(self, client, add_dataset_func):
+ dataset = add_dataset_func
+ expected_config = DataSet.ParserConfig(
+ client,
+ {
+ "image_context_size": 0,
+ "table_context_size": 0,
+ },
+ )
+ dataset.update({"chunk_method": "qa"})
+ assert str(dataset.parser_config) == str(expected_config), str(dataset)
+
+ retrieved_dataset = client.get_dataset(name=dataset.name)
+ assert str(retrieved_dataset.parser_config) == str(expected_config), str(retrieved_dataset)
+
+ @pytest.mark.p3
+ def test_parser_config_none_with_chunk_method_change(self, client, add_dataset_func):
+ dataset = add_dataset_func
+ expected_config = DataSet.ParserConfig(
+ client,
+ {
+ "image_context_size": 0,
+ "table_context_size": 0,
+ },
+ )
+ dataset.update({"chunk_method": "qa", "parser_config": None})
+ assert str(dataset.parser_config) == str(expected_config), str(dataset)
+
+ retrieved_dataset = client.get_dataset(name=dataset.name)
+ assert str(retrieved_dataset.parser_config) == str(expected_config), str(retrieved_dataset)
+
+ @pytest.mark.p2
+ @pytest.mark.parametrize(
+ "payload",
+ [
+ {"id": "id"},
+ {"tenant_id": "e57c1966f99211efb41e9e45646e0111"},
+ {"created_by": "created_by"},
+ {"create_date": "Tue, 11 Mar 2025 13:37:23 GMT"},
+ {"create_time": 1741671443322},
+ {"update_date": "Tue, 11 Mar 2025 13:37:23 GMT"},
+ {"update_time": 1741671443339},
+ {"document_count": 1},
+ {"chunk_count": 1},
+ {"token_num": 1},
+ {"status": "1"},
+ {"unknown_field": "unknown_field"},
+ ],
+ )
+ def test_field_unsupported(self, add_dataset_func, payload):
+ dataset = add_dataset_func
+ with pytest.raises(Exception) as exception_info:
+ dataset.update(payload)
+ assert "Extra inputs are not permitted" in str(exception_info.value), str(exception_info.value)
+
+ @pytest.mark.p2
+ def test_field_unset(self, client, add_dataset_func):
+ dataset = add_dataset_func
+ original_dataset = client.get_dataset(name=dataset.name)
+
+ dataset.update({"name": "default_unset"})
+
+ updated_dataset = client.get_dataset(name="default_unset")
+ assert updated_dataset.avatar == original_dataset.avatar, str(updated_dataset)
+ assert updated_dataset.description == original_dataset.description, str(updated_dataset)
+ assert updated_dataset.embedding_model == original_dataset.embedding_model, str(updated_dataset)
+ assert updated_dataset.permission == original_dataset.permission, str(updated_dataset)
+ assert updated_dataset.chunk_method == original_dataset.chunk_method, str(updated_dataset)
+ assert updated_dataset.pagerank == original_dataset.pagerank, str(updated_dataset)
+ assert str(updated_dataset.parser_config) == str(original_dataset.parser_config), str(updated_dataset)
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
new file mode 100644
index 0000000000..b60f5f2886
--- /dev/null
+++ b/test/testcases/test_sdk_api/test_file_management_within_dataset/conftest.py
@@ -0,0 +1,57 @@
+#
+# Copyright 2025 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.
+#
+
+
+import pytest
+from common import bulk_upload_documents, delete_all_documents
+from pytest import FixtureRequest
+from ragflow_sdk import DataSet, Document
+
+
[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 cleanup():
+ delete_all_documents(dataset)
+
+ request.addfinalizer(cleanup)
+ return dataset, documents[0]
+
+
[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)
+
+ request.addfinalizer(cleanup)
+ return dataset, documents
+
+
[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)
+
+ request.addfinalizer(cleanup)
+ return dataset, documents
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
new file mode 100644
index 0000000000..8b059aa7d9
--- /dev/null
+++ b/test/testcases/test_sdk_api/test_file_management_within_dataset/test_delete_documents.py
@@ -0,0 +1,120 @@
+#
+# Copyright 2025 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.
+#
+from concurrent.futures import ThreadPoolExecutor, as_completed
+
+import pytest
+from common import bulk_upload_documents, list_all_documents
+
+
+class TestDocumentsDeletion:
+ @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": ["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),
+ ],
+ )
+ def test_basic_scenarios(
+ self,
+ add_documents_func,
+ payload,
+ expected_message,
+ remaining,
+ ):
+ dataset, documents = add_documents_func
+ if callable(payload):
+ payload = payload([document.id for document in documents])
+
+ if expected_message:
+ with pytest.raises(Exception) as exception_info:
+ dataset.delete_documents(**payload)
+ assert expected_message in str(exception_info.value), str(exception_info.value)
+ else:
+ dataset.delete_documents(**payload)
+
+ documents = dataset.list_documents()
+ assert len(documents) == remaining, str(documents)
+
+ @pytest.mark.p3
+ @pytest.mark.parametrize(
+ "payload",
+ [
+ lambda r: {"ids": ["invalid_id"] + r},
+ lambda r: {"ids": r[:1] + ["invalid_id"] + r[1:3]},
+ lambda r: {"ids": r + ["invalid_id"]},
+ ],
+ )
+ def test_delete_partial_invalid_id(self, add_documents_func, payload):
+ dataset, documents = add_documents_func
+ payload = payload([document.id for document in documents])
+
+ with pytest.raises(Exception) as exception_info:
+ 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)
+
+ @pytest.mark.p3
+ def test_repeated_deletion(self, add_documents_func):
+ dataset, documents = add_documents_func
+ document_ids = [document.id for document in documents]
+ dataset.delete_documents(ids=document_ids)
+ 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)
+
+ @pytest.mark.p2
+ def test_duplicate_deletion(self, add_documents_func):
+ dataset, documents = add_documents_func
+ document_ids = [document.id for document in documents]
+ 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())
+
+
+def test_concurrent_deletion(add_dataset, tmp_path):
+ count = 100
+ dataset = add_dataset
+ documents = bulk_upload_documents(dataset, count, tmp_path)
+
+ def delete_doc(doc_id):
+ dataset.delete_documents(ids=[doc_id])
+
+ 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
+
+
+def test_delete_1k(add_dataset, tmp_path):
+ 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.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
new file mode 100644
index 0000000000..821f35f958
--- /dev/null
+++ b/test/testcases/test_sdk_api/test_file_management_within_dataset/test_download_document.py
@@ -0,0 +1,185 @@
+#
+# Copyright 2025 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.
+#
+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:
+ def __init__(self, content, payload, headers=None):
+ self.content = content
+ self._payload = payload
+ self.headers = headers or {}
+
+ def json(self):
+ if isinstance(self._payload, Exception):
+ raise self._payload
+ return self._payload
+
+
+ "content,payload",
+ [
+ (b"[]", []),
+ (b"[1, 2]", [1, 2]),
+ (b"null", None),
+ (b'"hello"', "hello"),
+ (b"42", 42),
+ (
+ b'{"code": 0, "message": "file content"}',
+ {"code": 0, "message": "file content"},
+ ),
+ ],
+)
+def test_download_preserves_attached_json_bytes(monkeypatch, content, payload):
+ response = _DownloadResponse(
+ content,
+ payload,
+ {"Content-Disposition": 'attachment; filename="sample.json"'},
+ )
+ document = Document(None, {"id": "doc", "dataset_id": "dataset"})
+ monkeypatch.setattr(document, "get", lambda *_args, **_kwargs: response)
+
+ assert document.download() == content
+
+
+def test_download_still_raises_api_error_without_attachment(monkeypatch):
+ response = _DownloadResponse(
+ b'{"code": 102, "message": "document not found"}',
+ {"code": 102, "message": "document not found"},
+ {"Content-Type": "application/json"},
+ )
+ document = Document(None, {"id": "doc", "dataset_id": "dataset"})
+ monkeypatch.setattr(document, "get", lambda *_args, **_kwargs: response)
+
+ with pytest.raises(Exception, match="document not found"):
+ document.download()
+
+
+def test_download_raises_api_error_with_extra_response_fields(monkeypatch):
+ response = _DownloadResponse(
+ b'{"code": 102, "message": "document not found", "data": null}',
+ {"code": 102, "message": "document not found", "data": None},
+ {"Content-Type": "application/json"},
+ )
+ document = Document(None, {"id": "doc", "dataset_id": "dataset"})
+ monkeypatch.setattr(document, "get", lambda *_args, **_kwargs: response)
+
+ with pytest.raises(Exception, match="document not found"):
+ document.download()
+
+
+ "content,payload",
+ [
+ (b'{"code": 0, "message": "success"}', {"code": 0, "message": "success"}),
+ (b"plain text", ValueError("not JSON")),
+ ],
+)
+def test_download_preserves_non_error_content_without_attachment(monkeypatch, content, payload):
+ response = _DownloadResponse(content, payload, {"Content-Type": "application/json"})
+ document = Document(None, {"id": "doc", "dataset_id": "dataset"})
+ monkeypatch.setattr(document, "get", lambda *_args, **_kwargs: response)
+
+ assert document.download() == content
+
+
+ "generate_test_files",
+ [
+ "docx",
+ "excel",
+ "ppt",
+ "image",
+ "pdf",
+ "txt",
+ "md",
+ "json",
+ "eml",
+ "html",
+ ],
+ indirect=True,
+)
+def test_file_type_validation(add_dataset, generate_test_files, request):
+ dataset = add_dataset
+ 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 compare_by_hash(fp, fp.with_stem("ragflow_test_download"))
+
+
+class TestDocumentDownload:
+ @pytest.mark.p3
+ def test_same_file_repeat(self, add_documents, tmp_path, ragflow_tmp_dir):
+ num = 5
+ _, documents = add_documents
+
+ 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"
+
+ @pytest.mark.p3
+ def test_download_error_json_raises(self, add_documents):
+ dataset, documents = add_documents
+ document = documents[0]
+ invalid_document = document.__class__(
+ document.rag,
+ {"id": "missing-document-id-for-download", "dataset_id": dataset.id},
+ )
+ with pytest.raises(Exception) as exception_info:
+ invalid_document.download()
+ assert str(exception_info.value), exception_info
+
+
+def test_concurrent_download(add_dataset, tmp_path):
+ count = 20
+ dataset = add_dataset
+ documents = bulk_upload_documents(dataset, count, tmp_path)
+
+ 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)
+
+ 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
+
+ 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",
+ )
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
new file mode 100644
index 0000000000..7547228397
--- /dev/null
+++ b/test/testcases/test_sdk_api/test_file_management_within_dataset/test_list_documents.py
@@ -0,0 +1,276 @@
+#
+# Copyright 2025 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.
+#
+from concurrent.futures import ThreadPoolExecutor, as_completed
+
+import pytest
+
+
+class TestDocumentsList:
+ @pytest.mark.p1
+ def test_default(self, add_documents):
+ dataset, _ = add_documents
+ documents = dataset.list_documents()
+ assert len(documents) == 5, str(documents)
+
+ @pytest.mark.p1
+ @pytest.mark.parametrize(
+ "params, expected_page_size, expected_message",
+ [
+ ({"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"),
+ ),
+ ],
+ )
+ 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)
+
+ @pytest.mark.p1
+ @pytest.mark.parametrize(
+ "params, expected_page_size, expected_message",
+ [
+ ({"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"),
+ ),
+ ],
+ )
+ 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)
+
+ @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")),
+ ],
+ )
+ def test_orderby(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.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.p2
+ @pytest.mark.parametrize(
+ "params, expected_num",
+ [
+ ({"keywords": None}, 5),
+ ({"keywords": ""}, 5),
+ ({"keywords": "0"}, 1),
+ ({"keywords": "ragflow_test_upload"}, 5),
+ ({"keywords": "unknown"}, 0),
+ ],
+ )
+ def test_keywords(self, add_documents, params, expected_num):
+ dataset, _ = add_documents
+ documents = dataset.list_documents(**params)
+ assert len(documents) == expected_num, str(documents)
+
+ @pytest.mark.p1
+ @pytest.mark.parametrize(
+ "params, expected_num, expected_message",
+ [
+ ({"name": None}, 5, ""),
+ ({"name": ""}, 5, ""),
+ ({"name": "ragflow_test_upload_0.txt"}, 1, ""),
+ ({"name": "unknown.txt"}, 0, "you don't own the document unknown.txt"),
+ ],
+ )
+ def test_name(self, add_documents, params, expected_num, 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_num, str(documents)
+ if params["name"] not in [None, ""]:
+ assert documents[0].name == params["name"], str(documents)
+
+ @pytest.mark.p1
+ @pytest.mark.parametrize(
+ "document_id, expected_num, expected_message",
+ [
+ (None, 5, ""),
+ ("", 5, ""),
+ (lambda docs: docs[0].id, 1, ""),
+ ("unknown.txt", 0, "you don't own the document unknown.txt"),
+ ],
+ )
+ def test_id(self, add_documents, document_id, expected_num, expected_message):
+ dataset, documents = add_documents
+ if callable(document_id):
+ params = {"id": document_id(documents)}
+ else:
+ params = {"id": document_id}
+
+ 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_num, str(documents)
+ if params["id"] not in [None, ""]:
+ assert documents[0].id == params["id"], str(documents)
+
+ @pytest.mark.p1
+ def test_ids(self, add_documents):
+ dataset, fixture_documents = add_documents
+ expected_ids = {document.id for document in fixture_documents[:2]}
+
+ documents = dataset.list_documents(ids=list(expected_ids))
+
+ assert len(documents) == 2, str(documents)
+ assert {document.id for document in documents} == expected_ids, str(documents)
+
+ @pytest.mark.p1
+ def test_empty_ids_is_unfiltered(self, add_documents):
+ dataset, fixture_documents = add_documents
+ expected_ids = {document.id for document in fixture_documents}
+
+ documents = dataset.list_documents(ids=[])
+
+ assert len(documents) == 5, str(documents)
+ assert {document.id for document in documents} == expected_ids, str(documents)
+
+ @pytest.mark.p3
+ def test_id_and_ids_raise_value_error(self, add_documents):
+ dataset, documents = add_documents
+
+ with pytest.raises(ValueError) as exception_info:
+ dataset.list_documents(id=documents[0].id, ids=[documents[1].id])
+
+ assert str(exception_info.value) == "Cannot use both 'id' and 'ids' parameters at the same time."
+
+ @pytest.mark.p3
+ @pytest.mark.parametrize(
+ "document_id, name, expected_num, expected_message",
+ [
+ (lambda docs: docs[0].id, "ragflow_test_upload_0.txt", 1, ""),
+ (lambda docs: docs[0].id, "ragflow_test_upload_1.txt", 0, ""),
+ (lambda docs: docs[0].id, "unknown", 0, "you don't own the document unknown"),
+ ("invalid_id", "ragflow_test_upload_0.txt", 0, "you don't own the document invalid_id"),
+ ],
+ )
+ def test_name_and_id(self, add_documents, document_id, name, expected_num, expected_message):
+ dataset, documents = add_documents
+ params = {"id": document_id(documents) if callable(document_id) else document_id, "name": name}
+
+ 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_num, str(documents)
+
+ @pytest.mark.p3
+ def test_concurrent_list(self, add_documents):
+ dataset, _ = add_documents
+ count = 100
+
+ def list_docs():
+ return dataset.list_documents()
+
+ with ThreadPoolExecutor(max_workers=5) as executor:
+ futures = [executor.submit(list_docs) for _ in range(count)]
+ responses = list(as_completed(futures))
+ assert len(responses) == count, responses
+ for future in futures:
+ docs = future.result()
+ assert len(docs) == 5, str(docs)
+
+ @pytest.mark.p3
+ def test_invalid_params(self, add_documents):
+ dataset, _ = add_documents
+ params = {"a": "b"}
+ with pytest.raises(TypeError) as exception_info:
+ dataset.list_documents(**params)
+ assert "got an unexpected keyword argument" in str(exception_info.value), str(exception_info.value)
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
new file mode 100644
index 0000000000..c739b0b38f
--- /dev/null
+++ b/test/testcases/test_sdk_api/test_file_management_within_dataset/test_parse_documents.py
@@ -0,0 +1,293 @@
+#
+# Copyright 2025 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.
+#
+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.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):
+ 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
+
+
+class TestDocumentsParse:
+ @pytest.mark.parametrize(
+ "payload, 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),
+ ],
+ )
+ def test_basic_scenarios(self, add_documents_func, payload, expected_message):
+ 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"])
+
+ @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),
+ ],
+ )
+ def test_parse_partial_invalid_document_id(self, add_documents_func, payload):
+ dataset, documents = add_documents_func
+ document_ids = [doc.id for doc in documents]
+ payload = payload(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)
+
+ condition(dataset, document_ids)
+ validate_document_details(dataset, document_ids)
+
+ @pytest.mark.p3
+ def test_repeated_parse(self, add_documents_func):
+ 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)
+ dataset.async_parse_documents(document_ids=document_ids)
+
+ @pytest.mark.p3
+ def test_duplicate_parse(self, add_documents_func):
+ dataset, documents = add_documents_func
+ document_ids = [doc.id for doc in documents]
+ dataset.async_parse_documents(document_ids=document_ids + document_ids)
+ condition(dataset, document_ids)
+ validate_document_details(dataset, 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)}
+ 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})]
+
+ monkeypatch.setattr(dataset, "list_documents", _list_documents)
+ monkeypatch.setattr("time.sleep", lambda *_args: pytest.fail("terminal documents must not be polled again"))
+
+ finished = dataset._get_documents_status(list(states))
+ assert sorted(finished) == sorted((doc_id, "DONE" if run == "RUNNING" else run, 2, 4) for doc_id, (run, _) in states.items())
+ 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
+ error = RuntimeError("temporary list failure")
+ calls = []
+
+ def _list_documents(id=None, **_kwargs):
+ calls.append(id)
+ raise error
+
+ monkeypatch.setattr(dataset, "list_documents", _list_documents)
+ monkeypatch.setattr("time.sleep", lambda *_args: pytest.fail("lookup failures must not be retried"))
+
+ with pytest.raises(RuntimeError, match="temporary list failure") as exc_info:
+ dataset._get_documents_status(["doc-1"])
+ assert exc_info.value is error
+ 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
+ calls = []
+
+ def _list_documents(id=None, **_kwargs):
+ calls.append(id)
+ return []
+
+ monkeypatch.setattr(dataset, "list_documents", _list_documents)
+ monkeypatch.setattr("time.sleep", lambda *_args: pytest.fail("missing documents must not be retried"))
+
+ with pytest.raises(RuntimeError, match="Document doc-1 not found"):
+ dataset._get_documents_status(["doc-1"])
+ assert calls == ["doc-1"]
+
+
+def test_parse_documents_keyboard_interrupt_triggers_cancel_then_returns_status(add_dataset_func, monkeypatch):
+ dataset = add_dataset_func
+ state = {"cancel_calls": 0, "status_calls": 0}
+ expected_status = [("doc-1", "DONE", 1, 2)]
+
+ def _raise_keyboard_interrupt(_document_ids):
+ raise KeyboardInterrupt
+
+ def _cancel(document_ids):
+ state["cancel_calls"] += 1
+ assert document_ids == ["doc-1"]
+
+ def _status(document_ids):
+ state["status_calls"] += 1
+ assert document_ids == ["doc-1"]
+ return expected_status
+
+ monkeypatch.setattr(dataset, "async_parse_documents", _raise_keyboard_interrupt)
+ 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
+
+
+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
+ state = {"status_calls": 0}
+
+ def _noop_parse(_document_ids):
+ return None
+
+ def _status(document_ids):
+ state["status_calls"] += 1
+ assert document_ids == ["doc-1"]
+ return [("doc-1", f"DONE-{state['status_calls']}", 1, 2)]
+
+ monkeypatch.setattr(dataset, "async_parse_documents", _noop_parse)
+ monkeypatch.setattr(dataset, "_get_documents_status", _status)
+
+ status = dataset.parse_documents(["doc-1"])
+ 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
+
+ class _Resp:
+ @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)
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
new file mode 100644
index 0000000000..3cc4051fb3
--- /dev/null
+++ b/test/testcases/test_sdk_api/test_file_management_within_dataset/test_stop_parse_documents.py
@@ -0,0 +1,41 @@
+#
+# Copyright 2025 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.
+#
+
+import pytest
+
+
+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 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
+
+
+class TestDocumentsParseStop:
+ pass
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
new file mode 100644
index 0000000000..78d5fbb217
--- /dev/null
+++ b/test/testcases/test_sdk_api/test_file_management_within_dataset/test_update_document.py
@@ -0,0 +1,514 @@
+#
+# Copyright 2025 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.
+#
+
+import pytest
+from configs import DOCUMENT_NAME_LIMIT
+from ragflow_sdk import DataSet
+from configs import DEFAULT_PARSER_CONFIG
+
+
+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"),
+ ("RAGFLOW_TEST_UPLOAD_1.TXT", ""),
+ ],
+ )
+ def test_name(self, add_documents, name, expected_message):
+ dataset, documents = add_documents
+ 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)
+
+ @pytest.mark.p2
+ @pytest.mark.parametrize(
+ "meta_fields, expected_message",
+ [
+ ({"test": "test"}, ""),
+ ("test", "meta_fields must be a dictionary"),
+ ],
+ )
+ def test_meta_fields(self, add_documents, meta_fields, expected_message):
+ _, documents = add_documents
+ document = documents[0]
+
+ if expected_message:
+ 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})
+
+ @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)
+
+ @pytest.mark.p2
+ @pytest.mark.parametrize(
+ "chunk_method, expected_message",
+ [
+ ("naive", ""),
+ ("manual", ""),
+ ("qa", ""),
+ ("table", ""),
+ ("paper", ""),
+ ("book", ""),
+ ("laws", ""),
+ ("presentation", ""),
+ ("picture", ""),
+ ("one", ""),
+ ("knowledge_graph", ""),
+ ("email", ""),
+ ("tag", ""),
+ ("", "`chunk_method` (empty string) is not valid"),
+ ("other_chunk_method", "`chunk_method` other_chunk_method doesn't exist"),
+ ],
+ )
+ def test_chunk_method(self, add_documents, chunk_method, expected_message):
+ dataset, documents = add_documents
+ 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)
+
+ @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]
+
+ with pytest.raises(Exception) as exception_info:
+ document.update(payload)
+ assert expected_message in str(exception_info.value), str(exception_info.value)
+
+ @pytest.mark.p2
+ @pytest.mark.parametrize(
+ "payload, expected_message",
+ [
+ ({"chunk_count": 1}, "can't change `chunk_count`"),
+ ],
+ )
+ 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)
+ 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
+ ],
+ )
+ 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)
+ assert expected_message in str(exception_info.value), str(exception_info.value)
+
+ @pytest.mark.p3
+ @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
+ ],
+ )
+ 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)
+ 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",
+ ],
+ "method": "light",
+ "batch_chunk_token_size": 4096,
+ },
+}
+
+
+class TestUpdateDocumentParserConfig:
+ @pytest.mark.p3
+ @pytest.mark.parametrize(
+ "chunk_method, parser_config, 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",
+ ),
+ (
+ "naive",
+ {"task_page_size": 0},
+ "Input should be greater than or equal to 1",
+ ),
+ pytest.param(
+ "naive",
+ {"task_page_size": 100000000},
+ "",
+ ),
+ (
+ "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,
+ }
+ },
+ "",
+ ),
+ ("naive", {"raptor": {"use_raptor": False}}, ""),
+ (
+ "naive",
+ {"invalid_key": "invalid_value"},
+ "",
+ ),
+ (
+ "naive",
+ {"auto_keywords": -1},
+ "Input should be greater than or equal to 0",
+ ),
+ pytest.param(
+ "naive",
+ {"auto_keywords": 32},
+ "",
+ ),
+ (
+ "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},
+ "",
+ ),
+ (
+ "naive",
+ {"topn_tags": 3.14},
+ "Input should be a valid integer",
+ ),
+ (
+ "naive",
+ {"topn_tags": "1024"},
+ "Input should be a valid integer",
+ ),
+ ],
+ )
+ def test_parser_config(self, client, add_documents, chunk_method, parser_config, expected_message):
+ dataset, documents = add_documents
+ document = documents[0]
+ from operator import attrgetter
+
+ update_data = {"chunk_method": chunk_method, "parser_config": parser_config}
+
+ 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)
+ 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)
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
new file mode 100644
index 0000000000..4ceb31624c
--- /dev/null
+++ b/test/testcases/test_sdk_api/test_file_management_within_dataset/test_upload_documents.py
@@ -0,0 +1,211 @@
+#
+# Copyright 2025 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.
+#
+import string
+from concurrent.futures import ThreadPoolExecutor, as_completed
+
+import pytest
+from configs import DOCUMENT_NAME_LIMIT
+from utils.file_utils import create_txt_file
+
+
+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")
+ 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)
+
+ @pytest.mark.p1
+ @pytest.mark.parametrize(
+ "generate_test_files",
+ [
+ "docx",
+ "excel",
+ "ppt",
+ "image",
+ "pdf",
+ "txt",
+ "md",
+ "json",
+ "eml",
+ "html",
+ ],
+ indirect=True,
+ )
+ def test_file_type_validation(self, 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:
+ assert document.dataset_id == dataset.id, str(document)
+ assert document.name == fp.name, str(document)
+
+ @pytest.mark.p3
+ @pytest.mark.parametrize(
+ "file_type",
+ ["exe", "unknown"],
+ )
+ def test_unsupported_file_type(self, add_dataset_func, tmp_path, file_type):
+ dataset = add_dataset_func
+ fp = tmp_path / f"ragflow_test.{file_type}"
+ fp.touch()
+
+ with fp.open("rb") as f:
+ blob = f.read()
+
+ with pytest.raises(Exception) as exception_info:
+ dataset.upload_documents([{"display_name": fp.name, "blob": blob}])
+ assert str(exception_info.value) == f"ragflow_test.{file_type}: This type of file has not been supported yet!", str(exception_info.value)
+
+ @pytest.mark.p2
+ def test_missing_file(self, add_dataset_func):
+ dataset = add_dataset_func
+ with pytest.raises(Exception) as exception_info:
+ dataset.upload_documents([])
+ assert str(exception_info.value) == "No file part!", str(exception_info.value)
+
+ @pytest.mark.p3
+ def test_empty_file(self, add_dataset_func, tmp_path):
+ dataset = add_dataset_func
+ fp = tmp_path / "empty.txt"
+ fp.touch()
+
+ 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.size == 0, str(document)
+
+ @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")
+
+ 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)
+
+ @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")
+
+ 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)
+
+ @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")
+
+ with fp.open("rb") as f:
+ blob = f.read()
+
+ documents = dataset.upload_documents([{"display_name": fp.name, "blob": blob}, {"display_name": fp.name, "blob": blob}])
+
+ assert len(documents) == 2, str(documents)
+ for i, document in enumerate(documents):
+ assert document.dataset_id == dataset.id, str(document)
+ expected_name = fp.name if i == 0 else f"{fp.stem}({i}){fp.suffix}"
+ assert document.name == expected_name, str(document)
+
+ @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")
+
+ with fp.open("rb") as f:
+ blob = f.read()
+
+ for i in range(3):
+ documents = dataset.upload_documents([{"display_name": fp.name, "blob": blob}])
+ assert len(documents) == 1, str(documents)
+ document = documents[0]
+ assert document.dataset_id == dataset.id, str(document)
+ expected_name = fp.name if i == 0 else f"{fp.stem}({i}){fp.suffix}"
+ assert document.name == expected_name, str(document)
+
+ @pytest.mark.p3
+ def test_filename_special_characters(self, add_dataset_func, tmp_path):
+ dataset = add_dataset_func
+ illegal_chars = '<>:"/\\|?*'
+ translation_table = str.maketrans({char: "_" for char in illegal_chars})
+ safe_filename = string.punctuation.translate(translation_table)
+ fp = tmp_path / f"{safe_filename}.txt"
+ fp.write_text("Sample text content")
+
+ with fp.open("rb") as f:
+ blob = f.read()
+
+ documents = dataset.upload_documents([{"display_name": fp.name, "blob": blob}])
+ 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
+ def test_multiple_files(self, client, 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")
+ 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)
+
+ @pytest.mark.p3
+ def test_concurrent_upload(self, client, 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)]
+
+ def upload_file(fp):
+ with fp.open("rb") as f:
+ blob = f.read()
+ return dataset.upload_documents([{"display_name": fp.name, "blob": blob}])
+
+ 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)
diff --git a/test/testcases/test_sdk_api/test_memory_management/conftest.py b/test/testcases/test_sdk_api/test_memory_management/conftest.py
new file mode 100644
index 0000000000..992ef74d10
--- /dev/null
+++ b/test/testcases/test_sdk_api/test_memory_management/conftest.py
@@ -0,0 +1,53 @@
+#
+# Copyright 2025 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.
+#
+import pytest
+import random
+
+
[email protected](scope="class")
+def add_memory_func(client, request):
+ def cleanup():
+ memory_list_res = client.list_memory()
+ exist_memory_ids = [memory.id for memory in memory_list_res["memory_list"]]
+ for memory_id in exist_memory_ids:
+ client.delete_memory(memory_id)
+
+ request.addfinalizer(cleanup)
+
+ memory_ids = []
+ for i in range(3):
+ payload = {
+ "name": f"test_memory_{i}",
+ "memory_type": ["raw"] + random.choices(["semantic", "episodic", "procedural"], k=random.randint(0, 3)),
+ "embd_id": "BAAI/bge-small-en-v1.5@Builtin",
+ "llm_id": "glm-4-flash@ZHIPU-AI",
+ }
+ res = client.create_memory(**payload)
+ memory_ids.append(res.id)
+ request.cls.memory_ids = memory_ids
+ return memory_ids
+
+
[email protected](scope="class")
+def delete_test_memory(client, request):
+ def cleanup():
+ memory_list_res = client.list_memory()
+ exist_memory_ids = [memory.id for memory in memory_list_res["memory_list"]]
+ for memory_id in exist_memory_ids:
+ client.delete_memory(memory_id)
+
+ request.addfinalizer(cleanup)
+ return
diff --git a/test/testcases/test_sdk_api/test_memory_management/test_create_memory.py b/test/testcases/test_sdk_api/test_memory_management/test_create_memory.py
new file mode 100644
index 0000000000..3a101c33f0
--- /dev/null
+++ b/test/testcases/test_sdk_api/test_memory_management/test_create_memory.py
@@ -0,0 +1,112 @@
+#
+# Copyright 2025 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.
+#
+import random
+import re
+
+import pytest
+from configs import INVALID_API_TOKEN, HOST_ADDRESS, IS_GO_PROXY, SDK_UNAUTHORIZED_ERROR_MESSAGE
+from ragflow_sdk import RAGFlow
+from hypothesis import example, given, settings
+from utils.hypothesis_utils import valid_names
+
+
+class TestAuthorization:
+ @pytest.mark.p2
+ @pytest.mark.parametrize(
+ "invalid_auth, expected_message",
+ [
+ (None, SDK_UNAUTHORIZED_ERROR_MESSAGE),
+ (INVALID_API_TOKEN, SDK_UNAUTHORIZED_ERROR_MESSAGE),
+ ],
+ ids=["empty_auth", "invalid_api_token"],
+ )
+ def test_auth_invalid(self, invalid_auth, expected_message):
+ client = RAGFlow(invalid_auth, HOST_ADDRESS)
+ with pytest.raises(Exception) as exception_info:
+ client.create_memory(**{"name": "test_memory", "memory_type": ["raw"], "embd_id": "BAAI/bge-small-en-v1.5@Builtin", "llm_id": "glm-4-flash@ZHIPU-AI"})
+ assert str(exception_info.value) == expected_message, str(exception_info.value)
+
+
[email protected]("delete_test_memory")
+class TestMemoryCreate:
+ @pytest.mark.p1
+ @given(name=valid_names())
+ @example("e" * 128)
+ @settings(max_examples=20)
+ def test_name(self, client, name):
+ payload = {
+ "name": name,
+ "memory_type": ["raw"] + random.choices(["semantic", "episodic", "procedural"], k=random.randint(0, 3)),
+ "embd_id": "BAAI/bge-small-en-v1.5@Builtin",
+ "llm_id": "glm-4-flash@ZHIPU-AI",
+ }
+ memory = client.create_memory(**payload)
+ pattern = rf"^{name}|{name}(?:\((\d+)\))?$"
+ escaped_name = re.escape(memory.name)
+ assert re.match(pattern, escaped_name), str(memory)
+
+ @pytest.mark.p2
+ @pytest.mark.parametrize(
+ "name, expected_message",
+ [
+ ("", "Memory name cannot be empty or whitespace."),
+ (" ", "Memory name cannot be empty or whitespace."),
+ ("a" * 129, f"Memory name '{'a' * 129}' exceeds limit of 128."),
+ ],
+ ids=["empty_name", "space_name", "too_long_name"],
+ )
+ def test_name_invalid(self, client, name, expected_message):
+ payload = {
+ "name": name,
+ "memory_type": ["raw"] + random.choices(["semantic", "episodic", "procedural"], k=random.randint(0, 3)),
+ "embd_id": "BAAI/bge-small-en-v1.5@Builtin",
+ "llm_id": "glm-4-flash@ZHIPU-AI",
+ }
+ with pytest.raises(Exception) as exception_info:
+ client.create_memory(**payload)
+ if IS_GO_PROXY:
+ if name == "":
+ expected_message = "failed on the 'required' tag"
+ elif not name.strip():
+ expected_message = "name can't be empty"
+ else:
+ expected_message = "could not create new memory"
+ assert expected_message in str(exception_info.value), str(exception_info.value)
+
+ @pytest.mark.p2
+ @given(name=valid_names())
+ @settings(deadline=None)
+ def test_type_invalid(self, client, name):
+ payload = {"name": name, "memory_type": ["something"], "embd_id": "BAAI/bge-small-en-v1.5@Builtin", "llm_id": "glm-4-flash@ZHIPU-AI"}
+ with pytest.raises(Exception) as exception_info:
+ client.create_memory(**payload)
+ expected_message = "memory type 'something' is not supported" if IS_GO_PROXY else f"Memory type '{ {'something'} }' is not supported."
+ assert str(exception_info.value) == expected_message, str(exception_info.value)
+
+ @pytest.mark.p3
+ def test_name_duplicated(self, client):
+ name = "duplicated_name_test"
+ payload = {
+ "name": name,
+ "memory_type": ["raw"] + random.choices(["semantic", "episodic", "procedural"], k=random.randint(0, 3)),
+ "embd_id": "BAAI/bge-small-en-v1.5@Builtin",
+ "llm_id": "glm-4-flash@ZHIPU-AI",
+ }
+ res1 = client.create_memory(**payload)
+ assert res1.name == name, str(res1)
+
+ res2 = client.create_memory(**payload)
+ assert res2.name == f"{name}(1)", str(res2)
diff --git a/test/testcases/test_sdk_api/test_memory_management/test_list_memory.py b/test/testcases/test_sdk_api/test_memory_management/test_list_memory.py
new file mode 100644
index 0000000000..54b95505ee
--- /dev/null
+++ b/test/testcases/test_sdk_api/test_memory_management/test_list_memory.py
@@ -0,0 +1,141 @@
+#
+# Copyright 2025 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.
+#
+from concurrent.futures import ThreadPoolExecutor, as_completed
+
+import pytest
+from ragflow_sdk import RAGFlow
+from configs import INVALID_API_TOKEN, HOST_ADDRESS, SDK_UNAUTHORIZED_ERROR_MESSAGE
+
+
+class TestAuthorization:
+ @pytest.mark.p2
+ @pytest.mark.parametrize(
+ "invalid_auth, expected_message",
+ [
+ (None, SDK_UNAUTHORIZED_ERROR_MESSAGE),
+ (INVALID_API_TOKEN, SDK_UNAUTHORIZED_ERROR_MESSAGE),
+ ],
+ )
+ def test_auth_invalid(self, invalid_auth, expected_message):
+ client = RAGFlow(invalid_auth, HOST_ADDRESS)
+ with pytest.raises(Exception) as exception_info:
+ client.list_memory()
+ assert str(exception_info.value) == expected_message, str(exception_info.value)
+
+
+class TestCapability:
+ @pytest.mark.p3
+ def test_capability(self, client):
+ count = 100
+ with ThreadPoolExecutor(max_workers=5) as executor:
+ futures = [executor.submit(client.list_memory) for _ in range(count)]
+ responses = list(as_completed(futures))
+ assert len(responses) == count, responses
+ assert all(future.result()["code"] == 0 for future in futures)
+
+
[email protected]("add_memory_func")
+class TestMemoryList:
+ @pytest.mark.p2
+ def test_params_unset(self, client):
+ res = client.list_memory()
+ assert len(res["memory_list"]) == 3, str(res)
+ assert res["total_count"] == 3, str(res)
+
+ @pytest.mark.p2
+ def test_params_empty(self, client):
+ res = client.list_memory(**{})
+ assert len(res["memory_list"]) == 3, str(res)
+ assert res["total_count"] == 3, str(res)
+
+ @pytest.mark.p1
+ @pytest.mark.parametrize(
+ "params, expected_page_size",
+ [
+ ({"page": 1, "page_size": 10}, 3),
+ ({"page": 2, "page_size": 10}, 0),
+ ({"page": 1, "page_size": 2}, 2),
+ ({"page": 2, "page_size": 2}, 1),
+ ({"page": 5, "page_size": 10}, 0),
+ ],
+ ids=["normal_first_page", "beyond_max_page", "normal_last_partial_page", "normal_middle_page", "full_data_single_page"],
+ )
+ def test_page(self, client, params, expected_page_size):
+ # have added 3 memories in fixture
+ res = client.list_memory(**params)
+ assert len(res["memory_list"]) == expected_page_size, str(res)
+ assert res["total_count"] == 3, str(res)
+
+ @pytest.mark.p2
+ def test_filter_memory_type(self, client):
+ res = client.list_memory(**{"memory_type": ["semantic"]})
+ for memory in res["memory_list"]:
+ assert "semantic" in memory.memory_type, str(memory)
+
+ @pytest.mark.p2
+ def test_filter_multi_memory_type(self, client):
+ res = client.list_memory(**{"memory_type": ["episodic", "procedural"]})
+ for memory in res["memory_list"]:
+ assert "episodic" in memory.memory_type or "procedural" in memory.memory_type, str(memory)
+
+ @pytest.mark.p2
+ def test_filter_storage_type(self, client):
+ res = client.list_memory(**{"storage_type": "table"})
+ for memory in res["memory_list"]:
+ assert memory.storage_type == "table", str(memory)
+
+ @pytest.mark.p2
+ def test_match_keyword(self, client):
+ res = client.list_memory(**{"keywords": "s"})
+ for memory in res["memory_list"]:
+ assert "s" in memory.name, str(memory)
+
+ @pytest.mark.p1
+ def test_get_config(self, client):
+ memory_list = client.list_memory()
+ assert len(memory_list["memory_list"]) > 0, str(memory_list)
+ memory = memory_list["memory_list"][0]
+ memory_id = memory.id
+ memory_config = memory.get_config()
+ assert memory_config.id == memory_id, memory_config
+ for field in [
+ "name",
+ "avatar",
+ "tenant_id",
+ "owner_name",
+ "memory_type",
+ "storage_type",
+ "embd_id",
+ "llm_id",
+ "permissions",
+ "description",
+ "memory_size",
+ "forgetting_policy",
+ "temperature",
+ "system_prompt",
+ "user_prompt",
+ ]:
+ assert hasattr(memory, field), memory_config
+
+ @pytest.mark.p2
+ def test_get_config_invalid_memory_id_raises(self, client):
+ memory_list = client.list_memory()
+ assert len(memory_list["memory_list"]) > 0, str(memory_list)
+ memory = memory_list["memory_list"][0]
+ memory.id = "missing-memory-id-for-config"
+ with pytest.raises(Exception) as exception_info:
+ memory.get_config()
+ assert str(exception_info.value), exception_info
diff --git a/test/testcases/test_sdk_api/test_memory_management/test_rm_memory.py b/test/testcases/test_sdk_api/test_memory_management/test_rm_memory.py
new file mode 100644
index 0000000000..a2099e4c57
--- /dev/null
+++ b/test/testcases/test_sdk_api/test_memory_management/test_rm_memory.py
@@ -0,0 +1,53 @@
+#
+# Copyright 2025 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.
+#
+import pytest
+from ragflow_sdk import RAGFlow
+from configs import INVALID_API_TOKEN, HOST_ADDRESS, SDK_UNAUTHORIZED_ERROR_MESSAGE
+
+
+class TestAuthorization:
+ @pytest.mark.p2
+ @pytest.mark.parametrize(
+ "invalid_auth, expected_message",
+ [
+ (None, SDK_UNAUTHORIZED_ERROR_MESSAGE),
+ (INVALID_API_TOKEN, SDK_UNAUTHORIZED_ERROR_MESSAGE),
+ ],
+ )
+ def test_auth_invalid(self, invalid_auth, expected_message):
+ client = RAGFlow(invalid_auth, HOST_ADDRESS)
+ with pytest.raises(Exception) as exception_info:
+ client.delete_memory("some_memory_id")
+ assert str(exception_info.value) == expected_message, str(exception_info.value)
+
+
[email protected]("add_memory_func")
+class TestMemoryDelete:
+ @pytest.mark.p1
+ def test_memory_id(self, client):
+ memory_ids = self.memory_ids
+ client.delete_memory(memory_ids[0])
+ res = client.list_memory()
+ assert res["total_count"] == 2, res
+
+ @pytest.mark.p2
+ def test_id_wrong_uuid(self, client):
+ with pytest.raises(Exception) as exception_info:
+ client.delete_memory("d94a8dc02c9711f0930f7fbc369eab6d")
+ assert exception_info.value, str(exception_info.value)
+
+ res = client.list_memory()
+ assert len(res["memory_list"]) == 2, res
diff --git a/test/testcases/test_sdk_api/test_memory_management/test_update_memory.py b/test/testcases/test_sdk_api/test_memory_management/test_update_memory.py
new file mode 100644
index 0000000000..2cb3bfc92d
--- /dev/null
+++ b/test/testcases/test_sdk_api/test_memory_management/test_update_memory.py
@@ -0,0 +1,162 @@
+#
+# Copyright 2025 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.
+#
+import random
+import pytest
+from configs import INVALID_API_TOKEN, HOST_ADDRESS, IS_GO_PROXY, SDK_UNAUTHORIZED_ERROR_MESSAGE
+from ragflow_sdk import RAGFlow, Memory
+from hypothesis import HealthCheck, example, given, settings
+from utils import encode_avatar
+from utils.file_utils import create_image_file
+from utils.hypothesis_utils import valid_names
+
+
+class TestAuthorization:
+ @pytest.mark.p2
+ @pytest.mark.parametrize(
+ "invalid_auth, expected_message",
+ [
+ (None, SDK_UNAUTHORIZED_ERROR_MESSAGE),
+ (INVALID_API_TOKEN, SDK_UNAUTHORIZED_ERROR_MESSAGE),
+ ],
+ ids=["empty_auth", "invalid_api_token"],
+ )
+ def test_auth_invalid(self, invalid_auth, expected_message):
+
+ with pytest.raises(Exception) as exception_info:
+ client = RAGFlow(invalid_auth, HOST_ADDRESS)
+ memory = Memory(client, {"id": "memory_id"})
+ memory.update({"name": "New_Name"})
+ assert str(exception_info.value) == expected_message, str(exception_info.value)
+
+
[email protected]("add_memory_func")
+class TestMemoryUpdate:
+ @pytest.mark.p1
+ @given(name=valid_names())
+ @example("f" * 128)
+ @settings(max_examples=20, suppress_health_check=[HealthCheck.function_scoped_fixture])
+ def test_name(self, client, name):
+ memory_ids = self.memory_ids
+ update_dict = {"name": name}
+ memory = Memory(client, {"id": random.choice(memory_ids)})
+ res = memory.update(update_dict)
+ assert res.name == name, str(res)
+
+ @pytest.mark.p2
+ @pytest.mark.parametrize(
+ "name, expected_message",
+ [
+ ("", "Memory name cannot be empty or whitespace."),
+ (" ", "Memory name cannot be empty or whitespace."),
+ ("a" * 129, f"Memory name '{'a' * 129}' exceeds limit of 128."),
+ ],
+ )
+ def test_name_invalid(self, client, name, expected_message):
+ memory_ids = self.memory_ids
+ update_dict = {"name": name}
+ memory = Memory(client, {"id": random.choice(memory_ids)})
+ with pytest.raises(Exception) as exception_info:
+ memory.update(update_dict)
+ if IS_GO_PROXY:
+ if len(name) > 128:
+ expected_message = "failed to update memory"
+ else:
+ expected_message = "name can't be empty"
+ assert expected_message in str(exception_info.value), str(exception_info.value)
+
+ @pytest.mark.p2
+ def test_duplicate_name(self, client):
+ memory_ids = self.memory_ids
+ update_dict = {"name": "Test_Memory"}
+ memory_0 = Memory(client, {"id": memory_ids[0]})
+ res_0 = memory_0.update(update_dict)
+ assert res_0.name == "Test_Memory", str(res_0)
+
+ memory_1 = Memory(client, {"id": memory_ids[1]})
+ res_1 = memory_1.update(update_dict)
+ assert res_1.name == "Test_Memory(1)", str(res_1)
+
+ @pytest.mark.p2
+ def test_avatar(self, client, tmp_path):
+ memory_ids = self.memory_ids
+ fn = create_image_file(tmp_path / "ragflow_test.png")
+ update_dict = {"avatar": f"data:image/png;base64,{encode_avatar(fn)}"}
+ memory = Memory(client, {"id": random.choice(memory_ids)})
+ res = memory.update(update_dict)
+ assert res.avatar == f"data:image/png;base64,{encode_avatar(fn)}", str(res)
+
+ @pytest.mark.p2
+ def test_description(self, client):
+ memory_ids = self.memory_ids
+ description = "This is a test description."
+ update_dict = {"description": description}
+ memory = Memory(client, {"id": random.choice(memory_ids)})
+ res = memory.update(update_dict)
+ assert res.description == description, str(res)
+
+ @pytest.mark.p1
+ def test_llm(self, client):
+ memory_ids = self.memory_ids
+ llm_id = "glm-4@ZHIPU-AI"
+ update_dict = {"llm_id": llm_id}
+ memory = Memory(client, {"id": random.choice(memory_ids)})
+ res = memory.update(update_dict)
+ assert res.llm_id == llm_id, str(res)
+
+ @pytest.mark.p2
+ @pytest.mark.parametrize("permission", ["me", "team"], ids=["me", "team"])
+ def test_permission(self, client, permission):
+ memory_ids = self.memory_ids
+ update_dict = {"permissions": permission}
+ memory = Memory(client, {"id": random.choice(memory_ids)})
+ res = memory.update(update_dict)
+ assert res.permissions == permission.lower().strip(), str(res)
+
+ @pytest.mark.p1
+ def test_memory_size(self, client):
+ memory_ids = self.memory_ids
+ memory_size = 1048576 # 1 MB
+ update_dict = {"memory_size": memory_size}
+ memory = Memory(client, {"id": random.choice(memory_ids)})
+ res = memory.update(update_dict)
+ assert res.memory_size == memory_size, str(res)
+
+ @pytest.mark.p1
+ def test_temperature(self, client):
+ memory_ids = self.memory_ids
+ temperature = 0.7
+ update_dict = {"temperature": temperature}
+ memory = Memory(client, {"id": random.choice(memory_ids)})
+ res = memory.update(update_dict)
+ assert res.temperature == temperature, str(res)
+
+ @pytest.mark.p1
+ def test_system_prompt(self, client):
+ memory_ids = self.memory_ids
+ system_prompt = "This is a system prompt."
+ update_dict = {"system_prompt": system_prompt}
+ memory = Memory(client, {"id": random.choice(memory_ids)})
+ res = memory.update(update_dict)
+ assert res.system_prompt == system_prompt, str(res)
+
+ @pytest.mark.p1
+ def test_user_prompt(self, client):
+ memory_ids = self.memory_ids
+ user_prompt = "This is a user prompt."
+ update_dict = {"user_prompt": user_prompt}
+ memory = Memory(client, {"id": random.choice(memory_ids)})
+ res = memory.update(update_dict)
+ assert res.user_prompt == user_prompt, res
diff --git a/test/testcases/test_sdk_api/test_message_management/conftest.py b/test/testcases/test_sdk_api/test_message_management/conftest.py
new file mode 100644
index 0000000000..8e374385cb
--- /dev/null
+++ b/test/testcases/test_sdk_api/test_message_management/conftest.py
@@ -0,0 +1,162 @@
+#
+# Copyright 2025 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.
+#
+import time
+import uuid
+
+import pytest
+import random
+from configs import IS_GO_PROXY
+
+
+MESSAGE_USER_ID = "sdk-api-test-user" if IS_GO_PROXY else ""
+
+
[email protected](scope="class")
+def add_empty_raw_type_memory(client, request):
+ def cleanup():
+ memory_list_res = client.list_memory()
+ exist_memory_ids = [memory.id for memory in memory_list_res["memory_list"]]
+ for _memory_id in exist_memory_ids:
+ client.delete_memory(_memory_id)
+
+ request.addfinalizer(cleanup)
+ payload = {"name": "test_memory_0", "memory_type": ["raw"], "embd_id": "BAAI/bge-small-en-v1.5@Builtin", "llm_id": "glm-4-flash@ZHIPU-AI"}
+ res = client.create_memory(**payload)
+ memory_id = res.id
+ request.cls.memory_id = memory_id
+ request.cls.memory_type = payload["memory_type"]
+ return memory_id
+
+
[email protected](scope="class")
+def add_empty_multiple_type_memory(client, request):
+ def cleanup():
+ memory_list_res = client.list_memory()
+ exist_memory_ids = [memory.id for memory in memory_list_res["memory_list"]]
+ for _memory_id in exist_memory_ids:
+ client.delete_memory(_memory_id)
+
+ request.addfinalizer(cleanup)
+ payload = {
+ "name": "test_memory_0",
+ "memory_type": ["raw"] + random.choices(["semantic", "episodic", "procedural"], k=random.randint(1, 3)),
+ "embd_id": "BAAI/bge-small-en-v1.5@Builtin",
+ "llm_id": "glm-4-flash@ZHIPU-AI",
+ }
+ res = client.create_memory(**payload)
+ memory_id = res.id
+ request.cls.memory_id = memory_id
+ request.cls.memory_type = payload["memory_type"]
+ return memory_id
+
+
[email protected](scope="class")
+def add_2_multiple_type_memory(client, request):
+ def cleanup():
+ memory_list_res = client.list_memory()
+ exist_memory_ids = [memory.id for memory in memory_list_res["memory_list"]]
+ for _memory_id in exist_memory_ids:
+ client.delete_memory(_memory_id)
+
+ request.addfinalizer(cleanup)
+ memory_ids = []
+ for i in range(2):
+ payload = {
+ "name": f"test_memory_{i}",
+ "memory_type": ["raw"] + random.choices(["semantic", "episodic", "procedural"], k=random.randint(1, 3)),
+ "embd_id": "BAAI/bge-small-en-v1.5@Builtin",
+ "llm_id": "glm-4-flash@ZHIPU-AI",
+ }
+ res = client.create_memory(**payload)
+ memory_ids.append(res.id)
+ request.cls.memory_ids = memory_ids
+ return memory_ids
+
+
[email protected](scope="class")
+def add_memory_with_multiple_type_message_func(client, request):
+ def cleanup():
+ memory_list_res = client.list_memory()
+ exist_memory_ids = [mem.id for mem in memory_list_res["memory_list"]]
+ for _memory_id in exist_memory_ids:
+ client.delete_memory(_memory_id)
+
+ request.addfinalizer(cleanup)
+
+ payload = {
+ "name": "test_memory_0",
+ "memory_type": ["raw"] + random.choices(["semantic", "episodic", "procedural"], k=random.randint(1, 3)),
+ "embd_id": "BAAI/bge-small-en-v1.5@Builtin",
+ "llm_id": "glm-4-flash@ZHIPU-AI",
+ }
+ memory = client.create_memory(**payload)
+ memory_id = memory.id
+ agent_id = uuid.uuid4().hex
+ message_payload = {
+ "memory_id": [memory_id],
+ "agent_id": agent_id,
+ "session_id": uuid.uuid4().hex,
+ "user_id": MESSAGE_USER_ID,
+ "user_input": "what is coriander?",
+ "agent_response": """
+Coriander is a versatile herb with two main edible parts, and its name can refer to both:
+1. Leaves and Stems (often called Cilantro or Fresh Coriander): These are the fresh, green, fragrant leaves and tender stems of the plant Coriandrum sativum. They have a bright, citrusy, and sometimes pungent flavor. Cilantro is widely used as a garnish or key ingredient in cuisines like Mexican, Indian, Thai, and Middle Eastern.
+2. Seeds (called Coriander Seeds): These are the dried, golden-brown seeds of the same plant. When ground, they become coriander powder. The seeds have a warm, nutty, floral, and slightly citrusy taste, completely different from the fresh leaves. They are a fundamental spice in curries, stews, pickles, and baking.
+Key Point of Confusion: The naming differs by region. In North America, "coriander" typically refers to the seeds, while "cilantro" refers to the fresh leaves. In the UK, Europe, and many other parts of the world, "coriander" refers to the fresh herb, and the seeds are called "coriander seeds."
+""",
+ }
+ client.add_message(**message_payload)
+ request.cls.memory_id = memory_id
+ request.cls.agent_id = agent_id
+ time.sleep(2) # make sure refresh to index before search
+ return memory_id
+
+
[email protected](scope="class")
+def add_memory_with_5_raw_message_func(client, request):
+ def cleanup():
+ memory_list_res = client.list_memory()
+ exist_memory_ids = [mem.id for mem in memory_list_res["memory_list"]]
+ for _memory_id in exist_memory_ids:
+ client.delete_memory(_memory_id)
+
+ request.addfinalizer(cleanup)
+
+ payload = {"name": "test_memory_1", "memory_type": ["raw"], "embd_id": "BAAI/bge-small-en-v1.5@Builtin", "llm_id": "glm-4-flash@ZHIPU-AI"}
+ memory = client.create_memory(**payload)
+ memory_id = memory.id
+ agent_ids = [uuid.uuid4().hex for _ in range(2)]
+ session_ids = [uuid.uuid4().hex for _ in range(5)]
+ for i in range(5):
+ message_payload = {
+ "memory_id": [memory_id],
+ "agent_id": agent_ids[i % 2],
+ "session_id": session_ids[i],
+ "user_id": MESSAGE_USER_ID,
+ "user_input": "what is coriander?",
+ "agent_response": """
+Coriander is a versatile herb with two main edible parts, and its name can refer to both:
+1. Leaves and Stems (often called Cilantro or Fresh Coriander): These are the fresh, green, fragrant leaves and tender stems of the plant Coriandrum sativum. They have a bright, citrusy, and sometimes pungent flavor. Cilantro is widely used as a garnish or key ingredient in cuisines like Mexican, Indian, Thai, and Middle Eastern.
+2. Seeds (called Coriander Seeds): These are the dried, golden-brown seeds of the same plant. When ground, they become coriander powder. The seeds have a warm, nutty, floral, and slightly citrusy taste, completely different from the fresh leaves. They are a fundamental spice in curries, stews, pickles, and baking.
+Key Point of Confusion: The naming differs by region. In North America, "coriander" typically refers to the seeds, while "cilantro" refers to the fresh leaves. In the UK, Europe, and many other parts of the world, "coriander" refers to the fresh herb, and the seeds are called "coriander seeds."
+""",
+ }
+ client.add_message(**message_payload)
+ request.cls.memory_id = memory_id
+ request.cls.agent_ids = agent_ids
+ request.cls.session_ids = session_ids
+ time.sleep(2) # make sure refresh to index before search
+ return memory_id
diff --git a/test/testcases/test_sdk_api/test_message_management/test_add_message.py b/test/testcases/test_sdk_api/test_message_management/test_add_message.py
new file mode 100644
index 0000000000..fc0dcd4435
--- /dev/null
+++ b/test/testcases/test_sdk_api/test_message_management/test_add_message.py
@@ -0,0 +1,145 @@
+#
+# Copyright 2025 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.
+#
+import time
+import uuid
+import pytest
+from ragflow_sdk import RAGFlow, Memory
+from configs import HOST_ADDRESS, INVALID_API_TOKEN, IS_GO_PROXY, SDK_UNAUTHORIZED_ERROR_MESSAGE
+
+
+MESSAGE_USER_ID = "sdk-api-test-user" if IS_GO_PROXY else ""
+
+
+class TestAuthorization:
+ @pytest.mark.p2
+ @pytest.mark.parametrize(
+ "invalid_auth, expected_message",
+ [
+ (None, SDK_UNAUTHORIZED_ERROR_MESSAGE),
+ (INVALID_API_TOKEN, SDK_UNAUTHORIZED_ERROR_MESSAGE),
+ ],
+ )
+ def test_auth_invalid(self, invalid_auth, expected_message):
+ client = RAGFlow(invalid_auth, HOST_ADDRESS)
+ with pytest.raises(Exception) as exception_info:
+ client.add_message(**{"memory_id": [""], "agent_id": "", "session_id": "", "user_id": MESSAGE_USER_ID, "user_input": "what is pineapple?", "agent_response": ""})
+ assert str(exception_info.value) == expected_message, str(exception_info.value)
+
+
[email protected]("add_empty_raw_type_memory")
+class TestAddRawMessage:
+ @pytest.mark.p3
+ def test_add_raw_message(self, client):
+ memory_id = self.memory_id
+ agent_id = uuid.uuid4().hex
+ session_id = uuid.uuid4().hex
+ message_payload = {
+ "memory_id": [memory_id],
+ "agent_id": agent_id,
+ "session_id": session_id,
+ "user_id": MESSAGE_USER_ID,
+ "user_input": "what is pineapple?",
+ "agent_response": """
+A pineapple is a tropical fruit known for its sweet, tangy flavor and distinctive, spiky appearance. Here are the key facts:
+Scientific Name: Ananas comosus
+Physical Description: It has a tough, spiky, diamond-patterned outer skin (rind) that is usually green, yellow, or brownish. Inside, the juicy yellow flesh surrounds a fibrous core.
+Growth: Unlike most fruits, pineapples do not grow on trees. They grow from a central stem as a composite fruit, meaning they are formed from many individual berries that fuse together around the core. They grow on a short, leafy plant close to the ground.
+Uses: Pineapples are eaten fresh, cooked, grilled, juiced, or canned. They are a popular ingredient in desserts, fruit salads, savory dishes (like pizzas or ham glazes), smoothies, and cocktails.
+Nutrition: They are a good source of Vitamin C, manganese, and contain an enzyme called bromelain, which aids in digestion and can tenderize meat.
+Symbolism: The pineapple is a traditional symbol of hospitality and welcome in many cultures.
+Are you asking about the fruit itself, or its use in a specific context?
+""",
+ }
+ add_res = client.add_message(**message_payload)
+ assert add_res == "All add to task.", str(add_res)
+ time.sleep(2) # make sure refresh to index before search
+ memory = Memory(client, {"id": memory_id})
+ message_res = memory.list_memory_messages(**{"agent_id": agent_id, "keywords": session_id})
+ assert message_res["messages"]["total_count"] > 0
+ for message in message_res["messages"]["message_list"]:
+ assert message["agent_id"] == agent_id, message
+ assert message["session_id"] == session_id, message
+
+
[email protected]("add_empty_multiple_type_memory")
+class TestAddMultipleTypeMessage:
+ @pytest.mark.p1
+ def test_add_multiple_type_message(self, client):
+ memory_id = self.memory_id
+ agent_id = uuid.uuid4().hex
+ session_id = uuid.uuid4().hex
+ message_payload = {
+ "memory_id": [memory_id],
+ "agent_id": agent_id,
+ "session_id": session_id,
+ "user_id": MESSAGE_USER_ID,
+ "user_input": "what is pineapple?",
+ "agent_response": """
+A pineapple is a tropical fruit known for its sweet, tangy flavor and distinctive, spiky appearance. Here are the key facts:
+Scientific Name: Ananas comosus
+Physical Description: It has a tough, spiky, diamond-patterned outer skin (rind) that is usually green, yellow, or brownish. Inside, the juicy yellow flesh surrounds a fibrous core.
+Growth: Unlike most fruits, pineapples do not grow on trees. They grow from a central stem as a composite fruit, meaning they are formed from many individual berries that fuse together around the core. They grow on a short, leafy plant close to the ground.
+Uses: Pineapples are eaten fresh, cooked, grilled, juiced, or canned. They are a popular ingredient in desserts, fruit salads, savory dishes (like pizzas or ham glazes), smoothies, and cocktails.
+Nutrition: They are a good source of Vitamin C, manganese, and contain an enzyme called bromelain, which aids in digestion and can tenderize meat.
+Symbolism: The pineapple is a traditional symbol of hospitality and welcome in many cultures.
+Are you asking about the fruit itself, or its use in a specific context?
+""",
+ }
+ add_res = client.add_message(**message_payload)
+ assert add_res == "All add to task.", str(add_res)
+ time.sleep(2) # make sure refresh to index before search
+ memory = Memory(client, {"id": memory_id})
+ message_res = memory.list_memory_messages(**{"agent_id": agent_id, "keywords": session_id})
+ assert message_res["messages"]["total_count"] > 0
+ for message in message_res["messages"]["message_list"]:
+ assert message["agent_id"] == agent_id, message
+ assert message["session_id"] == session_id, message
+
+
[email protected]("add_2_multiple_type_memory")
+class TestAddToMultipleMemory:
+ @pytest.mark.p3
+ def test_add_to_multiple_memory(self, client):
+ memory_ids = self.memory_ids
+ agent_id = uuid.uuid4().hex
+ session_id = uuid.uuid4().hex
+ message_payload = {
+ "memory_id": memory_ids,
+ "agent_id": agent_id,
+ "session_id": session_id,
+ "user_id": MESSAGE_USER_ID,
+ "user_input": "what is pineapple?",
+ "agent_response": """
+A pineapple is a tropical fruit known for its sweet, tangy flavor and distinctive, spiky appearance. Here are the key facts:
+Scientific Name: Ananas comosus
+Physical Description: It has a tough, spiky, diamond-patterned outer skin (rind) that is usually green, yellow, or brownish. Inside, the juicy yellow flesh surrounds a fibrous core.
+Growth: Unlike most fruits, pineapples do not grow on trees. They grow from a central stem as a composite fruit, meaning they are formed from many individual berries that fuse together around the core. They grow on a short, leafy plant close to the ground.
+Uses: Pineapples are eaten fresh, cooked, grilled, juiced, or canned. They are a popular ingredient in desserts, fruit salads, savory dishes (like pizzas or ham glazes), smoothies, and cocktails.
+Nutrition: They are a good source of Vitamin C, manganese, and contain an enzyme called bromelain, which aids in digestion and can tenderize meat.
+Symbolism: The pineapple is a traditional symbol of hospitality and welcome in many cultures.
+Are you asking about the fruit itself, or its use in a specific context?
+""",
+ }
+ add_res = client.add_message(**message_payload)
+ assert add_res == "All add to task.", str(add_res)
+ time.sleep(2) # make sure refresh to index before search
+ for memory_id in memory_ids:
+ memory = Memory(client, {"id": memory_id})
+ message_res = memory.list_memory_messages(**{"agent_id": agent_id, "keywords": session_id})
+ assert message_res["messages"]["total_count"] > 0
+ for message in message_res["messages"]["message_list"]:
+ assert message["agent_id"] == agent_id, message
+ assert message["session_id"] == session_id, message
diff --git a/test/testcases/test_sdk_api/test_message_management/test_forget_message.py b/test/testcases/test_sdk_api/test_message_management/test_forget_message.py
new file mode 100644
index 0000000000..c3f51110ec
--- /dev/null
+++ b/test/testcases/test_sdk_api/test_message_management/test_forget_message.py
@@ -0,0 +1,53 @@
+#
+# Copyright 2025 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.
+#
+import random
+import pytest
+from ragflow_sdk import RAGFlow, Memory
+from configs import INVALID_API_TOKEN, HOST_ADDRESS, SDK_UNAUTHORIZED_ERROR_MESSAGE
+
+
+class TestAuthorization:
+ @pytest.mark.p2
+ @pytest.mark.parametrize(
+ "invalid_auth, expected_message",
+ [
+ (None, SDK_UNAUTHORIZED_ERROR_MESSAGE),
+ (INVALID_API_TOKEN, SDK_UNAUTHORIZED_ERROR_MESSAGE),
+ ],
+ )
+ def test_auth_invalid(self, invalid_auth, expected_message):
+ client = RAGFlow(invalid_auth, HOST_ADDRESS)
+ with pytest.raises(Exception) as exception_info:
+ memory = Memory(client, {"id": "empty_memory_id"})
+ memory.forget_message(0)
+ assert str(exception_info.value) == expected_message, str(exception_info.value)
+
+
[email protected]("add_memory_with_5_raw_message_func")
+class TestForgetMessage:
+ @pytest.mark.p3
+ def test_forget_message(self, client):
+ memory_id = self.memory_id
+ memory = Memory(client, {"id": memory_id})
+ list_res = memory.list_memory_messages()
+ assert len(list_res["messages"]["message_list"]) > 0
+
+ message = random.choice(list_res["messages"]["message_list"])
+ res = memory.forget_message(message["message_id"])
+ assert res, str(res)
+
+ forgot_message_res = memory.get_message_content(message["message_id"])
+ assert forgot_message_res["forget_at"] not in ["-", ""], forgot_message_res
diff --git a/test/testcases/test_sdk_api/test_message_management/test_get_message_content.py b/test/testcases/test_sdk_api/test_message_management/test_get_message_content.py
new file mode 100644
index 0000000000..a223da5f10
--- /dev/null
+++ b/test/testcases/test_sdk_api/test_message_management/test_get_message_content.py
@@ -0,0 +1,53 @@
+#
+# Copyright 2025 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.
+#
+import random
+
+import pytest
+from ragflow_sdk import RAGFlow, Memory
+from configs import INVALID_API_TOKEN, HOST_ADDRESS, SDK_UNAUTHORIZED_ERROR_MESSAGE
+
+
+class TestAuthorization:
+ @pytest.mark.p2
+ @pytest.mark.parametrize(
+ "invalid_auth, expected_message",
+ [
+ (None, SDK_UNAUTHORIZED_ERROR_MESSAGE),
+ (INVALID_API_TOKEN, SDK_UNAUTHORIZED_ERROR_MESSAGE),
+ ],
+ )
+ def test_auth_invalid(self, invalid_auth, expected_message):
+ client = RAGFlow(invalid_auth, HOST_ADDRESS)
+ with pytest.raises(Exception) as exception_info:
+ memory = Memory(client, {"id": "empty_memory_id"})
+ memory.get_message_content(0)
+ assert str(exception_info.value) == expected_message, str(exception_info.value)
+
+
[email protected]("add_memory_with_multiple_type_message_func")
+class TestGetMessageContent:
+ @pytest.mark.p1
+ def test_get_message_content(self, client):
+ memory_id = self.memory_id
+ recent_messages = client.get_recent_messages([memory_id])
+ assert len(recent_messages) > 0, recent_messages
+ message = random.choice(recent_messages)
+ message_id = message["message_id"]
+ memory = Memory(client, {"id": memory_id})
+ content_res = memory.get_message_content(message_id)
+ for field in ["content", "content_embed"]:
+ assert field in content_res
+ assert content_res[field] is not None, content_res
diff --git a/test/testcases/test_sdk_api/test_message_management/test_get_recent_message.py b/test/testcases/test_sdk_api/test_message_management/test_get_recent_message.py
new file mode 100644
index 0000000000..786873c793
--- /dev/null
+++ b/test/testcases/test_sdk_api/test_message_management/test_get_recent_message.py
@@ -0,0 +1,63 @@
+#
+# Copyright 2025 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.
+#
+import random
+
+import pytest
+from ragflow_sdk import RAGFlow
+from configs import INVALID_API_TOKEN, HOST_ADDRESS, SDK_UNAUTHORIZED_ERROR_MESSAGE
+
+
+class TestAuthorization:
+ @pytest.mark.p2
+ @pytest.mark.parametrize(
+ "invalid_auth, expected_message",
+ [
+ (None, SDK_UNAUTHORIZED_ERROR_MESSAGE),
+ (INVALID_API_TOKEN, SDK_UNAUTHORIZED_ERROR_MESSAGE),
+ ],
+ )
+ def test_auth_invalid(self, invalid_auth, expected_message):
+ client = RAGFlow(invalid_auth, HOST_ADDRESS)
+ with pytest.raises(Exception) as exception_info:
+ client.get_recent_messages(["some_memory_id"])
+ assert str(exception_info.value) == expected_message, str(exception_info.value)
+
+
[email protected]("add_memory_with_5_raw_message_func")
+class TestGetRecentMessage:
+ @pytest.mark.p3
+ def test_get_recent_messages(self, client):
+ memory_id = self.memory_id
+ res = client.get_recent_messages([memory_id])
+ assert len(res) == 5, res
+
+ @pytest.mark.p3
+ def test_filter_recent_messages_by_agent(self, client):
+ memory_id = self.memory_id
+ agent_ids = self.agent_ids
+ agent_id = random.choice(agent_ids)
+ res = client.get_recent_messages(**{"agent_id": agent_id, "memory_id": [memory_id]})
+ for message in res:
+ assert message["agent_id"] == agent_id, message
+
+ @pytest.mark.p2
+ def test_filter_recent_messages_by_session(self, client):
+ memory_id = self.memory_id
+ session_ids = self.session_ids
+ session_id = random.choice(session_ids)
+ res = client.get_recent_messages(**{"session_id": session_id, "memory_id": [memory_id]})
+ for message in res:
+ assert message["session_id"] == session_id, message
diff --git a/test/testcases/test_sdk_api/test_message_management/test_list_message.py b/test/testcases/test_sdk_api/test_message_management/test_list_message.py
new file mode 100644
index 0000000000..0ae2ac35db
--- /dev/null
+++ b/test/testcases/test_sdk_api/test_message_management/test_list_message.py
@@ -0,0 +1,102 @@
+#
+# Copyright 2025 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.
+#
+import os
+import random
+
+import pytest
+from ragflow_sdk import RAGFlow, Memory
+from configs import INVALID_API_TOKEN, HOST_ADDRESS, SDK_UNAUTHORIZED_ERROR_MESSAGE
+from utils.engine_utils import get_doc_engine
+
+
+class TestAuthorization:
+ @pytest.mark.p2
+ @pytest.mark.parametrize(
+ "invalid_auth, expected_message",
+ [
+ (None, SDK_UNAUTHORIZED_ERROR_MESSAGE),
+ (INVALID_API_TOKEN, SDK_UNAUTHORIZED_ERROR_MESSAGE),
+ ],
+ )
+ def test_auth_invalid(self, invalid_auth, expected_message):
+ client = RAGFlow(invalid_auth, HOST_ADDRESS)
+ with pytest.raises(Exception) as exception_info:
+ memory = Memory(client, {"id": "empty_memory_id"})
+ memory.list_memory_messages()
+ assert str(exception_info.value) == expected_message, str(exception_info.value)
+
+
[email protected]("add_memory_with_5_raw_message_func")
+class TestMessageList:
+ @pytest.mark.p3
+ def test_params_unset(self, client):
+ memory_id = self.memory_id
+ memory = Memory(client, {"id": memory_id})
+ res = memory.list_memory_messages()
+ assert len(res["messages"]["message_list"]) == 5, str(res)
+
+ @pytest.mark.p3
+ def test_params_empty(self, client):
+ memory_id = self.memory_id
+ memory = Memory(client, {"id": memory_id})
+ res = memory.list_memory_messages(**{})
+ assert len(res["messages"]["message_list"]) == 5, str(res)
+
+ @pytest.mark.p1
+ @pytest.mark.parametrize(
+ "params, expected_page_size",
+ [
+ ({"page": 1, "page_size": 10}, 5),
+ ({"page": 2, "page_size": 10}, 0),
+ ({"page": 1, "page_size": 2}, 2),
+ ({"page": 3, "page_size": 2}, 1),
+ ({"page": 5, "page_size": 10}, 0),
+ ],
+ ids=["normal_first_page", "beyond_max_page", "normal_last_partial_page", "normal_middle_page", "full_data_single_page"],
+ )
+ def test_page_size(self, client, params, expected_page_size):
+ # have added 5 messages in fixture
+ memory_id = self.memory_id
+ memory = Memory(client, {"id": memory_id})
+ res = memory.list_memory_messages(**params)
+ assert len(res["messages"]["message_list"]) == expected_page_size, str(res)
+
+ @pytest.mark.p2
+ def test_filter_agent_id(self, client):
+ memory_id = self.memory_id
+ agent_ids = self.agent_ids
+ agent_id = random.choice(agent_ids)
+ memory = Memory(client, {"id": memory_id})
+ res = memory.list_memory_messages(**{"agent_id": agent_id})
+ for message in res["messages"]["message_list"]:
+ assert message["agent_id"] == agent_id, message
+
+ @pytest.mark.p2
+ @pytest.mark.skipif(os.getenv("DOC_ENGINE") == "infinity", reason="Not support.")
+ def test_search_keyword(self, client):
+ if get_doc_engine(client) == "infinity":
+ pytest.skip("Not support.")
+ memory_id = self.memory_id
+ session_ids = self.session_ids
+ session_id = random.choice(session_ids)
+ slice_start = random.randint(0, len(session_id) - 2)
+ slice_end = random.randint(slice_start + 1, len(session_id) - 1)
+ keyword = session_id[slice_start:slice_end]
+ memory = Memory(client, {"id": memory_id})
+ res = memory.list_memory_messages(**{"keywords": keyword})
+ assert len(res["messages"]["message_list"]) > 0, res
+ for message in res["messages"]["message_list"]:
+ assert keyword in message["session_id"], message
diff --git a/test/testcases/test_sdk_api/test_message_management/test_search_message.py b/test/testcases/test_sdk_api/test_message_management/test_search_message.py
new file mode 100644
index 0000000000..2ce08e54d0
--- /dev/null
+++ b/test/testcases/test_sdk_api/test_message_management/test_search_message.py
@@ -0,0 +1,75 @@
+#
+# Copyright 2025 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.
+#
+import pytest
+from ragflow_sdk import RAGFlow, Memory
+from configs import INVALID_API_TOKEN, HOST_ADDRESS, SDK_UNAUTHORIZED_ERROR_MESSAGE
+
+
+class TestAuthorization:
+ @pytest.mark.p2
+ @pytest.mark.parametrize(
+ "invalid_auth, expected_message",
+ [
+ (None, SDK_UNAUTHORIZED_ERROR_MESSAGE),
+ (INVALID_API_TOKEN, SDK_UNAUTHORIZED_ERROR_MESSAGE),
+ ],
+ )
+ def test_auth_invalid(self, invalid_auth, expected_message):
+ client = RAGFlow(invalid_auth, HOST_ADDRESS)
+ with pytest.raises(Exception) as exception_info:
+ client.search_message("", ["empty_memory_id"])
+ assert str(exception_info.value) == expected_message, str(exception_info.value)
+
+
[email protected]("add_memory_with_multiple_type_message_func")
+class TestSearchMessage:
+ @pytest.mark.p1
+ def test_query(self, client):
+ memory_id = self.memory_id
+ memory = Memory(client, {"id": memory_id})
+ list_res = memory.list_memory_messages()
+ assert list_res["messages"]["total_count"] > 0
+
+ query = "Coriander is a versatile herb with two main edible parts. What's its name can refer to?"
+ res = client.search_message(**{"memory_id": [memory_id], "query": query})
+ assert len(res) > 0
+
+ @pytest.mark.p2
+ def test_query_with_agent_filter(self, client):
+ memory_id = self.memory_id
+ memory = Memory(client, {"id": memory_id})
+ list_res = memory.list_memory_messages()
+ assert list_res["messages"]["total_count"] > 0
+
+ agent_id = self.agent_id
+ query = "Coriander is a versatile herb with two main edible parts. What's its name can refer to?"
+ res = client.search_message(**{"memory_id": [memory_id], "query": query, "agent_id": agent_id})
+ assert len(res) > 0
+ for message in res:
+ assert message["agent_id"] == agent_id, message
+
+ @pytest.mark.p2
+ def test_query_with_not_default_params(self, client):
+ memory_id = self.memory_id
+ memory = Memory(client, {"id": memory_id})
+ list_res = memory.list_memory_messages()
+ assert list_res["messages"]["total_count"] > 0
+
+ query = "Coriander is a versatile herb with two main edible parts. What's its name can refer to?"
+ params = {"similarity_threshold": 0.1, "keywords_similarity_weight": 0.6, "top_n": 4}
+ res = client.search_message(**{"memory_id": [memory_id], "query": query, **params})
+ assert len(res) > 0
+ assert len(res) <= params["top_n"]
diff --git a/test/testcases/test_sdk_api/test_message_management/test_update_message_status.py b/test/testcases/test_sdk_api/test_message_management/test_update_message_status.py
new file mode 100644
index 0000000000..6ce49bc45e
--- /dev/null
+++ b/test/testcases/test_sdk_api/test_message_management/test_update_message_status.py
@@ -0,0 +1,72 @@
+#
+# Copyright 2025 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.
+#
+import random
+
+import pytest
+from ragflow_sdk import RAGFlow, Memory
+from configs import INVALID_API_TOKEN, HOST_ADDRESS, SDK_UNAUTHORIZED_ERROR_MESSAGE
+
+
+class TestAuthorization:
+ @pytest.mark.p2
+ @pytest.mark.parametrize(
+ "invalid_auth, expected_message",
+ [
+ (None, SDK_UNAUTHORIZED_ERROR_MESSAGE),
+ (INVALID_API_TOKEN, SDK_UNAUTHORIZED_ERROR_MESSAGE),
+ ],
+ )
+ def test_auth_invalid(self, invalid_auth, expected_message):
+ client = RAGFlow(invalid_auth, HOST_ADDRESS)
+ with pytest.raises(Exception) as exception_info:
+ memory = Memory(client, {"id": "empty_memory_id"})
+ memory.update_message_status(0, False)
+ assert str(exception_info.value) == expected_message, str(exception_info.value)
+
+
[email protected]("add_memory_with_5_raw_message_func")
+class TestUpdateMessageStatus:
+ @pytest.mark.p3
+ def test_update_to_false(self, client):
+ memory_id = self.memory_id
+ memory = Memory(client, {"id": memory_id})
+ list_res = memory.list_memory_messages()
+ assert len(list_res["messages"]["message_list"]) > 0, str(list_res)
+
+ message = random.choice(list_res["messages"]["message_list"])
+ res = memory.update_message_status(message["message_id"], False)
+ assert res, str(res)
+
+ updated_message_res = memory.get_message_content(message["message_id"])
+ assert not updated_message_res["status"], str(updated_message_res)
+
+ @pytest.mark.p3
+ def test_update_to_true(self, client):
+ memory_id = self.memory_id
+ memory = Memory(client, {"id": memory_id})
+ list_res = memory.list_memory_messages()
+ assert len(list_res["messages"]["message_list"]) > 0, str(list_res)
+ # set 1 random message to false first
+ message = random.choice(list_res["messages"]["message_list"])
+ set_to_false_res = memory.update_message_status(message["message_id"], False)
+ assert set_to_false_res, str(set_to_false_res)
+ updated_message_res = memory.get_message_content(message["message_id"])
+ assert not updated_message_res["status"], updated_message_res
+ # set to true
+ set_to_true_res = memory.update_message_status(message["message_id"], True)
+ assert set_to_true_res, str(set_to_true_res)
+ res = memory.get_message_content(message["message_id"])
+ assert res["status"], res
diff --git a/test/testcases/test_sdk_api/test_session_management/conftest.py b/test/testcases/test_sdk_api/test_session_management/conftest.py
new file mode 100644
index 0000000000..351dfcf4ad
--- /dev/null
+++ b/test/testcases/test_sdk_api/test_session_management/conftest.py
@@ -0,0 +1,49 @@
+#
+# Copyright 2025 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.
+#
+import pytest
+from common import batch_add_sessions_with_chat_assistant, delete_all_sessions
+from pytest import FixtureRequest
+from ragflow_sdk import Chat, DataSet, Document, Session
+
+
[email protected](scope="class")
+def add_sessions_with_chat_assistant(request: FixtureRequest, add_chat_assistants: tuple[DataSet, Document, list[Chat]]) -> tuple[Chat, list[Session]]:
+ def cleanup():
+ for chat_assistant in chat_assistants:
+ try:
+ delete_all_sessions(chat_assistant)
+ except Exception:
+ pass
+
+ request.addfinalizer(cleanup)
+
+ _, _, chat_assistants = add_chat_assistants
+ return chat_assistants[0], batch_add_sessions_with_chat_assistant(chat_assistants[0], 5)
+
+
[email protected](scope="function")
+def add_sessions_with_chat_assistant_func(request: FixtureRequest, add_chat_assistants: tuple[DataSet, Document, list[Chat]]) -> tuple[Chat, list[Session]]:
+ def cleanup():
+ for chat_assistant in chat_assistants:
+ try:
+ delete_all_sessions(chat_assistant)
+ except Exception:
+ pass
+
+ request.addfinalizer(cleanup)
+
+ _, _, chat_assistants = add_chat_assistants
+ return chat_assistants[0], batch_add_sessions_with_chat_assistant(chat_assistants[0], 5)
diff --git a/test/testcases/test_sdk_api/test_session_management/test_create_session_with_chat_assistant.py b/test/testcases/test_sdk_api/test_session_management/test_create_session_with_chat_assistant.py
new file mode 100644
index 0000000000..08fb241162
--- /dev/null
+++ b/test/testcases/test_sdk_api/test_session_management/test_create_session_with_chat_assistant.py
@@ -0,0 +1,217 @@
+#
+# Copyright 2025 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.
+#
+from concurrent.futures import ThreadPoolExecutor, as_completed
+
+import pytest
+from common import list_all_sessions
+from configs import HOST_ADDRESS, SESSION_WITH_CHAT_NAME_LIMIT
+from ragflow_sdk import RAGFlow
+from ragflow_sdk.modules.session import Session
+
+
+class _DummyStreamResponse:
+ def __init__(self, lines):
+ self._lines = lines
+
+ def iter_lines(self, decode_unicode=True):
+ del decode_unicode
+ for line in self._lines:
+ yield line
+
+
+class _DummyJsonResponse:
+ def __init__(self, payload):
+ self._payload = payload
+
+ def json(self):
+ return self._payload
+
+
[email protected]("clear_session_with_chat_assistants")
+class TestSessionWithChatAssistantCreate:
+ @pytest.mark.p3
+ @pytest.mark.parametrize(
+ "name, expected_message",
+ [
+ ("valid_name", ""),
+ pytest.param("a" * (SESSION_WITH_CHAT_NAME_LIMIT + 1), "", marks=pytest.mark.skip(reason="issues/")),
+ pytest.param(1, "", marks=pytest.mark.skip(reason="issues/")),
+ ("", "`name` can not be empty"),
+ ("duplicated_name", ""),
+ ("case insensitive", ""),
+ ],
+ )
+ def test_name(self, add_chat_assistants, name, expected_message):
+ _, _, chat_assistants = add_chat_assistants
+ chat_assistant = chat_assistants[0]
+
+ if name == "duplicated_name":
+ chat_assistant.create_session(name=name)
+ elif name == "case insensitive":
+ chat_assistant.create_session(name=name.upper())
+
+ if expected_message:
+ with pytest.raises(Exception) as exception_info:
+ chat_assistant.create_session(name=name)
+ assert expected_message in str(exception_info.value)
+ else:
+ session = chat_assistant.create_session(name=name)
+ assert session.name == name, str(session)
+ assert session.chat_id == chat_assistant.id, str(session)
+
+ @pytest.mark.p3
+ def test_concurrent_create_session(self, add_chat_assistants):
+ count = 1000
+ _, _, chat_assistants = add_chat_assistants
+ chat_assistant = chat_assistants[0]
+
+ with ThreadPoolExecutor(max_workers=5) as executor:
+ futures = [executor.submit(chat_assistant.create_session, name=f"session with chat assistant test {i}") for i in range(count)]
+ responses = list(as_completed(futures))
+ assert len(responses) == count, responses
+
+ updated_sessions = list_all_sessions(chat_assistant, limit=count + 1)
+ assert len(updated_sessions) == count
+
+ @pytest.mark.p3
+ def test_add_session_to_deleted_chat_assistant(self, client, add_chat_assistants):
+ _, _, chat_assistants = add_chat_assistants
+ chat_assistant = chat_assistants[0]
+
+ client.delete_chats(ids=[chat_assistant.id])
+ with pytest.raises(Exception) as exception_info:
+ chat_assistant.create_session(name="valid_name")
+ assert "no authorization" in str(exception_info.value)
+
+
+def test_session_module_streaming_and_helper_paths_unit(monkeypatch):
+ client = RAGFlow("token", HOST_ADDRESS)
+ chat_session = Session(client, {"id": "session-chat", "chat_id": "chat-1"})
+ chat_done_session = Session(client, {"id": "session-chat-done", "chat_id": "chat-1"})
+ agent_session = Session(client, {"id": "session-agent", "agent_id": "agent-1"})
+ calls = []
+
+ chat_stream = _DummyStreamResponse(
+ [
+ "",
+ "data: {bad json}",
+ 'data: {"event":"workflow_started","data":{"content":"skip"}}',
+ '{"data":{"answer":"chat-answer","reference":{"chunks":[{"id":"chunk-1"}]}}}',
+ 'data: {"data": true}',
+ "data: [DONE]",
+ ]
+ )
+ agent_stream = _DummyStreamResponse(
+ [
+ "data: {bad json}",
+ 'data: {"event":"message","data":{"content":"agent-answer"}}',
+ 'data: {"event":"message_end","data":{"content":"done"}}',
+ ]
+ )
+
+ def _chat_post(path, json=None, stream=False, files=None):
+ calls.append(("chat", path, json, stream, files))
+ return chat_stream
+
+ def _agent_post(path, json=None, stream=False, files=None):
+ calls.append(("agent", path, json, stream, files))
+ return agent_stream
+
+ monkeypatch.setattr(chat_session, "post", _chat_post)
+ monkeypatch.setattr(
+ chat_done_session,
+ "post",
+ lambda *_args, **_kwargs: _DummyStreamResponse(['{"data":{"answer":"chat-done","reference":{"chunks":[]}}}', "data: [DONE]"]),
+ )
+ monkeypatch.setattr(agent_session, "post", _agent_post)
+
+ chat_messages = list(chat_session.ask("hello chat", stream=True, temperature=0.2))
+ assert len(chat_messages) == 1
+ assert chat_messages[0].content == "chat-answer"
+ assert chat_messages[0].reference == [{"id": "chunk-1"}]
+
+ chat_done_messages = list(chat_done_session.ask("hello done", stream=True))
+ assert len(chat_done_messages) == 1
+ assert chat_done_messages[0].content == "chat-done"
+
+ agent_messages = list(agent_session.ask("hello agent", stream=True, top_p=0.8))
+ assert len(agent_messages) == 1
+ assert agent_messages[0].content == "agent-answer"
+
+ assert calls[0][1] == "/chats/chat-1/completions"
+ assert calls[0][2]["question"] == "hello chat"
+ assert calls[0][2]["session_id"] == "session-chat"
+ assert calls[0][2]["temperature"] == 0.2
+ assert calls[0][3] is True
+ assert calls[1][1] == "/agents/chat/completions"
+ assert calls[1][2]["agent_id"] == "agent-1"
+ assert calls[1][2]["query"] == "hello agent"
+ assert calls[1][2]["session_id"] == "session-agent"
+ assert calls[1][2]["openai-compatible"] is False
+ assert calls[1][2]["top_p"] == 0.8
+ assert calls[1][3] is True
+
+
+def test_agent_session_preserves_non_stream_reference_chunks(monkeypatch, caplog):
+ first_chunk = {"id": "chunk-1", "content": "first source"}
+ second_chunk = {"id": "chunk-2", "content": "second source"}
+ session = Session(None, {"id": "session-agent", "agent_id": "agent-1"})
+ response = _DummyJsonResponse(
+ {
+ "data": {
+ "event": "message_end",
+ "data": {
+ "content": "agent-answer",
+ "reference": {"chunks": {"500": first_chunk, "100": second_chunk}},
+ },
+ }
+ }
+ )
+ monkeypatch.setattr(session, "post", lambda *_args, **_kwargs: response)
+
+ with caplog.at_level("DEBUG", logger="ragflow_sdk.modules.session"):
+ messages = list(session.ask("hello agent", stream=False))
+
+ assert len(messages) == 1
+ assert messages[0].content == "agent-answer"
+ assert messages[0].reference == [first_chunk, second_chunk]
+ assert "session_id=session-agent event=message_end reference_count=2" in caplog.text
+ assert "first source" not in caplog.text
+ assert "second source" not in caplog.text
+
+
+def test_agent_session_streams_all_message_references_without_duplicate_content(monkeypatch):
+ first_chunk = {"id": "chunk-1", "content": "first source"}
+ second_chunk = {"id": "chunk-2", "content": "second source"}
+ session = Session(None, {"id": "session-agent", "agent_id": "agent-1"})
+ response = _DummyStreamResponse(
+ [
+ 'data: {"event":"message","data":{"content":"first answer"}}',
+ 'data: {"event":"message_end","data":{"content":"first answer","reference":{"chunks":{"123":{"id":"chunk-1","content":"first source"}}}}}',
+ 'data: {"event":"message","data":{"content":"second answer"}}',
+ 'data: {"event":"message_end","data":{"content":"second answer","reference":{"chunks":{"456":{"id":"chunk-2","content":"second source"}}}}}',
+ "data: [DONE]",
+ ]
+ )
+ monkeypatch.setattr(session, "post", lambda *_args, **_kwargs: response)
+
+ messages = list(session.ask("hello agent", stream=True))
+
+ assert "".join(message.content for message in messages) == "first answersecond answer"
+ assert [message.reference for message in messages if message.reference] == [[first_chunk], [second_chunk]]
diff --git a/test/testcases/test_sdk_api/test_session_management/test_delete_sessions_with_chat_assistant.py b/test/testcases/test_sdk_api/test_session_management/test_delete_sessions_with_chat_assistant.py
new file mode 100644
index 0000000000..ae996dc659
--- /dev/null
+++ b/test/testcases/test_sdk_api/test_session_management/test_delete_sessions_with_chat_assistant.py
@@ -0,0 +1,111 @@
+#
+# Copyright 2025 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.
+#
+
+from concurrent.futures import ThreadPoolExecutor, as_completed
+
+import pytest
+from common import batch_add_sessions_with_chat_assistant
+
+
+class TestSessionWithChatAssistantDelete:
+ @pytest.mark.parametrize(
+ "payload",
+ [
+ pytest.param(lambda r: {"ids": ["invalid_id"] + r}, marks=pytest.mark.p3),
+ pytest.param(lambda r: {"ids": r[:1] + ["invalid_id"] + r[1:5]}, marks=pytest.mark.p3),
+ pytest.param(lambda r: {"ids": r + ["invalid_id"]}, marks=pytest.mark.p3),
+ ],
+ )
+ def test_delete_partial_invalid_id(self, add_sessions_with_chat_assistant_func, payload):
+ chat_assistant, sessions = add_sessions_with_chat_assistant_func
+ if callable(payload):
+ payload = payload([session.id for session in sessions])
+
+ chat_assistant.delete_sessions(**payload)
+
+ sessions = chat_assistant.list_sessions()
+ assert len(sessions) == 0
+
+ @pytest.mark.p3
+ def test_repeated_deletion(self, add_sessions_with_chat_assistant_func):
+ chat_assistant, sessions = add_sessions_with_chat_assistant_func
+ session_ids = {"ids": [session.id for session in sessions]}
+
+ chat_assistant.delete_sessions(**session_ids)
+
+ with pytest.raises(Exception) as exception_info:
+ chat_assistant.delete_sessions(**session_ids)
+ assert "The chat doesn't own the session" in str(exception_info.value)
+
+ @pytest.mark.p3
+ def test_duplicate_deletion(self, add_sessions_with_chat_assistant_func):
+ chat_assistant, sessions = add_sessions_with_chat_assistant_func
+ session_ids = {"ids": [session.id for session in sessions] * 2}
+ chat_assistant.delete_sessions(**session_ids)
+ sessions = chat_assistant.list_sessions()
+ assert len(sessions) == 0
+
+ @pytest.mark.p3
+ def test_concurrent_deletion(self, add_chat_assistants):
+ count = 100
+ _, _, chat_assistants = add_chat_assistants
+ chat_assistant = chat_assistants[0]
+ sessions = batch_add_sessions_with_chat_assistant(chat_assistant, count)
+
+ with ThreadPoolExecutor(max_workers=5) as executor:
+ futures = [executor.submit(chat_assistant.delete_sessions, ids=[sessions[i].id]) for i in range(count)]
+ responses = list(as_completed(futures))
+ assert len(responses) == count, responses
+
+ @pytest.mark.p3
+ def test_delete_1k(self, add_chat_assistants):
+ count = 1_000
+ _, _, chat_assistants = add_chat_assistants
+ chat_assistant = chat_assistants[0]
+ ssessions = batch_add_sessions_with_chat_assistant(chat_assistant, count)
+ chat_assistant.delete_sessions(ids=[ssession.id for ssession in ssessions])
+
+ sessions = chat_assistant.list_sessions()
+ assert len(sessions) == 0
+
+ @pytest.mark.parametrize(
+ "payload, expected_message, remaining",
+ [
+ pytest.param(None, "", 5, marks=pytest.mark.p3),
+ pytest.param({"ids": ["invalid_id"]}, "The chat doesn't own the session invalid_id", 5, marks=pytest.mark.p3),
+ pytest.param("not json", """AttributeError("\'str\' object has no attribute \'get\'")""", 5, marks=pytest.mark.skip),
+ pytest.param(lambda r: {"ids": r[:1]}, "", 4, marks=pytest.mark.p3),
+ pytest.param(lambda r: {"ids": r}, "", 0, marks=pytest.mark.p3),
+ pytest.param({"ids": []}, "", 5, marks=pytest.mark.p3),
+ ],
+ )
+ def test_basic_scenarios(self, add_sessions_with_chat_assistant_func, payload, expected_message, remaining):
+ chat_assistant, sessions = add_sessions_with_chat_assistant_func
+ if callable(payload):
+ payload = payload([session.id for session in sessions])
+
+ if expected_message:
+ with pytest.raises(Exception) as exception_info:
+ chat_assistant.delete_sessions(**payload)
+ assert expected_message in str(exception_info.value)
+ else:
+ if payload is None:
+ chat_assistant.delete_sessions()
+ else:
+ chat_assistant.delete_sessions(**payload)
+
+ sessions = chat_assistant.list_sessions()
+ assert len(sessions) == remaining
diff --git a/test/testcases/test_sdk_api/test_session_management/test_list_sessions_with_chat_assistant.py b/test/testcases/test_sdk_api/test_session_management/test_list_sessions_with_chat_assistant.py
new file mode 100644
index 0000000000..21b6fea9f9
--- /dev/null
+++ b/test/testcases/test_sdk_api/test_session_management/test_list_sessions_with_chat_assistant.py
@@ -0,0 +1,281 @@
+#
+# Copyright 2025 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.
+#
+import pytest
+from concurrent.futures import ThreadPoolExecutor, as_completed
+from configs import HOST_ADDRESS
+from ragflow_sdk import RAGFlow
+from ragflow_sdk.modules.session import Message, Session
+
+
[email protected](scope="session")
+def auth():
+ return "unit-auth"
+
+
[email protected](scope="session", autouse=True)
+def set_tenant_info():
+ return None
+
+
+class TestSessionsWithChatAssistantList:
+ @pytest.mark.p3
+ def test_list_sessions_raises_on_nonzero_response(self, add_sessions_with_chat_assistant, monkeypatch):
+ chat_assistant, _ = add_sessions_with_chat_assistant
+
+ class _DummyResponse:
+ def json(self):
+ return {"code": 1, "message": "boom"}
+
+ monkeypatch.setattr(chat_assistant, "get", lambda *_args, **_kwargs: _DummyResponse())
+
+ with pytest.raises(Exception) as exception_info:
+ chat_assistant.list_sessions()
+ assert "boom" in str(exception_info.value)
+
+ @pytest.mark.p1
+ @pytest.mark.parametrize(
+ "params, expected_page_size, expected_message",
+ [
+ ({"page": None, "page_size": 2}, 0, "not instance of"),
+ pytest.param({"page": 0, "page_size": 2}, 0, "ValueError('Search does not support negative slicing.')", marks=pytest.mark.skip),
+ ({"page": 2, "page_size": 2}, 2, ""),
+ ({"page": 3, "page_size": 2}, 1, ""),
+ ({"page": "3", "page_size": 2}, 0, "not instance of"),
+ pytest.param({"page": -1, "page_size": 2}, 0, "ValueError('Search does not support negative slicing.')", marks=pytest.mark.skip),
+ pytest.param({"page": "a", "page_size": 2}, 0, """ValueError("invalid literal for int() with base 10: \'a\'")""", marks=pytest.mark.skip),
+ ],
+ )
+ def test_page(self, add_sessions_with_chat_assistant, params, expected_page_size, expected_message):
+ chat_assistant, _ = add_sessions_with_chat_assistant
+ if expected_message:
+ with pytest.raises(Exception) as exception_info:
+ chat_assistant.list_sessions(**params)
+ assert expected_message in str(exception_info.value)
+ else:
+ sessions = chat_assistant.list_sessions(**params)
+ assert len(sessions) == expected_page_size
+
+ @pytest.mark.p1
+ @pytest.mark.parametrize(
+ "params, expected_page_size, expected_message",
+ [
+ ({"page_size": None}, 0, "not instance of"),
+ ({"page_size": 0}, 5, ""),
+ ({"page_size": 1}, 1, ""),
+ ({"page_size": 6}, 5, ""),
+ ({"page_size": "1"}, 0, "not instance of"),
+ pytest.param({"page_size": -1}, 5, "", marks=pytest.mark.skip),
+ pytest.param({"page_size": "a"}, 0, """ValueError("invalid literal for int() with base 10: \'a\'")""", marks=pytest.mark.skip),
+ ],
+ )
+ def test_page_size(self, add_sessions_with_chat_assistant, params, expected_page_size, expected_message):
+ chat_assistant, _ = add_sessions_with_chat_assistant
+ if expected_message:
+ with pytest.raises(Exception) as exception_info:
+ chat_assistant.list_sessions(**params)
+ assert expected_message in str(exception_info.value)
+ else:
+ sessions = chat_assistant.list_sessions(**params)
+ assert len(sessions) == expected_page_size
+
+ @pytest.mark.p3
+ @pytest.mark.parametrize(
+ "params, expected_message",
+ [
+ ({"orderby": None}, "not instance of"),
+ ({"orderby": "create_time"}, ""),
+ ({"orderby": "update_time"}, ""),
+ ({"orderby": "name", "desc": "False"}, "not instance of"),
+ pytest.param({"orderby": "unknown"}, "orderby should be create_time or update_time", marks=pytest.mark.skip(reason="issues/")),
+ ],
+ )
+ def test_orderby(self, add_sessions_with_chat_assistant, params, expected_message):
+ chat_assistant, _ = add_sessions_with_chat_assistant
+ if expected_message:
+ with pytest.raises(Exception) as exception_info:
+ chat_assistant.list_sessions(**params)
+ assert expected_message in str(exception_info.value)
+ else:
+ chat_assistant.list_sessions(**params)
+
+ @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}, ""),
+ ({"desc": "false"}, "not instance of"),
+ ({"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/")),
+ ],
+ )
+ def test_desc(self, add_sessions_with_chat_assistant, params, expected_message):
+ chat_assistant, _ = add_sessions_with_chat_assistant
+ if expected_message:
+ with pytest.raises(Exception) as exception_info:
+ chat_assistant.list_sessions(**params)
+ assert expected_message in str(exception_info.value)
+ else:
+ chat_assistant.list_sessions(**params)
+
+ @pytest.mark.p1
+ @pytest.mark.parametrize(
+ "params, expected_num, expected_message",
+ [
+ ({"name": None}, 0, "not instance of"),
+ ({"name": ""}, 5, ""),
+ ({"name": "session_with_chat_assistant_1"}, 1, ""),
+ ({"name": "unknown"}, 0, ""),
+ ],
+ )
+ def test_name(self, add_sessions_with_chat_assistant, params, expected_num, expected_message):
+ chat_assistant, _ = add_sessions_with_chat_assistant
+ if expected_message:
+ with pytest.raises(Exception) as exception_info:
+ chat_assistant.list_sessions(**params)
+ assert expected_message in str(exception_info.value)
+ else:
+ sessions = chat_assistant.list_sessions(**params)
+ if params["name"] == "session_with_chat_assistant_1":
+ assert sessions[0].name == params["name"]
+ else:
+ assert len(sessions) == expected_num
+
+ @pytest.mark.p1
+ @pytest.mark.parametrize(
+ "session_id, expected_num, expected_message",
+ [
+ (None, 0, "not instance of"),
+ ("", 5, ""),
+ (lambda r: r[0], 1, ""),
+ ("unknown", 0, ""),
+ ],
+ )
+ def test_id(self, add_sessions_with_chat_assistant, session_id, expected_num, expected_message):
+ chat_assistant, sessions = add_sessions_with_chat_assistant
+ if callable(session_id):
+ params = {"id": session_id([s.id for s in sessions])}
+ else:
+ params = {"id": session_id}
+
+ if expected_message:
+ with pytest.raises(Exception) as exception_info:
+ chat_assistant.list_sessions(**params)
+ assert expected_message in str(exception_info.value)
+ else:
+ list_sessions = chat_assistant.list_sessions(**params)
+ if "id" in params and params["id"] == sessions[0].id:
+ assert list_sessions[0].id == params["id"]
+ else:
+ assert len(list_sessions) == expected_num
+
+ @pytest.mark.p3
+ @pytest.mark.parametrize(
+ "session_id, name, expected_num, expected_message",
+ [
+ (lambda r: r[0], "session_with_chat_assistant_0", 1, ""),
+ (lambda r: r[0], "session_with_chat_assistant_100", 0, ""),
+ (lambda r: r[0], "unknown", 0, ""),
+ ("id", "session_with_chat_assistant_0", 0, ""),
+ ],
+ )
+ def test_name_and_id(self, add_sessions_with_chat_assistant, session_id, name, expected_num, expected_message):
+ chat_assistant, sessions = add_sessions_with_chat_assistant
+ if callable(session_id):
+ params = {"id": session_id([s.id for s in sessions]), "name": name}
+ else:
+ params = {"id": session_id, "name": name}
+
+ if expected_message:
+ with pytest.raises(Exception) as exception_info:
+ chat_assistant.list_sessions(**params)
+ assert expected_message in str(exception_info.value)
+ else:
+ list_sessions = chat_assistant.list_sessions(**params)
+ assert len(list_sessions) == expected_num
+
+ @pytest.mark.p3
+ def test_concurrent_list(self, add_sessions_with_chat_assistant):
+ count = 100
+ chat_assistant, _ = add_sessions_with_chat_assistant
+ with ThreadPoolExecutor(max_workers=5) as executor:
+ futures = [executor.submit(chat_assistant.list_sessions) for _ in range(count)]
+ responses = list(as_completed(futures))
+ assert len(responses) == count, responses
+
+ @pytest.mark.p3
+ def test_list_chats_after_deleting_associated_chat_assistant(self, client, add_sessions_with_chat_assistant):
+ chat_assistant, _ = add_sessions_with_chat_assistant
+ client.delete_chats(ids=[chat_assistant.id])
+
+ with pytest.raises(Exception) as exception_info:
+ chat_assistant.list_sessions()
+ assert "no authorization" in str(exception_info.value)
+
+
+def test_session_module_error_paths_unit(monkeypatch):
+ client = RAGFlow("token", HOST_ADDRESS)
+
+ unknown_session = Session(client, {"id": "session-unknown", "chat_id": "chat-1"})
+ unknown_session._Session__session_type = "unknown" # noqa: SLF001
+ with pytest.raises(Exception) as exception_info:
+ list(unknown_session.ask("hello", stream=False))
+ assert "Unknown session type" in str(exception_info.value)
+
+ bad_json_session = Session(client, {"id": "session-bad-json", "chat_id": "chat-1"})
+
+ class _BadJsonResponse:
+ def json(self):
+ raise ValueError("json decode failed")
+
+ monkeypatch.setattr(bad_json_session, "post", lambda *_args, **_kwargs: _BadJsonResponse())
+ with pytest.raises(Exception) as exception_info:
+ list(bad_json_session.ask("hello", stream=False))
+ assert "Invalid response" in str(exception_info.value)
+
+ ok_json_session = Session(client, {"id": "session-ok-json", "chat_id": "chat-1"})
+
+ class _OkJsonResponse:
+ def json(self):
+ return {"data": {"answer": "ok-answer", "reference": {"chunks": [{"id": "chunk-ok"}]}}}
+
+ monkeypatch.setattr(ok_json_session, "post", lambda *_args, **_kwargs: _OkJsonResponse())
+ ok_messages = list(ok_json_session.ask("hello", stream=False))
+ assert len(ok_messages) == 1
+ assert ok_messages[0].content == "ok-answer"
+ assert ok_messages[0].reference == [{"id": "chunk-ok"}]
+
+ transport_session = Session(client, {"id": "session-transport", "chat_id": "chat-1"})
+ monkeypatch.setattr(
+ transport_session,
+ "post",
+ lambda *_args, **_kwargs: (_ for _ in ()).throw(RuntimeError("transport boom")),
+ )
+ with pytest.raises(RuntimeError) as exception_info:
+ list(transport_session.ask("hello", stream=False))
+ assert "transport boom" in str(exception_info.value)
+
+ message = Message(client, {})
+ assert message.content == "Hi! I am your assistant, can I help you?"
+ assert message.reference is None
+ assert message.role == "assistant"
+ assert message.prompt is None
+ assert message.id is None
diff --git a/test/testcases/test_sdk_api/test_session_management/test_update_session_with_chat_assistant.py b/test/testcases/test_sdk_api/test_session_management/test_update_session_with_chat_assistant.py
new file mode 100644
index 0000000000..b92cc11eb9
--- /dev/null
+++ b/test/testcases/test_sdk_api/test_session_management/test_update_session_with_chat_assistant.py
@@ -0,0 +1,98 @@
+#
+# Copyright 2025 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.
+#
+from concurrent.futures import ThreadPoolExecutor, as_completed
+from random import randint
+
+import pytest
+from configs import SESSION_WITH_CHAT_NAME_LIMIT
+
+
+class TestSessionWithChatAssistantUpdate:
+ @pytest.mark.parametrize(
+ "payload, expected_message",
+ [
+ pytest.param({"name": "valid_name"}, "", marks=pytest.mark.p3),
+ pytest.param({"name": "a" * (SESSION_WITH_CHAT_NAME_LIMIT + 1)}, "", marks=pytest.mark.skip(reason="issues/")),
+ pytest.param({"name": 1}, "", marks=pytest.mark.skip(reason="issues/")),
+ pytest.param({"name": ""}, "`name` can not be empty", marks=pytest.mark.p3),
+ pytest.param({"name": "duplicated_name"}, "", marks=pytest.mark.p3),
+ pytest.param({"name": "case insensitive"}, "", marks=pytest.mark.p3),
+ ],
+ )
+ def test_name(self, add_sessions_with_chat_assistant_func, payload, expected_message):
+ chat_assistant, sessions = add_sessions_with_chat_assistant_func
+ session = sessions[0]
+
+ if payload["name"] == "duplicated_name":
+ session.update(payload)
+ elif payload["name"] == "case insensitive":
+ session.update({"name": payload["name"].upper()})
+
+ if expected_message:
+ with pytest.raises(Exception) as exception_info:
+ session.update(payload)
+ assert expected_message in str(exception_info.value)
+ else:
+ session.update(payload)
+ updated_session = chat_assistant.list_sessions(id=session.id)[0]
+ assert updated_session.name == payload["name"]
+
+ @pytest.mark.p3
+ def test_repeated_update_session(self, add_sessions_with_chat_assistant_func):
+ _, sessions = add_sessions_with_chat_assistant_func
+ session = sessions[0]
+
+ session.update({"name": "valid_name_1"})
+ session.update({"name": "valid_name_2"})
+
+ @pytest.mark.p3
+ @pytest.mark.parametrize(
+ "payload, expected_message",
+ [
+ pytest.param({"unknown_key": "unknown_value"}, "ValueError", marks=pytest.mark.skip),
+ ({}, ""),
+ pytest.param(None, "TypeError", marks=pytest.mark.skip),
+ ],
+ )
+ def test_invalid_params(self, add_sessions_with_chat_assistant_func, payload, expected_message):
+ _, sessions = add_sessions_with_chat_assistant_func
+ session = sessions[0]
+
+ if expected_message:
+ with pytest.raises(Exception) as exception_info:
+ session.update(payload)
+ assert expected_message in str(exception_info.value)
+ else:
+ session.update(payload)
+
+ @pytest.mark.p3
+ def test_concurrent_update_session(self, add_sessions_with_chat_assistant_func):
+ count = 50
+ _, sessions = add_sessions_with_chat_assistant_func
+
+ with ThreadPoolExecutor(max_workers=5) as executor:
+ futures = [executor.submit(sessions[randint(0, 4)].update, {"name": f"update session test {i}"}) for i in range(count)]
+ responses = list(as_completed(futures))
+ assert len(responses) == count, responses
+
+ @pytest.mark.p3
+ def test_update_session_to_deleted_chat_assistant(self, client, add_sessions_with_chat_assistant_func):
+ chat_assistant, sessions = add_sessions_with_chat_assistant_func
+ client.delete_chats(ids=[chat_assistant.id])
+
+ with pytest.raises(Exception) as exception_info:
+ sessions[0].update({"name": "valid_name"})
+ assert "no authorization" in str(exception_info.value)