test_list_sessions_with_chat_assistant.py
Overview
This file contains automated tests for verifying the behavior of the list_sessions method of a Chat Assistant component within the InfiniFlow system. The primary focus is to validate session listing functionality under various input parameters and edge cases, including pagination, sorting, filtering by session attributes (such as id and name), and concurrency.
Tests are implemented using the pytest framework with parameterized test cases and different pytest markers indicating priority or skip reasons. The file ensures robustness by testing both valid and invalid inputs and expected exceptions.
Classes and Methods
Class: TestSessionsWithChatAssistantList
This test class groups all test cases related to the list_sessions method of a chat assistant instance. It assumes the existence of a pytest fixture add_sessions_with_chat_assistant that provides a tuple (chat_assistant, sessions) — the chat assistant object and a list of session objects to test against.
Test Methods
Each test method uses pytest.mark.parametrize to run tests across multiple input scenarios.
test_page(self, add_sessions_with_chat_assistant, params, expected_page_size, expected_message)
Purpose: Tests the handling of the
pageparameter inlist_sessions.Parameters:
params(dict): Dictionary containing pagination parameters, specificallypageandpage_size.expected_page_size(int): Expected number of sessions returned.expected_message(str): Expected error message substring if an exception is expected.
Behavior:
If
expected_messageis non-empty, the test asserts that an exception with that message is raised.Otherwise, the test asserts the returned sessions list length matches
expected_page_size.
Example usage:
params = {"page": 2, "page_size": 2} sessions = chat_assistant.list_sessions(**params) assert len(sessions) == 2Notes:
Skipped tests for negative or invalid
pagevalues indicate known issues or unsupported scenarios.
test_page_size(self, add_sessions_with_chat_assistant, params, expected_page_size, expected_message)
Purpose: Tests the handling of the
page_sizeparameter inlist_sessions.Parameters:
params(dict): Dictionary containing thepage_size.expected_page_size(int): Expected number of sessions returned.expected_message(str): Expected error message substring if exception is anticipated.
Behavior:
Similar to
test_page, but focuses onpage_sizevalidation.
Example:
params = {"page_size": 1} sessions = chat_assistant.list_sessions(**params) assert len(sessions) == 1Notes:
Tests assert correct truncation at maximum session count (e.g., max 5).
Some tests for negative or malformed
page_sizevalues are skipped.
test_orderby(self, add_sessions_with_chat_assistant, params, expected_message)
Purpose: Tests sorting order by fields like
create_timeandupdate_time.Parameters:
params(dict): Containsorderbyand optionallydesc.expected_message(str): Expected error message if invalid parameters are passed.
Behavior:
Validates allowed sort fields (
create_time,update_time).Checks type of
descparameter.
Example:
params = {"orderby": "create_time"} chat_assistant.list_sessions(**params)Notes:
Invalid orderby fields or wrong types for
desccause exceptions.Some invalid scenarios are skipped due to known issues.
test_desc(self, add_sessions_with_chat_assistant, params, expected_message)
Purpose: Tests the boolean
descparameter controlling ascending/descending order.Parameters:
params(dict): Containsdescand optionallyorderby.expected_message(str): Expected error message if invaliddescvalues are used.
Behavior:
Only boolean
TrueorFalsevalues are accepted fordesc.String booleans or other types cause exceptions.
Example:
params = {"desc": True} chat_assistant.list_sessions(**params)Notes:
Skipped tests cover unsupported string representations of booleans.
test_name(self, add_sessions_with_chat_assistant, params, expected_num, expected_message)
Purpose: Tests filtering sessions by their
name.Parameters:
params(dict): Containsnamefilter.expected_num(int): Number of sessions expected in the result.expected_message(str): Expected error message if invalid.
Behavior:
Asserts that returned sessions match requested names or raise errors if invalid.
Example:
params = {"name": "session_with_chat_assistant_1"} sessions = chat_assistant.list_sessions(**params) assert sessions[0].name == "session_with_chat_assistant_1"
test_id(self, add_sessions_with_chat_assistant, session_id, expected_num, expected_message)
Purpose: Tests filtering by session
id.Parameters:
session_id(str or callable): Session ID or a callable that extracts an ID from the sessions list.expected_num(int): Number of sessions expected.expected_message(str): Expected error substring if exception is expected.
Behavior:
If
session_idis callable, it is called with all session IDs to select one.Validates correct session filtering by ID.
Example:
session_id = lambda ids: ids[0] sessions = chat_assistant.list_sessions(id=session_id([s.id for s in sessions])) assert sessions[0].id == session_id([s.id for s in sessions])
test_name_and_id(self, add_sessions_with_chat_assistant, session_id, name, expected_num, expected_message)
Purpose: Tests combined filtering by
idandname.Parameters:
session_id(str or callable): Session ID or callable returning ID.name(str): Session name filter.expected_num(int): Expected count of sessions.expected_message(str): Expected exception message substring.
Behavior:
Validates that the intersection of filters works properly.
Example:
params = {"id": session_id([s.id for s in sessions]), "name": "session_with_chat_assistant_0"} sessions = chat_assistant.list_sessions(**params) assert len(sessions) == expected_num
test_concurrent_list(self, add_sessions_with_chat_assistant)
Purpose: Verifies thread-safety and concurrent access for
list_sessions.Behavior:
Executes 100 concurrent calls to
list_sessionsusing a ThreadPoolExecutor with 5 workers.Asserts all calls complete successfully.
Example:
with ThreadPoolExecutor(max_workers=5) as executor: futures = [executor.submit(chat_assistant.list_sessions) for _ in range(100)] results = [f.result() for f in futures] assert len(results) == 100
test_list_chats_after_deleting_associated_chat_assistant(self, client, add_sessions_with_chat_assistant)
Purpose: Tests behavior of
list_sessionswhen the associated chat assistant has been deleted.Parameters:
client: A client fixture used to perform deletion.
Behavior:
Deletes the chat assistant.
Asserts that subsequent calls to
list_sessionsraise an exception indicating lack of ownership.
Example:
client.delete_chats(ids=[chat_assistant.id]) with pytest.raises(Exception) as excinfo: chat_assistant.list_sessions() assert "You don't own the assistant" in str(excinfo.value)
Implementation Details and Algorithms
The tests extensively use
pytest.mark.parametrizeto define multiple input/output scenarios in a concise manner.Exception testing is done with
pytest.raisescontext managers to verify error messages.Some invalid input cases are marked with
pytest.mark.skipindicating known issues or unsupported functionality.Concurrent testing uses Python's
concurrent.futures.ThreadPoolExecutorandas_completedto manage and verify multiple simultaneous calls.The tests rely on a fixture named
add_sessions_with_chat_assistantwhich is expected to create a chat assistant instance pre-populated with multiple sessions for testing.
Interaction with Other System Components
The file interacts primarily with the Chat Assistant object which exposes the
list_sessionsmethod.It uses the pytest framework for automated testing.
It interacts with a
clientobject in one test to delete chat assistants, simulating real-world operations that affect session availability.The tests assume the existence of session objects with attributes like
idandname.This test suite helps ensure that the session listing API behaves correctly before integration with other system components such as UI or backend services.
Usage Example
Assuming you have a pytest fixture add_sessions_with_chat_assistant in your test suite that returns a (chat_assistant, sessions) tuple:
pytest test_list_sessions_with_chat_assistant.py
This runs all parameterized tests validating the session listing functionality under various scenarios.
Mermaid Diagram: Class Structure
classDiagram
class TestSessionsWithChatAssistantList {
+test_page(params, expected_page_size, expected_message)
+test_page_size(params, expected_page_size, expected_message)
+test_orderby(params, expected_message)
+test_desc(params, expected_message)
+test_name(params, expected_num, expected_message)
+test_id(session_id, expected_num, expected_message)
+test_name_and_id(session_id, name, expected_num, expected_message)
+test_concurrent_list()
+test_list_chats_after_deleting_associated_chat_assistant(client)
}
TestSessionsWithChatAssistantList ..> pytest
TestSessionsWithChatAssistantList ..> ThreadPoolExecutor
Summary
This file is a comprehensive pytest-based test suite for the session listing capabilities of a Chat Assistant component in InfiniFlow. It rigorously tests input validation, sorting, filtering, pagination, concurrency, and error handling to ensure the robustness and correctness of the list_sessions API. It is designed to be integrated into the CI/CD pipeline for continuous validation of session management features.