conftest.py
Overview
conftest.py is a Pytest configuration and fixtures file designed for integration testing within the InfiniFlow project. It provides reusable test fixtures and utility functions that simplify setting up the test environment, particularly focusing on preparing datasets, documents, and chat assistant instances needed to test the RAGFlow system's chat and document parsing functionality.
Key functionalities include:
A polling condition function to wait until all documents in a dataset are fully parsed.
A fixture that sets up a dataset with a parsed document and creates multiple chat assistants associated with that dataset.
Automatic cleanup of created chat sessions after tests run to ensure state isolation between tests.
This file primarily enables reliable, repeatable integration tests by managing asynchronous operations and resource lifecycle within the RAGFlow environment.
Detailed Components
Imports
pytest: Pytest testing framework core.FixtureRequest: To register finalizer cleanup functions scoped to test requests.batch_create_chat_assistants(fromcommon): Utility to create multiple chat assistants.Chat, DataSet, Document, RAGFlow (from
ragflow_sdk): Core SDK entities for chat and document handling.wait_for(fromutils): Decorator utility for polling/wait-until patterns.
Function: condition
@wait_for(30, 1, "Document parsing timeout")
def condition(_dataset: DataSet) -> bool:
Purpose
Checks whether all documents within a given dataset have finished parsing by verifying that each document's run status is "DONE". This function is decorated with wait_for to retry this condition every 1 second for up to 30 seconds, raising a timeout error if the condition is not met.
Parameters
_dataset(DataSet): The dataset whose documents are verified.
Returns
bool:Trueif all documents haverun == "DONE", otherwiseFalse.
Usage Example
condition(dataset) # Will wait until all documents in dataset are parsed or timeout occurs
Implementation Details
Retrieves up to 1000 documents from the dataset.
Iterates over each document and checks the
runfield.Returns
Falseimmediately if any document is not done.Returns
Trueonly if all documents are done.
Fixture: add_chat_assistants_func
@pytest.fixture(scope="function")
def add_chat_assistants_func(
request: FixtureRequest,
client: RAGFlow,
add_document: tuple[DataSet, Document]
) -> tuple[DataSet, Document, list[Chat]]:
Purpose
Provides a test fixture that prepares and returns a dataset, a parsed document, and a list of chat assistants for use in test functions.
This fixture handles:
Scheduling document parsing asynchronously.
Blocking test execution until document parsing completes using the
conditionfunction.Creating 5 chat assistants linked to the dataset.
Registering cleanup to delete all chat sessions after the test to maintain test isolation.
Parameters
request(FixtureRequest): Pytest request object to manage fixture finalization.client(RAGFlow): The RAGFlow client instance used to interact with the backend.add_document(tuple[DataSet, Document]): A tuple containing a dataset and a document that has been previously added (likely another fixture).
Returns
tuple[DataSet, Document, list[Chat]]: The dataset, the parsed document, and a list of created chat assistant instances.
Usage Example
def test_chat_functionality(add_chat_assistants_func):
dataset, document, chat_assistants = add_chat_assistants_func
# perform test assertions and interactions with chat_assistants
Implementation Details
Defines a
cleanupfunction to delete all chats viaclient.delete_chats(ids=None).Registers
cleanupto run after the test function finishes.Calls
async_parse_documentson the dataset with the document's ID.Uses the
conditionfunction to wait for parsing completion.Creates 5 chat assistants using
batch_create_chat_assistants.
Important Implementation Details
Polling with Timeout: The use of the
wait_fordecorator aroundconditionimplements a polling mechanism with a timeout of 30 seconds and 1-second intervals, ensuring that tests proceed only after asynchronous document parsing completes.Resource Cleanup: The fixture leverages Pytest's finalizer mechanism (
request.addfinalizer) to clean up created chats, preventing test pollution and ensuring repeatability.Integration with External SDK: This file relies heavily on the
ragflow_sdkto interact with dataset, document, chat, and client abstractions, indicating it is tightly coupled with the RAGFlow backend or service.Batch Creation of Chat Assistants: The utility
batch_create_chat_assistantsis used to instantiate multiple chat assistants efficiently, abstracting repetitive chat creation logic.
Interaction with Other Parts of the System
ragflow_sdkEntities: Interfaces withDataSet,Document,Chat, andRAGFlowclient objects to perform dataset and chat-related operations.common.batch_create_chat_assistants: Utilizes a shared utility to create chat assistants, implying this function is defined in a common utilities module.utils.wait_for: Uses a generic polling decorator to handle asynchronous waiting, indicating reliance on a utility module for synchronization primitives.add_documentFixture: Depends on another fixture (add_document) which presumably creates and returns an initial dataset and document for testing.Pytest Framework: Leverages Pytest’s fixture lifecycle and cleanup mechanisms for test setup and teardown.
Visual Diagram
flowchart TD
subgraph Fixtures & Functions
condition["condition(_dataset: DataSet)"]
add_chat_assistants_func["add_chat_assistants_func(request, client, add_document)"]
end
subgraph External Modules
wait_for["wait_for(timeout=30s, interval=1s)"]
batch_create["batch_create_chat_assistants(client, 5)"]
client["RAGFlow client"]
add_document["add_document fixture\n(DataSet, Document)"]
end
condition -- decorated by --> wait_for
add_chat_assistants_func --> add_document
add_chat_assistants_func --> client
add_chat_assistants_func --> condition
add_chat_assistants_func --> batch_create
add_chat_assistants_func -- cleanup --> client
condition -->|checks| client
Summary
conftest.py is a key testing support file in the InfiniFlow project that provides:
A robust way to wait for asynchronous document parsing to complete.
A fixture that creates a dataset with parsed documents and multiple chat assistants.
Automatic cleanup of chat sessions to maintain test state hygiene.
This file is integral to the testing workflow for components relying on document ingestion and chat assistant creation in the RAGFlow system, enabling consistent and reliable integration tests.