Back to Repositories

Testing Query Transformation Components in LlamaIndex Core

This test suite validates the query transformation functionality in the LlamaIndex framework, specifically focusing on the DecomposeQueryTransform component. It ensures proper query decomposition and transformation behavior with mock data.

Test Coverage Overview

The test coverage focuses on the DecomposeQueryTransform class functionality, verifying query string manipulation and embedding generation.

  • Tests basic query transformation with mock prompts
  • Validates query string concatenation with index summary
  • Ensures correct embedding string generation

Implementation Analysis

The testing approach uses a patched LLM predictor and mock prompts to isolate the query transformation logic. The implementation follows a straightforward unit testing pattern, validating the transformation pipeline from input query to final query bundle.

  • Uses dependency injection via patch_llm_predictor
  • Implements mock prompt templates
  • Validates query bundle properties

Technical Details

  • Testing Framework: Python’s built-in testing framework
  • Mock Utilities: Custom mock_utils module
  • Key Components: DecomposeQueryTransform class
  • Test Dependencies: MOCK_DECOMPOSE_PROMPT

Best Practices Demonstrated

The test demonstrates clean unit testing practices with proper isolation of dependencies and clear assertion statements. It follows the Arrange-Act-Assert pattern and maintains focused test scope.

  • Isolated testing environment
  • Clear test function naming
  • Explicit assertions
  • Mock data usage

run-llama/llama_index

llama-index-core/tests/indices/query/query_transform/test_base.py

            
"""Test query transform."""

from llama_index.core.indices.query.query_transform.base import (
    DecomposeQueryTransform,
)
from tests.indices.query.query_transform.mock_utils import MOCK_DECOMPOSE_PROMPT


def test_decompose_query_transform(patch_llm_predictor) -> None:
    """Test decompose query transform."""
    query_transform = DecomposeQueryTransform(
        decompose_query_prompt=MOCK_DECOMPOSE_PROMPT
    )

    query_str = "What is?"
    new_query_bundle = query_transform.run(query_str, {"index_summary": "Foo bar"})
    assert new_query_bundle.query_str == "What is?:Foo bar"
    assert new_query_bundle.embedding_strs == ["What is?:Foo bar"]