test_delete_sessions_with_chat_assistant.py
Overview
This file contains a suite of automated tests designed to verify the correctness, robustness, and concurrency behavior of the session deletion functionality of a Chat Assistant component within the InfiniFlow system.
The primary focus is on the method delete_sessions of the chat_assistant object. These tests cover various scenarios including:
Deletion with partially invalid session IDs.
Handling repeated or duplicate deletion requests.
Concurrent deletion operations.
Large-scale deletion (e.g., deleting 1,000 sessions).
Basic scenarios with expected error handling.
The tests are written using the pytest framework and utilize helper functions and fixtures (such as add_sessions_with_chat_assistant_func and add_chat_assistants) for setup.
Classes and Methods
Class: TestSessionWithChatAssistantDelete
This class encapsulates multiple test cases that validate different aspects of the delete_sessions functionality of a chat assistant instance.
Method: test_delete_partial_invalid_id(self, add_sessions_with_chat_assistant_func, payload)
Purpose:
Test deletion behavior when the payload contains a mixture of valid and invalid session IDs.
Parameters:
add_sessions_with_chat_assistant_func: pytest fixture returning a tuple (chat_assistant, sessions) where:chat_assistant: the chat assistant instance.sessions: a list of session objects with valid IDs.
payload: A lambda function that generates a dictionary with an"ids"key, mixing valid session IDs and an"invalid_id"string. This is parameterized with three variations to test invalid IDs in different positions.
Behavior:
Calls
delete_sessionswith the generated payload.Asserts that all sessions are deleted (i.e., no sessions left after deletion).
Usage Example:
# Suppose sessions have IDs ['s1', 's2', 's3']
payload = lambda r: {"ids": ["invalid_id"] + r} # e.g., {"ids": ["invalid_id", "s1", "s2", "s3"]}
test_obj.test_delete_partial_invalid_id(add_sessions_with_chat_assistant_func, payload)
Method: test_repeated_deletion(self, add_sessions_with_chat_assistant_func)
Purpose:
Ensure that attempting to delete the same sessions twice raises an appropriate exception.
Parameters:
add_sessions_with_chat_assistant_func: as above.
Behavior:
Deletes all sessions once successfully.
Attempts to delete the same sessions again.
Expects an exception with the message
"The chat doesn't own the session".
Method: test_duplicate_deletion(self, add_sessions_with_chat_assistant_func)
Purpose:
Check that if session IDs are duplicated in the deletion request, the operation still completes successfully.
Parameters:
add_sessions_with_chat_assistant_func: as above.
Behavior:
Sends a deletion request where each session ID appears twice.
Verifies all sessions are deleted successfully without error.
Method: test_concurrent_deletion(self, add_chat_assistants)
Purpose:
Test deletion behavior under concurrent access to ensure thread safety and correct handling of multiple simultaneous deletions.
Parameters:
add_chat_assistants: pytest fixture returning multiple chat assistants; the first is used here.
Behavior:
Adds 100 sessions to the first chat assistant.
Uses
ThreadPoolExecutorwith 5 workers to concurrently delete each session in its own thread.Asserts all deletions complete (number of futures equals count).
Method: test_delete_1k(self, add_chat_assistants)
Purpose:
Stress-test the deletion logic by deleting 1,000 sessions at once.
Parameters:
add_chat_assistants: as above.
Behavior:
Adds 1,000 sessions to the first chat assistant.
Deletes all sessions in a single call.
Verifies no sessions remain.
Method: test_basic_scenarios(self, add_sessions_with_chat_assistant_func, payload, expected_message, remaining)
Purpose:
Validate multiple basic scenarios including error cases and normal deletions.
Parameters:
add_sessions_with_chat_assistant_func: as above.payload: Various payload inputs includingNone, invalid IDs, non-JSON strings, subsets of IDs, and empty lists.expected_message: Expected exception message substring if an error is expected.remaining: Number of sessions expected to remain after deletion.
Behavior:
If
payloadis callable, generates the actual payload using session IDs.If an
expected_messageis provided, asserts the deletion raises the corresponding exception.Otherwise, asserts successful deletion.
Verifies the remaining number of sessions matches expectation.
Important Implementation Details and Algorithms
Parameterized Testing:
The use ofpytest.mark.parametrizeallows testing multiple input variations cleanly and efficiently.Concurrent Deletion Handling:
Thetest_concurrent_deletionmethod uses Python'sThreadPoolExecutorto simulate multiple threads attempting deletions simultaneously, testing the thread safety of thedelete_sessionsmethod.Exception Validation:
Several tests expect specific exceptions when invalid or repeated deletions occur, validating proper error handling in the API.Batch Operations:
The tests demonstrate batch deletion by passing multiple session IDs in a single call, scaling up to 1,000 sessions.
Interactions with Other Parts of the System
chat_assistantObject:
Central to this file, this object represents the chat assistant service with methods:delete_sessions(ids: List[str]): Deletes sessions by their IDs.list_sessions(): Lists current sessions.
Fixtures:
add_sessions_with_chat_assistant_func: Provides a chat assistant instance with pre-added sessions.add_chat_assistants: Provides multiple chat assistant instances for concurrency tests.
Helper Function:
batch_add_sessions_with_chat_assistant(chat_assistant, count): Addscountsessions in batch for testing.
pytest Framework:
Used extensively for test parameterization, marking, exception handling, and test execution.
Visual Diagram
classDiagram
class TestSessionWithChatAssistantDelete {
+test_delete_partial_invalid_id(payload)
+test_repeated_deletion()
+test_duplicate_deletion()
+test_concurrent_deletion()
+test_delete_1k()
+test_basic_scenarios(payload, expected_message, remaining)
}
TestSessionWithChatAssistantDelete --> pytest
TestSessionWithChatAssistantDelete --> "chat_assistant"
TestSessionWithChatAssistantDelete --> add_sessions_with_chat_assistant_func
TestSessionWithChatAssistantDelete --> add_chat_assistants
TestSessionWithChatAssistantDelete --> batch_add_sessions_with_chat_assistant
Summary
This test module verifies the session deletion features of a chat assistant service, covering edge cases, concurrency, and large-scale operations. It ensures the system behaves correctly in the presence of invalid inputs, repeated operations, and concurrent requests, thus safeguarding the reliability and stability of session management in the InfiniFlow system.