conftest.py
Overview
The conftest.py file is a configuration module for pytest, providing reusable test fixtures that set up and tear down test data related to document management within the InfiniFlow testing environment. Specifically, it facilitates the creation and cleanup of datasets and documents through API calls, ensuring isolated and consistent test states across function- and class-scoped tests.
The fixtures in this file automate the lifecycle of knowledge base documents—uploading a specified number of documents before tests run and deleting them afterward to maintain a clean environment. This helps in testing workflows that involve document ingestion, retrieval, and deletion in a controlled manner.
Fixtures
1. add_document_func
Scope:
functionPurpose: Uploads a single document to a newly created dataset before a test function runs and cleans up by deleting all documents in that dataset after the test completes.
Parameters:
request(pytest fixture): Provides access to the requesting test context for adding finalizers (cleanup).WebApiAuth: Authentication object/token used for API requests.add_dataset: Fixture that creates a dataset and returns its ID.ragflow_tmp_dir: Temporary directory path used as source for document uploads.
Returns: Tuple (dataset_id, document)
dataset_id(str): The ID of the created dataset.document(dict): The first document uploaded (single document).
Usage example:
def test_single_document_upload(add_document_func): dataset_id, document = add_document_func # proceed with assertions or API calls using dataset_id and documentImplementation details:
Calls
bulk_upload_documentswith count=1 to upload one document.Registers a cleanup function via
request.addfinalizerthat lists all documents in the dataset and deletes them one by one after the test.
2. add_documents
Scope:
classPurpose: Uploads five documents to a newly created dataset before any tests in a test class run, and cleans up all uploaded documents after all tests in the class complete.
Parameters:
request(pytest fixture): For registering cleanup actions.WebApiAuth: Authentication for API requests.add_dataset: Fixture creating a dataset, returning its ID.ragflow_tmp_dir: Temporary directory path for document sources.
Returns: Tuple
(dataset_id, documents)dataset_id(str): The ID of the created dataset.documents(list): List of 5 uploaded document dictionaries.
Usage example:
@pytest.mark.usefixtures("add_documents") class TestDocumentBatch: def test_documents_exist(self, add_documents): dataset_id, docs = add_documents assert len(docs) == 5Implementation details:
Uses
bulk_upload_documentswith count=5.Cleanup deletes all documents in the dataset at the end of the class test session.
3. add_documents_func
Scope:
functionPurpose: Similar to
add_documents, but uploads 3 documents for each test function, using a dataset created by theadd_dataset_funcfixture.Parameters:
request: Pytest request object.WebApiAuth: Authentication for API.add_dataset_func: Fixture that creates a dataset per test function.ragflow_tmp_dir: Temporary directory path.
Returns: Tuple
(dataset_id, documents)dataset_id(str): Dataset identifier.documents(list): List of 3 uploaded documents.
Usage example:
def test_three_docs_uploaded(add_documents_func): dataset_id, docs = add_documents_func assert len(docs) == 3Implementation details:
Uploads 3 documents using
bulk_upload_documents.Registers cleanup to delete all documents after each test function completes.
Important Implementation Details
Cleanup Mechanism:
Each fixture usesrequest.addfinalizer()to register a cleanup function that runs after the test or test class finishes. This function lists documents by the dataset ID and deletes them one by one using API calls. This ensures no residual data persists between tests, maintaining test isolation.Document Upload:
Thebulk_upload_documentsutility function is invoked with parameters:WebApiAuthfor authentication.dataset_idto associate documents with the correct knowledge base.A count specifying how many documents to upload (1, 3, or 5 depending on fixture).
ragflow_tmp_diras the source directory for the document files.
Dataset Creation:
The fixtures depend onadd_datasetoradd_dataset_func(not defined in this file) which are expected to return a dataset ID. These are prerequisite fixtures that handle dataset lifecycle.API Interaction:
The fixtures rely on the imported functionslist_documentsanddelete_documentto communicate with the backend API for document management.
Interaction with Other Parts of the System
Dependent Fixtures:
add_datasetandadd_dataset_funcprovide dataset IDs. These are likely defined elsewhere and handle dataset creation.WebApiAuthprovides the authentication context for API requests.ragflow_tmp_dirprovides a temporary directory with documents for upload.
Utility Functions:
bulk_upload_documentshandles the actual upload process of documents to a dataset.list_documentsretrieves documents for cleanup.delete_documentremoves documents post-test.
Testing Workflow:
Tests that require document data will use these fixtures to ensure datasets and documents are prepared and cleaned automatically, avoiding manual setup and teardown.
Visual Diagram
flowchart TD
A[add_document_func (function scope)] -->|uses| B[add_dataset]
A -->|calls| C[bulk_upload_documents (count=1)]
A -->|calls| D[list_documents]
A -->|calls| E[delete_document]
F[add_documents (class scope)] -->|uses| B
F -->|calls| C[bulk_upload_documents (count=5)]
F -->|calls| D
F -->|calls| E
G[add_documents_func (function scope)] -->|uses| H[add_dataset_func]
G -->|calls| C[bulk_upload_documents (count=3)]
G -->|calls| D
G -->|calls| E
B -->|creates| I[Dataset]
H -->|creates| I
C -->|uploads docs to| I
D -->|lists docs from| I
E -->|deletes docs from| I
Summary
conftest.py defines pytest fixtures that automate the setup and teardown of document datasets used in testing. It ensures that documents are uploaded before tests and removed afterward, providing a clean slate for each test run. The fixtures vary by scope and number of documents uploaded, relying on external dataset creation fixtures and utility functions for API interactions. This modular setup streamlines testing workflows involving document ingestion and management within the InfiniFlow testing framework.