test_retrieval_chunks.py
Overview
This file contains a suite of automated tests designed to validate the behavior of chunk retrieval functionality in the InfiniFlow system. The tests are implemented using the pytest framework and focus on verifying the correctness, robustness, and edge cases of the retrieve method of a client object. The retrieval method is expected to fetch data chunks based on various input parameters such as dataset_ids, document_ids, pagination options, similarity weights, and reranking configurations.
The tests cover:
Basic retrieval scenarios with required and optional parameters.
Pagination behavior through
pageandpage_sizeparameters.Weighting of vector similarity in search results.
Limiting the number of results with
top_k.Reranking capabilities.
Keyword-based search toggles.
Concurrent retrieval requests to test thread safety and performance.
The file primarily ensures that the retrieval mechanism handles input validation properly, returns expected results, and gracefully raises exceptions on invalid input.
Classes and Methods
Class: TestChunksRetrieval
This class encapsulates multiple test methods, each focused on testing different aspects of chunk retrieval.
Method: test_basic_scenarios
def test_basic_scenarios(self, client, add_chunks, payload, expected_page_size, expected_message):
Purpose: Tests basic retrieval scenarios with different combinations of inputs such as presence or absence of
dataset_idsanddocument_ids.Parameters:
client: A test client instance capable of executing theretrievemethod.add_chunks: A pytest fixture that provides pre-populated dataset, document, and chunks for testing.payload(dict): Input arguments forretrieveincluding keys like"question","dataset_ids", and"document_ids".expected_page_size(int): The expected number of chunks returned.expected_message(str): Expected error message substring if an exception is expected.
Behavior:
If
expected_messageis non-empty, the test expects an exception when callingretrieve.Otherwise, it verifies the number of chunks retrieved matches
expected_page_size.
Usage example:
payload = {"question": "chunk", "dataset_ids": [dataset.id]} chunks = client.retrieve(**payload) assert len(chunks) == 4
Method: test_page
def test_page(self, client, add_chunks, payload, expected_page_size, expected_message):
Purpose: Validates chunk retrieval behavior concerning the
pageparameter, including edge cases like invalid page numbers and types.Parameters: Same as
test_basic_scenarios, withpayloadincludingpageandpage_size.Notes: Some test cases are skipped due to known issues or unsupported behaviors.
Exception testing: Checks for errors related to negative slicing or invalid conversions to integer.
Example:
payload = {"question": "chunk", "dataset_ids": [dataset.id], "page": 2, "page_size": 2} chunks = client.retrieve(**payload) assert len(chunks) == 2
Method: test_page_size
def test_page_size(self, client, add_chunks, payload, expected_page_size, expected_message):
Purpose: Tests the influence of the
page_sizeparameter on the number of chunks retrieved.Parameters: Similar to above, focusing on
page_sizevalues and their string or numeric types.Edge cases: Tests for invalid string inputs and
Nonevalues that raise exceptions.Example:
payload = {"question": "chunk", "dataset_ids": [dataset.id], "page_size": "1"} chunks = client.retrieve(**payload) assert len(chunks) == 1
Method: test_vector_similarity_weight
def test_vector_similarity_weight(self, client, add_chunks, payload, expected_page_size, expected_message):
Purpose: Checks how the
vector_similarity_weightparameter affects retrieval, including valid floats and invalid string inputs.Parameters:
payloadincludesvector_similarity_weight.Behavior: Expects exceptions on invalid float conversions.
Example:
payload = {"question": "chunk", "dataset_ids": [dataset.id], "vector_similarity_weight": 0.5} chunks = client.retrieve(**payload) assert len(chunks) == 4
Method: test_top_k
def test_top_k(self, client, add_chunks, payload, expected_page_size, expected_message):
Purpose: Tests the
top_kparameter to limit the number of chunks retrieved.Parameters:
payloadincludestop_k.Special notes: Some tests are skipped based on the environment variable
DOC_ENGINEdue to differences in supported features or behaviors.Edge cases: Negative, zero, string inputs for
top_kthat trigger errors.Example:
payload = {"question": "chunk", "dataset_ids": [dataset.id], "top_k": 10} chunks = client.retrieve(**payload) assert len(chunks) == 4
Method: test_rerank_id
def test_rerank_id(self, client, add_chunks, payload, expected_message):
Purpose: Tests reranking functionality by specifying a
rerank_id.Parameters:
payloadincludesrerank_id.Notes: This test is currently skipped, possibly due to external dependencies or incomplete implementation.
Behavior: Expects exceptions for unauthorized rerank models.
Example:
payload = {"question": "chunk", "dataset_ids": [dataset.id], "rerank_id": "BAAI/bge-reranker-v2-m3"} chunks = client.retrieve(**payload) assert len(chunks) > 0
Method: test_keyword
def test_keyword(self, client, add_chunks, payload, expected_page_size, expected_message):
Purpose: Tests keyword-based retrieval toggles using the
keywordparameter.Parameters:
payloadincludes boolean or string representations of boolean values.Notes: The test is currently skipped.
Behavior: Checks if retrieval works correctly regardless of keyword flag variations.
Example:
payload = {"question": "chunk test", "dataset_ids": [dataset.id], "keyword": True} chunks = client.retrieve(**payload) assert len(chunks) == 5
Method: test_concurrent_retrieval
def test_concurrent_retrieval(self, client, add_chunks):
Purpose: Verifies thread safety and performance of concurrent retrieval requests.
Parameters:
client: test client instance.add_chunks: test data fixture.
Implementation: Uses
ThreadPoolExecutorto issue 100 concurrentretrievecalls with 5 worker threads.Validation: Ensures that all futures complete and that the number of responses matches the number of requests.
Example:
payload = {"question": "chunk", "dataset_ids": [dataset.id]} with ThreadPoolExecutor(max_workers=5) as executor: futures = [executor.submit(client.retrieve, **payload) for _ in range(100)] responses = list(as_completed(futures)) assert len(responses) == 100
Important Implementation Details
Parameterized Testing: The use of
pytest.mark.parametrizeallows concise specification of multiple test inputs and expected outputs or exceptions in a tabular form.Exception Handling: Tests that expect exceptions use
pytest.raisesto assert that the correct error messages are raised for invalid inputs.Conditional Skips: Some tests are skipped depending on environment variables or known issues, using
pytest.mark.skipandpytest.mark.skipif.Concurrent Execution: The
test_concurrent_retrievalmethod ensures that the retrieval mechanism is thread-safe and can handle multiple simultaneous requests without failure.Fixtures: The tests depend on pytest fixtures
clientandadd_chunksto provide a testing client and pre-populated test data respectively.
Interaction with Other Parts of the System
Client Object: The
clientused in tests is expected to provide aretrievemethod that accepts various parameters to fetch chunks. This client interfaces with the underlying InfiniFlow retrieval engine.Test Data Fixtures:
add_chunksfixture provides datasets, documents, and chunks used as test inputs, likely interacting with mock or test databases.Environment Variables: The behavior of some tests depends on the
DOC_ENGINEenvironment variable, indicating integration with different document indexing/search backends such as Infinity, OpenSearch, or Elasticsearch.Rerank Models: The
rerank_idparameter tests integration with reranking models, indicating interaction with deployed ranking models or services.
Visual Diagram
classDiagram
class TestChunksRetrieval {
+test_basic_scenarios(client, add_chunks, payload, expected_page_size, expected_message)
+test_page(client, add_chunks, payload, expected_page_size, expected_message)
+test_page_size(client, add_chunks, payload, expected_page_size, expected_message)
+test_vector_similarity_weight(client, add_chunks, payload, expected_page_size, expected_message)
+test_top_k(client, add_chunks, payload, expected_page_size, expected_message)
+test_rerank_id(client, add_chunks, payload, expected_message)
+test_keyword(client, add_chunks, payload, expected_page_size, expected_message)
+test_concurrent_retrieval(client, add_chunks)
}
Summary
test_retrieval_chunks.py is a comprehensive test module for validating the chunk retrieval feature of the InfiniFlow system. It ensures that the retrieval API:
Correctly handles required and optional parameters.
Supports pagination and result limiting.
Appropriately applies vector similarity weighting.
Integrates with reranking models.
Handles keyword filters.
Supports concurrent access without errors.
By covering both positive and negative test cases with parameterized inputs and exception checks, this test suite helps maintain the reliability and robustness of the chunk retrieval functionality as the system evolves.