test_delete_sessions_with_chat_assistant.py
Overview
This file contains automated test cases targeting the functionality related to deleting chat assistant sessions in the InfiniFlow system. It primarily uses pytest to validate the behavior of APIs that manage chat sessions under various authorization states, input parameters, concurrency scenarios, and edge cases.
The tests verify:
Proper handling of invalid authorization tokens.
Validation of chat assistant IDs during deletion requests.
Correct processing of deletion requests with mixed valid and invalid session IDs.
Behavior when deleting sessions multiple times or with duplicate IDs.
Concurrent deletion of sessions to test thread safety and consistency.
Bulk deletion with a large number of sessions (up to 1000).
Various other edge cases and expected error responses.
The test suite ensures robustness and correctness of the session deletion API (delete_session_with_chat_assistants) and its interactions with session listing (list_session_with_chat_assistants) and session creation utilities.
Imports and Dependencies
concurrent.futures.ThreadPoolExecutor, as_completed: For simulating concurrent deletion requests.pytest: Testing framework used for parametrized and marked test cases.commonmodule functions:batch_add_sessions_with_chat_assistant: Add multiple sessions to a chat assistant.delete_session_with_chat_assistants: API call to delete sessions.list_session_with_chat_assistants: API call to list sessions.
configs.INVALID_API_TOKEN: Constant representing an invalid API token.libs.auth.RAGFlowHttpApiAuth: Authentication helper class for API requests.
Classes and Methods
1. TestAuthorization
This class contains tests focused on validating authorization logic when attempting to delete sessions.
Methods
test_invalid_auth(self, invalid_auth, expected_code, expected_message)Purpose:
Tests the deletion API's response to invalid or missing authorization tokens.Parameters:
invalid_auth: EitherNoneor an instance ofRAGFlowHttpApiAuthcreated with an invalid API token.expected_code: Expected error code returned by the API.expected_message: Expected error message string.
Behavior:
Callsdelete_session_with_chat_assistantswith invalid authorization and asserts that the response code and message match expectations.Example usage:
test_auth = TestAuthorization() test_auth.test_invalid_auth(None, 0, "`Authorization` can't be empty")
2. TestSessionWithChatAssistantDelete
This class contains extensive tests for the session deletion API under various scenarios, including invalid inputs, concurrency, and large data sets.
Methods
test_invalid_chat_assistant_id(self, HttpApiAuth, add_sessions_with_chat_assistant_func, chat_assistant_id, expected_code, expected_message)Tests behavior when deleting sessions with invalid or empty chat assistant IDs.
test_delete_partial_invalid_id(self, HttpApiAuth, add_sessions_with_chat_assistant_func, payload)Tests deletion when the payload contains a mix of valid and invalid session IDs. It verifies that errors are correctly reported and that valid sessions are deleted.
test_repeated_deletion(self, HttpApiAuth, add_sessions_with_chat_assistant_func)Tests behavior when attempting to delete the same sessions multiple times, expecting errors on repeated attempts.
test_duplicate_deletion(self, HttpApiAuth, add_sessions_with_chat_assistant_func)Tests deletion when duplicate session IDs are included in the payload, expecting the API to handle duplicates gracefully and report errors.
test_concurrent_deletion(self, HttpApiAuth, add_chat_assistants)Tests concurrent deletion of sessions using multiple threads to ensure thread safety and correctness.
test_delete_1k(self, HttpApiAuth, add_chat_assistants)Tests bulk deletion of 1000 sessions to verify performance and correctness at scale.
test_basic_scenarios(self, HttpApiAuth, add_sessions_with_chat_assistant_func, payload, expected_code, expected_message, remaining)Tests various edge cases with different payloads, including empty lists, invalid IDs, malformed input, and ensures the correct number of sessions remain after deletion.
Parameters Explanation (common across tests)
HttpApiAuth: Fixture providing authenticated API access.add_sessions_with_chat_assistant_func: Fixture that adds sessions and returns a tuple(chat_assistant_id, session_ids).add_chat_assistants: Fixture that adds multiple chat assistants and returns identifiers.chat_assistant_id: String identifier for a chat assistant.session_ids: List of session ID strings.payload: Dictionary representing the deletion request body, typically{"ids": [...]}.expected_code: Integer representing the expected response code from the API.expected_message: String representing the expected error or success message.remaining: Integer representing how many sessions should remain after deletion.
Implementation Details and Algorithms
The tests utilize parameterization to cover multiple input cases efficiently.
Concurrency is tested using Python's
ThreadPoolExecutorto simulate multiple simultaneous deletion requests, verifying the system's thread safety.Errors returned by the API include custom codes and messages indicating specific failure reasons such as invalid tokens, unauthorized access, or duplicate IDs.
Result assertions check for both expected success and error conditions, ensuring the API handles all cases gracefully.
After deletion attempts, the tests often call
list_session_with_chat_assistantsto confirm the state of sessions, ensuring data integrity.
Interaction with Other System Components
Auth Layer:
UsesRAGFlowHttpApiAuthfor authenticating API calls, testing both valid and invalid tokens.API Endpoints:
delete_session_with_chat_assistants: The core API under test for session deletion.list_session_with_chat_assistants: Verifies the current sessions post-deletion.batch_add_sessions_with_chat_assistant: Used to set up test data by adding multiple sessions.
Test Fixtures:
The test methods rely on pytest fixtures (e.g.,HttpApiAuth,add_sessions_with_chat_assistant_func,add_chat_assistants) to prepare test data and authentication context.
Visual Diagram
classDiagram
class TestAuthorization {
+test_invalid_auth(invalid_auth, expected_code, expected_message)
}
class TestSessionWithChatAssistantDelete {
+test_invalid_chat_assistant_id(HttpApiAuth, add_sessions_with_chat_assistant_func, chat_assistant_id, expected_code, expected_message)
+test_delete_partial_invalid_id(HttpApiAuth, add_sessions_with_chat_assistant_func, payload)
+test_repeated_deletion(HttpApiAuth, add_sessions_with_chat_assistant_func)
+test_duplicate_deletion(HttpApiAuth, add_sessions_with_chat_assistant_func)
+test_concurrent_deletion(HttpApiAuth, add_chat_assistants)
+test_delete_1k(HttpApiAuth, add_chat_assistants)
+test_basic_scenarios(HttpApiAuth, add_sessions_with_chat_assistant_func, payload, expected_code, expected_message, remaining)
}
TestAuthorization ..> delete_session_with_chat_assistants : uses
TestSessionWithChatAssistantDelete ..> delete_session_with_chat_assistants : uses
TestSessionWithChatAssistantDelete ..> list_session_with_chat_assistants : uses
TestSessionWithChatAssistantDelete ..> batch_add_sessions_with_chat_assistant : uses
Summary
This file is a comprehensive pytest suite testing session deletion functionalities in a chat assistant system.
It handles authorization validation, error scenarios, concurrency, and performance with bulk data.
The tests ensure API reliability and correctness, providing confidence in the session management subsystem.
The use of fixtures and parameterized tests makes it maintainable and extensible for future test cases.