test_delete_chat_assistants.py
Overview
This file contains a suite of automated tests for the deletion functionality of chat assistants in the InfiniFlow platform. It primarily validates the behavior of the delete_chat_assistants API endpoint under various scenarios, including authorization validation, input correctness, concurrency, and edge cases such as duplicate or invalid IDs.
The tests use the pytest framework and leverage helper functions from other modules (common, configs, libs.auth) to perform setup, authentication, and API interactions. The goal is to ensure that the deletion process is robust, correctly handles errors, and maintains data integrity.
Test Classes and Their Functionalities
1. TestAuthorization
Purpose:
Validates that the delete_chat_assistants API enforces proper authorization and returns appropriate error codes and messages when invalid or missing credentials are used.
Key Method:
test_invalid_auth(self, invalid_auth, expected_code, expected_message)Parameters:
invalid_auth: An invalid or missing authentication object (e.g.,Noneor an invalid API token wrapped inRAGFlowHttpApiAuth).expected_code: The expected error code returned by the API.expected_message: The expected error message returned by the API.
Functionality:
Callsdelete_chat_assistantswith the provided invalid authentication and asserts that the returned response code and message match the expectations.Example Usage:
test_auth = RAGFlowHttpApiAuth("invalid_token") response = delete_chat_assistants(test_auth) assert response["code"] == 109 assert response["message"] == "Authentication error: API key is invalid!"
2. TestChatAssistantsDelete
Purpose:
Tests the core functionality of deleting chat assistants, including handling of various payloads, invalid IDs, duplicates, repeated deletions, and concurrency.
Key Methods:
test_basic_scenarios(self, HttpApiAuth, add_chat_assistants_func, payload, expected_code, expected_message, remaining)Parameters:
HttpApiAuth: Valid authentication object for API calls.add_chat_assistants_func: A fixture or helper function that adds chat assistants and returns their IDs.payload: The deletion request payload (can beNone, empty, malformed, or contain IDs).expected_code: Expected response code from the API.expected_message: Expected response message (usually error messages if code ≠ 0).remaining: Expected number of chat assistants remaining after deletion.
Functionality:
Adds chat assistants.
Calls
delete_chat_assistantswith the given payload (supports callables to generate dynamic payloads).Validates the response code and message.
Confirms the number of remaining chat assistants matches expectations by calling
list_chat_assistants.
test_delete_partial_invalid_id(self, HttpApiAuth, add_chat_assistants_func, payload)Tests deletion payloads containing both valid and invalid assistant IDs. Ensures the API deletes valid IDs while reporting errors for invalid ones and that no assistants remain after successful deletions.
test_repeated_deletion(self, HttpApiAuth, add_chat_assistants_func)Verifies that trying to delete already deleted assistants returns a "not found" error.
test_duplicate_deletion(self, HttpApiAuth, add_chat_assistants_func)Checks behavior when the deletion payload contains duplicate assistant IDs. The API should delete the unique assistants once and report duplicates in errors.
test_concurrent_deletion(self, HttpApiAuth)Tests thread-safe deletion by concurrently deleting multiple assistants using a thread pool executor. Ensures all deletions succeed without race conditions.
test_delete_10k(self, HttpApiAuth)Stress-tests the deletion endpoint by creating and deleting a large batch (1,000 assistants) to verify performance and correctness under load.
Important Implementation Details and Algorithms
Parameterized Testing:
Usespytest.mark.parametrizeto systematically test multiple input scenarios and expected outcomes.Dynamic Payload Generation:
Some test cases accept callable payloads that receive the list of current chat assistant IDs to generate test-specific deletion requests, increasing test flexibility.Concurrency Testing:
EmploysThreadPoolExecutorto simulate multiple simultaneous deletion requests, ensuring that concurrent operations do not cause inconsistent states or failures.Validation of Edge Cases:
Tests handle cases such as empty payloads, invalid JSON-like strings, invalid or special-character IDs, repeated deletions, and duplicates, ensuring robust error handling.Test Prioritization:
Usespytest.mark.p1andpytest.mark.p3to categorize test priority levels.
Interactions with Other Parts of the System
commonModule:
Provides utility functions:batch_create_chat_assistants: Creates multiple chat assistants for testing.delete_chat_assistants: The core API call tested here.list_chat_assistants: Retrieves current chat assistants to verify test effects.
configsModule:
Supplies constants likeINVALID_API_TOKENfor testing auth errors.libs.authModule:
Contains theRAGFlowHttpApiAuthclass, which wraps API tokens for authenticated requests.Test Fixtures:
Fixtures likeHttpApiAuthandadd_chat_assistants_funcare assumed to be defined elsewhere, providing authenticated session contexts and setup helpers.
Usage Example
def test_delete_single_assistant(HttpApiAuth, add_chat_assistants_func):
_, _, chat_assistant_ids = add_chat_assistants_func
payload = {"ids": chat_assistant_ids[:1]}
res = delete_chat_assistants(HttpApiAuth, payload)
assert res["code"] == 0
remaining = list_chat_assistants(HttpApiAuth)
assert len(remaining["data"]) == len(chat_assistant_ids) - 1
Mermaid Class Diagram
classDiagram
class TestAuthorization {
+test_invalid_auth(invalid_auth, expected_code, expected_message)
}
class TestChatAssistantsDelete {
+test_basic_scenarios(HttpApiAuth, add_chat_assistants_func, payload, expected_code, expected_message, remaining)
+test_delete_partial_invalid_id(HttpApiAuth, add_chat_assistants_func, payload)
+test_repeated_deletion(HttpApiAuth, add_chat_assistants_func)
+test_duplicate_deletion(HttpApiAuth, add_chat_assistants_func)
+test_concurrent_deletion(HttpApiAuth)
+test_delete_10k(HttpApiAuth)
}
Summary
test_delete_chat_assistants.py is a critical part of the InfiniFlow testing suite targeting the chat assistant deletion API. It ensures the API correctly handles authentication, validates input payloads, deals gracefully with invalid or duplicate IDs, and supports high concurrency and large batch operations without failure. Through comprehensive parameterized and concurrent tests, it safeguards the robustness and reliability of chat assistant management in the platform.