test_list_sessions_with_chat_assistant.py
Overview
test_list_sessions_with_chat_assistant.py is a test suite designed to validate the functionality, robustness, and authorization behavior of the API endpoint responsible for listing sessions associated with a chat assistant within the InfiniFlow platform.
The file uses the pytest framework to define multiple test cases that cover:
Authorization validation (valid and invalid tokens)
Pagination parameters (
page,page_size)Sorting and ordering (
orderby,desc)Filtering by session attributes (
name,id)Concurrent access and performance under load
Behavior when associated chat assistants are deleted
Handling of unexpected or invalid parameters
These tests ensure the API behaves correctly, handles errors gracefully, and enforces security restrictions.
Detailed Descriptions
Imports and Dependencies
ThreadPoolExecutorfromconcurrent.futures: Used to simulate concurrent API requests.pytest: Testing framework used for test case definition and parameterization.Functions and constants imported from:
common: IncludesINVALID_API_TOKEN,delete_chat_assistants, andlist_session_with_chat_assistants.libs.auth: Contains theRAGFlowHttpApiAuthclass for API authorization.libs.utils: Provides a utility functionis_sortedto verify sorting order in responses.
Classes and Methods
1. class TestAuthorization
Tests the authorization mechanics of the list_session_with_chat_assistants API call.
Method: test_invalid_auth
Purpose: Verifies that requests without authorization or with invalid API tokens are rejected appropriately.
Parameters (via
pytest.mark.parametrize):auth: Authorization object orNone.expected_code: Expected error code returned by the API.expected_message: Expected error message.
Test Flow:
Calls
list_session_with_chat_assistantswith providedauth.Asserts response code and message match expectations.
Example Usage:
auth = RAGFlowHttpApiAuth(INVALID_API_TOKEN) res = list_session_with_chat_assistants(auth, "chat_assistant_id") assert res["code"] == 109 assert "API key is invalid" in res["message"]
2. class TestSessionsWithChatAssistantList
Contains numerous parameterized tests to ensure the API correctly handles various request parameters and scenarios.
Common Parameters for Tests
get_http_api_auth: A fixture providing a valid authorization object.add_sessions_with_chat_assistant: A fixture that adds test sessions linked to a chat assistant and returns(chat_assistant_id, session_ids).
Method: test_page
Purpose: Tests pagination behavior with different
pageparameter values.Parameters:
params: Dict withpageandpage_size.expected_code: Expected API response code.expected_page_size: Expected number of sessions returned.expected_message: Expected error message if any.
Details:
Validates handling of
None, integer, string, negative, and invalid page inputs.Some invalid cases are skipped due to known issues.
Assertion: Checks response code and size or error message.
Method: test_page_size
Purpose: Tests the API's response to various
page_sizeparameter values.Parameters: Similar structure to
test_page.Details:
Tests
None, zero, positive integers, strings, and invalid negative or non-numeric values.
Assertion: Verifies correct number of sessions returned or appropriate error messages.
Method: test_orderby
Purpose: Ensures sorting by different fields (
create_time,update_time,name) works as expected.Parameters:
params: Dict withorderbyand optionaldesc.expected_code: Expected API response code.assertions: Lambda function to check sorting correctness.expected_message: Expected error message for invalidorderby.
Details: Skips invalid
orderbydue to known issues.Usage Example:
res = list_session_with_chat_assistants(auth, chat_assistant_id, params={"orderby": "create_time"}) assert is_sorted(res["data"], "create_time", True)
Method: test_desc
Purpose: Tests the effect of the
descparameter controlling ascending/descending order.Parameters: Similar to
test_orderbywith variousdescvalues (True,"true",False,"false", invalid).Details: Skips invalid
descvalues.Assertion: Uses
is_sortedto confirm order direction.
Method: test_name
Purpose: Filters sessions by their
nameattribute.Parameters:
params: Dict with keyname.expected_code,expected_num,expected_message: Expected results.
Details:
Tests filtering with exact name, partial, empty, and unknown names.
Assertion: Verifies returned session names and count.
Method: test_id
Purpose: Filters sessions by their
id.Parameters:
session_id: Can beNone, empty string, a callable returning an ID, or an unknown ID.Other expected result parameters.
Details:
Supports dynamic fetching of session IDs via callable.
Assertion: Checks correct filtering and error messages.
Method: test_name_and_id
Purpose: Tests combined filtering by both
nameandid.Parameters: Mix of session IDs and session names.
Assertion: Checks for correct filtering results.
Method: test_concurrent_list
Purpose: Validates the API can handle concurrent access without errors.
Implementation:
Uses
ThreadPoolExecutorwith 5 threads.Submits 100 concurrent
list_session_with_chat_assistantsrequests.Checks all responses have success code
0.
Method: test_invalid_params
Purpose: Tests that unexpected query parameters do not break the API.
Details: Passes an irrelevant parameter
{"a": "b"}and expects a successful response with default data.
Method: test_list_chats_after_deleting_associated_chat_assistant
Purpose: Ensures that after deleting a chat assistant, listing its sessions fails gracefully.
Details:
Deletes a chat assistant using
delete_chat_assistants.Attempts to list sessions for the deleted assistant.
Expects error code
102with a message indicating ownership issues.
Important Implementation Details
Parameter Validation: The tests cover boundary and invalid inputs for pagination and filtering parameters to verify robustness.
Sorting Checks: Sorting is verified by the utility function
is_sorted, which checks order of returned session lists.Authorization: Uses
RAGFlowHttpApiAuthto simulate valid and invalid API token usage.Concurrency: A multithreaded test simulates high-load scenarios ensuring thread safety and performance.
Fixtures: The tests rely on external fixtures (not defined here) such as
get_http_api_authandadd_sessions_with_chat_assistantto set up valid test contexts.Skipped Tests: Some tests are skipped due to known issues or unsupported features (marked with
pytest.mark.skipand skip reasons).
Interaction with Other Parts of the System
commonmodule: Provides helper functions to interact with chat assistant sessions (list_session_with_chat_assistants,delete_chat_assistants) and constants likeINVALID_API_TOKEN.libs.auth: Supplies authorization classes for API authentication.libs.utils: Utility functions for data validation (e.g., sorting checks).API Under Test: The tests directly target the HTTP API endpoint that lists chat assistant sessions, ensuring it integrates correctly with authentication and data management layers.
Test Fixtures: Though not shown, fixtures provide test data setup and teardown, enabling isolated and repeatable tests.
Visual Diagram
classDiagram
class TestAuthorization {
+test_invalid_auth(auth, expected_code, expected_message)
}
class TestSessionsWithChatAssistantList {
+test_page(get_http_api_auth, add_sessions_with_chat_assistant, params, expected_code, expected_page_size, expected_message)
+test_page_size(get_http_api_auth, add_sessions_with_chat_assistant, params, expected_code, expected_page_size, expected_message)
+test_orderby(get_http_api_auth, add_sessions_with_chat_assistant, params, expected_code, assertions, expected_message)
+test_desc(get_http_api_auth, add_sessions_with_chat_assistant, params, expected_code, assertions, expected_message)
+test_name(get_http_api_auth, add_sessions_with_chat_assistant, params, expected_code, expected_num, expected_message)
+test_id(get_http_api_auth, add_sessions_with_chat_assistant, session_id, expected_code, expected_num, expected_message)
+test_name_and_id(get_http_api_auth, add_sessions_with_chat_assistant, session_id, name, expected_code, expected_num, expected_message)
+test_concurrent_list(get_http_api_auth, add_sessions_with_chat_assistant)
+test_invalid_params(get_http_api_auth, add_sessions_with_chat_assistant)
+test_list_chats_after_deleting_associated_chat_assistant(get_http_api_auth, add_sessions_with_chat_assistant)
}
TestAuthorization --> list_session_with_chat_assistants
TestSessionsWithChatAssistantList --> list_session_with_chat_assistants
TestSessionsWithChatAssistantList --> delete_chat_assistants
TestSessionsWithChatAssistantList --> is_sorted
TestAuthorization --> RAGFlowHttpApiAuth
TestSessionsWithChatAssistantList --> RAGFlowHttpApiAuth
Summary
This file is a comprehensive test suite that rigorously verifies the list_session_with_chat_assistants API endpoint:
It ensures proper authorization enforcement.
Validates all input parameters related to pagination, filtering, and sorting.
Checks behavior under concurrent requests.
Tests system responses when linked chat assistants are deleted.
Uses parameterized tests to cover many scenarios efficiently.
Skips tests with known issues, indicating ongoing development or limitations.
This suite is critical for maintaining API reliability, security, and performance in the InfiniFlow platform.