test_update_session_with_chat_assistant.py
Overview
This file contains a comprehensive suite of automated tests designed to verify the functionality, correctness, and robustness of the update_session_with_chat_assistant API endpoint in the InfiniFlow system. The tests focus on updating sessions associated with chat assistants, including validation of authorization, parameter correctness, concurrency handling, and edge cases like updating sessions tied to deleted chat assistants.
The key objectives of these tests are to:
Ensure proper authorization and error handling for invalid or missing API tokens.
Validate the behavior of session updates with various
namepayloads, including boundary and invalid cases.Confirm the handling of invalid chat assistant IDs and session IDs.
Test repeated and concurrent updates to sessions.
Verify correct error responses when updating sessions linked to deleted chat assistants.
These tests use the pytest framework and rely on helper APIs and fixtures defined elsewhere in the project.
Classes and Tests
TestAuthorization
Tests related to validating the authorization of API requests when updating a session.
Methods
test_invalid_auth(invalid_auth, expected_code, expected_message)Purpose: Verify that the API rejects requests with missing or invalid authorization tokens.
Parameters:
invalid_auth: An authorization object orNoneto simulate invalid authorization.expected_code(int): The expected error code returned by the API.expected_message(str): The expected error message returned by the API.
Behavior: Calls
update_session_with_chat_assistantwith invalid authorization and asserts that the error code and message match expectations.Usage Example:
auth = RAGFlowHttpApiAuth(INVALID_API_TOKEN) res = update_session_with_chat_assistant(auth, "chat_assistant_id", "session_id") assert res["code"] == 109 assert res["message"] == "Authentication error: API key is invalid!"
TestSessionWithChatAssistantUpdate
Tests focused on the core functionality and edge cases of updating a chat assistant session.
Methods
test_name(HttpApiAuth, add_sessions_with_chat_assistant_func, payload, expected_code, expected_message)Purpose: Validate updating the session's
namefield with various payloads, including valid, empty, duplicated, case-insensitive, and over-limit names.Parameters:
HttpApiAuth: Valid authentication fixture.add_sessions_with_chat_assistant_func: Fixture providing a chat assistant ID and session IDs.payload(dict): The data to update, typically containing anamekey.expected_code(int): Expected API response code.expected_message(str): Expected API response message.
Behavior: Updates a session's name and verifies the response code and message. For successful updates, confirms the updated name matches via a list query.
Notes: Some cases are skipped due to known issues.
Usage Example:
payload = {"name": "valid_name"} res = update_session_with_chat_assistant(HttpApiAuth, chat_assistant_id, session_id, payload) assert res["code"] == 0 res_list = list_session_with_chat_assistants(HttpApiAuth, chat_assistant_id, {"id": session_id}) assert res_list["data"][0]["name"] == "valid_name"
test_invalid_chat_assistant_id(HttpApiAuth, add_sessions_with_chat_assistant_func, chat_assistant_id, expected_code, expected_message)Purpose: Test the API's response to invalid or empty chat assistant IDs.
Parameters: Similar to above.
Behavior: Attempts to update with invalid chat assistant IDs and checks for proper error codes and messages.
test_invalid_session_id(HttpApiAuth, add_sessions_with_chat_assistant_func, session_id, expected_code, expected_message)Purpose: Test handling of invalid or empty session IDs.
Behavior: Similar to chat assistant ID tests but focused on session IDs.
test_repeated_update_session(HttpApiAuth, add_sessions_with_chat_assistant_func)Purpose: Confirm that repeated updates to the same session succeed without error.
Behavior: Performs two sequential updates with different names and asserts success.
test_invalid_params(HttpApiAuth, add_sessions_with_chat_assistant_func, payload, expected_code, expected_message)Purpose: Validate API behavior when given invalid or empty update payloads.
Behavior: Tests with unknown keys, empty dicts, and
None, expecting errors or success accordingly.
test_concurrent_update_session(HttpApiAuth, add_sessions_with_chat_assistant_func)Purpose: Test concurrent updates to multiple sessions to verify thread-safety and consistency.
Behavior: Uses
ThreadPoolExecutorto fire 50 concurrent update requests with randomized session IDs and unique names. Asserts all succeed with code 0.
test_update_session_to_deleted_chat_assistant(HttpApiAuth, add_sessions_with_chat_assistant_func)Purpose: Verify that sessions cannot be updated if their associated chat assistant has been deleted.
Behavior: Deletes the chat assistant, then attempts to update a session. Expects error code 102 and an appropriate message.
Important Implementation Details
Use of
pytest.mark.parametrize: Parameterizes tests to run multiple cases with different inputs and expected results, improving coverage and reducing code duplication.Fixtures: The tests depend on fixtures like
HttpApiAuthandadd_sessions_with_chat_assistant_func, which presumably provide authenticated API clients and pre-created sessions for testing.Error Handling: Tests cover a broad range of expected error scenarios, including invalid auth, invalid IDs, invalid payloads, and concurrency issues.
Concurrency Testing: Uses Python's
concurrent.futures.ThreadPoolExecutorto simulate multiple simultaneous updates, ensuring the API handles concurrent requests gracefully.Selective Skipping: Some tests are skipped due to known issues or unimplemented features, indicated by
pytest.mark.skipwith reasons.API Interaction: The tests exercise the following key API functions imported from the
commonmodule:update_session_with_chat_assistant— the main function under test.list_session_with_chat_assistants— to verify updates.delete_chat_assistants— to test deletion effects.
Interactions with Other Parts of the System
commonModule: Provides helper functions for chat assistant session management API calls.libs.auth.RAGFlowHttpApiAuth: Handles authentication tokens for API requests.configs: Supplies constants like invalid tokens and name length limits used in tests.Test Fixtures: These are likely defined elsewhere in the test suite to set up test data and authenticated contexts.
API Server: The tests assume a running backend API server that processes session update requests and returns structured JSON responses with
codeandmessagefields.
Visual Diagram
Below is a class diagram representing the test classes and their primary test methods, illustrating the organization and key functionalities tested.
classDiagram
class TestAuthorization {
+test_invalid_auth(invalid_auth, expected_code, expected_message)
}
class TestSessionWithChatAssistantUpdate {
+test_name(HttpApiAuth, add_sessions_with_chat_assistant_func, payload, expected_code, expected_message)
+test_invalid_chat_assistant_id(HttpApiAuth, add_sessions_with_chat_assistant_func, chat_assistant_id, expected_code, expected_message)
+test_invalid_session_id(HttpApiAuth, add_sessions_with_chat_assistant_func, session_id, expected_code, expected_message)
+test_repeated_update_session(HttpApiAuth, add_sessions_with_chat_assistant_func)
+test_invalid_params(HttpApiAuth, add_sessions_with_chat_assistant_func, payload, expected_code, expected_message)
+test_concurrent_update_session(HttpApiAuth, add_sessions_with_chat_assistant_func)
+test_update_session_to_deleted_chat_assistant(HttpApiAuth, add_sessions_with_chat_assistant_func)
}
Summary
The test_update_session_with_chat_assistant.py file is a critical part of the InfiniFlow test suite, ensuring the robustness and correctness of the session update functionality tied to chat assistants. Its thorough coverage helps maintain API stability and user experience by validating authorization, input validation, concurrency, and error handling in various realistic scenarios.