Skip to content

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

test(sdk): audit memory management tests (#21670)

Sbaaoui Idrissdad48069054b
diff --git a/test/testcases/test_sdk_api/conftest.py b/test/testcases/test_sdk_api/conftest.py
index 2c185e0356..545af42c09 100644
--- a/test/testcases/test_sdk_api/conftest.py
+++ b/test/testcases/test_sdk_api/conftest.py
@@ -1,5 +1,5 @@
#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
+# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@@ -65,7 +65,6 @@ GO_ONLY_PATH_SKIPS = {
"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",
diff --git a/test/testcases/test_sdk_api/test_memory_management/conftest.py b/test/testcases/test_sdk_api/test_memory_management/conftest.py
index 992ef74d10..0559bc4885 100644
--- a/test/testcases/test_sdk_api/test_memory_management/conftest.py
+++ b/test/testcases/test_sdk_api/test_memory_management/conftest.py
@@ -1,5 +1,5 @@
#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
+# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@@ -13,41 +13,118 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
+"""Memory-only fixtures; run with this folder's pytest.ini and --confcutdir."""
+
+import os
+from uuid import uuid4
+
import pytest
-import random
+import requests
+from ragflow_sdk import RAGFlow
+
+
[email protected](autouse=True)
+def request_boundary(request, monkeypatch):
+ send = requests.sessions.Session.request
+
+ if request.node.get_closest_marker("integration") is None:
+
+ def forbid_network(*args, **kwargs):
+ pytest.fail("Service-free memory tests must not open a network connection")
+
+ monkeypatch.setattr(requests.sessions.Session, "send", forbid_network)
+
+ def bounded_request(session, *args, **kwargs):
+ kwargs.setdefault("timeout", 30)
+ return send(session, *args, **kwargs)
+
+ monkeypatch.setattr(requests.sessions.Session, "request", bounded_request)
+
+
[email protected](scope="session")
+def integration_settings():
+ required = ("RAGFLOW_SDK_BASE_URL", "RAGFLOW_SDK_API_KEY", "RAGFLOW_SDK_TENANT_ID")
+ if os.getenv("RAGFLOW_SDK_ISOLATED") != "1" or any(not os.getenv(key) for key in required):
+ pytest.fail(
+ "Memory integrations require a verified disposable Go backend and dedicated empty tenant: "
+ "RAGFLOW_SDK_ISOLATED=1, RAGFLOW_SDK_BASE_URL, RAGFLOW_SDK_API_KEY, RAGFLOW_SDK_TENANT_ID. "
+ "Run service-free tests with -m 'not integration'."
+ )
+ if os.getenv("PYTEST_XDIST_WORKER"):
+ pytest.fail("Memory integrations require serial execution in a dedicated tenant")
+ return dict(zip(("base_url", "api_key", "tenant_id"), (os.environ[key] for key in required)))
[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)
[email protected](scope="session")
+def isolated_tenant(integration_settings):
+ rag = RAGFlow(integration_settings["api_key"], integration_settings["base_url"])
+ response = requests.get(f"{rag.api_url}/memories", headers=rag.authorization_header, params={"page_size": 1}, timeout=30)
+ response.raise_for_status()
+ payload = response.json()
+ assert payload["code"] == 0, payload
+ assert payload["data"]["total_count"] == 0, "Refusing integrations: tenant has accessible memories"
+ assert payload["data"]["memory_list"] == [], payload
+ return integration_settings
- request.addfinalizer(cleanup)
- memory_ids = []
- for i in range(3):
+def model_ids():
+ keys = {"embd_id": "RAGFLOW_SDK_MEMORY_EMBD_ID", "llm_id": "RAGFLOW_SDK_MEMORY_LLM_ID"}
+ if any(not os.getenv(key) for key in keys.values()):
+ pytest.fail("Configure memory model identifiers explicitly: " + ", ".join(keys.values()))
+ return {field: os.environ[key] for field, key in keys.items()}
+
+
+def client(isolated_tenant, monkeypatch):
+ rag = RAGFlow(isolated_tenant["api_key"], isolated_tenant["base_url"])
+ owned = []
+ create = rag.create_memory
+
+ def tracked_create(*args, **kwargs):
+ memory = create(*args, **kwargs)
+ owned.append(memory.id)
+ assert memory.tenant_id == isolated_tenant["tenant_id"], "Created memory belongs to unexpected tenant"
+ return memory
+
+ monkeypatch.setattr(rag, "create_memory", tracked_create)
+ yield rag
+ errors = []
+ for memory_id in owned:
+ try:
+ payload = rag.get(f"/memories/{memory_id}/config").json()
+ if payload["code"] == 0:
+ rag.delete_memory(memory_id)
+ else:
+ assert payload["code"] == 404, payload
+ payload = rag.get(f"/memories/{memory_id}/config").json()
+ assert payload["code"] == 404, payload
+ except Exception as exc: # SDK raises plain Exception; report all cleanup failures.
+ errors.append(f"{memory_id}: {exc}")
+ assert not errors, "Owned memory cleanup failed: " + "; ".join(errors)
+
+
+def memory_factory(client, model_ids):
+ def create(name=None, memory_type=None, **overrides):
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",
+ "name": name if name is not None else f"sdk-memory-{uuid4().hex}",
+ "memory_type": memory_type if memory_type is not None else ["raw"],
+ **model_ids,
+ **overrides,
}
- 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
+ return client.create_memory(**payload)
+
+ return create
+
+
+def memory(memory_factory):
+ return memory_factory()
+
+
+def memory_group(memory_factory):
+ prefix = f"sdk-memory-{uuid4().hex}"
+ types = (["raw"], ["raw", "semantic"], ["raw", "episodic", "procedural"])
+ return prefix, [memory_factory(name=f"{prefix}-{i}", memory_type=value) for i, value in enumerate(types)]
diff --git a/test/testcases/test_sdk_api/test_memory_management/pytest.ini b/test/testcases/test_sdk_api/test_memory_management/pytest.ini
new file mode 100644
index 0000000000..69d69e7f1a
--- /dev/null
+++ b/test/testcases/test_sdk_api/test_memory_management/pytest.ini
@@ -0,0 +1,20 @@
+#
+# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+[pytest]
+pythonpath = ../../../../sdk/python
+addopts = --strict-markers
+markers =
+ integration: requires a verified isolated Go backend and dedicated empty tenant
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
index 3a101c33f0..ea3ffc0a6b 100644
--- 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
@@ -1,5 +1,5 @@
#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
+# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@@ -13,100 +13,74 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
-import random
-import re
+from uuid import uuid4
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)
+
+pytestmark = pytest.mark.integration
+
+
[email protected]("invalid_auth", [None, "invalid-api-token"])
+def test_auth_invalid(client, integration_settings, invalid_auth, model_ids, monkeypatch):
+ rag = RAGFlow(invalid_auth, integration_settings["base_url"])
+ with monkeypatch.context() as patch:
+ patch.setattr(client, "authorization_header", rag.authorization_header)
+ with pytest.raises(Exception, match="^Invalid access token$"):
+ client.create_memory(name=f"unauthorized-{uuid4().hex}", memory_type=["raw"], **model_ids)
+
+
[email protected]("name", ["a", "e" * 128, "记忆 café [test].+"])
+def test_name(memory_factory, name):
+ memory = memory_factory(name=name)
+ assert memory.id
+ assert memory.name == name
+ assert memory.get_config().name == name
+
+
[email protected]("name", ["", " ", "a" * 129])
+def test_name_invalid(memory_factory, name):
+ with pytest.raises(Exception, match="(?i)(name|required|128)"):
+ memory_factory(name=name)
+
+
[email protected]("memory_type", [["something"], ["raw", "something"], [], "raw", None])
+def test_type_invalid(client, model_ids, memory_type):
+ with pytest.raises(Exception, match="(?i)(memory.?type|memory type|required|unmarshal)"):
+ client.create_memory(name=f"sdk-invalid-{uuid4().hex}", memory_type=memory_type, **model_ids)
+
+
[email protected]("memory_type", [["raw"], ["raw", "semantic"], ["raw", "episodic", "procedural"], ["RAW", "raw", "semantic"]])
+def test_types(memory_factory, memory_type):
+ memory = memory_factory(memory_type=memory_type)
+ expected = {value.lower() for value in memory_type}
+ assert set(memory.memory_type) == expected
+ assert set(memory.get_config().memory_type) == expected
+
+
+def test_name_duplicated(memory_factory):
+ name = f"sdk-memory-{uuid4().hex}"
+ first = memory_factory(name=name)
+ second = memory_factory(name=name)
+ assert first.id != second.id
+ assert first.name == name
+ assert second.name == f"{name}(1)"
+ assert first.get_config().name == name
+ assert second.get_config().name == f"{name}(1)"
+
+
[email protected]("field", ["embd_id", "llm_id"])
[email protected]("value", ["", None, []])
+def test_required_model_id(memory_factory, field, value):
+ with pytest.raises(Exception, match="(?i)(required|unmarshal|model)"):
+ memory_factory(**{field: value})
+
+
+def test_create_defaults(memory_factory):
+ memory = memory_factory().get_config()
+ assert memory.storage_type == "table"
+ assert memory.permissions == "me"
+ assert memory.memory_size == 5242880
+ assert memory.forgetting_policy == "FIFO"
+ assert memory.temperature == 0.5
+ assert memory.system_prompt
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
index 54b95505ee..764555e44e 100644
--- 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
@@ -1,5 +1,5 @@
#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
+# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@@ -13,129 +13,148 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
-from concurrent.futures import ThreadPoolExecutor, as_completed
+from concurrent.futures import ThreadPoolExecutor
+from uuid import uuid4
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
+from ragflow_sdk import Memory, RAGFlow
+
+pytestmark = pytest.mark.integration
+
+
+def ids(result):
+ assert result["code"] == 0, result
+ assert isinstance(result["total_count"], int), result
+ assert all(isinstance(memory, Memory) for memory in result["memory_list"])
+ return {memory.id for memory in result["memory_list"]}
+
+
[email protected]("invalid_auth", [None, "invalid-api-token"])
+def test_auth_invalid(integration_settings, invalid_auth):
+ rag = RAGFlow(invalid_auth, integration_settings["base_url"])
+ with pytest.raises(Exception, match="^Invalid access token$"):
+ rag.list_memory()
+
+
+def test_capability(client, memory_group):
+ prefix, memories = memory_group
+ expected = {memory.id for memory in memories}
+ with ThreadPoolExecutor(max_workers=3) as executor:
+ futures = [executor.submit(client.list_memory, keywords=prefix) for _ in range(6)]
+ for future in futures:
+ result = future.result()
+ assert ids(result) == expected
+ assert result["total_count"] == len(expected)
+
+
+def test_params_unset(client, memory_group):
+ _, memories = memory_group
+ result = client.list_memory()
+ assert {memory.id for memory in memories} <= ids(result)
+ assert len(result["memory_list"]) <= 50
+ assert result["total_count"] >= len(memories)
+
+
[email protected]("page,page_size,expected_count", [(1, 10, 3), (2, 10, 0), (1, 2, 2), (2, 2, 1), (5, 10, 0)])
+def test_page(client, memory_group, page, page_size, expected_count):
+ prefix, memories = memory_group
+ result = client.list_memory(keywords=prefix, page=page, page_size=page_size)
+ returned = ids(result)
+ assert len(result["memory_list"]) == len(returned) == expected_count
+ assert returned <= {memory.id for memory in memories}
+ assert result["total_count"] == len(memories)
+
+
+def test_filter_memory_type(client, memory_group):
+ prefix, memories = memory_group
+ result = client.list_memory(keywords=prefix, memory_type="semantic")
+ assert ids(result) == {memories[1].id}
+ assert result["total_count"] == 1
+ assert result["memory_list"][0].memory_type == ["raw", "semantic"]
+
+
[email protected]("types", [["semantic", "procedural"], "semantic,procedural"])
+def test_filter_multi_memory_type(client, memory_group, types):
+ prefix, memories = memory_group
+ result = client.list_memory(keywords=prefix, memory_type=types)
+ assert ids(result) == {memories[1].id, memories[2].id}
+ assert result["total_count"] == 2
+
+
[email protected]("storage_type,expected", [("table", 3), ("unknown", 0)])
+def test_filter_storage_type(client, memory_group, storage_type, expected):
+ prefix, memories = memory_group
+ result = client.list_memory(keywords=prefix, storage_type=storage_type)
+ assert ids(result) == ({memory.id for memory in memories} if expected else set())
+ assert result["total_count"] == expected
+ assert all(memory.storage_type == storage_type for memory in result["memory_list"])
+
+
+def test_match_keyword(client, memory_group):
+ prefix, memories = memory_group
+ result = client.list_memory(keywords=f"{prefix}-1")
+ assert ids(result) == {memories[1].id}
+ assert result["total_count"] == 1
+ assert result["memory_list"][0].name == f"{prefix}-1"
+
+
+def test_get_config(client, memory):
+ changed = {"description": "persisted config", "memory_size": 1048576, "temperature": 0.7, "avatar": "data:image/png;base64,aGVsbG8=", "user_prompt": "User prompt"}
+ memory.update(changed)
+ fetched = Memory(client, {"id": memory.id}).get_config()
+ assert fetched.id == memory.id
+ for field, value in changed.items():
+ assert getattr(fetched, field) == value
+ for field in ("name", "tenant_id", "memory_type", "storage_type", "embd_id", "llm_id", "permissions", "forgetting_policy", "system_prompt"):
+ assert getattr(fetched, field) == getattr(memory, field)
+ payload = client.get(f"/memories/{memory.id}/config").json()
+ assert payload["code"] == 0, payload
+ assert fetched.owner_name == payload["data"].get("owner_name")
+
+
+def test_get_config_invalid_memory_id_raises(client):
+ with pytest.raises(Exception, match="^Resource not found$"):
+ Memory(client, {"id": uuid4().hex}).get_config()
+
+
[email protected]("multiple", [False, True])
+def test_tenant_filter(client, memory_group, multiple):
+ prefix, memories = memory_group
+ tenant = memories[0].tenant_id
+ result = client.list_memory(keywords=prefix, tenant_id=[tenant, uuid4().hex] if multiple else tenant)
+ assert ids(result) == {memory.id for memory in memories}
+ assert result["total_count"] == len(memories)
+ assert all(memory.tenant_id == tenant for memory in result["memory_list"])
+ absent = client.list_memory(keywords=prefix, tenant_id=uuid4().hex)
+ assert ids(absent) == set()
+ assert absent["total_count"] == 0
+
+
[email protected]("params", [{"page": 0}, {"page": -1}, {"page": "bad"}, {"page_size": 0}, {"page_size": -1}, {"page_size": "bad"}])
+def test_invalid_pagination_uses_defaults(client, memory_group, params):
+ prefix, memories = memory_group
+ result = client.list_memory(keywords=prefix, **params)
+ assert ids(result) == {memory.id for memory in memories}
+ assert result["total_count"] == len(memories)
+
+
+def test_unknown_memory_type(client, memory_group):
+ prefix, _ = memory_group
+ result = client.list_memory(keywords=prefix, memory_type="unknown")
+ assert ids(result) == set()
+ assert result["total_count"] == 0
+
+
+def test_pages_partition_owned_memories(client, memory_group):
+ prefix, memories = memory_group
+ first = ids(client.list_memory(keywords=prefix, page=1, page_size=2))
+ second = ids(client.list_memory(keywords=prefix, page=2, page_size=2))
+ assert not first & second
+ assert first | second == {memory.id for memory in memories}
+
+
[email protected]("invalid_auth", [None, "invalid-api-token"])
+def test_get_config_unauthorized(memory, integration_settings, invalid_auth):
+ rag = RAGFlow(invalid_auth, integration_settings["base_url"])
+ with pytest.raises(Exception, match="^Invalid access token$"):
+ Memory(rag, {"id": memory.id}).get_config()
diff --git a/test/testcases/test_sdk_api/test_memory_management/test_memory_fixtures.py b/test/testcases/test_sdk_api/test_memory_management/test_memory_fixtures.py
new file mode 100644
index 0000000000..45a95e77bf
--- /dev/null
+++ b/test/testcases/test_sdk_api/test_memory_management/test_memory_fixtures.py
@@ -0,0 +1,79 @@
+#
+# 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.
+#
+from inspect import unwrap
+from types import SimpleNamespace
+
+import pytest
+
+import conftest
+
+
+def fake_client(monkeypatch, *, fail_cleanup=False):
+ state = {"unrelated": {"id": "unrelated"}}
+ deleted = []
+
+ def create(name, **kwargs):
+ if name == "setup-failure":
+ raise RuntimeError("creation failed")
+ state[name] = {"id": name, "tenant_id": "tenant"}
+ return SimpleNamespace(**state[name])
+
+ def delete(memory_id):
+ deleted.append(memory_id)
+ if fail_cleanup and memory_id == "first":
+ raise RuntimeError("cleanup failed")
+ del state[memory_id]
+
+ rag = SimpleNamespace(
+ create_memory=create,
+ delete_memory=delete,
+ get=lambda path: SimpleNamespace(json=lambda: {"code": 0 if path.split("/")[2] in state else 404}),
+ )
+ monkeypatch.setattr(conftest, "RAGFlow", lambda *args: rag)
+ lifecycle = unwrap(getattr(conftest, "client"))({"api_key": "key", "base_url": "unused", "tenant_id": "tenant"}, monkeypatch)
+ return next(lifecycle), lifecycle, state, deleted
+
+
+def test_cleanup_owns_only_created_ids_after_partial_setup(monkeypatch):
+ rag, lifecycle, state, deleted = fake_client(monkeypatch)
+ rag.create_memory(name="first")
+ rag.create_memory(name="second")
+ with pytest.raises(RuntimeError, match="creation failed"):
+ rag.create_memory(name="setup-failure")
+ with pytest.raises(StopIteration):
+ next(lifecycle)
+ assert deleted == ["first", "second"]
+ assert set(state) == {"unrelated"}
+
+
+def test_cleanup_reports_failure_and_continues(monkeypatch):
+ rag, lifecycle, state, deleted = fake_client(monkeypatch, fail_cleanup=True)
+ rag.create_memory(name="first")
+ rag.create_memory(name="second")
+ with pytest.raises(AssertionError, match="first: cleanup failed"):
+ next(lifecycle)
+ assert deleted == ["first", "second"]
+ assert set(state) == {"unrelated", "first"}
+
+
+def test_cleanup_accepts_already_deleted_owned_memory(monkeypatch):
+ rag, lifecycle, state, deleted = fake_client(monkeypatch)
+ memory = rag.create_memory(name="first")
+ rag.delete_memory(memory.id)
+ with pytest.raises(StopIteration):
+ next(lifecycle)
+ assert deleted == ["first"]
+ assert set(state) == {"unrelated"}
diff --git a/test/testcases/test_sdk_api/test_memory_management/test_memory_sdk_contract.py b/test/testcases/test_sdk_api/test_memory_management/test_memory_sdk_contract.py
new file mode 100644
index 0000000000..be91a803dd
--- /dev/null
+++ b/test/testcases/test_sdk_api/test_memory_management/test_memory_sdk_contract.py
@@ -0,0 +1,198 @@
+#
+# 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 json
+from unittest.mock import Mock
+from urllib.parse import parse_qs, urlsplit
+
+import pytest
+from ragflow_sdk import Memory, RAGFlow
+
+CREATE_ARGUMENTS = {
+ "name": "sdk-memory",
+ "memory_type": ["raw", "semantic"],
+ "embd_id": "BAAI/bge-small-en-v1.5@Local@Builtin",
+ "llm_id": "glm-4-flash@CI@ZHIPU-AI",
+}
+
+
+def sdk():
+ return RAGFlow("memory-contract-token", "https://unused.invalid")
+
+
+def http(monkeypatch):
+ send = Mock(return_value=Mock(json=Mock(return_value={"code": 0})))
+ monkeypatch.setattr("requests.sessions.Session.send", send)
+ return send
+
+
+def assert_request(http, method, path):
+ http.assert_called_once()
+ request = http.call_args.args[0]
+ url = urlsplit(request.url)
+ assert (request.method, url.scheme, url.netloc, url.path) == (method, "https", "unused.invalid", f"/api/v1{path}")
+ assert request.headers["Authorization"] == "Bearer memory-contract-token"
+ return request
+
+
+ "model_ids",
+ [
+ {"embd_id": "embedding@provider", "llm_id": "chat@provider"},
+ {"embd_id": "embedding@instance@provider", "llm_id": "chat@instance@provider"},
+ {"embd_id": "cda680e08a9c4e4e8c140d223fcde610", "llm_id": "cf5485e08a9c4e4e8c140d223fcde610"},
+ ],
+ ids=["default-instance", "named-instance", "tenant-model-ids"],
+)
+def test_create_payload_and_memory_response(sdk, http, model_ids):
+ arguments = {**CREATE_ARGUMENTS, **model_ids}
+ row = {"id": "memory-id", **arguments}
+ http.return_value.json.return_value = {"code": 0, "data": row}
+
+ memory = sdk.create_memory(**arguments)
+
+ assert isinstance(memory, Memory)
+ assert memory.rag is sdk
+ for field, value in row.items():
+ assert getattr(memory, field) == value
+ request = assert_request(http, "POST", "/memories")
+ assert json.loads(request.body) == arguments
+
+
[email protected]("missing", ["name", "memory_type", "embd_id", "llm_id"])
+def test_create_requires_each_argument(sdk, http, missing):
+ arguments = {key: value for key, value in CREATE_ARGUMENTS.items() if key != missing}
+ with pytest.raises(TypeError, match=missing):
+ sdk.create_memory(**arguments)
+ http.assert_not_called()
+
+
[email protected]("unsupported", ["description", "storage_type"])
+def test_create_rejects_unsupported_sdk_arguments(sdk, http, unsupported):
+ with pytest.raises(TypeError, match=unsupported):
+ sdk.create_memory(**CREATE_ARGUMENTS, **{unsupported: "value"})
+ http.assert_not_called()
+
+
+ "filters,expected_query",
+ [
+ ({}, {"page": ["1"], "page_size": ["50"]}),
+ (
+ {"page": 2, "page_size": 1, "tenant_id": "tenant-1", "memory_type": "raw", "storage_type": "table", "keywords": "sdk memory"},
+ {"page": ["2"], "page_size": ["1"], "tenant_id": ["tenant-1"], "memory_type": ["raw"], "storage_type": ["table"], "keywords": ["sdk memory"]},
+ ),
+ (
+ {"tenant_id": ["tenant-1", "tenant-2"], "memory_type": ["raw", "episodic"]},
+ {"page": ["1"], "page_size": ["50"], "tenant_id": ["tenant-1", "tenant-2"], "memory_type": ["raw", "episodic"]},
+ ),
+ ],
+ ids=["defaults", "all-filters", "repeated-filter-keys"],
+)
+def test_list_query_and_response_mapping(sdk, http, filters, expected_query):
+ http.return_value.json.return_value = {
+ "code": 0,
+ "message": "success",
+ "data": {"memory_list": [{"id": "memory-id", "name": "sdk-memory", "memory_type": ["raw", "semantic"]}], "total_count": 7},
+ }
+
+ result = sdk.list_memory(**filters)
+
+ assert (result["code"], result["message"], result["total_count"]) == (0, "success", 7)
+ assert len(result["memory_list"]) == 1
+ memory = result["memory_list"][0]
+ assert isinstance(memory, Memory)
+ assert memory.rag is sdk
+ assert (memory.id, memory.name, memory.memory_type) == ("memory-id", "sdk-memory", ["raw", "semantic"])
+ request = assert_request(http, "GET", "/memories")
+ assert parse_qs(urlsplit(request.url).query) == expected_query
+ assert request.body is None
+
+
[email protected]("operation", ["update", "get_config"])
+def test_configuration_hydrates_server_values(sdk, http, operation):
+ memory = Memory(sdk, {"id": "memory-id"})
+ updates = {
+ "name": "persisted-name",
+ "avatar": "data:image/png;base64,aGVsbG8=",
+ "description": "persisted-description",
+ "llm_id": "gpt-4o@default@OpenAI",
+ "permissions": "team",
+ "memory_size": 1024 * 1024,
+ "temperature": 0,
+ "system_prompt": "Persisted system prompt.",
+ "user_prompt": "Persisted user prompt.",
+ }
+ persisted = {
+ "id": memory.id,
+ "tenant_id": "tenant-id",
+ "owner_name": "memory-owner",
+ "memory_type": ["raw", "episodic"],
+ "storage_type": "table",
+ "embd_id": CREATE_ARGUMENTS["embd_id"],
+ "forgetting_policy": "FIFO",
+ **updates,
+ }
+ http.return_value.json.return_value = {"code": 0, "data": persisted}
+
+ result = memory.update(updates) if operation == "update" else memory.get_config()
+
+ assert result is memory
+ assert memory.rag is sdk
+ for field, value in persisted.items():
+ assert getattr(memory, field) == value
+ if operation == "update":
+ request = assert_request(http, "PUT", "/memories/memory-id")
+ assert json.loads(request.body) == updates
+ else:
+ request = assert_request(http, "GET", "/memories/memory-id/config")
+ assert request.body is None
+
+
+def test_delete_returns_none_and_sends_owned_id(sdk, http):
+ assert sdk.delete_memory("memory-id") is None
+ request = assert_request(http, "DELETE", "/memories/memory-id")
+ assert json.loads(request.body) == {}
+
+
+ "operation,method,path",
+ [
+ ("create", "POST", "/memories"),
+ ("list", "GET", "/memories"),
+ ("update", "PUT", "/memories/memory-id"),
+ ("get_config", "GET", "/memories/memory-id/config"),
+ ("delete", "DELETE", "/memories/memory-id"),
+ ],
+)
+def test_api_errors_propagate(sdk, http, operation, method, path):
+ http.return_value.json.return_value = {"code": 401, "message": "memory request rejected"}
+ memory = Memory(sdk, {"id": "memory-id", "name": "unchanged"})
+ operations = {
+ "create": lambda: sdk.create_memory(**CREATE_ARGUMENTS),
+ "list": sdk.list_memory,
+ "update": lambda: memory.update({"name": "new-name"}),
+ "get_config": memory.get_config,
+ "delete": lambda: sdk.delete_memory(memory.id),
+ }
+
+ with pytest.raises(Exception, match="^memory request rejected$"):
+ operations[operation]()
+
+ assert memory.name == "unchanged"
+ assert_request(http, method, path)
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
index a2099e4c57..131d0a34fd 100644
--- 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
@@ -1,5 +1,5 @@
#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
+# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@@ -13,41 +13,48 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
+from uuid import uuid4
+
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
+from ragflow_sdk import Memory, RAGFlow
+
+pytestmark = pytest.mark.integration
+
+
[email protected]("invalid_auth", [None, "invalid-api-token"])
+def test_auth_invalid(client, memory, integration_settings, invalid_auth):
+ rag = RAGFlow(invalid_auth, integration_settings["base_url"])
+ with pytest.raises(Exception, match="^Invalid access token$"):
+ rag.delete_memory(memory.id)
+ assert Memory(client, {"id": memory.id}).get_config().id == memory.id
+
+
+def test_memory_id(client, memory_group):
+ prefix, memories = memory_group
+ assert client.delete_memory(memories[0].id) is None
+ result = client.list_memory(keywords=prefix)
+ assert {memory.id for memory in result["memory_list"]} == {memory.id for memory in memories[1:]}
+ assert result["total_count"] == 2
+ with pytest.raises(Exception, match="^Resource not found$"):
+ Memory(client, {"id": memories[0].id}).get_config()
+
+
[email protected]("memory_id", ["not-a-uuid", None])
+def test_id_wrong_uuid(client, memory_group, memory_id):
+ prefix, memories = memory_group
+ with pytest.raises(Exception, match="^Resource not found$"):
+ client.delete_memory(memory_id)
+ result = client.list_memory(keywords=prefix)
+ assert {memory.id for memory in result["memory_list"]} == {memory.id for memory in memories}
+ assert result["total_count"] == len(memories)
+
+
+def test_nonexistent_memory(client):
+ with pytest.raises(Exception, match="^Resource not found$"):
+ client.delete_memory(uuid4().hex)
+
+
+def test_repeated_delete(client, memory):
+ client.delete_memory(memory.id)
+ with pytest.raises(Exception, match="^Resource not found$"):
+ client.delete_memory(memory.id)
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
index 2cb3bfc92d..dc35c46a08 100644
--- 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
@@ -1,5 +1,5 @@
#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
+# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@@ -13,150 +13,172 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
-import random
+import os
+
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
+from ragflow_sdk import Memory, RAGFlow
+
+
+pytestmark = pytest.mark.integration
+
+
+def _assert_persisted(client, memory, changes):
+ result = memory.update(changes)
+ assert result is memory
+ fresh = Memory(client, {"id": memory.id}).get_config()
+ for field, expected in changes.items():
+ assert getattr(result, field) == expected
+ assert getattr(fresh, field) == expected
+ return fresh
class TestAuthorization:
- @pytest.mark.p2
+ @pytest.mark.parametrize("invalid_auth", [None, "invalid-sdk-memory-token"], ids=["missing", "invalid"])
+ def test_auth_invalid(self, client, memory, integration_settings, invalid_auth):
+ original = Memory(client, {"id": memory.id}).get_config().name
+ unauthorized = RAGFlow(invalid_auth, integration_settings["base_url"])
+ with pytest.raises(Exception, match="^Invalid access token$") as error:
+ Memory(unauthorized, {"id": memory.id}).update({"name": "Unauthorized change"})
+ assert str(error.value)
+ assert Memory(client, {"id": memory.id}).get_config().name == original
+
+
+class TestMemoryUpdate:
+ @pytest.mark.parametrize("kind", ["ascii", "unicode", "trimmed", "max_length"])
+ def test_name(self, client, memory, kind):
+ name = f"renamed-{memory.id}"
+ if kind == "unicode":
+ name = f"记忆-{memory.id}"
+ elif kind == "max_length":
+ name = "a" * (128 - len(memory.id)) + memory.id
+ submitted = f" {name} " if kind == "trimmed" else name
+ result = memory.update({"name": submitted})
+ assert result.name == name
+ assert Memory(client, {"id": memory.id}).get_config().name == name
+
@pytest.mark.parametrize(
- "invalid_auth, expected_message",
+ "name, message",
[
- (None, SDK_UNAUTHORIZED_ERROR_MESSAGE),
- (INVALID_API_TOKEN, SDK_UNAUTHORIZED_ERROR_MESSAGE),
+ ("", "name cannot be empty"),
+ (" \t\n", "name cannot be empty"),
+ ("a" * 129, None),
+ ("a" * 257, "name length"),
],
- ids=["empty_auth", "invalid_api_token"],
)
- def test_auth_invalid(self, invalid_auth, expected_message):
+ def test_name_invalid(self, client, memory, name, message):
+ original = Memory(client, {"id": memory.id}).get_config().name
+ with pytest.raises(Exception, match=message):
+ memory.update({"name": name})
+ assert Memory(client, {"id": memory.id}).get_config().name == original
+
+ def test_duplicate_name(self, client, memory, memory_factory):
+ other = memory_factory()
+ original = Memory(client, {"id": memory.id}).get_config().name
+ with pytest.raises(Exception, match="duplicated memory name"):
+ memory.update({"name": other.name})
+ assert Memory(client, {"id": memory.id}).get_config().name == original
+ assert Memory(client, {"id": other.id}).get_config().name == other.name
+
+ @pytest.mark.parametrize("avatar", ["data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+jRZkAAAAASUVORK5CYII=", ""])
+ def test_avatar(self, client, memory, avatar):
+ if not avatar:
+ memory.update({"avatar": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+jRZkAAAAASUVORK5CYII="})
+ _assert_persisted(client, memory, {"avatar": avatar})
+
+ @pytest.mark.parametrize("description", ["SDK memory description", ""])
+ def test_description(self, client, memory, description):
+ memory.update({"description": "previous description"})
+ _assert_persisted(client, memory, {"description": description})
+
+ def test_llm(self, client, memory, model_ids):
+ llm_id = os.environ.get("RAGFLOW_SDK_MEMORY_UPDATE_LLM_ID")
+ if not llm_id or llm_id == model_ids["llm_id"]:
+ pytest.fail("Requires RAGFLOW_SDK_MEMORY_UPDATE_LLM_ID naming a second configured chat model")
+ _assert_persisted(client, memory, {"llm_id": llm_id})
+
+ @pytest.mark.parametrize("permission", ["me", "team", "all", " TEAM "])
+ def test_permission(self, client, memory, permission):
+ memory.update({"permissions": "team" if permission == "me" else "me"})
+ result = memory.update({"permissions": permission})
+ expected = permission.strip().lower()
+ assert result.permissions == expected
+ assert Memory(client, {"id": memory.id}).get_config().permissions == expected
+
+ @pytest.mark.parametrize("memory_size", [1, 1048576, 5242880])
+ def test_memory_size(self, client, memory, memory_size):
+ _assert_persisted(client, memory, {"memory_size": memory_size})
+
+ @pytest.mark.parametrize("temperature", [0, 0.7, 1])
+ def test_temperature(self, client, memory, temperature):
+ _assert_persisted(client, memory, {"temperature": temperature})
+
+ @pytest.mark.parametrize("system_prompt", ["custom", ""])
+ def test_system_prompt(self, client, memory, system_prompt):
+ original = Memory(client, {"id": memory.id}).get_config().system_prompt or ""
+ if system_prompt:
+ system_prompt = original + "\nUse concise language when extracting memories."
+ else:
+ memory.update({"system_prompt": original + "\nPrevious custom instruction."})
+ _assert_persisted(client, memory, {"system_prompt": system_prompt})
+
+ @pytest.mark.parametrize("user_prompt", ["Remember these preferences.", ""])
+ def test_user_prompt(self, client, memory, user_prompt):
+ memory.update({"user_prompt": "previous user prompt"})
+ _assert_persisted(client, memory, {"user_prompt": user_prompt})
- 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",
+ "changes, 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."),
+ ({"permissions": ""}, "unknown permission"),
+ ({"permissions": "owner"}, "unknown permission"),
+ ({"memory_size": -1}, "memory size should be in range"),
+ ({"memory_size": 0}, "memory size should be in range"),
+ ({"memory_size": 5242881}, "memory size should be in range"),
+ ({"temperature": -0.1}, "temperature should be in range"),
+ ({"temperature": 1.1}, "temperature should be in range"),
+ ({"forgetting_policy": "LRU"}, "forgetting policy.*not supported"),
+ ({"memory_type": ["unknown"]}, "memory type.*not supported"),
+ ({"temperature": "warm"}, "cannot unmarshal"),
+ ({"memory_size": "large"}, "cannot unmarshal"),
],
)
- 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
+ def test_invalid_update(self, client, memory, changes, message):
+ original = Memory(client, {"id": memory.id}).get_config()
+ with pytest.raises(Exception, match=message):
+ memory.update({"description": "must not persist", **changes})
+ fresh = Memory(client, {"id": memory.id}).get_config()
+ assert fresh.description == original.description
+ for field in changes:
+ assert getattr(fresh, field) == getattr(original, field)
+
+ @pytest.mark.parametrize("policy", ["FIFO", " fifo "])
+ def test_forgetting_policy(self, client, memory, policy):
+ result = memory.update({"forgetting_policy": policy})
+ assert result.forgetting_policy == "FIFO"
+ assert Memory(client, {"id": memory.id}).get_config().forgetting_policy == "FIFO"
+
+ def test_identity_fields_immutable(self, client, memory):
+ original = Memory(client, {"id": memory.id}).get_config()
+ result = memory.update(
+ {
+ "id": "cannot-reassign-id",
+ "tenant_id": "cannot-reassign-owner",
+ "storage_type": "unsupported-storage",
+ "description": "supported field still updates",
+ }
+ )
+ assert result.id == original.id
+ fresh = Memory(client, {"id": original.id}).get_config()
+ for field in ("id", "tenant_id", "storage_type"):
+ assert getattr(result, field) == getattr(original, field)
+ assert getattr(fresh, field) == getattr(original, field)
+ assert fresh.description == "supported field still updates"
+
+ def test_empty_update(self, client, memory):
+ original = Memory(client, {"id": memory.id}).get_config()
+ result = memory.update({})
+ assert result is memory
+ fresh = Memory(client, {"id": memory.id}).get_config()
+ for field in ("name", "memory_type", "permissions", "memory_size", "temperature"):
+ assert getattr(result, field) == getattr(original, field)
+ assert getattr(fresh, field) == getattr(original, field)