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:
Liu An
2025-06-09 18:13:26 +08:00
committed by GitHub
parent 9a69d5f367
commit 5cc2eda362
18 changed files with 649 additions and 138 deletions

View File

@ -14,12 +14,14 @@
# limitations under the License.
#
from time import sleep
import pytest
from common import (
add_chunk,
batch_add_chunks,
batch_create_chat_assistants,
batch_create_datasets,
bulk_upload_documents,
create_chat_assistant,
delete_chat_assistants,
delete_datasets,
delete_session_with_chat_assistants,
@ -103,14 +105,14 @@ def clear_chat_assistants(request, api_key):
@pytest.fixture(scope="function")
def clear_session_with_chat_assistants(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)
_, _, chat_assistant_ids = add_chat_assistants
@pytest.fixture(scope="class")
def add_dataset(request, api_key):
@ -145,16 +147,8 @@ def add_chunks(api_key, add_document):
dataset_id, document_id = add_document
parse_documents(api_key, dataset_id, {"document_ids": [document_id]})
condition(api_key, dataset_id)
chunk_ids = []
for i in range(4):
res = add_chunk(api_key, dataset_id, document_id, {"content": f"chunk test {i}"})
chunk_ids.append(res["data"]["chunk"]["id"])
# issues/6487
from time import sleep
sleep(1)
chunk_ids = batch_add_chunks(api_key, dataset_id, document_id, 4)
sleep(1) # issues/6487
return dataset_id, document_id, chunk_ids
@ -168,10 +162,4 @@ def add_chat_assistants(request, api_key, add_document):
dataset_id, document_id = add_document
parse_documents(api_key, dataset_id, {"document_ids": [document_id]})
condition(api_key, dataset_id)
chat_assistant_ids = []
for i in range(5):
res = create_chat_assistant(api_key, {"name": f"test_chat_assistant_{i}", "dataset_ids": [dataset_id]})
chat_assistant_ids.append(res["data"]["id"])
return dataset_id, document_id, chat_assistant_ids
return dataset_id, document_id, batch_create_chat_assistants(api_key, 5)

View File

@ -14,7 +14,7 @@
# limitations under the License.
#
import pytest
from common import create_chat_assistant, delete_chat_assistants, list_documents, parse_documents
from common import batch_create_chat_assistants, delete_chat_assistants, list_documents, parse_documents
from utils import wait_for
@ -37,10 +37,4 @@ def add_chat_assistants_func(request, api_key, add_document):
dataset_id, document_id = add_document
parse_documents(api_key, dataset_id, {"document_ids": [document_id]})
condition(api_key, dataset_id)
chat_assistant_ids = []
for i in range(5):
res = create_chat_assistant(api_key, {"name": f"test_chat_assistant_{i}", "dataset_ids": [dataset_id]})
chat_assistant_ids.append(res["data"]["id"])
return dataset_id, document_id, chat_assistant_ids
return dataset_id, document_id, batch_create_chat_assistants(api_key, 5)

View File

@ -15,8 +15,10 @@
#
from time import sleep
import pytest
from common import add_chunk, delete_chunks, list_documents, parse_documents
from common import batch_add_chunks, delete_chunks, list_documents, parse_documents
from utils import wait_for
@ -31,22 +33,15 @@ def condition(_auth, _dataset_id):
@pytest.fixture(scope="function")
def add_chunks_func(request, api_key, add_document):
def cleanup():
delete_chunks(api_key, dataset_id, document_id, {"chunk_ids": []})
request.addfinalizer(cleanup)
dataset_id, document_id = add_document
parse_documents(api_key, dataset_id, {"document_ids": [document_id]})
condition(api_key, dataset_id)
chunk_ids = []
for i in range(4):
res = add_chunk(api_key, dataset_id, document_id, {"content": f"chunk test {i}"})
chunk_ids.append(res["data"]["chunk"]["id"])
chunk_ids = batch_add_chunks(api_key, dataset_id, document_id, 4)
# issues/6487
from time import sleep
sleep(1)
def cleanup():
delete_chunks(api_key, dataset_id, document_id, {"chunk_ids": chunk_ids})
request.addfinalizer(cleanup)
return dataset_id, document_id, chunk_ids

View File

@ -21,35 +21,32 @@ from common import bulk_upload_documents, delete_documents
@pytest.fixture(scope="function")
def add_document_func(request, api_key, add_dataset, ragflow_tmp_dir):
dataset_id = add_dataset
document_ids = bulk_upload_documents(api_key, dataset_id, 1, ragflow_tmp_dir)
def cleanup():
delete_documents(api_key, dataset_id, {"ids": None})
request.addfinalizer(cleanup)
return dataset_id, document_ids[0]
dataset_id = add_dataset
return dataset_id, bulk_upload_documents(api_key, dataset_id, 1, ragflow_tmp_dir)[0]
@pytest.fixture(scope="class")
def add_documents(request, api_key, add_dataset, ragflow_tmp_dir):
dataset_id = add_dataset
document_ids = bulk_upload_documents(api_key, dataset_id, 5, ragflow_tmp_dir)
def cleanup():
delete_documents(api_key, dataset_id, {"ids": None})
request.addfinalizer(cleanup)
return dataset_id, document_ids
dataset_id = add_dataset
return dataset_id, bulk_upload_documents(api_key, dataset_id, 5, ragflow_tmp_dir)
@pytest.fixture(scope="function")
def add_documents_func(request, api_key, add_dataset_func, ragflow_tmp_dir):
dataset_id = add_dataset_func
document_ids = bulk_upload_documents(api_key, dataset_id, 3, ragflow_tmp_dir)
def cleanup():
delete_documents(api_key, dataset_id, {"ids": None})
request.addfinalizer(cleanup)
return dataset_id, document_ids
dataset_id = add_dataset_func
return dataset_id, bulk_upload_documents(api_key, dataset_id, 3, ragflow_tmp_dir)

View File

@ -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)

View File

@ -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):

View File

@ -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):

View File

@ -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):

View File

@ -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):