conftest.py
Overview
The conftest.py file defines pytest fixtures used to manage dataset creation and cleanup during test execution for the InfiniFlow project. It provides reusable setup and teardown logic to create multiple datasets before tests run and ensure their deletion afterwards, maintaining test isolation and preventing side effects from persisting datasets.
Fixtures in this file leverage helper functions batch_create_datasets and delete_datasets from a common module to interact with the HTTP API authenticated via a get_http_api_auth fixture (assumed to be defined elsewhere in the test suite). Two fixtures with different scopes are provided:
add_datasets: creates 5 datasets once per test class.add_datasets_func: creates 3 datasets once per individual test function.
Both fixtures ensure cleanup by deleting all datasets after the tests complete.
Detailed Explanation
Imports
pytest: Provides the testing framework and fixture support.batch_create_datasets: Utility function to create multiple datasets via API.delete_datasets: Utility function to delete datasets via API.
Fixtures
add_datasets
@pytest.fixture(scope="class")
def add_datasets(get_http_api_auth, request):
...
Scope:
"class"— runs once per test class that uses this fixture.Parameters:
get_http_api_auth: Fixture that provides authentication credentials for HTTP API calls.request: Pytest built-in fixture used to add finalizers (cleanup functions).
Functionality:
Defines a
cleanupfunction that callsdelete_datasetswith no specific IDs ({"ids": None}), which is assumed to delete all datasets.Registers
cleanupas a finalizer to run after all tests in the class finish.Calls
batch_create_datasetswith5to create five datasets before tests run.Returns the created datasets to the test functions.
Return Value: A collection or list of the 5 newly created datasets.
Usage Example:
@pytest.mark.usefixtures("add_datasets")
class TestDatasetOperations:
def test_dataset_count(self, add_datasets):
assert len(add_datasets) == 5
add_datasets_func
@pytest.fixture(scope="function")
def add_datasets_func(get_http_api_auth, request):
...
Scope:
"function"— runs once per test function that uses this fixture.Parameters:
get_http_api_auth: Fixture for API authentication.request: Used to add finalizers.
Functionality:
Defines a cleanup function identical to the one in
add_datasetsfor deleting all datasets.Registers this cleanup function to run after each test function completes.
Creates 3 datasets via
batch_create_datasets.Returns the created datasets.
Return Value: Collection or list of the 3 newly created datasets.
Usage Example:
def test_modify_datasets(add_datasets_func):
assert len(add_datasets_func) == 3
# perform operations on datasets
Implementation Details
Both fixtures rely on the
request.addfinalizermethod to register a cleanup function that ensures datasets are deleted after the tests run, preventing leftover test artifacts.The cleanup function calls
delete_datasetswith{"ids": None}, implying a bulk delete of all datasets owned or accessible under the current API authentication context.The fixtures differ only in scope and number of datasets created to accommodate different test isolation requirements.
The file imports
batch_create_datasetsanddelete_datasetsfrom acommonmodule, indicating that dataset creation and deletion logic is abstracted and reusable across tests.
Interaction With Other Parts of the System
commonModule: This file depends on thecommonmodule for dataset management functions, meaning changes tobatch_create_datasetsordelete_datasetswill directly affect fixture behavior.get_http_api_authFixture: This authentication fixture is crucial for API calls but is defined elsewhere; it provides the necessary credentials or session objects.Test Suites: Tests that require datasets use these fixtures to automatically handle dataset lifecycle, simplifying test code and improving reliability.
API Backend: The fixtures communicate with the InfiniFlow backend API to create and delete datasets, enabling integration testing of dataset-related features.
Mermaid Diagram: Flowchart of Fixture Workflow
flowchart TD
subgraph Fixtures
A[add_datasets (class scope)]
B[add_datasets_func (function scope)]
end
subgraph Common Module
C[batch_create_datasets]
D[delete_datasets]
end
subgraph Test Execution
E[Test functions/classes]
end
A --> C
B --> C
A -->|finalizer| D
B -->|finalizer| D
E --> A
E --> B
Diagram Explanation:
The two fixtures
add_datasetsandadd_datasets_funccallbatch_create_datasetsto create datasets.Both register a finalizer to call
delete_datasetsfor cleanup.Test functions or classes consume these fixtures to gain access to prepared datasets.
The
commonmodule encapsulates dataset API interaction logic.
Summary
The conftest.py file provides two pytest fixtures that manage dataset creation and deletion for testing the InfiniFlow API. It abstracts setup and teardown logic, promoting test isolation and reliability by ensuring datasets are consistently created and cleaned up. The fixtures differ in scope and dataset count to fit different test requirements. This modular approach simplifies test suite maintenance and reduces boilerplate code in individual tests.