test_create_chat_assistant.py
Overview
This file contains automated test cases for validating the creation of chat assistants in the InfiniFlow system using the ragflow_sdk.Chat API. It primarily uses the pytest framework to:
Verify input validation (chat assistant names, dataset ownership, and chat parameters).
Test integration of auxiliary settings such as avatars, LLM (Large Language Model) parameters, and prompt customizations.
Ensure correct error handling when invalid data is provided.
Confirm the correct behavior of the
create_chatmethod under various scenarios.
The tests are organized into two main classes:
TestChatAssistantCreate: Focused on general chat assistant creation tests.TestChatAssistantCreate2: Tests related to document parsing and dataset ownership constraints.
Classes and Methods
1. TestChatAssistantCreate
This class tests various aspects of the chat assistant creation process, including validation for names, dataset IDs, avatars, LLM settings, and prompts.
Decorators:
@pytest.mark.usefixtures("clear_chat_assistants"): Ensures that chat assistants are cleared before each test to avoid interference.Individual tests are marked with priority tags like
@pytest.mark.p1,@pytest.mark.p2, and @pytest.mark.p3 and often use additional fixtures such asadd_chunks.
Methods:
test_name(self, client, name, expected_message)
Purpose:
Validates chat assistant creation with different name inputs, including valid names, empty names, overly long names, non-string types, and duplicate names (case insensitive).
Parameters:
client: Test client instance to interact with the chat API.name: The chat assistant name parameter to test.expected_message: Expected error message if creation should fail, empty string otherwise.
Behavior:
For duplicate names, it first creates a chat with the given or uppercased name.
If an error is expected, asserts that the exception message contains the expected text.
If no error, asserts the created chat's name matches the input.
Example Usage:
client.create_chat(name="valid_name") # Should succeed
client.create_chat(name="") # Should raise exception with "`name` is required."
test_dataset_ids(self, client, add_chunks, dataset_ids, expected_message)
Purpose:
Tests chat creation with various dataset ID inputs, including empty lists, valid dataset IDs, and invalid or unauthorized dataset IDs.
Parameters:
client: Client instance.add_chunks: Fixture providing a valid dataset.dataset_ids: Dataset ID or list of IDs to assign to the chat. Can be a callable returning a list.expected_message: Expected error message if failure is anticipated; empty otherwise.
Behavior:
Converts callable
dataset_idsinto actual dataset IDs.Verifies ownership of dataset IDs during chat creation.
Asserts correct handling of authorized and unauthorized dataset IDs.
test_avatar(self, client, tmp_path)
Purpose:
Tests if an avatar image can be uploaded and assigned to a chat assistant during creation.
Parameters:
client: Client instance.tmp_path: Temporary directory path for creating image files.
Behavior:
Creates a dummy image file.
Encodes the avatar and submits it during chat creation.
Asserts the chat assistant is created with the avatar.
test_llm(self, client, add_chunks, llm, expected_message)
Purpose:
Validates the integration of LLM configurations when creating a chat assistant.
Parameters:
client: Client instance.add_chunks: Fixture providing a valid dataset.llm: Dictionary with LLM parameters such asmodel_name,temperature,top_p, etc.expected_message: Expected error message if creation is invalid.
Behavior:
Wraps the LLM dict into a
Chat.LLMobject.Tests valid and invalid LLM parameter combinations.
If valid, asserts that the chat assistant's LLM properties match the inputs or default values.
test_prompt(self, client, add_chunks, prompt, expected_message)
Purpose:
Tests custom prompt configurations during chat assistant creation.
Parameters:
client: Client instance.add_chunks: Fixture providing a valid dataset.prompt: Dictionary specifying prompt parameters such assimilarity_threshold,empty_response,opener,show_quote, etc.expected_message: Expected error message if creation is invalid.
Behavior:
Wraps the prompt dict into a
Chat.Promptobject.Tests a variety of valid and invalid prompt parameter sets.
Verifies that properties of the created chat assistant's prompt match the input or default values.
2. TestChatAssistantCreate2
This class contains tests related to dataset ownership and document parsing during chat assistant creation.
Methods:
test_unparsed_document(self, client, add_document)
Purpose:
Checks that the system raises an error if a chat assistant is created with datasets containing unparsed documents.
Parameters:
client: Client instance.add_document: Fixture that adds a document to a dataset.
Behavior:
Attempts to create a chat assistant referencing a dataset with unparsed documents.
Asserts that an appropriate exception is thrown indicating dataset ownership or parsing issues.
Important Implementation Details and Algorithms
Parameterized Tests: The use of
pytest.mark.parametrizeallows running the same test logic with multiple different inputs to cover edge cases and expected failures.Fixtures: Fixtures like
clear_chat_assistants,add_chunks, andadd_documentprepare the test environment by ensuring a clean state and providing necessary data.Error Handling: The tests expect exceptions with specific messages when inputs violate constraints (e.g., duplicated names, unauthorized datasets, invalid LLM or prompt parameters).
Use of
attrgetter: Theoperator.attrgetteris used to dynamically fetch nested attributes from chat assistant objects for assertions.Skipping Tests: Some test cases are skipped using
pytest.mark.skipwhen input parameters are known to cause issues or are out of scope.
Interaction with Other Modules
ragflow_sdk.Chat: Core SDK class used to create chat assistants and wrap LLM and prompt configurations.configs: Supplies constants likeCHAT_ASSISTANT_NAME_LIMITfor validation rules.utilsandutils.file_utils: Provide utility functions such asencode_avatarandcreate_image_filefor encoding image data and generating temporary image files.pytest: Testing framework used to define tests, manage fixtures, and specify test parameters.
The file tests the integration of these components to ensure the chat assistant creation API behaves correctly under various scenarios.
Visual Diagram
classDiagram
class TestChatAssistantCreate {
<<pytest test class>>
+test_name(client, name, expected_message)
+test_dataset_ids(client, add_chunks, dataset_ids, expected_message)
+test_avatar(client, tmp_path)
+test_llm(client, add_chunks, llm, expected_message)
+test_prompt(client, add_chunks, prompt, expected_message)
}
class TestChatAssistantCreate2 {
<<pytest test class>>
+test_unparsed_document(client, add_document)
}
TestChatAssistantCreate ..> Chat.LLM : uses
TestChatAssistantCreate ..> Chat.Prompt : uses
TestChatAssistantCreate ..> pytest : uses
TestChatAssistantCreate2 ..> pytest : uses
TestChatAssistantCreate ..> utils.encode_avatar : uses
TestChatAssistantCreate ..> utils.file_utils.create_image_file : uses
Summary
This test file is essential for verifying the robustness and correctness of the chat assistant creation functionality within the InfiniFlow platform. By covering input validation, parameter handling, and error scenarios, it ensures that the chat assistant creation API behaves as expected, improving reliability and user experience. The tests interact heavily with other system components such as dataset management, LLM configuration, and prompt customization, making this a critical integration test suite.