mirror of
https://github.com/infiniflow/ragflow.git
synced 2025-12-08 20:42:30 +08:00
Test: Refactor test fixtures and add SDK session management tests (#8141)
### What problem does this PR solve? - Consolidate HTTP API test fixtures using batch operations (batch_add_chunks, batch_create_chat_assistants) - Fix fixture initialization order in clear_session_with_chat_assistants - Add new SDK API test suite for session management (create/delete/list/update) ### Type of change - [x] Add test cases - [x] Refactoring
This commit is contained in:
@ -14,40 +14,28 @@
|
||||
# limitations under the License.
|
||||
#
|
||||
import pytest
|
||||
from common import create_session_with_chat_assistant, delete_session_with_chat_assistants
|
||||
from common import batch_add_sessions_with_chat_assistant, delete_session_with_chat_assistants
|
||||
|
||||
|
||||
@pytest.fixture(scope="class")
|
||||
def add_sessions_with_chat_assistant(request, api_key, add_chat_assistants):
|
||||
_, _, chat_assistant_ids = add_chat_assistants
|
||||
|
||||
def cleanup():
|
||||
for chat_assistant_id in chat_assistant_ids:
|
||||
delete_session_with_chat_assistants(api_key, chat_assistant_id)
|
||||
|
||||
request.addfinalizer(cleanup)
|
||||
|
||||
session_ids = []
|
||||
for i in range(5):
|
||||
res = create_session_with_chat_assistant(api_key, chat_assistant_ids[0], {"name": f"session_with_chat_assistant_{i}"})
|
||||
session_ids.append(res["data"]["id"])
|
||||
|
||||
return chat_assistant_ids[0], session_ids
|
||||
_, _, chat_assistant_ids = add_chat_assistants
|
||||
return chat_assistant_ids[0], batch_add_sessions_with_chat_assistant(api_key, chat_assistant_ids[0], 5)
|
||||
|
||||
|
||||
@pytest.fixture(scope="function")
|
||||
def add_sessions_with_chat_assistant_func(request, api_key, add_chat_assistants):
|
||||
_, _, chat_assistant_ids = add_chat_assistants
|
||||
|
||||
def cleanup():
|
||||
for chat_assistant_id in chat_assistant_ids:
|
||||
delete_session_with_chat_assistants(api_key, chat_assistant_id)
|
||||
|
||||
request.addfinalizer(cleanup)
|
||||
|
||||
session_ids = []
|
||||
for i in range(5):
|
||||
res = create_session_with_chat_assistant(api_key, chat_assistant_ids[0], {"name": f"session_with_chat_assistant_{i}"})
|
||||
session_ids.append(res["data"]["id"])
|
||||
|
||||
return chat_assistant_ids[0], session_ids
|
||||
_, _, chat_assistant_ids = add_chat_assistants
|
||||
return chat_assistant_ids[0], batch_add_sessions_with_chat_assistant(api_key, chat_assistant_ids[0], 5)
|
||||
|
||||
@ -13,7 +13,7 @@
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from concurrent.futures import ThreadPoolExecutor, as_completed
|
||||
|
||||
import pytest
|
||||
from common import INVALID_API_TOKEN, SESSION_WITH_CHAT_NAME_LIMIT, create_session_with_chat_assistant, delete_chat_assistants, list_session_with_chat_assistants
|
||||
@ -83,12 +83,12 @@ class TestSessionWithChatAssistantCreate:
|
||||
|
||||
@pytest.mark.p3
|
||||
def test_concurrent_create_session(self, api_key, add_chat_assistants):
|
||||
chunk_num = 1000
|
||||
count = 1000
|
||||
_, _, chat_assistant_ids = add_chat_assistants
|
||||
res = list_session_with_chat_assistants(api_key, chat_assistant_ids[0])
|
||||
if res["code"] != 0:
|
||||
assert False, res
|
||||
chunks_count = len(res["data"])
|
||||
sessions_count = len(res["data"])
|
||||
|
||||
with ThreadPoolExecutor(max_workers=5) as executor:
|
||||
futures = [
|
||||
@ -98,14 +98,15 @@ class TestSessionWithChatAssistantCreate:
|
||||
chat_assistant_ids[0],
|
||||
{"name": f"session with chat assistant test {i}"},
|
||||
)
|
||||
for i in range(chunk_num)
|
||||
for i in range(count)
|
||||
]
|
||||
responses = [f.result() for f in futures]
|
||||
assert all(r["code"] == 0 for r in responses)
|
||||
res = list_session_with_chat_assistants(api_key, chat_assistant_ids[0], {"page_size": chunk_num})
|
||||
responses = list(as_completed(futures))
|
||||
assert len(responses) == count, responses
|
||||
assert all(future.result()["code"] == 0 for future in futures)
|
||||
res = list_session_with_chat_assistants(api_key, chat_assistant_ids[0], {"page_size": count * 2})
|
||||
if res["code"] != 0:
|
||||
assert False, res
|
||||
assert len(res["data"]) == chunks_count + chunk_num
|
||||
assert len(res["data"]) == sessions_count + count
|
||||
|
||||
@pytest.mark.p3
|
||||
def test_add_session_to_deleted_chat_assistant(self, api_key, add_chat_assistants):
|
||||
|
||||
@ -13,7 +13,7 @@
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from concurrent.futures import ThreadPoolExecutor, as_completed
|
||||
|
||||
import pytest
|
||||
from common import INVALID_API_TOKEN, batch_add_sessions_with_chat_assistant, delete_session_with_chat_assistants, list_session_with_chat_assistants
|
||||
@ -105,9 +105,9 @@ class TestSessionWithChatAssistantDelete:
|
||||
|
||||
@pytest.mark.p3
|
||||
def test_concurrent_deletion(self, api_key, add_chat_assistants):
|
||||
sessions_num = 100
|
||||
count = 100
|
||||
_, _, chat_assistant_ids = add_chat_assistants
|
||||
session_ids = batch_add_sessions_with_chat_assistant(api_key, chat_assistant_ids[0], sessions_num)
|
||||
session_ids = batch_add_sessions_with_chat_assistant(api_key, chat_assistant_ids[0], count)
|
||||
|
||||
with ThreadPoolExecutor(max_workers=5) as executor:
|
||||
futures = [
|
||||
@ -117,10 +117,11 @@ class TestSessionWithChatAssistantDelete:
|
||||
chat_assistant_ids[0],
|
||||
{"ids": session_ids[i : i + 1]},
|
||||
)
|
||||
for i in range(sessions_num)
|
||||
for i in range(count)
|
||||
]
|
||||
responses = [f.result() for f in futures]
|
||||
assert all(r["code"] == 0 for r in responses)
|
||||
responses = list(as_completed(futures))
|
||||
assert len(responses) == count, responses
|
||||
assert all(future.result()["code"] == 0 for future in futures)
|
||||
|
||||
@pytest.mark.p3
|
||||
def test_delete_1k(self, api_key, add_chat_assistants):
|
||||
|
||||
@ -13,7 +13,7 @@
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from concurrent.futures import ThreadPoolExecutor, as_completed
|
||||
|
||||
import pytest
|
||||
from common import INVALID_API_TOKEN, delete_chat_assistants, list_session_with_chat_assistants
|
||||
@ -222,11 +222,13 @@ class TestSessionsWithChatAssistantList:
|
||||
|
||||
@pytest.mark.p3
|
||||
def test_concurrent_list(self, api_key, add_sessions_with_chat_assistant):
|
||||
count = 100
|
||||
chat_assistant_id, _ = add_sessions_with_chat_assistant
|
||||
with ThreadPoolExecutor(max_workers=5) as executor:
|
||||
futures = [executor.submit(list_session_with_chat_assistants, api_key, chat_assistant_id) for i in range(100)]
|
||||
responses = [f.result() for f in futures]
|
||||
assert all(r["code"] == 0 for r in responses)
|
||||
futures = [executor.submit(list_session_with_chat_assistants, api_key, chat_assistant_id) for i in range(count)]
|
||||
responses = list(as_completed(futures))
|
||||
assert len(responses) == count, responses
|
||||
assert all(future.result()["code"] == 0 for future in futures)
|
||||
|
||||
@pytest.mark.p3
|
||||
def test_invalid_params(self, api_key, add_sessions_with_chat_assistant):
|
||||
|
||||
@ -13,7 +13,7 @@
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from concurrent.futures import ThreadPoolExecutor, as_completed
|
||||
from random import randint
|
||||
|
||||
import pytest
|
||||
@ -122,7 +122,7 @@ class TestSessionWithChatAssistantUpdate:
|
||||
|
||||
@pytest.mark.p3
|
||||
def test_concurrent_update_session(self, api_key, add_sessions_with_chat_assistant_func):
|
||||
chunk_num = 50
|
||||
count = 50
|
||||
chat_assistant_id, session_ids = add_sessions_with_chat_assistant_func
|
||||
|
||||
with ThreadPoolExecutor(max_workers=5) as executor:
|
||||
@ -134,10 +134,11 @@ class TestSessionWithChatAssistantUpdate:
|
||||
session_ids[randint(0, 4)],
|
||||
{"name": f"update session test {i}"},
|
||||
)
|
||||
for i in range(chunk_num)
|
||||
for i in range(count)
|
||||
]
|
||||
responses = [f.result() for f in futures]
|
||||
assert all(r["code"] == 0 for r in responses)
|
||||
responses = list(as_completed(futures))
|
||||
assert len(responses) == count, responses
|
||||
assert all(future.result()["code"] == 0 for future in futures)
|
||||
|
||||
@pytest.mark.p3
|
||||
def test_update_session_to_deleted_chat_assistant(self, api_key, add_sessions_with_chat_assistant_func):
|
||||
|
||||
Reference in New Issue
Block a user