test_rm_documents.py
Overview
This file contains automated test cases focused on verifying the document deletion functionality within the InfiniFlow system. It extensively tests the delete_document API endpoint, covering scenarios around authorization, input validation, concurrency, and system behavior when deleting multiple documents. The tests use the pytest framework for structuring tests and assertions.
The primary goals of this test suite are to ensure that:
Unauthorized or improperly authorized requests are rejected.
Deletion requests handle invalid inputs gracefully.
Document deletions succeed under normal conditions.
The system behaves correctly when deleting documents repeatedly.
The deletion API can handle concurrent requests without failure.
Bulk document deletion (e.g., 100 documents) works reliably.
Classes and Functions
TestAuthorization
Purpose:
This test class verifies that the API properly enforces authorization rules for the document deletion endpoint.
Method: test_invalid_auth
Parameters:
invalid_auth: Authorization object or None representing invalid credentials.expected_code: Expected error code from the API.expected_message: Expected error message text.
Description:
Tests thedelete_documentfunction with invalid or missing authorization tokens. It checks that the system returns a 401 Unauthorized error code and the appropriate message.Usage Example:
test_auth = RAGFlowWebApiAuth("invalid_token")
res = delete_document(test_auth)
assert res["code"] == 401
assert "<Unauthorized" in res["message"]
TestDocumentsDeletion
Purpose:
These tests validate various document deletion scenarios, focusing on input validation, repeated deletion attempts, and expected system state after deletion.
Method: test_basic_scenarios
Parameters:
WebApiAuth: Valid authorization object fixture.add_documents_func: Fixture returning a tuple(kb_id, document_ids)of knowledge base ID and list of document IDs.payload: The payload to send todelete_document. Can beNone, malformed, or a callable generating a valid payload.expected_code: Expected response code from the API.expected_message: Expected response message from the API.remaining: Expected number of documents left after deletion.
Description:
Tests deletion with various invalid payloads (missing doc_id, empty doc_id, invalid doc_id formats, non-JSON payload) and a valid deletion case via a callable payload. It ensures the API returns appropriate error codes and messages, and verifies the document count in the knowledge base after the operation.Usage Example:
payload = {"doc_id": "some_doc_id"}
res = delete_document(WebApiAuth, payload)
assert res["code"] == 0 # success
Method: test_repeated_deletion
Parameters:
WebApiAuth: Valid authorization object fixture.add_documents_func: Fixture returning(kb_id, document_ids).
Description:
First deletes all documents successfully, then attempts to delete the same documents again, expecting failure with a "No authorization." message (code 109). This tests idempotency and error handling for repeated deletion requests.
test_concurrent_deletion
Parameters:
WebApiAuth: Valid authorization object fixture.add_dataset: Fixture that creates and returns a knowledge base ID.tmp_path: Fixture providing a temporary filesystem path for document uploads.
Description:
Tests deletion of 100 documents concurrently using a thread pool with 5 workers, validating that all concurrent delete operations succeed without errors or race conditions.Implementation Detail:
Usesconcurrent.futures.ThreadPoolExecutorto run multipledelete_documentcalls in parallel, waiting for all to complete.
test_delete_100
Parameters:
WebApiAuth: Valid authorization object fixture.add_dataset: Fixture returning a knowledge base ID.tmp_path: Temporary path fixture.
Description:
Uploads 100 documents, verifies the count, deletes each document sequentially, and finally verifies that the knowledge base is empty. This tests system behavior under bulk deletion in a single-threaded manner.
Important Implementation Details
Authorization Handling:
Tests useRAGFlowWebApiAuthobjects instantiated with valid or invalid tokens to simulate authorized and unauthorized requests.Fixtures Usage:
The tests depend on pytest fixtures such asWebApiAuth,add_documents_func,add_dataset, andtmp_pathto provide valid test contexts, including authenticated sessions, documents, and storage locations.Concurrency Testing:
Uses Python’sThreadPoolExecutorto simulate concurrent API calls, which is crucial for detecting race conditions or thread-safety issues in the document deletion logic.Error Codes and Messages:
The system returns custom error codes like 101 (missing arguments) and 109 (authorization failure), which the tests explicitly check to confirm correct error handling.
Interaction with Other Components
delete_documentfunction (fromcommonmodule):
This is the core API call tested for deleting documents. It interacts with the backend service responsible for document management.list_documentsfunction (fromcommonmodule):
Used to verify the current state of documents in a knowledge base after deletions.bulk_upload_documentsfunction (fromcommonmodule):
Used to prepare test data by uploading multiple documents before deletion tests.RAGFlowWebApiAuthclass (fromlibs.auth):
Provides authentication tokens used in API calls.The test suite assumes the existence of a backend knowledge base system that supports document CRUD operations, authorization, and error reporting.
Visual Diagram
classDiagram
class TestAuthorization {
+test_invalid_auth(invalid_auth, expected_code, expected_message)
}
class TestDocumentsDeletion {
+test_basic_scenarios(WebApiAuth, add_documents_func, payload, expected_code, expected_message, remaining)
+test_repeated_deletion(WebApiAuth, add_documents_func)
}
class Functions {
+test_concurrent_deletion(WebApiAuth, add_dataset, tmp_path)
+test_delete_100(WebApiAuth, add_dataset, tmp_path)
}
TestAuthorization --|> pytest
TestDocumentsDeletion --|> pytest
Functions --|> pytest
Summary
test_rm_documents.py is a critical test suite verifying document deletion in the InfiniFlow platform. It ensures robustness against invalid authorizations, input errors, and concurrency issues. By validating both error handling and success paths, it helps maintain the integrity and reliability of document management APIs. The file collaborates closely with common utility functions and authentication mechanisms to set up realistic test scenarios.