test_list_chat_assistants.py
Overview
This file contains automated test cases for verifying the functionality of the chat assistants listing feature in the InfiniFlow system. It uses the pytest framework to systematically validate how the system's client interface handles various query parameters and edge cases when retrieving lists of chat assistants.
The primary focus is to ensure that the list_chats() method of the client returns expected results, correctly handles pagination, sorting, filtering, and concurrency, and gracefully manages error scenarios and invalid inputs.
Detailed Explanation of Contents
Imports
ThreadPoolExecutor,as_completedfromconcurrent.futures: Used to run concurrent requests for testing thread safety and concurrency behavior.pytest: Testing framework used to define and execute the test cases.
Class: TestChatAssistantsList
This test class groups all tests related to listing chat assistants. It is decorated with @pytest.mark.usefixtures("add_chat_assistants"), meaning it depends on the add_chat_assistants fixture to prepare test data before tests run.
Methods:
test_default(self, client)
Purpose: Verify that the default call to
list_chats()returns exactly 5 chat assistants.Parameters:
client: The test client interface to the system.
Assertions: The number of assistants returned equals 5.
Usage Example:
assistants = client.list_chats() assert len(assistants) == 5
test_page(self, client, params, expected_page_size, expected_message)
Purpose: Test pagination behavior using various
pageandpage_sizevalues.Parameters:
client: The test client.params: Dict containing pagination parameterspageandpage_size.expected_page_size: Expected number of results returned.expected_message: Expected exception message substring if error anticipated.
Behavior:
If
expected_messageis set, the test expects an exception with a message containing that string.Otherwise, it asserts that the number of returned chat assistants matches
expected_page_size.
Notes: Some tests are skipped due to known issues (
issues/5851).Example:
client.list_chats(page=0, page_size=2) # expect 2 results client.list_chats(page="3", page_size=2) # expect exception mentioning "not instance of"
test_page_size(self, client, params, expected_page_size, expected_message)
Purpose: Focuses specifically on testing the
page_sizeparameter behavior.Parameters: Same as
test_page, but onlypage_sizeis varied.Behavior: Similar pattern of asserting correct number of results or exception messages.
Example:
client.list_chats(page_size=1) # expect 1 result client.list_chats(page_size="1") # expect exception "not instance of"
test_orderby(self, client, params, expected_message)
Purpose: Validate sorting behavior by different fields.
Parameters:
params: Dict withorderby(and optionallydesc) keys.expected_message: Expected error message substring if sorting parameters are invalid.
Valid values:
orderbyshould be"create_time"or"update_time".Example:
client.list_chats(orderby="create_time") client.list_chats(orderby="unknown") # expect exception about orderby value
test_desc(self, client, params, expected_message)
Purpose: Tests the
descparameter that controls ascending/descending order.Parameters:
params: Dict containingdescand optionallyorderby.expected_message: Expected error message substring for invaliddescvalues.
Valid values:
descmust be a booleanTrueorFalse.Example:
client.list_chats(desc=True) client.list_chats(desc="True") # expect exception about not instance of bool
test_name(self, client, params, expected_num, expected_message)
Purpose: Tests filtering chat assistants by their
nameattribute.Parameters:
params: Dict withnamekey.expected_num: Expected count of results.expected_message: Expected error message substring if name not found.
Behavior:
If
nameisNoneor empty string, all assistants are returned.Otherwise, it asserts returned names match filters or error is raised.
Example:
client.list_chats(name="test_chat_assistant_1") # expect one match client.list_chats(name="unknown") # expect exception "The chat doesn't exist"
test_id(self, client, add_chat_assistants, chat_assistant_id, expected_num, expected_message)
Purpose: Tests filtering by chat assistant
id.Parameters:
chat_assistant_id: Can be a string or a lambda that selects an id from created assistants.expected_num: Number of expected results.expected_message: Expected error substring if no match.
Notes: Uses the
add_chat_assistantsfixture to get existing assistants.Example:
client.list_chats(id="some-valid-id") client.list_chats(id="unknown") # expect exception "The chat doesn't exist"
test_name_and_id(self, client, add_chat_assistants, chat_assistant_id, name, expected_num, expected_message)
Purpose: Tests combined filtering by both
idandname.Parameters:
chat_assistant_id: Similar to above.name: Name filter.expected_num: Expected number of results.expected_message: Expected error substring if no matching chat.
Example:
client.list_chats(id="valid-id", name="test_chat_assistant_0") client.list_chats(id="id", name="chat_assistant_0") # expect exception
test_concurrent_list(self, client)
Purpose: Stress test to verify that concurrent calls to
list_chats()work correctly.Implementation: Uses a
ThreadPoolExecutorwith 5 workers to run 100 concurrent requests.Assertion: The number of completed responses equals the number of requests.
Example:
with ThreadPoolExecutor(max_workers=5) as executor: futures = [executor.submit(client.list_chats) for _ in range(100)] responses = list(as_completed(futures)) assert len(responses) == 100
test_list_chats_after_deleting_associated_dataset(self, client, add_chat_assistants)
Purpose: Checks that deleting the dataset associated with chat assistants does not affect the ability to list those assistants.
Parameters:
Uses
add_chat_assistantsfixture to get dataset and chats.
Behavior: Deletes the dataset, then asserts that all 5 assistants still appear in
list_chats().Example:
client.delete_datasets(ids=[dataset.id]) assistants = client.list_chats() assert len(assistants) == 5
Important Implementation Details
Fixture Dependency: The tests rely on the
add_chat_assistantsfixture, which presumably sets up a predefined set of chat assistants and an associated dataset, ensuring a known test environment.Error Skipping: Some test cases are marked with
pytest.mark.skipreferencingissues/5851, indicating known bugs or issues that prevent those tests from running successfully at this time.Parameter Validation: The tests rigorously check parameter validation for
page,page_size,orderby,desc,name, andid, ensuring the client validates inputs and raises appropriate exceptions.Concurrency Testing: Use of
ThreadPoolExecutortests thread safety and performance under concurrent load.Combined Filters: The tests check not only individual filters but combinations (e.g., filtering by both
idandname), verifying logical AND behavior.
Interaction with Other Parts of the System
Client Interface: The tests exercise the
client.list_chats()method, which is the interface to retrieve chat assistants from the system backend.Dataset Management: The system associates chat assistants with datasets, as evidenced by the test deleting datasets and verifying chat assistant list integrity.
Fixtures: The
add_chat_assistantsfixture is crucial and likely interacts with backend services or mocks to provision test data.Error Handling: The tests verify that the client propagates meaningful exceptions or error messages related to invalid parameters or missing data.
Visual Diagram
classDiagram
class TestChatAssistantsList {
<<pytest test class>>
+test_default(client)
+test_page(client, params, expected_page_size, expected_message)
+test_page_size(client, params, expected_page_size, expected_message)
+test_orderby(client, params, expected_message)
+test_desc(client, params, expected_message)
+test_name(client, params, expected_num, expected_message)
+test_id(client, add_chat_assistants, chat_assistant_id, expected_num, expected_message)
+test_name_and_id(client, add_chat_assistants, chat_assistant_id, name, expected_num, expected_message)
+test_concurrent_list(client)
+test_list_chats_after_deleting_associated_dataset(client, add_chat_assistants)
}
Summary
test_list_chat_assistants.py is a comprehensive suite of automated tests validating the robustness and correctness of the chat assistants listing API. It covers parameter validation, filtering, sorting, pagination, concurrency, and system behavior after data deletion. This suite ensures that the list_chats() client method behaves as expected across a variety of normal and edge cases, improving the reliability of the chat assistant feature in InfiniFlow.