test_update_chat_assistant.py
Overview
This file contains automated tests for the update_chat_assistant functionality within the InfiniFlow project. The primary purpose is to verify that the chat assistant update API behaves correctly under various conditions, including authorization validation, payload correctness, and parameter boundary cases.
The tests use pytest for parametrization and test categorization (e.g., priority marks like p1, p3) and cover scenarios such as invalid authentication, validation of chat assistant properties (name, dataset associations, avatar, LLM configuration, and prompt customization).
Test Classes and Methods
1. TestAuthorization
Tests that the API correctly handles invalid or missing authorization tokens.
test_invalid_auth(self, invalid_auth, expected_code, expected_message)
Parameters:
invalid_auth: An authorization object orNonerepresenting missing or invalid credentials.expected_code: Expected response code from the API.expected_message: Expected error message describing the failure reason.
Functionality:
Calls
update_chat_assistantwith the providedinvalid_auth.Asserts that the response code and message match expected values.
Usage Example:
auth = None # Missing auth header
res = update_chat_assistant(auth, "chat_assistant_id")
assert res["code"] == 0
assert res["message"] == "`Authorization` can't be empty"
Test cases include:
Missing
Authorizationheader.Invalid API token.
2. TestChatAssistantUpdate
Contains multiple tests to validate the update functionality with various payloads.
a. test_name(self, HttpApiAuth, add_chat_assistants_func, payload, expected_code, expected_message)
Parameters:
HttpApiAuth: Valid authentication object.add_chat_assistants_func: Fixture that provides pre-created assistants and dataset IDs.payload: Dictionary with thenamefield to test.expected_code: Expected response code.expected_message: Expected error message.
Functionality:
Attempts to update a chat assistant's name.
Verifies API response code and message.
If successful, verifies the updated name via
list_chat_assistants.
Test cases include:
Valid names.
Too long names exceeding
CHAT_ASSISTANT_NAME_LIMIT.Non-string or empty names.
Duplicate names (case-insensitive).
b. test_dataset_ids(self, HttpApiAuth, add_chat_assistants_func, dataset_ids, expected_code, expected_message)
Parameters:
dataset_ids: List of valid or invalid dataset IDs, or a callable returning a list.Other parameters as above.
Functionality:
Attempts to update chat assistant with given dataset IDs.
Checks ownership validation and response correctness.
Test cases include:
Empty list.
Valid dataset IDs.
Invalid dataset ID (not owned by user).
c. test_avatar(self, HttpApiAuth, add_chat_assistants_func, tmp_path)
Parameters:
Uses
tmp_pathfixture to create a temporary image file.
Functionality:
Creates an image file.
Encodes it as avatar data.
Updates the chat assistant's avatar.
Asserts success code.
d. test_llm(self, HttpApiAuth, add_chat_assistants_func, llm, expected_code, expected_message)
Parameters:
llm: Dictionary specifying LLM parameters such as model name, temperature, penalties, etc.
Functionality:
Tests various LLM configuration parameters for validity.
Checks default values when no LLM config is provided.
Verifies error handling for unknown model names and invalid parameter types/ranges.
Test cases cover:
Valid models (e.g.,
"glm-4").Unknown models.
Various numeric parameters within acceptable ranges.
Skipped tests for out-of-range or type-invalid parameters.
e. test_prompt(self, HttpApiAuth, add_chat_assistants_func, prompt, expected_code, expected_message)
Parameters:
prompt: Dictionary specifying prompt configuration such as similarity thresholds, weights, variables, custom strings, and flags.
Functionality:
Validates prompt parameter correctness.
Ensures required placeholders (e.g.,
{knowledge}) are present when necessary.Default prompt values are checked if none provided.
Verifies error handling for unsupported types or missing placeholders.
Test cases include:
Different numeric thresholds.
Custom empty responses and openers.
Flags like
show_quote.Prompts with and without required placeholders.
Skipped tests for invalid types.
Important Implementation Details
Test Categorization:
Tests are marked withpytest.mark.p1(high priority),p3(lower priority), or skipped due to known issues (marked with reason "issues/").Parametrization:
Many tests usepytest.mark.parametrizeto test multiple input cases efficiently.Fixtures:
The tests rely on fixtures such asHttpApiAuthfor authentication andadd_chat_assistants_functo create test data setup.Payload Validation:
The update API is tested for schema validation, including string length limits, type checks, and business logic validations like ownership and duplication.Encoding Avatars:
Theencode_avatarutility encodes image files for avatar updates, tested via a temporary generated image.LLM and Prompt Settings:
These are complex nested configurations tested for fine-grained correctness.
Interaction with Other System Components
Functions Under Test:
update_chat_assistant: Main function to update chat assistant configurations.list_chat_assistants: Retrieves chat assistant details to verify updates.
Utilities and Configurations Imported:
CHAT_ASSISTANT_NAME_LIMITandINVALID_API_TOKENfromconfigsfor validation and testing invalid auth.RAGFlowHttpApiAuthfromlibs.authfor API authentication handling.encode_avatarandcreate_image_filefor avatar-related testing.
Test Setup:
The test suite assumes the presence of fixtures that create initial chat assistants and datasets for meaningful update tests.
Visual Diagram
The following class diagram summarizes the structure of the test file, focusing on classes and their test methods.
classDiagram
class TestAuthorization {
+test_invalid_auth(invalid_auth, expected_code, expected_message)
}
class TestChatAssistantUpdate {
+test_name(HttpApiAuth, add_chat_assistants_func, payload, expected_code, expected_message)
+test_dataset_ids(HttpApiAuth, add_chat_assistants_func, dataset_ids, expected_code, expected_message)
+test_avatar(HttpApiAuth, add_chat_assistants_func, tmp_path)
+test_llm(HttpApiAuth, add_chat_assistants_func, llm, expected_code, expected_message)
+test_prompt(HttpApiAuth, add_chat_assistants_func, prompt, expected_code, expected_message)
}
Summary
test_update_chat_assistant.py is a comprehensive pytest-based suite targeting the update operations of chat assistants in the InfiniFlow platform. It rigorously validates authorization, input parameters, and complex nested configurations such as LLM and prompts, ensuring the backend enforces constraints and behaves predictably under diverse scenarios. This file is essential for maintaining API stability and correctness when evolving chat assistant features.