test_delete_chat_assistants.py
Overview
test_delete_chat_assistants.py is a test suite designed to validate the deletion functionality of chat assistants in the InfiniFlow system. It uses the pytest framework to run a series of parameterized and scenario-based tests that verify the correctness, robustness, and concurrency behavior of the chat assistant deletion API.
The file ensures that chat assistants can be deleted correctly under various conditions, including valid and invalid IDs, partial invalid inputs, duplicate deletions, concurrent deletions, and bulk deletions (up to 1000 assistants). It also checks proper error handling when attempting to delete non-existent assistants.
Classes and Methods
Class: TestChatAssistantsDelete
This class contains all test methods related to deleting chat assistants. Each method tests a specific aspect or scenario of the deletion process.
Method: test_basic_scenarios(self, client, add_chat_assistants_func, payload, expected_message, remaining)
Purpose: Tests various basic scenarios of deleting chat assistants using different payloads, including empty payloads, invalid IDs, and valid IDs.
Parameters:
client: A test client instance used to interact with the chat assistant API.add_chat_assistants_func: A fixture/function that adds chat assistants before tests and returns details.payload: The input dictionary or None specifying the IDs of chat assistants to delete. Can be a callable that receives the list of current assistant IDs.expected_message: The expected error message substring if deletion should fail.remaining: Expected number of chat assistants remaining after deletion.
Behavior:
If
payloadis callable, it is invoked with the current chat assistant IDs to generate the payload dynamically.If an error is expected (
expected_messageis set), it asserts that the appropriate exception is raised with the correct message.Otherwise, it performs the deletion and verifies the remaining chat assistants count.
Usage Example:
def test_scenario(client, add_chat_assistants_func): payload = {"ids": ["some_valid_id"]} expected_message = "" remaining = 4 test_instance = TestChatAssistantsDelete() test_instance.test_basic_scenarios(client, add_chat_assistants_func, payload, expected_message, remaining)
Method: test_delete_partial_invalid_id(self, client, add_chat_assistants_func, payload)
Purpose: Tests deletion where the payload includes both valid and invalid chat assistant IDs.
Parameters:
client: Test client instance.add_chat_assistants_func: Fixture to add chat assistants.payload: Callable that returns a payload dictionary with an invalid ID mixed with valid IDs.
Behavior:
Deletes the specified chat assistants.
Ensures that all valid assistants are deleted even if some IDs are invalid.
Verifies that no chat assistants remain after deletion.
Usage Example:
def test_partial_invalid(client, add_chat_assistants_func): payload_func = lambda ids: {"ids": ["invalid_id"] + ids} test_instance = TestChatAssistantsDelete() test_instance.test_delete_partial_invalid_id(client, add_chat_assistants_func, payload_func)
Method: test_repeated_deletion(self, client, add_chat_assistants_func)
Purpose: Verifies the behavior when attempting to delete the same chat assistants multiple times.
Parameters:
client: Test client instance.add_chat_assistants_func: Fixture that pre-populates chat assistants.
Behavior:
Deletes all chat assistants once successfully.
Attempts a second deletion of the same assistants, expecting an error indicating assistants are not found.
Usage:
Typical pytest test method; no external parameters needed.
Method: test_duplicate_deletion(self, client, add_chat_assistants_func)
Purpose: Tests deletion with duplicate IDs in the deletion request.
Parameters:
client: Test client instance.add_chat_assistants_func: Fixture adding chat assistants.
Behavior:
Sends deletion request containing duplicated IDs.
Ensures all assistants are deleted and none remain.
Usage:
Executed as part of the pytest suite.
Method: test_concurrent_deletion(self, client)
Purpose: Tests the concurrency robustness of the deletion API by deleting multiple chat assistants in parallel threads.
Parameters:
client: Test client instance.
Behavior:
Creates 100 chat assistants.
Uses a
ThreadPoolExecutorwith 5 workers to issue deletion requests concurrently for each chat assistant.Asserts no exceptions occurred and all deletions completed.
Usage:
Useful to verify thread safety and concurrent handling of deletion.
Method: test_delete_1k(self, client)
Purpose: Verifies the system can handle deletion of a large batch (1000) of chat assistants.
Parameters:
client: Test client instance.
Behavior:
Uses
batch_create_chat_assistantsutility to create 1000 chat assistants.Deletes all 1000 in one API call.
Asserts that no assistants remain after deletion.
Usage:
Tests scalability and performance for bulk deletions.
Important Implementation Details
Parameterized Testing: Uses
pytest.mark.parametrizeto run multiple input scenarios with varying payloads and expected outcomes.Exception Handling: Tests anticipate exceptions with invalid IDs and check the exception messages for correctness.
Callable Payloads: Some parameters accept lambdas to dynamically construct payloads based on the current state of chat assistants.
Concurrency: Uses Python's
concurrent.futures.ThreadPoolExecutorto simulate concurrent deletion requests, ensuring the API handles parallel operations safely.Bulk Operations: Includes testing for large-scale deletions (1,000 assistants), which verifies the system's ability to handle high-volume requests efficiently.
Interaction with Other Parts of the System
client: This test suite depends on aclientfixture or object that provides methods:create_chat(name): Creates a new chat assistant and returns an object with anid.delete_chats(ids=...): Deletes chat assistants by IDs.list_chats(): Lists all existing chat assistants.
add_chat_assistants_func: A fixture that pre-populates the system with chat assistants for testing.batch_create_chat_assistants: A utility function imported fromcommonthat facilitates bulk creation of chat assistants for performance tests.
This file primarily tests the deletion aspect of chat assistants, working closely with the chat assistant creation and listing APIs to set up and verify test conditions.
Mermaid Diagram: Class Structure of TestChatAssistantsDelete
classDiagram
class TestChatAssistantsDelete {
+test_basic_scenarios(client, add_chat_assistants_func, payload, expected_message, remaining)
+test_delete_partial_invalid_id(client, add_chat_assistants_func, payload)
+test_repeated_deletion(client, add_chat_assistants_func)
+test_duplicate_deletion(client, add_chat_assistants_func)
+test_concurrent_deletion(client)
+test_delete_1k(client)
}
Summary
test_delete_chat_assistants.py is a comprehensive pytest-based test suite that ensures the chat assistant deletion API of InfiniFlow behaves correctly under a variety of normal, boundary, error, and concurrent conditions. It leverages parameterization, exception assertions, concurrency testing, and bulk operations to validate robustness and correctness. This file is a critical part of the system's quality assurance for managing chat assistant lifecycle operations.