test_update_dataset.py
Overview
test_update_dataset.py is a comprehensive test suite designed to validate the functionality, robustness, and correctness of the dataset update mechanism within the InfiniFlow platform. It primarily focuses on testing the update method of the DataSet class from the ragflow_sdk, ensuring that dataset properties such as name, avatar, description, embedding model, permissions, chunk method, pagerank, and parser configurations are correctly validated, applied, and persisted.
The file uses the pytest framework for structuring tests, with additional support from hypothesis for property-based testing and concurrency testing via concurrent.futures.ThreadPoolExecutor. This test suite covers both positive scenarios (valid updates) and negative scenarios (invalid input handling, error cases), ensuring that the dataset update functionality adheres strictly to expected business rules and input constraints.
Classes and Their Responsibilities
1. TestRquest
Purpose:
Tests basic request validation related to empty payload updates on a dataset.Methods:
test_payload_empty(self, add_dataset_func)
Verifies that updating a dataset with an empty payload raises an exception indicating no properties were modified.
2. TestCapability
Purpose:
Tests the capability of the dataset update method to handle concurrent update requests safely.Methods:
test_update_dateset_concurrent(self, add_dataset_func)
Launches 100 concurrent update requests on the same dataset with different names and asserts all complete successfully, ensuring thread safety.
3. TestDatasetUpdate
Purpose:
Contains extensive tests validating each updatable field of a dataset, including name, avatar, description, embedding model, permissions, chunk method, pagerank, and parser configuration.Key Methods and Their Testing Focus:
Name Update and Validation
test_name(self, client, add_dataset_func, name)
Tests valid dataset names using property-based testing with hypothesis.test_name_invalid(self, add_dataset_func, name, expected_message)
Tests invalid dataset names, including empty, too long, non-string, and None values.test_name_duplicated(self, add_datasets_func) & test_name_case_insensitive(self, add_datasets_func)
Tests enforcement of unique dataset names, case-insensitive.
Avatar Update and Validation
test_avatar(self, client, add_dataset_func, tmp_path)
Tests updating avatar with valid base64-encoded image data with MIME prefix.test_avatar_exceeds_limit_length(self, add_dataset_func)
Tests avatar data exceeding length limits.test_avatar_invalid_prefix(self, add_dataset_func, tmp_path, avatar_prefix, expected_message)
Tests various invalid or missing MIME prefixes in avatar data.test_avatar_none(self, client, add_dataset_func)
Tests setting avatar to None.
Description Update and Validation
test_description(self, client, add_dataset_func)
Tests updating description with valid string.test_description_exceeds_limit_length(self, add_dataset_func)
Tests description exceeding length limits.test_description_none(self, client, add_dataset_func)
Tests setting description to None.
Embedding Model Update and Validation
test_embedding_model(self, client, add_dataset_func, embedding_model)
Tests updating embedding model with several valid identifiers.test_embedding_model_invalid(self, add_dataset_func, name, embedding_model)
Tests invalid or unauthorized embedding models.test_embedding_model_format(self, add_dataset_func, name, embedding_model)
Tests format validation of embedding model identifier (must be<model_name>@<provider>).test_embedding_model_none(self, client, add_dataset_func)
Tests setting embedding model to None resets to default.
Permission Update and Validation
test_permission(self, client, add_dataset_func, permission)
Tests valid permission values"me"and"team".test_permission_invalid(self, add_dataset_func, permission)
Tests invalid permission inputs, including empty strings, unknown values, type errors, and case/whitespace variations.test_permission_none(self, add_dataset_func)
Tests that setting permission to None raises an exception.
Chunk Method Update and Validation
test_chunk_method(self, client, add_dataset_func, chunk_method)
Tests updating chunk method with allowed values like"naive","book","email", etc.test_chunk_method_invalid(self, add_dataset_func, chunk_method)
Tests invalid chunk methods.test_chunk_method_none(self, add_dataset_func)
Tests that setting chunk method to None raises an exception.
Pagerank Update and Validation
test_pagerank(self, client, add_dataset_func, pagerank)
Tests valid pagerank values (0, 50, 100) when the doc engine supports it.test_pagerank_set_to_0(self, client, add_dataset_func)
Tests updating pagerank from non-zero to zero, ensuring persistence.test_pagerank_infinity(self, client, add_dataset_func)
Tests that pagerank cannot be set when the doc engine is not elasticsearch.test_pagerank_invalid(self, add_dataset_func, pagerank, expected_message)
Tests invalid pagerank values (negative, above 100).test_pagerank_none(self, add_dataset_func)
Tests that setting pagerank to None raises an exception.
Parser Configuration Update and Validation
test_parser_config(self, client, add_dataset_func, parser_config)
Tests updating parser config with various valid settings, including nested dicts under keys likegraphragand raptor.test_parser_config_invalid(self, add_dataset_func, parser_config, expected_message)
Tests various invalid parser config values, including type errors, out-of-range values, and missing required fields.test_parser_config_empty(self, client, add_dataset_func)
Tests updating with an empty parser config resets to default config.test_parser_config_none(self, client, add_dataset_func)
Tests updating with None parser config resets to default config.test_parser_config_empty_with_chunk_method_change(self, client, add_dataset_func)
Tests that updating chunk method to "qa" with empty parser config results in parser config with only raptor andgraphragdisabled.test_parser_config_unset_with_chunk_method_change(self, client, add_dataset_func)
Tests chunk method update without explicit parser config behaves similarly.test_parser_config_none_with_chunk_method_change(self, client, add_dataset_func)
Tests chunk method update with parser config set to None.
Unsupported and Unset Fields Validation
test_field_unsupported(self, add_dataset_func, payload)
Tests that updates with unsupported fields raise validation errors.test_field_unset(self, client, add_dataset_func)
Tests that fields not included in the update payload remain unchanged.
Implementation Details and Algorithms
Validation and Exception Handling:
The tests verify that theupdatemethod correctly validates input fields based on type, length, format, and business logic constraints. Invalid inputs raise exceptions with clear error messages.Concurrency Test:
The concurrent update test uses a thread pool to simulate multiple simultaneous updates to the same dataset, checking for race conditions or data corruption.Property-Based Testing:
The hypothesis library is used to generate diverse valid dataset names, ensuring that the name update logic handles a broad range of inputs robustly.Parameterized Tests:
Many test methods use pytest.mark.parametrize to systematically test multiple input variants and expected outcomes in a concise manner.Parser Configuration Handling:
The parser config is a complex nested dictionary with various optional settings affecting how documents are parsed. Tests ensure that individual fields and nested subfields are validated and correctly updated.Environment Conditional Testing:
Some tests are conditionally skipped based on environment variables (e.g.,DOC_ENGINE) to reflect feature availability dependent on system configuration.
Interaction with Other Components
ragflow_sdk.DataSet:
The core class under test, representing datasets managed by the InfiniFlow system. Theupdatemethod of this class is the primary focus.client:
Represents an API client used to retrieve datasets after updates to verify persistence and correctness.add_dataset_funcandadd_datasets_func:
Fixtures that provide pre-created dataset instances to test update operations.utils.encode_avatar&utils.file_utils.create_image_file:
Utility functions used to generate and encode image files for avatar update tests.configs.DATASET_NAME_LIMIT:
Configuration constant used to validate maximum allowed dataset name length.hypothesis &
pytest:
Testing frameworks used for property-based and parameterized testing.
The tests ensure that dataset updates are consistent and persistent across the system, correctly reflecting changes on retrieval via the client interface.
Usage Examples (from Tests)
Updating dataset name:
dataset.update({"name": "new_dataset_name"}) assert dataset.name == "new_dataset_name"Updating avatar with base64-encoded PNG:
avatar_data = "data:image/png;base64," + encode_avatar(image_file) dataset.update({"avatar": avatar_data}) assert dataset.avatar == avatar_dataSetting embedding model:
dataset.update({"embedding_model": "BAAI/bge-large-zh-v1.5@BAAI"}) assert dataset.embedding_model == "BAAI/bge-large-zh-v1.5@BAAI"Updating parser config with nested settings:
parser_cfg = {"auto_keywords": 16, "graphrag": {"use_graphrag": True}} dataset.update({"parser_config": parser_cfg}) assert dataset.parser_config.auto_keywords == 16 assert dataset.parser_config.graphrag.use_graphrag is TrueHandling invalid update payload:
with pytest.raises(Exception): dataset.update({"unknown_field": "value"})
Mermaid Diagram: Class Structure
classDiagram
class TestRquest {
+test_payload_empty(add_dataset_func)
}
class TestCapability {
+test_update_dateset_concurrent(add_dataset_func)
}
class TestDatasetUpdate {
+test_name(client, add_dataset_func, name)
+test_name_invalid(add_dataset_func, name, expected_message)
+test_name_duplicated(add_datasets_func)
+test_name_case_insensitive(add_datasets_func)
+test_avatar(client, add_dataset_func, tmp_path)
+test_avatar_exceeds_limit_length(add_dataset_func)
+test_avatar_invalid_prefix(add_dataset_func, tmp_path, avatar_prefix, expected_message)
+test_avatar_none(client, add_dataset_func)
+test_description(client, add_dataset_func)
+test_description_exceeds_limit_length(add_dataset_func)
+test_description_none(client, add_dataset_func)
+test_embedding_model(client, add_dataset_func, embedding_model)
+test_embedding_model_invalid(add_dataset_func, name, embedding_model)
+test_embedding_model_format(add_dataset_func, name, embedding_model)
+test_embedding_model_none(client, add_dataset_func)
+test_permission(client, add_dataset_func, permission)
+test_permission_invalid(add_dataset_func, permission)
+test_permission_none(add_dataset_func)
+test_chunk_method(client, add_dataset_func, chunk_method)
+test_chunk_method_invalid(add_dataset_func, chunk_method)
+test_chunk_method_none(add_dataset_func)
+test_pagerank(client, add_dataset_func, pagerank)
+test_pagerank_set_to_0(client, add_dataset_func)
+test_pagerank_infinity(client, add_dataset_func)
+test_pagerank_invalid(add_dataset_func, pagerank, expected_message)
+test_pagerank_none(add_dataset_func)
+test_parser_config(client, add_dataset_func, parser_config)
+test_parser_config_invalid(add_dataset_func, parser_config, expected_message)
+test_parser_config_empty(client, add_dataset_func)
+test_parser_config_none(client, add_dataset_func)
+test_parser_config_empty_with_chunk_method_change(client, add_dataset_func)
+test_parser_config_unset_with_chunk_method_change(client, add_dataset_func)
+test_parser_config_none_with_chunk_method_change(client, add_dataset_func)
+test_field_unsupported(add_dataset_func, payload)
+test_field_unset(client, add_dataset_func)
}
TestCapability ..> TestRquest : uses
TestDatasetUpdate ..> TestCapability : extends testing scope
Summary
test_update_dataset.py provides a rigorous and exhaustive set of unit and integration tests targeting the dataset update functionality within InfiniFlow's SDK. It validates input correctness, error handling, concurrency, and data persistence for a variety of dataset attributes. This file is essential to maintaining the quality and reliability of dataset modifications across the system and serves as a reference for expected input formats and constraints.
End of documentation.