conftest.py


Overview

The conftest.py file serves as a centralized configuration and fixture provider for pytest-based automated testing within the InfiniFlow project. It defines a collection of pytest fixtures and utility functions that facilitate setting up, managing, and tearing down test resources related to datasets, documents, chat assistants, and file artifacts. These fixtures help standardize test preparation steps such as:

The file relies heavily on the pytest framework's fixture mechanism and integrates with the RAGFlow SDK, utility modules, and common batch operations to streamline test setup and cleanup.


Detailed Descriptions

Functions and Fixtures


condition(_dataset: DataSet) -> bool

A helper function decorated with the @wait_for retry wrapper that checks if all documents within a given dataset have finished processing.


generate_test_files(request: FixtureRequest, tmp_path: Path) -> dict[str, Path]

Pytest fixture that generates multiple types of test files in a temporary directory. The fixture selectively creates files based on the parameter passed to it.


ragflow_tmp_dir(request: FixtureRequest, tmp_path_factory: Path) -> Path

Pytest fixture creating a uniquely named temporary directory scoped to the test class.


client(token: str) -> RAGFlow

Session-scoped pytest fixture that instantiates an authenticated RAGFlow client API object.


clear_datasets(request: FixtureRequest, client: RAGFlow)

Function-scoped fixture that ensures deletion of all datasets after a test completes.


clear_chat_assistants(request: FixtureRequest, client: RAGFlow)

Function-scoped fixture that cleans up all chat assistants after a test.


clear_session_with_chat_assistants(request, add_chat_assistants)

Function-scoped fixture that deletes all sessions associated with chat assistants after a test.


add_dataset(request: FixtureRequest, client: RAGFlow) -> DataSet

Class-scoped fixture that creates a new dataset for tests and ensures its deletion afterward.


add_dataset_func(request: FixtureRequest, client: RAGFlow) -> DataSet

Function-scoped version of add_dataset fixture, providing dataset setup and teardown per test function.


add_document(add_dataset: DataSet, ragflow_tmp_dir: Path) -> tuple[DataSet, Document]

Class-scoped fixture that uploads a document to a dataset and returns both.


add_chunks(request: FixtureRequest, add_document: tuple[DataSet, Document]) -> tuple[DataSet, Document, list[Chunk]]

Class-scoped fixture that adds chunks to an uploaded document after ensuring document parsing is complete.


add_chat_assistants(request, client, add_document) -> tuple[DataSet, Document, list[Chat]]

Class-scoped fixture to create multiple chat assistants associated with a document.


Important Implementation Details


Interaction with Other System Components


Usage Examples

Example of using the add_chunks fixture in a test (pytest style):

def test_chunk_processing(add_chunks):
    dataset, document, chunks = add_chunks
    assert len(chunks) == 4
    for chunk in chunks:
        assert chunk.text is not None

Example of parameterizing generate_test_files fixture:

@pytest.mark.parametrize("generate_test_files", ["pdf"], indirect=True)
def test_pdf_upload(generate_test_files):
    pdf_path = generate_test_files["pdf"]
    # Use pdf_path to upload and test

Mermaid Flowchart Diagram

The following flowchart illustrates the key fixtures and their dependencies showing how test resources are composed from base elements and cleaned up after tests.

flowchart TD
    A[RAGFlow Client (client)] --> B[Datasets (add_dataset)]
    B --> C[Documents (add_document)]
    C --> D[Chunks (add_chunks)]
    C --> E[Chat Assistants (add_chat_assistants)]

    F[generate_test_files] --> C
    G[Temporary Directory (ragflow_tmp_dir)] --> C

    subgraph Cleanup
        B -.->|delete_datasets| H[Dataset Cleanup]
        E -.->|delete_chats| I[Chat Assistants Cleanup]
        D -.->|delete_chunks| J[Chunk Cleanup]
    end

    C -.->|async_parse_documents + wait_for condition| D
    C -.->|async_parse_documents + wait_for condition| E

Summary

conftest.py is a critical infrastructure file for setting up the testing environment in the InfiniFlow project. It abstracts the complexity of preparing datasets, documents, chunks, and chat assistants while ensuring proper cleanup and synchronization. By leveraging pytest fixtures, batch operations, and utility functions, it enables robust, repeatable, and isolated tests that interact with the RAGFlow backend and file system in a controlled manner.