test_update_chat_assistant.py
Overview
This file contains a suite of automated tests for validating the update functionality of chat assistants in the InfiniFlow project. It is implemented using the pytest framework and focuses on verifying the behavior of the update() method of chat assistant objects exposed via the ragflow_sdk.Chat class.
The tests cover multiple update scenarios including:
Updating the chat assistant's name with validation on length, uniqueness, and type.
Updating the chat assistant's avatar image.
Updating the underlying language model (LLM) configuration with various parameter boundary tests.
Updating prompt-related configurations including similarity thresholds, weights, and text templates.
The file ensures that invalid update payloads raise appropriate exceptions and that valid updates persist correctly when retrieved via the client API.
Detailed Explanation
Imports
attrgetterfromoperator: Used for dynamic attribute access in assertions.pytest: Testing framework.CHAT_ASSISTANT_NAME_LIMITfromconfigs: Configuration constant limiting assistant name length.Chatfromragflow_sdk: Core SDK class representing chat assistants.encode_avatarandcreate_image_filefromutils: Utility functions for avatar image handling and file creation.
Class: TestChatAssistantUpdate
This test class groups all test cases related to updating chat assistants.
Method: test_name(self, client, add_chat_assistants_func, payload, expected_message)
Tests updating the name field of a chat assistant with different payloads.
Parameters:
client: Test fixture providing API client access.add_chat_assistants_func: Fixture that creates test chat assistants and datasets.payload: Dictionary containing thenameto update.expected_message: Expected error message string if the update should fail; empty if the update should succeed.
Behavior:
Retrieves the first chat assistant from the fixture.
If
expected_messageis non-empty, expects an exception on update and asserts its message.Otherwise, updates the chat assistant and verifies the updated name matches the payload.
Usage Example:
payload = {"name": "valid_name"} TestChatAssistantUpdate().test_name(client, add_chat_assistants_func, payload, "")Test Cases:
Valid name within length limit.
Name exceeding length limit (skipped).
Non-string name (skipped).
Empty string name (error expected).
Duplicate names (error expected, case insensitive).
Method: test_avatar(self, client, add_chat_assistants_func, tmp_path)
Tests updating the chat assistant's avatar image.
Parameters:
client: API client.add_chat_assistants_func: Fixture for creating chat assistants and datasets.tmp_path: Temporary directory path for creating test image files.
Behavior:
Creates a dummy image file.
Encodes the image as avatar data.
Updates the chat assistant with new
name,avatar, and associateddataset_ids.Verifies that the avatar is set and name is updated.
Usage Example:
TestChatAssistantUpdate().test_avatar(client, add_chat_assistants_func, tmp_path)
Method: test_llm(self, client, add_chat_assistants_func, llm, expected_message)
Tests updating the LLM (Language Learning Model) configuration of a chat assistant.
Parameters:
client: API client.add_chat_assistants_func: Fixture providing chat assistants and dataset.llm: Dictionary with LLM configuration parameters.expected_message: Expected error message substring if update should fail; empty if update should succeed.
Behavior:
Prepares a payload with
name,llm, anddataset_ids.If an error is expected, asserts exception message contains
expected_message.Otherwise, performs update and validates each field in the updated LLM matches the payload.
If
llmis empty, asserts default LLM values are set.
Tested Parameters:
model_name: Valid and unknown model names.temperature: Boundary values 0 and 1, invalid values skipped.top_p,presence_penalty,frequency_penalty,max_token: Various boundary tests.Unknown keys in LLM dictionary are skipped.
Usage Example:
llm_config = {"model_name": "glm-4"} TestChatAssistantUpdate().test_llm(client, add_chat_assistants_func, llm_config, "")
Method: test_prompt(self, client, add_chat_assistants_func, prompt, expected_message)
Tests updating the prompt configuration of a chat assistant.
Parameters:
client: API client.add_chat_assistants_func: Fixture providing datasets and assistants.prompt: Dictionary with prompt configuration.expected_message: Expected error message substring if update should fail; empty if update should succeed.
Behavior:
Creates payload with
name,prompt, anddataset_ids.If an error is expected, asserts exception message contains
expected_message.Otherwise, updates and verifies prompt attributes.
Special handling for
keywords_similarity_weight(asserts 1 - value).If
promptis empty, asserts default prompt configuration is set.
Prompt Fields Tested:
similarity_threshold,keywords_similarity_weight,variables,top_n,empty_response,opener,show_quote,prompttext.Invalid types and unknown fields are skipped.
Usage Example:
prompt_config = {"similarity_threshold": 0.5, "empty_response": "No data found."} TestChatAssistantUpdate().test_prompt(client, add_chat_assistants_func, prompt_config, "")
Important Implementation Details
The tests leverage
pytest.mark.parametrizeto systematically test multiple input cases and expected results for each update field.Skipped tests (
pytest.mark.skip) highlight known issues or unimplemented validation paths.Validation errors are expected to raise generic
Exceptioninstances with specific messages.Attribute access in assertions uses
operator.attrgetterto dynamically get nested attributes, supporting flexible verification.The tests depend on a fixture
add_chat_assistants_funcwhich must provide a dataset and multiple chat assistants for meaningful validations.Avatar updates are tested by creating an actual image file in a temporary directory, encoding it, and verifying persistence.
Interaction with Other System Components
ragflow_sdk.Chat: The core SDK class under test;update()method is the focus.configs.CHAT_ASSISTANT_NAME_LIMIT: Provides constraints on allowed name length.utils.encode_avatarandutils.file_utils.create_image_file: Utilities used to prepare avatar images for testing.clientfixture: Acts as the API interface to fetch updated chat assistant data post-update.pytestframework: Orchestrates test execution, parameterization, and assertion handling.
This test file ensures the robustness and correctness of chat assistant updates, which are critical for user customization and AI behavior tuning in the overall InfiniFlow platform.
Visual Diagram
classDiagram
class TestChatAssistantUpdate {
+test_name(client, add_chat_assistants_func, payload, expected_message)
+test_avatar(client, add_chat_assistants_func, tmp_path)
+test_llm(client, add_chat_assistants_func, llm, expected_message)
+test_prompt(client, add_chat_assistants_func, prompt, expected_message)
}
TestChatAssistantUpdate ..> Chat : uses update()
TestChatAssistantUpdate ..> pytest : uses testing framework
TestChatAssistantUpdate ..> encode_avatar : encodes avatar images
TestChatAssistantUpdate ..> create_image_file : creates image files
TestChatAssistantUpdate ..> CHAT_ASSISTANT_NAME_LIMIT : uses name length constraint
Summary
test_update_chat_assistant.py is a comprehensive test module validating the update mechanics of chat assistants in InfiniFlow. It rigorously tests name, avatar, LLM, and prompt updates, asserting proper validation, error handling, and persistence. The file heavily relies on the ragflow_sdk.Chat class and utility functions, and serves as a critical quality gate for chat assistant customization features.