test_delete_chat_assistants.py
Overview
This file contains a comprehensive suite of automated tests targeting the deletion functionality of chat assistants in the InfiniFlow system. It primarily verifies the correctness, robustness, and edge-case handling of the delete_chat_assistants API endpoint under various authorization scenarios and payload inputs.
The tests ensure that:
Authorization is correctly enforced.
Deletion requests handle invalid, empty, duplicate, and partial-invalid assistant IDs gracefully.
The system supports concurrent deletion without data corruption.
Large batch deletions (up to 10,000 assistants) succeed efficiently.
These tests help maintain the integrity and reliability of chat assistant management capabilities within the system.
Detailed Documentation
Imports and Dependencies
pytest: Testing framework used for structuring and running tests.ThreadPoolExecutorfromconcurrent.futures: Used to test concurrent deletion.commonmodule imports:INVALID_API_TOKEN: Constant representing an invalid API token.batch_create_chat_assistants: Helper function to create multiple chat assistants.delete_chat_assistants: Function to delete chat assistants via API.list_chat_assistants: Function to list existing chat assistants.
RAGFlowHttpApiAuthfromlibs.auth: Class for representing HTTP API authorization.
Classes
TestAuthorization
Class to test authorization scenarios for the delete_chat_assistants API.
Methods
test_invalid_auth(auth, expected_code, expected_message)
Tests deletion requests with invalid or missing authorization tokens.
Parameters:
auth: Authorization object orNone.expected_code(int): Expected response code.expected_message(str): Expected error message.
Returns: None. Asserts response correctness.
Usage Example:
auth = RAGFlowHttpApiAuth(INVALID_API_TOKEN) test_case = TestAuthorization() test_case.test_invalid_auth(auth, 109, "Authentication error: API key is invalid!")
TestChatAssistantsDelete
Class containing tests for various deletion scenarios of chat assistants.
Methods
test_basic_scenarios(get_http_api_auth, add_chat_assistants_func, payload, expected_code, expected_message, remaining)
Tests deletion with various payloads including
None, empty lists, invalid IDs, malformed JSON, and valid IDs.Parameters:
get_http_api_auth: Fixture providing valid authorization.add_chat_assistants_func: Fixture to add initial chat assistants, returns their IDs.payload: Deletion request payload; can be a dict, string, or callable returning dict.expected_code(int): Expected response code.expected_message(str): Expected error message if any.remaining(int): Expected number of assistants left after deletion.
Returns: None. Asserts API response and remaining assistants count.
test_delete_partial_invalid_id(get_http_api_auth, add_chat_assistants_func, payload)
Tests deletion where the payload contains a mix of valid and invalid assistant IDs.
Parameters: Same as above, but
payloadincludes invalid IDs mixed with valid ones.Behavior: Expects partial success, error message for invalid IDs, and all valid assistants deleted.
test_repeated_deletion(get_http_api_auth, add_chat_assistants_func)
Tests behavior when attempting to delete the same assistants twice.
Behavior: First deletion succeeds; second returns not found error.
test_duplicate_deletion(get_http_api_auth, add_chat_assistants_func)
Tests deletion request with duplicate assistant IDs.
Behavior: Deletion succeeds but error about duplicates is reported.
test_concurrent_deletion(get_http_api_auth)
Tests concurrent deletion of many assistants using multithreading.
Behavior: All concurrent deletions succeed without conflict.
test_delete_10k(get_http_api_auth)
Tests deletion of a very large batch (10,000) of chat assistants.
Behavior: Deletion succeeds and leaves zero assistants.
Important Implementation Details and Algorithms
Parameterization with pytest: Many tests use
@pytest.mark.parametrizeto test multiple input scenarios with a single test function, improving coverage and reducing code duplication.Callable payloads: Some test cases accept a lambda function as payload to dynamically generate deletion IDs based on the test setup.
Concurrent deletion test: Uses
ThreadPoolExecutorto simulate concurrent API calls, testing thread-safety and race conditions.Error handling checks: Tests verify specific error codes and messages returned by the API to ensure meaningful feedback on invalid operations.
Batch size handling: The 10k deletion test ensures the system can handle large scale operations efficiently.
Interaction with Other System Components
API Authentication: Uses
RAGFlowHttpApiAuthto authenticate requests.Chat Assistant Lifecycle Management: Relies on helper functions
batch_create_chat_assistants,delete_chat_assistants, andlist_chat_assistantsfrom thecommonmodule to manipulate chat assistant entities during tests.Test Fixtures: Uses pytest fixtures (e.g.,
get_http_api_auth,add_chat_assistants_func) to provide necessary setup such as authorization tokens and initial data creation.System Under Test: The file tests API endpoints presumably implemented elsewhere in the system that handle chat assistant deletion logic.
Mermaid Class Diagram
classDiagram
class TestAuthorization {
+test_invalid_auth(auth, expected_code, expected_message)
}
class TestChatAssistantsDelete {
+test_basic_scenarios(get_http_api_auth, add_chat_assistants_func, payload, expected_code, expected_message, remaining)
+test_delete_partial_invalid_id(get_http_api_auth, add_chat_assistants_func, payload)
+test_repeated_deletion(get_http_api_auth, add_chat_assistants_func)
+test_duplicate_deletion(get_http_api_auth, add_chat_assistants_func)
+test_concurrent_deletion(get_http_api_auth)
+test_delete_10k(get_http_api_auth)
}
Summary
This test module ensures the robustness of the chat assistant deletion API by validating authorization, handling of invalid inputs, concurrent operations, and large batch processing. It is an essential component of the InfiniFlow testing suite, safeguarding the reliability and security of chat assistant management.