use super::common::{base_plan, open_policy, snapshot};
use vlorql::VlorQl;
use vlorql_core::schema::{
ComparisonOperator, DataType, Expression, FromClause, Predicate, Projection, QueryPlan,
};
use vlorql_llm::MockLlmClient;
fn facade_with_compile_cache() -> VlorQl {
VlorQl::builder()
.with_schema(snapshot())
.with_dialect_name("postgres")
.with_policy(open_policy())
.with_llm_client(MockLlmClient::success(base_plan()))
.with_compile_cache(1024, 60)
.build()
.expect("facade should build")
}
fn facade_with_all_caches() -> VlorQl {
VlorQl::builder()
.with_schema(snapshot())
.with_dialect_name("postgres")
.with_policy(open_policy())
.with_llm_client(MockLlmClient::success(base_plan()))
.with_compile_cache(1024, 60)
.with_prompt_cache(100, 60)
.build()
.expect("facade should build")
}
fn plan_with_filter() -> QueryPlan {
QueryPlan {
select: vec![Projection::Column {
table: Some("users".to_owned()),
column: "id".to_owned(),
alias: None,
}],
from: FromClause {
table: "users".to_owned(),
alias: None,
},
r#where: Some(Predicate::Comparison {
left: Expression::ColumnRef {
table: Some("users".to_owned()),
column: "id".to_owned(),
},
op: ComparisonOperator::Gt,
right: Expression::Literal {
value: serde_json::json!(10),
data_type: DataType::Int,
},
}),
group_by: None,
having: None,
order_by: None,
limit: None,
offset: None,
joins: None,
ctes: None,
distinct: false,
distinct_on: None,
set_operation: None, }
}
#[tokio::test]
async fn compile_cache_hits_on_second_call() {
let vlorql = facade_with_compile_cache();
let first = vlorql
.query("list users with id > 10")
.await
.expect("first query should succeed");
let second = vlorql
.query("list users with id > 10")
.await
.expect("second query should succeed");
assert_eq!(
first.sql, second.sql,
"cached compiled SQL should match the first compilation"
);
assert_eq!(
first.parameters, second.parameters,
"cached parameters should match"
);
}
#[tokio::test]
async fn compile_cache_invalidation_forces_recompile() {
let vlorql = facade_with_compile_cache();
let first = vlorql
.query("list users with id > 10")
.await
.expect("first query should succeed");
let plan = plan_with_filter();
let validated = vlorql.validate_only(&plan).expect("plan should validate");
vlorql.invalidate_compile_cache(&validated).await;
let second = vlorql
.query("list users with id > 10")
.await
.expect("second query should succeed");
assert_eq!(
first.sql, second.sql,
"re-compiled SQL should be identical to the original"
);
}
#[tokio::test]
async fn compile_cache_isolation_by_dialect() {
let vlorql = facade_with_compile_cache();
let _ = vlorql
.query("list users")
.await
.expect("query should succeed");
let compile_cache = vlorql.compile_cache();
assert!(
compile_cache.is_some(),
"compile cache should be configured"
);
}
#[tokio::test]
async fn prompt_cache_is_configured() {
let vlorql = facade_with_all_caches();
let prompt_cache = vlorql.prompt_cache();
assert!(prompt_cache.is_some(), "prompt cache should be configured");
}
#[tokio::test]
async fn clear_all_caches_does_not_break_queries() {
let vlorql = facade_with_compile_cache();
let _ = vlorql
.query("list users with id > 10")
.await
.expect("query before clear should succeed");
vlorql.clear_all_caches();
let result = vlorql
.query("list users with id > 10")
.await
.expect("query after clear should succeed");
assert!(!result.sql.is_empty(), "compiled SQL should not be empty");
}
#[tokio::test]
async fn schema_cache_is_configured() {
let vlorql = VlorQl::builder()
.with_schema(snapshot())
.with_dialect_name("postgres")
.with_policy(open_policy())
.with_schema_cache(10, 60)
.build()
.expect("facade with schema cache should build");
let schema_cache = vlorql.schema_cache();
assert!(schema_cache.is_some(), "schema cache should be configured");
vlorql.invalidate_schema_cache("nonexistent");
}