From e5f9d148e716a1070ecbc04bdad0a45abcb42770 Mon Sep 17 00:00:00 2001 From: liu an Date: Tue, 15 Apr 2025 14:54:26 +0800 Subject: [PATCH] Test: Added test cases for Delete Sessions With Chat Assistant HTTP API (#7025) ### What problem does this PR solve? cover [Delete chat assistant's sessions](https://ragflow.io/docs/dev/http_api_reference#delete-chat-assistants-sessions) endpoints ### Type of change - [x] Add test cases --- sdk/python/test/test_http_api/common.py | 8 + ...est_delete_sessions_with_chat_assistant.py | 172 ++++++++++++++++++ 2 files changed, 180 insertions(+) create mode 100644 sdk/python/test/test_http_api/test_session_management/test_delete_sessions_with_chat_assistant.py diff --git a/sdk/python/test/test_http_api/common.py b/sdk/python/test/test_http_api/common.py index c0e84281a..424624f28 100644 --- a/sdk/python/test/test_http_api/common.py +++ b/sdk/python/test/test_http_api/common.py @@ -247,3 +247,11 @@ def delete_session_with_chat_assistants(auth, chat_assistant_id, payload=None): url = f"{HOST_ADDRESS}{SESSION_WITH_CHAT_ASSISTANT_API_URL}".format(chat_id=chat_assistant_id) res = requests.delete(url=url, headers=HEADERS, auth=auth, json=payload) return res.json() + + +def batch_add_sessions_with_chat_assistant(auth, chat_assistant_id, num): + session_ids = [] + for i in range(num): + res = create_session_with_chat_assistant(auth, chat_assistant_id, {"name": f"session_with_chat_assistant_{i}"}) + session_ids.append(res["data"]["id"]) + return session_ids diff --git a/sdk/python/test/test_http_api/test_session_management/test_delete_sessions_with_chat_assistant.py b/sdk/python/test/test_http_api/test_session_management/test_delete_sessions_with_chat_assistant.py new file mode 100644 index 000000000..3d175156e --- /dev/null +++ b/sdk/python/test/test_http_api/test_session_management/test_delete_sessions_with_chat_assistant.py @@ -0,0 +1,172 @@ +# +# 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 + +import pytest +from common import INVALID_API_TOKEN, batch_add_sessions_with_chat_assistant, delete_session_with_chat_assistants, list_session_with_chat_assistants +from libs.auth import RAGFlowHttpApiAuth + + +class TestAuthorization: + @pytest.mark.parametrize( + "auth, expected_code, expected_message", + [ + (None, 0, "`Authorization` can't be empty"), + ( + RAGFlowHttpApiAuth(INVALID_API_TOKEN), + 109, + "Authentication error: API key is invalid!", + ), + ], + ) + def test_invalid_auth(self, auth, expected_code, expected_message): + res = delete_session_with_chat_assistants(auth, "chat_assistant_id") + assert res["code"] == expected_code + assert res["message"] == expected_message + + +class TestSessionWithChatAssistantDelete: + @pytest.mark.parametrize( + "chat_assistant_id, expected_code, expected_message", + [ + ("", 100, ""), + ( + "invalid_chat_assistant_id", + 102, + "You don't own the chat", + ), + ], + ) + def test_invalid_chat_assistant_id(self, get_http_api_auth, add_sessions_with_chat_assistant_func, chat_assistant_id, expected_code, expected_message): + _, session_ids = add_sessions_with_chat_assistant_func + res = delete_session_with_chat_assistants(get_http_api_auth, chat_assistant_id, {"ids": session_ids}) + assert res["code"] == expected_code + assert res["message"] == expected_message + + @pytest.mark.parametrize( + "payload", + [ + lambda r: {"ids": ["invalid_id"] + r}, + lambda r: {"ids": r[:1] + ["invalid_id"] + r[1:5]}, + lambda r: {"ids": r + ["invalid_id"]}, + ], + ) + def test_delete_partial_invalid_id(self, get_http_api_auth, add_sessions_with_chat_assistant_func, payload): + chat_assistant_id, session_ids = add_sessions_with_chat_assistant_func + if callable(payload): + payload = payload(session_ids) + res = delete_session_with_chat_assistants(get_http_api_auth, chat_assistant_id, payload) + assert res["code"] == 0 + assert res["data"]["errors"][0] == "The chat doesn't own the session invalid_id" + + res = list_session_with_chat_assistants(get_http_api_auth, chat_assistant_id) + if res["code"] != 0: + assert False, res + assert len(res["data"]) == 0 + + def test_repeated_deletion(self, get_http_api_auth, add_sessions_with_chat_assistant_func): + chat_assistant_id, session_ids = add_sessions_with_chat_assistant_func + payload = {"ids": session_ids} + res = delete_session_with_chat_assistants(get_http_api_auth, chat_assistant_id, payload) + assert res["code"] == 0 + + res = delete_session_with_chat_assistants(get_http_api_auth, chat_assistant_id, payload) + assert res["code"] == 102 + assert "The chat doesn't own the session" in res["message"] + + def test_duplicate_deletion(self, get_http_api_auth, add_sessions_with_chat_assistant_func): + chat_assistant_id, session_ids = add_sessions_with_chat_assistant_func + res = delete_session_with_chat_assistants(get_http_api_auth, chat_assistant_id, {"ids": session_ids * 2}) + assert res["code"] == 0 + assert "Duplicate session ids" in res["data"]["errors"][0] + assert res["data"]["success_count"] == 5 + + res = list_session_with_chat_assistants(get_http_api_auth, chat_assistant_id) + if res["code"] != 0: + assert False, res + assert len(res["data"]) == 0 + + @pytest.mark.slow + def test_concurrent_deletion(self, get_http_api_auth, add_chat_assistants): + sessions_num = 100 + _, _, chat_assistant_ids = add_chat_assistants + session_ids = batch_add_sessions_with_chat_assistant(get_http_api_auth, chat_assistant_ids[0], sessions_num) + + with ThreadPoolExecutor(max_workers=5) as executor: + futures = [ + executor.submit( + delete_session_with_chat_assistants, + get_http_api_auth, + chat_assistant_ids[0], + {"ids": session_ids[i : i + 1]}, + ) + for i in range(sessions_num) + ] + responses = [f.result() for f in futures] + assert all(r["code"] == 0 for r in responses) + + @pytest.mark.slow + def test_delete_1k(self, get_http_api_auth, add_chat_assistants): + sessions_num = 1_000 + _, _, chat_assistant_ids = add_chat_assistants + session_ids = batch_add_sessions_with_chat_assistant(get_http_api_auth, chat_assistant_ids[0], sessions_num) + + res = delete_session_with_chat_assistants(get_http_api_auth, chat_assistant_ids[0], {"ids": session_ids}) + assert res["code"] == 0 + + res = list_session_with_chat_assistants(get_http_api_auth, chat_assistant_ids[0]) + if res["code"] != 0: + assert False, res + assert len(res["data"]) == 0 + + @pytest.mark.parametrize( + "payload, expected_code, expected_message, remaining", + [ + pytest.param(None, 0, """TypeError("argument of type \'NoneType\' is not iterable")""", 0, marks=pytest.mark.skip), + ({"ids": ["invalid_id"]}, 102, "The chat doesn't own the session invalid_id", 5), + pytest.param( + "not json", + 100, + """AttributeError("\'str\' object has no attribute \'get\'")""", + 5, + marks=pytest.mark.skip, + ), + (lambda r: {"ids": r[:1]}, 0, "", 4), + (lambda r: {"ids": r}, 0, "", 0), + ({"ids": []}, 0, "", 0), + ], + ) + def test_basic_scenarios( + self, + get_http_api_auth, + add_sessions_with_chat_assistant_func, + payload, + expected_code, + expected_message, + remaining, + ): + chat_assistant_id, session_ids = add_sessions_with_chat_assistant_func + if callable(payload): + payload = payload(session_ids) + res = delete_session_with_chat_assistants(get_http_api_auth, chat_assistant_id, payload) + assert res["code"] == expected_code + if res["code"] != 0: + assert res["message"] == expected_message + + res = list_session_with_chat_assistants(get_http_api_auth, chat_assistant_id) + if res["code"] != 0: + assert False, res + assert len(res["data"]) == remaining