conftest.py
Overview
The conftest.py file provides pytest fixtures for managing chat assistant sessions within the InfiniFlow testing framework. It is designed to help test cases by setting up and tearing down multiple chat assistant sessions automatically, reducing boilerplate code and ensuring resource cleanup after tests run. The fixtures handle creation of chat sessions linked to chat assistants, and ensure these sessions are deleted after tests complete.
This file interacts closely with utility functions imported from the common module, which encapsulate HTTP API calls to create and delete chat sessions. It also relies on other fixtures (such as get_http_api_auth and add_chat_assistants) which provide authentication and chat assistant setup.
Detailed Explanations
Imports
pytest: The Python testing framework used here for defining fixtures.create_session_with_chat_assistant,delete_session_with_chat_assistants(fromcommon): Utility functions that perform session creation and deletion over the HTTP API.
Fixtures
1. add_sessions_with_chat_assistant
Scope:
class(setup once per test class)Purpose: Creates 5 new chat sessions associated with the first chat assistant from
add_chat_assistantsfixture, returns the chat assistant ID and list of session IDs, and ensures cleanup after all tests in the class finish.
Parameters
request: pytest's built-in request context object, used to register finalizer cleanup functions.get_http_api_auth: Fixture providing HTTP API authentication credentials or token.add_chat_assistants: Fixture that presumably adds chat assistants and returns their IDs.
Returns
A tuple:
(chat_assistant_id: str, session_ids: List[str])chat_assistant_id: ID of the first chat assistant used to create sessions.session_ids: List of 5 session IDs created for that assistant.
Usage Example
def test_example(add_sessions_with_chat_assistant):
chat_assistant_id, session_ids = add_sessions_with_chat_assistant
assert len(session_ids) == 5
# Interact with sessions in tests here
Implementation Details
Retrieves chat assistant IDs from
add_chat_assistants.Defines a
cleanupfunction that deletes all sessions linked to these chat assistants after tests.Registers the cleanup function as a finalizer with
request.addfinalizer.Creates 5 sessions using
create_session_with_chat_assistantfor the first chat assistant.Collects and returns the created session IDs.
2. add_sessions_with_chat_assistant_func
Scope:
function(setup for each individual test function)Purpose: Similar to
add_sessions_with_chat_assistantbut creates and cleans up sessions for every test function instead of once per class.
Parameters
Same as
add_sessions_with_chat_assistant.
Returns
Same as
add_sessions_with_chat_assistant.
Usage Example
def test_individual(add_sessions_with_chat_assistant_func):
chat_assistant_id, session_ids = add_sessions_with_chat_assistant_func
assert len(session_ids) == 5
# Test with fresh sessions per test function
Implementation Details
The internal logic mirrors that of
add_sessions_with_chat_assistant.By using function scope, it ensures tests do not share session state, improving isolation.
Important Implementation Details
Both fixtures create exactly 5 sessions linked to the first chat assistant ID provided.
The session names follow the pattern
"session_with_chat_assistant_{i}"whereiranges from 0 to 4.Cleanup calls
delete_session_with_chat_assistantson each chat assistant ID, which presumably deletes all sessions belonging to that assistant.The use of
request.addfinalizer(cleanup)ensures that cleanup is guaranteed even if tests fail or raise exceptions.This design promotes resource cleanup and test isolation, critical in integration testing involving remote APIs.
Interaction with Other Parts of the System
commonmodule: Provides session creation and deletion functions that interact with the backend API.get_http_api_authfixture: Supplies authentication tokens or credentials necessary for API calls.add_chat_assistantsfixture: Sets up chat assistants and returns their IDs, foundational for creating sessions.Test cases: These fixtures are intended to be used by test modules/classes to provide pre-configured chat sessions, minimizing setup code within tests themselves.
Visual Diagram
This file is a utility file mainly defining fixtures and their workflows. The diagram below shows the flow and relationships between the main functions and fixtures within conftest.py and their interactions.
flowchart TD
A[add_chat_assistants Fixture] --> B[get_chat_assistant_ids]
B --> C[add_sessions_with_chat_assistant]
B --> D[add_sessions_with_chat_assistant_func]
C --> E[create_session_with_chat_assistant (5 times)]
D --> F[create_session_with_chat_assistant (5 times)]
C --> G[register cleanup finalizer]
D --> G
G --> H[delete_session_with_chat_assistants for each chat_assistant_id]
subgraph Usage in Tests
C --> I[Test Class uses add_sessions_with_chat_assistant]
D --> J[Test Function uses add_sessions_with_chat_assistant_func]
end
Summary
The conftest.py file defines two pytest fixtures that facilitate the creation and cleanup of multiple chat assistant sessions to be used in testing. By abstracting session management and cleanup, it enables tests to focus on core logic while maintaining environment stability and isolation. Its effective use of pytest's fixture scopes and finalizers ensures robust, repeatable, and clean test runs within the InfiniFlow project.