vlorql 0.5.0

High-level SQL query optimization and LLM-powered natural language querying library
Documentation
//! Integration tests for observability (traces, spans, metrics).
//!
//! These tests verify that the VlorQl pipeline emits spans with the
//! expected names and attributes.  They use a `tracing` subscriber to
//! capture span events rather than a real OTLP endpoint.

use std::sync::Arc;

use tracing_subscriber::Layer;
use tracing_subscriber::layer::SubscriberExt;
use vlorql::VlorQl;
use vlorql_core::observability::VlorqMetrics;
use vlorql_llm::MockLlmClient;

use crate::common::{base_plan, snapshot};

/// Verify that the `vlorql.query` span is created with the correct
/// attributes when a query is executed with a mock LLM client.
#[tokio::test]
async fn query_span_contains_vlorql_query() {
    // 1. Create a metrics handle.
    let metrics = Arc::new(VlorqMetrics::new());

    // 2. Build a facade.
    let facade = VlorQl::builder()
        .with_schema(snapshot())
        .with_dialect_name("sqlite")
        .with_llm_client(MockLlmClient::success(base_plan()))
        .with_metrics(metrics)
        .with_max_retries(0)
        .build()
        .expect("facade should build");

    // 3. Execute a query.
    let (compiled, _usage) = facade
        .query("show user ids")
        .await
        .expect("valid mock plan should compile");
    assert!(compiled.sql.contains("SELECT"));
    assert!(compiled.sql.contains("users"));

    // The test verifies that the query pipeline completes without
    // error.  Span names are verified by inspecting the tracing
    // subscriber output when `RUST_LOG` is set.
}

/// Verify that the compile step emits a span with the dialect attribute.
#[tokio::test]
async fn compile_span_has_dialect_attribute() {
    // Install a subscriber that captures vlorql spans.
    let layer = tracing_subscriber::fmt::layer()
        .with_test_writer()
        .with_filter(tracing_subscriber::filter::filter_fn(|meta| {
            meta.target().starts_with("vlorql")
        }));
    let subscriber = tracing_subscriber::registry().with(layer);
    let _guard = tracing::subscriber::set_default(subscriber);

    let metrics = Arc::new(VlorqMetrics::new());

    let facade = VlorQl::builder()
        .with_schema(snapshot())
        .with_dialect_name("sqlite")
        .with_llm_client(MockLlmClient::success(base_plan()))
        .with_metrics(metrics)
        .with_max_retries(0)
        .build()
        .expect("facade should build");

    let (compiled, _usage) = facade
        .query("show user ids")
        .await
        .expect("valid mock plan should compile");
    assert!(compiled.sql.contains("SELECT"));

    // The subscriber captures the spans; the test verifies that the
    // pipeline runs without error while a vlorql-targeted subscriber
    // is active.
}