test_update_document.py
Overview
test_update_document.py is a comprehensive test suite designed to validate the behavior and robustness of the document update functionality within the InfiniFlow platform. It uses the pytest framework to systematically verify various scenarios related to updating documents, including authorization checks, field validations, and configuration parsing.
The tests are primarily focused on the update_document API function, ensuring that inputs are validated correctly, error codes and messages are returned as expected, and that updates persist when valid. The file also verifies interactions with the document listing API (list_documents) to confirm the update effects.
This file plays a crucial role in maintaining the integrity and reliability of document update operations in the system.
Classes and Test Suites
1. TestAuthorization
Purpose:
Tests authorization behavior when updating a document, checking how the system handles missing or invalid API authentication tokens.
Key Method:
test_invalid_auth(self, invalid_auth, expected_code, expected_message)Parameters:
invalid_auth: An authorization object orNone, representing invalid or missing credentials.expected_code: Expected error code returned by the API.expected_message: Expected error message returned by the API.
Description:
Callsupdate_documentwith invalid authorization and verifies the error code and message.Example Usage:
auth = None # Missing authorization response = update_document(auth, "dataset_id", "document_id") assert response["code"] == 0 # Should fail with code 0 (empty authorization)
2. TestDocumentsUpdated
Purpose:
Validates updating various fields of a document, including name changes, metadata fields, chunk methods, and invalid or restricted fields. Also tests invalid dataset and document IDs.
Key Methods and Parameters:
test_name(self, HttpApiAuth, add_documents, name, expected_code, expected_message)Tests updating the document's name with various inputs, including valid names, names exceeding limits, empty or missing extensions, and duplicates.
test_invalid_document_id(self, HttpApiAuth, add_documents, document_id, expected_code, expected_message)Tests behavior when updating with invalid or empty document IDs.
test_invalid_dataset_id(self, HttpApiAuth, add_documents, dataset_id, expected_code, expected_message)Tests behavior with invalid or empty dataset IDs.
test_meta_fields(self, HttpApiAuth, add_documents, meta_fields, expected_code, expected_message)Tests updating the
meta_fieldsproperty of a document, including invalid types.
test_chunk_method(self, HttpApiAuth, add_documents, chunk_method, expected_code, expected_message)Tests updating the chunking method for document processing with various supported and unsupported methods.
test_invalid_field(self, HttpApiAuth, add_documents, payload, expected_code, expected_message)Tests attempts to update restricted or invalid fields such as
chunk_count,create_date,progress,token_count, etc. Many test cases are skipped due to known issues.
Usage Example:
res = update_document(HttpApiAuth, dataset_id, document_id, {"name": "new_name.txt"})
assert res["code"] == 0 # Success
3. TestUpdateDocumentParserConfig
Purpose:
Validates the parser_config parameter when updating documents, ensuring that configurations are correctly validated and applied.
Method:
test_parser_config(self, HttpApiAuth, add_documents, chunk_method, parser_config, expected_code, expected_message)Parameters:
chunk_method: The chunking method to use (e.g., "naive").parser_config: Dictionary specifying parser configuration options.expected_code: Expected API response code.expected_message: Expected error message if any.
Description:
Tests valid and invalid parser configurations, including parameters likechunk_token_num,layout_recognize,html4excel,delimiter,task_page_size, and nestedraptorsettings. Some tests are skipped due to open issues.Example:
parser_config = { "chunk_token_num": 512, "layout_recognize": "DeepDOC", "html4excel": False, "delimiter": r"\n", "task_page_size": 12, "raptor": {"use_raptor": False}, } res = update_document(HttpApiAuth, dataset_id, document_id, { "chunk_method": "naive", "parser_config": parser_config }) assert res["code"] == 0
Important Implementation Details
The file uses
pytestfixtures such asHttpApiAuthandadd_documentsfor authentication and document setup.Parameterized tests (
@pytest.mark.parametrize) systematically test multiple input scenarios with expected results.Several test cases are skipped (
@pytest.mark.skip) due to known issues, referenced by issue numbers.The tests validate not only success cases but also confirm that error codes and messages match expected values for invalid input or unauthorized access.
The tests confirm that when an update is successful, the changes are persisted and reflected by querying the document list API.
The authorization tests use
RAGFlowHttpApiAuthfrom thelibs.authmodule for token-based authentication simulation.The file imports constants like
DOCUMENT_NAME_LIMITandINVALID_API_TOKENfrom configuration modules to ensure consistent validation boundaries.
Interactions with Other System Components
API Functions:
update_document(auth, dataset_id, document_id, update_data): Core function under test, responsible for applying updates to a document.list_documents(auth, dataset_id, filters): Used to verify the effect of updates by retrieving document info.
Authentication:
Uses
RAGFlowHttpApiAuthclass to simulate API key authentication.
Configurations:
Uses constants such as
DOCUMENT_NAME_LIMITandINVALID_API_TOKENfromconfigsfor test validity.
Fixtures:
Relies on pytest fixtures
HttpApiAuth(valid authentication token) andadd_documents(prepares dataset and document IDs for testing).
The file verifies the integration between document update logic, authentication modules, and document retrieval APIs.
Visual Diagram
classDiagram
class TestAuthorization {
+test_invalid_auth(invalid_auth, expected_code, expected_message)
}
class TestDocumentsUpdated {
+test_name(HttpApiAuth, add_documents, name, expected_code, expected_message)
+test_invalid_document_id(HttpApiAuth, add_documents, document_id, expected_code, expected_message)
+test_invalid_dataset_id(HttpApiAuth, add_documents, dataset_id, expected_code, expected_message)
+test_meta_fields(HttpApiAuth, add_documents, meta_fields, expected_code, expected_message)
+test_chunk_method(HttpApiAuth, add_documents, chunk_method, expected_code, expected_message)
+test_invalid_field(HttpApiAuth, add_documents, payload, expected_code, expected_message)
}
class TestUpdateDocumentParserConfig {
+test_parser_config(HttpApiAuth, add_documents, chunk_method, parser_config, expected_code, expected_message)
}
TestAuthorization --> update_document
TestDocumentsUpdated --> update_document
TestDocumentsUpdated --> list_documents
TestUpdateDocumentParserConfig --> update_document
TestUpdateDocumentParserConfig --> list_documents
Summary
This test suite ensures that document update operations in InfiniFlow are secure, robust, and behave as expected under a wide range of conditions. It validates authorization mechanisms, field-specific constraints, and configuration parsing, contributing to the platform's overall reliability and correctness. The file tightly integrates with authentication and document retrieval components, reflecting real usage scenarios in the system.