test_delete_datasets.py
Overview
test_delete_datasets.py is a comprehensive test suite designed to validate the functionality, robustness, and authorization mechanisms of the dataset deletion API endpoint in the InfiniFlow system. It leverages the pytest framework to organize tests that cover various scenarios including authorization failures, payload validation, deletion capabilities on large datasets, concurrency, and edge cases related to dataset identifiers.
The file ensures that the dataset deletion API behaves correctly under valid and invalid conditions, verifies that proper error codes and messages are returned, and confirms that datasets are properly removed from the system when requested. It also tests concurrency control by attempting simultaneous deletion operations.
Classes and Their Methods
1. TestAuthorization
Purpose:
Tests related to authorization checks on the dataset deletion API.
Methods:
test_auth_invalid(self, invalid_auth, expected_code, expected_message)Parameters:
invalid_auth: An authorization object orNonerepresenting invalid or missing authorization.expected_code: The expected error code returned by the API.expected_message: The expected error message returned by the API.
Returns: None. Asserts the correctness of the API response.
Description:
Tests the deletion API's response when authorization is missing or invalid. It uses parameterization to test multiple invalid authorization scenarios.Usage Example:
test_instance = TestAuthorization() test_instance.test_auth_invalid(None, 0, "`Authorization` can't be empty")
2. TestRquest
(Note: The class name likely has a typo and should be TestRequest.)
Purpose:
Tests related to request validation, especially content type and payload format.
Methods:
test_content_type_bad(self, HttpApiAuth)Parameters:
HttpApiAuth: Valid authorization fixture.
Returns: None. Asserts the API returns an error for unsupported content types.
Description:
Sends a request with an invalidContent-Typeheader and expects the API to reject it.
test_payload_bad(self, HttpApiAuth, payload, expected_message)Parameters:
HttpApiAuth: Valid authorization fixture.payload: Malformed or invalid JSON payload (string).expected_message: Expected error message describing the issue.
Returns: None. Asserts the API returns appropriate errors for malformed or invalid payloads.
Description:
Parameterized test for invalid JSON payloads: malformed syntax and wrong data types.
test_payload_unset(self, HttpApiAuth)Parameters:
HttpApiAuth: Valid authorization fixture.
Returns: None. Asserts the API rejects requests with unset payload.
Description:
Attempts a delete request with no data and expects a syntax error response.
3. TestCapability
Purpose:
Tests the functional capabilities of the dataset deletion API, including bulk deletion and concurrent deletion.
Methods:
test_delete_dataset_1k(self, HttpApiAuth)Parameters:
HttpApiAuth: Valid authorization fixture.
Returns: None. Asserts successful deletion of 1000 datasets and emptiness afterward.
Description:
Creates 1000 datasets, deletes them all in one request, then verifies no datasets remain.
test_concurrent_deletion(self, HttpApiAuth)Parameters:
HttpApiAuth: Valid authorization fixture.
Returns: None. Asserts that concurrent deletion requests behave correctly.
Description:
Creates 1000 datasets and deletes each dataset using concurrent threads (max 5 workers). Verifies all delete operations return success.
4. TestDatasetsDelete
Purpose:
Tests edge cases and detailed validation scenarios around dataset IDs used in delete requests.
Methods:
test_ids(self, HttpApiAuth, add_datasets_func, func, expected_code, remaining)Parameters:
HttpApiAuth: Valid authorization fixture.add_datasets_func: Fixture that adds datasets and returns their IDs.func: Callable that generates payload dictionary from dataset IDs.expected_code: Expected API response code.remaining: Expected number of datasets left after deletion.
Returns: None. Checks that deleting single or multiple datasets works as expected.
test_ids_empty(self, HttpApiAuth)Tests deletion with empty
idslist; expects success and datasets remain unchanged.
test_ids_none(self, HttpApiAuth)Tests deletion with
idsset toNone; expects success and all datasets deleted.
test_id_not_uuid(self, HttpApiAuth)Tests deletion with an invalid UUID string; expects error about invalid UUID1 format.
test_id_not_uuid1(self, HttpApiAuth)Tests deletion with a UUID that is not UUID1 format; expects error.
test_id_wrong_uuid(self, HttpApiAuth)Tests deletion with a UUID that the user lacks permission for; expects permission error.
test_ids_partial_invalid(self, HttpApiAuth, add_datasets_func, func)Tests deletion payloads with mix of valid and invalid dataset IDs; expects partial failure.
test_ids_duplicate(self, HttpApiAuth, add_datasets_func)Tests deletion with duplicate dataset IDs; expects error about duplicates.
test_repeated_delete(self, HttpApiAuth, add_datasets_func)Tests deleting already deleted datasets; expects permission denied error on second attempt.
test_field_unsupported(self, HttpApiAuth)Tests deletion requests with unexpected fields; expects error about extra inputs.
Important Implementation Details and Algorithms
Authorization Checks:
The tests simulate missing or invalid API keys to verify that the deletion API enforces authentication.Payload Validation:
Checks include malformed JSON, incorrect data types (e.g., string instead of object), and unsupported content types to verify strict input validation.UUID Format Validation:
The system expects UUID1 format for dataset IDs. Tests cover invalid UUIDs and correct error messaging.Concurrency Testing:
The use ofThreadPoolExecutorwith multiple worker threads simulates concurrent deletion requests. This tests the thread-safety and atomicity of delete operations.Duplicate ID Detection:
The API rejects requests containing duplicate dataset IDs, ensuring input integrity.Permission Checks:
Deletion requests including IDs for which the user lacks permission are rejected with appropriate error codes.
Interaction with Other Parts of the System
Imports from
common:batch_create_datasets: Used to create multiple datasets for testing.delete_datasets: The core API call under test. Sends delete requests.list_datasets: Used to verify the current state of datasets after deletions.
configs.INVALID_API_TOKEN:
Used to simulate invalid authorization scenarios.libs.auth.RAGFlowHttpApiAuth:
Represents the HTTP API authentication mechanism.Fixtures (pytest):
The tests use fixtures likeHttpApiAuth,add_datasets_func, andadd_dataset_func(assumed to be defined elsewhere in the test environment) to provide prepared datasets and authorized API sessions.
Usage Example of a Test Case
def test_delete_single_dataset(HttpApiAuth, add_datasets_func):
dataset_ids = add_datasets_func
payload = {"ids": dataset_ids[:1]}
response = delete_datasets(HttpApiAuth, payload)
assert response["code"] == 0
remaining = list_datasets(HttpApiAuth)
assert len(remaining["data"]) == len(dataset_ids) - 1
Mermaid Diagram: Class and Method Structure
classDiagram
class TestAuthorization {
+test_auth_invalid(invalid_auth, expected_code, expected_message)
}
class TestRquest {
+test_content_type_bad(HttpApiAuth)
+test_payload_bad(HttpApiAuth, payload, expected_message)
+test_payload_unset(HttpApiAuth)
}
class TestCapability {
+test_delete_dataset_1k(HttpApiAuth)
+test_concurrent_deletion(HttpApiAuth)
}
class TestDatasetsDelete {
+test_ids(HttpApiAuth, add_datasets_func, func, expected_code, remaining)
+test_ids_empty(HttpApiAuth)
+test_ids_none(HttpApiAuth)
+test_id_not_uuid(HttpApiAuth)
+test_id_not_uuid1(HttpApiAuth)
+test_id_wrong_uuid(HttpApiAuth)
+test_ids_partial_invalid(HttpApiAuth, add_datasets_func, func)
+test_ids_duplicate(HttpApiAuth, add_datasets_func)
+test_repeated_delete(HttpApiAuth, add_datasets_func)
+test_field_unsupported(HttpApiAuth)
}
Summary
test_delete_datasets.py is a critical test module that validates the dataset deletion API's correctness, security, and stability. By covering authorization failures, input validation, concurrency, and permission scenarios, it ensures that the dataset deletion feature maintains data integrity and adheres to security policies. The test suite integrates tightly with dataset creation and listing utilities to verify end-to-end behavior.
This file is essential for maintaining the quality of the InfiniFlow dataset management subsystem and preventing regressions in dataset deletion functionality.