test_create_session_with_chat_assistant.py
Overview
This test file test_create_session_with_chat_assistant.py is part of the InfiniFlow project and contains automated tests for the creation of chat sessions associated with chat assistants. The main focus is on validating the session creation logic, including name validation, concurrency handling, and error cases such as attempting to add sessions to deleted chat assistants.
The tests use the pytest framework and rely on fixtures to set up the environment (clear_session_with_chat_assistants, add_chat_assistants) and shared configuration constants (e.g., SESSION_WITH_CHAT_NAME_LIMIT). It also uses Python’s concurrent.futures.ThreadPoolExecutor to simulate concurrent session creation.
Classes and Methods
TestSessionWithChatAssistantCreate
This test class groups tests for verifying the behavior of creating sessions within chat assistants. It uses the pytest.mark.usefixtures decorator to ensure the clear_session_with_chat_assistants fixture is applied before each test, which presumably clears existing sessions and assistants to maintain a clean test environment.
Methods
test_name(self, add_chat_assistants, name, expected_message)
Purpose:
Validates the session creation with different session name inputs, checking for correct name handling, error raising on invalid inputs, and duplicate or case-insensitive name behavior.Parameters:
add_chat_assistants(fixture): Provides a tuple where the third element is a list of chat assistant instances.name(str or int): The name to be used for creating the session.expected_message(str): Expected error message substring if session creation should fail.
Behavior and Tests:
Tests valid names (
"valid_name").Tests names exceeding the configured length limit (
SESSION_WITH_CHAT_NAME_LIMIT).Tests non-string names (e.g., integer
1).Tests empty string names (should raise error with message "
namecan not be empty.").Tests duplicated names (same name created twice).
Tests case-insensitive duplicate detection (creating session with uppercased name when original exists).
Error Handling:
When an error is expected, the test asserts that the exception message contains theexpected_message.Example Usage:
# Assuming chat_assistant is an instance of a chat assistant session = chat_assistant.create_session(name="valid_name") assert session.name == "valid_name"
test_concurrent_create_session(self, add_chat_assistants)
Purpose:
Tests that the system correctly handles the concurrent creation of many sessions (1000) on a single chat assistant without loss or errors.Parameters:
add_chat_assistants(fixture): Provides chat assistants.
Implementation Details:
Uses a
ThreadPoolExecutorwith 5 worker threads to submit 1000 session creation tasks concurrently.Waits for all futures to complete and verifies the number of completed tasks matches the number of requests.
Fetches all sessions after creation and asserts the total count equals 1000.
Example Usage:
# Concurrent session creation simulation with ThreadPoolExecutor(max_workers=5) as executor: futures = [executor.submit(chat_assistant.create_session, name=f"session {i}") for i in range(1000)]
test_add_session_to_deleted_chat_assistant(self, client, add_chat_assistants)
Purpose:
Verifies that creating a session on a deleted chat assistant fails with an appropriate error.Parameters:
client: Presumably an API client or system interface that allows deleting chat assistants.add_chat_assistants(fixture): Provides chat assistants.
Implementation Details:
Deletes the chat assistant via
client.delete_chats(ids=[chat_assistant.id]).Attempts to create a session on the deleted assistant and asserts that an exception with message
"You do not own the assistant"is raised.
Example Usage:
client.delete_chats(ids=[chat_assistant.id]) with pytest.raises(Exception): chat_assistant.create_session(name="valid_name")
Important Implementation Details
Name Validation:
Session names must respect length constraints controlled bySESSION_WITH_CHAT_NAME_LIMIT. Empty names and duplicated names (case-insensitive) are rejected.Concurrency Handling:
The test simulates high concurrency using a thread pool to ensure the underlying session creation logic handles race conditions and data integrity.Error Handling:
Explicit checks for ownership and existence of chat assistants before allowing session creation.Use of Fixtures:
Fixtures likeclear_session_with_chat_assistantsandadd_chat_assistantsare used to prepare and clean the test environment but are defined outside this file.
Interaction with Other Parts of the System
configsModule:
ImportsSESSION_WITH_CHAT_NAME_LIMITwhich defines the maximum allowed length for session names.clientFixture or Object:
Used in tests to delete chat assistants, affecting test state.add_chat_assistantsFixture:
Provides initialized chat assistant instances required for session creation tests.Chat Assistant API:
The key interactions tested are through thechat_assistant.create_session(name)method andchat_assistant.list_sessions(page_size)to verify session management.Exception Handling:
The tests expect the chat assistant API to raise exceptions with meaningful messages when invalid operations occur.
Mermaid Class Diagram
The file contains a single test class TestSessionWithChatAssistantCreate with three test methods. The diagram below represents the class structure.
classDiagram
class TestSessionWithChatAssistantCreate {
<<pytest test class>>
+test_name(name, expected_message)
+test_concurrent_create_session()
+test_add_session_to_deleted_chat_assistant(client)
}
Summary
test_create_session_with_chat_assistant.py is a focused test suite that ensures reliable and correct behavior for creating chat sessions in chat assistants. It covers input validation, concurrency robustness, and ownership enforcement, thereby helping maintain the integrity and usability of the chat session management subsystem in InfiniFlow.