conftest.py
Overview
The conftest.py file is a pytest configuration module designed to provide reusable fixtures and helper functions for testing the InfiniFlow project, specifically focusing on chat assistant and document parsing workflows. It facilitates setup and teardown operations for tests that require:
Adding and cleaning up chat assistants.
Managing documents and datasets.
Polling for asynchronous document parsing completion.
This file integrates with the project's API clients and utility functions to streamline test preparation and cleanup, ensuring test isolation and reliability.
Detailed Explanation
Imports
pytest: The testing framework used.create_chat_assistant,delete_chat_assistants,list_documnets,parse_documnets: Common API helper functions imported from thecommonmodule (note the consistent misspelling of "documents" as "documnets" in the code).wait_for: A decorator fromlibs.utilsthat implements a retry/wait mechanism to repeatedly check a condition until success or timeout.
Functions and Fixtures
1. condition(_auth, _dataset_id)
@wait_for(30, 1, "Document parsing timeout")
def condition(_auth, _dataset_id):
res = list_documnets(_auth, _dataset_id)
for doc in res["data"]["docs"]:
if doc["run"] != "DONE":
return False
return True
Purpose:
This function polls the status of document parsing for a specific dataset until all documents have theirrunstatus as"DONE". It is decorated withwait_forto retry every second for up to 30 seconds before timing out.Parameters:
_auth: Authentication credentials/token for API calls._dataset_id: Identifier of the dataset whose documents are being checked.
Returns:
Trueif all documents have finished parsing (run == "DONE").Falseotherwise, triggering a retry in the decorator.
Usage:
Used internally to wait for asynchronous document parsing to complete before proceeding with tests that depend on parsed data.
2. add_chat_assistants_func(request, get_http_api_auth, add_document)
@pytest.fixture(scope="function")
def add_chat_assistants_func(request, get_http_api_auth, add_document):
def cleanup():
delete_chat_assistants(get_http_api_auth)
request.addfinalizer(cleanup)
dataset_id, document_id = add_document
parse_documnets(get_http_api_auth, dataset_id, {"document_ids": [document_id]})
condition(get_http_api_auth, dataset_id)
chat_assistant_ids = []
for i in range(5):
res = create_chat_assistant(get_http_api_auth, {"name": f"test_chat_assistant_{i}", "dataset_ids": [dataset_id]})
chat_assistant_ids.append(res["data"]["id"])
return dataset_id, document_id, chat_assistant_ids
Purpose:
This pytest fixture sets up the environment needed to test chat assistants. It:Adds a document to a dataset.
Parses the document and waits for completion.
Creates five chat assistants linked to that dataset.
Cleans up chat assistants after the test finishes.
Parameters:
request: A pytest built-in fixture for test request context, used here to register cleanup logic.get_http_api_auth: Fixture or function providing authentication credentials for API calls.add_document: Fixture or function that returns a tuple(dataset_id, document_id)representing newly added documents and datasets.
Returns:
dataset_id: The dataset identifier used for creating chat assistants.document_id: The document identifier used for parsing.chat_assistant_ids: List of IDs of the five chat assistants created for testing.
Cleanup:
Registers a finalizer to delete all chat assistants after each test function that uses this fixture completes, ensuring no leftover state.Usage Example:
def test_chat_assistant_functionality(add_chat_assistants_func):
dataset_id, document_id, chat_assistant_ids = add_chat_assistants_func
# Perform test assertions or API calls with created chat assistants
assert len(chat_assistant_ids) == 5
Important Implementation Details
Polling with
wait_forDecorator:
Theconditionfunction uses thewait_fordecorator to implement polling. This decorator repeatedly invokesconditionevery 1 second, up to 30 seconds, to check if document parsing is complete. This pattern ensures tests wait properly for asynchronous operations without hard-coded sleeps.Cleanup via
request.addfinalizer:
The fixture usesrequest.addfinalizerto register thecleanupfunction, which deletes all chat assistants after the test run, preventing pollution of test state across multiple tests.API Integration:
The file depends heavily on external API utility functions (create_chat_assistant,delete_chat_assistants, etc.) to interact with backend services, indicating this is part of an integration or end-to-end testing strategy.Typographical Consistency:
The file consistently usesdocumnetsinstead ofdocumentsin function names and calls, reflecting either a legacy naming or a typo that is propagated throughout the codebase.
Interaction with Other System Components
commonModule:
Provides API client functions for managing chat assistants and documents. This file imports and uses these helpers to perform operations on backend resources.libs.utilsModule:
Supplies thewait_fordecorator that handles retry logic for asynchronous operations.Test Environment:
This file is part of the test suite and interacts with fixtures likeget_http_api_authandadd_documentwhich are likely defined elsewhere in the test framework. These fixtures provide authentication and document setup, respectively.Backend API:
The chat assistants and document parsing workflows tested here correspond to backend services that the InfiniFlow project exposes. This file helps ensure those services behave as expected under test conditions.
Mermaid Flowchart Diagram
flowchart TD
A[add_chat_assistants_func Fixture]
B[add_document Fixture]
C[get_http_api_auth Fixture]
D[parse_documnets()]
E[condition() with wait_for]
F[create_chat_assistant() x5]
G[delete_chat_assistants() Cleanup]
A --> B
A --> C
A --> D
D --> E
E --> F
A --> G
Explanation:
The
add_chat_assistants_funcfixture depends onadd_documentandget_http_api_authfixtures.It calls
parse_documnetsto start parsing the document.It waits for parsing completion using
conditionwrapped withwait_for.Upon success, it creates 5 chat assistants.
Registers a cleanup function to delete all chat assistants after the test.
Summary
conftest.py is a critical pytest configuration file that enables robust testing of chat assistant and document parsing features in the InfiniFlow project. By providing a reusable fixture that handles setup and teardown, it simplifies test development and ensures tests run against a clean, predictable environment. Its use of polling and cleanup patterns exemplifies best practices in integration testing asynchronous backend workflows.