use super::{openapi_or_skip, validate_ref};
use crate::grpc_route::test_api;
use crate::qdrant_grpc::qdrant;
use qql_core::parser::Parser;
use qql_plan::PlannedOperation;
use qql_plan::plan::{plan, to_rest_route};
#[test]
fn numeric_shard_key_stays_numeric_on_both_transports() {
let stmt = Parser::parse("DELETE FROM docs WHERE id = 1 SHARD 101;").unwrap();
let op = plan(&stmt).unwrap();
let route = to_rest_route(&op).expect("rest route");
let body = route.body_json().unwrap();
assert_eq!(body["shard_key"], serde_json::json!(101));
assert!(
route.query.iter().all(|(k, _)| k != "shard_key"),
"routing rides the body field, not the query string: {:?}",
route.query
);
let PlannedOperation::Delete {
collection,
request,
..
} = &op
else {
panic!("expected Delete plan");
};
assert_eq!(collection, "docs");
let selector = test_api::shard_key_selector(&request.shard_key);
let keys = selector.unwrap().shard_keys;
assert_eq!(keys.len(), 1);
match keys[0].key.as_ref() {
Some(qdrant::shard_key::Key::Number(n)) => assert_eq!(*n, 101),
other => panic!("expected numeric shard, got {other:?}"),
}
}
#[test]
fn rest_grpc_query_parity_timeout_consistency_shard_multi() {
let stmt = Parser::parse(
"QUERY NEAREST VECTOR [[0.1, 0.2], [0.3, 0.4]] FROM docs \
USING colbert SHARD 'acme' \
PARAMS (timeout = 15, consistency = quorum) LIMIT 7;",
)
.unwrap();
let op = plan(&stmt).unwrap();
let PlannedOperation::Query {
collection,
request,
} = &op
else {
panic!("expected Query plan");
};
assert_eq!(collection, "docs");
let route = to_rest_route(&op).expect("rest route");
assert_eq!(route.path, "/collections/docs/points/query");
assert!(route.query.iter().any(|(k, v)| k == "timeout" && v == "15"));
assert!(
route
.query
.iter()
.any(|(k, v)| k == "consistency" && v == "quorum")
);
let body = route.body_json().unwrap();
assert_eq!(body["limit"], 7);
assert_eq!(body["using"], "colbert");
assert_eq!(body["shard_key"], "acme");
assert!(body["query"]["nearest"].is_array());
assert!(body.get("timeout").is_none());
assert!(body.get("consistency").is_none());
let grpc = test_api::to_query_points(request, collection).expect("to_query_points");
assert_eq!(grpc.collection_name, "docs");
assert_eq!(grpc.using.as_deref(), Some("colbert"));
assert_eq!(grpc.limit, Some(7));
assert_eq!(grpc.timeout, Some(15));
assert!(grpc.read_consistency.is_some());
let rc = grpc.read_consistency.as_ref().unwrap();
use qdrant::read_consistency::Value as RcVal;
match rc.value.as_ref() {
Some(RcVal::Type(t)) => {
assert_eq!(*t, qdrant::ReadConsistencyType::Quorum as i32);
}
other => panic!("expected quorum type, got {other:?}"),
}
assert!(grpc.shard_key_selector.is_some());
let sk = &grpc.shard_key_selector.as_ref().unwrap().shard_keys;
assert_eq!(sk.len(), 1);
match sk[0].key.as_ref() {
Some(qdrant::shard_key::Key::Keyword(k)) => assert_eq!(k, "acme"),
other => panic!("expected keyword shard, got {other:?}"),
}
let q = grpc.query.as_ref().expect("query");
use qdrant::query::Variant as Qv;
use qdrant::vector_input::Variant as Vi;
match q.variant.as_ref() {
Some(Qv::Nearest(vi)) => match vi.variant.as_ref() {
Some(Vi::MultiDense(md)) => {
assert_eq!(md.vectors.len(), 2);
assert_eq!(md.vectors[0].data, vec![0.1, 0.2]);
assert_eq!(md.vectors[1].data, vec![0.3, 0.4]);
}
other => panic!("expected MultiDense, got {other:?}"),
},
other => panic!("expected Nearest, got {other:?}"),
}
}
#[cfg(feature = "rest")]
#[test]
fn rest_grpc_empty_payload_parity() {
use std::collections::HashMap;
let stmt = Parser::parse("SCROLL FROM docs LIMIT 1;").unwrap();
let op = plan(&stmt).unwrap();
let rest = crate::rest_response::parse_planned(
&op,
serde_json::json!({"result": {"points": [{"id": 1, "payload": {}}]}, "status": "ok"}),
)
.expect("REST empty payload parses");
let rest_hit = &rest.data.hits().expect("hits")[0];
assert_eq!(rest_hit.payload, Some(HashMap::new()));
let grpc_hit = test_api::retrieved_point_to_hit(qdrant::RetrievedPoint {
id: Some(qdrant::PointId {
point_id_options: Some(qdrant::point_id::PointIdOptions::Num(1)),
}),
payload: Default::default(),
vectors: None,
shard_key: None,
order_value: None,
})
.expect("gRPC empty payload converts");
assert_eq!(grpc_hit.payload, rest_hit.payload);
}
#[test]
fn rest_grpc_geo_polygon_parity() {
let stmt = Parser::parse(
"QUERY TEXT 'x' MODEL 'e5' FROM docs WHERE area GEO_POLYGON \
{exterior: [{lat: -70.0, lon: -70.0}, {lat: 60.0, lon: -70.0}, {lat: 60.0, lon: 60.0}, {lat: -70.0, lon: 60.0}], \
interiors: [[{lat: -50.0, lon: -50.0}, {lat: 50.0, lon: -50.0}, {lat: 50.0, lon: 50.0}, {lat: -50.0, lon: 50.0}]]};",
)
.unwrap();
let op = plan(&stmt).unwrap();
let PlannedOperation::Query {
collection,
request,
} = &op
else {
panic!("expected Query");
};
let body = to_rest_route(&op).expect("rest route").body_json().unwrap();
let rest_polygon = &body["filter"]["must"][0]["geo_polygon"];
assert_eq!(
rest_polygon["exterior"]["points"].as_array().map(Vec::len),
Some(4)
);
assert_eq!(rest_polygon["interiors"].as_array().map(Vec::len), Some(1));
assert_eq!(rest_polygon["exterior"]["points"][0]["lat"], -70.0);
let grpc = test_api::to_query_points(request, collection).unwrap();
let condition = grpc
.filter
.expect("gRPC filter converts")
.must
.swap_remove(0);
let Some(qdrant::condition::ConditionOneOf::Field(field)) = condition.condition_one_of else {
panic!("expected a field condition");
};
let proto_polygon = field.geo_polygon.expect("REST body has geo_polygon");
let exterior = proto_polygon.exterior.expect("exterior ring");
assert_eq!(exterior.points.len(), 4);
assert_eq!(exterior.points[0].lat, -70.0);
assert_eq!(proto_polygon.interiors.len(), 1);
}
#[test]
fn rest_grpc_scroll_order_by_parity() {
let stmt = Parser::parse("SCROLL FROM docs ORDER BY ts DESC START FROM 7 LIMIT 5;").unwrap();
let op = plan(&stmt).unwrap();
let PlannedOperation::Scroll {
collection,
request,
} = &op
else {
panic!("expected Scroll");
};
let body = to_rest_route(&op).expect("rest route").body_json().unwrap();
assert_eq!(body["order_by"]["key"], "ts");
assert_eq!(body["order_by"]["direction"], "desc");
assert_eq!(body["order_by"]["start_from"], 7);
let grpc = test_api::to_scroll_points(request, collection).unwrap();
let order_by = grpc.order_by.expect("REST body has order_by");
assert_eq!(order_by.key, "ts");
assert_eq!(order_by.direction, Some(qdrant::Direction::Desc as i32));
match order_by.start_from.and_then(|sf| sf.value) {
Some(qdrant::start_from::Value::Integer(7)) => {}
other => panic!("expected integer START FROM 7, got {other:?}"),
}
}
#[test]
fn rest_grpc_count_exact_parity() {
let stmt = Parser::parse("COUNT FROM docs WITH (exact = false);").unwrap();
let op = plan(&stmt).unwrap();
let PlannedOperation::Count {
collection,
request,
} = &op
else {
panic!("expected Count");
};
let body = to_rest_route(&op).expect("rest route").body_json().unwrap();
assert_eq!(body["exact"], false);
assert_eq!(
test_api::to_count_points(request, collection)
.unwrap()
.exact,
Some(false)
);
}
#[test]
fn rest_grpc_relevance_feedback_parity() {
let stmt = Parser::parse(
"QUERY RELEVANCE FEEDBACK TARGET POINT 42 FEEDBACK ((POINT 43, 0.5), (POINT 44, -0.2)) STRATEGY NAIVE (a = 1.0, b = 0.5, c = 0.5) FROM docs USING dense LIMIT 10;",
)
.unwrap();
let op = plan(&stmt).unwrap();
let PlannedOperation::Query {
collection,
request,
} = &op
else {
panic!("expected Query");
};
let body = to_rest_route(&op).expect("rest route").body_json().unwrap();
let rf = &body["query"]["relevance_feedback"];
assert!(
rf.is_object(),
"REST relevance_feedback missing: {}",
body["query"]
);
assert_eq!(rf["feedback"].as_array().map(Vec::len), Some(2));
assert_eq!(rf["strategy"]["naive"]["a"], 1.0);
let grpc = test_api::to_query_points(request, collection).unwrap();
use qdrant::query::Variant as Qv;
let Some(Qv::RelevanceFeedback(fb)) = grpc.query.as_ref().and_then(|q| q.variant.as_ref())
else {
panic!("expected RelevanceFeedback, got {:?}", grpc.query);
};
assert!(fb.target.is_some(), "feedback target must convert");
assert_eq!(fb.feedback.len(), 2, "both feedback items must convert");
assert!(fb.strategy.is_some(), "naive strategy must convert");
}
#[test]
fn match_except_filter_contract_matches_openapi() {
let Some(openapi) = openapi_or_skip() else {
return;
};
let stmt =
Parser::parse("QUERY TEXT 'x' MODEL 'e5' FROM docs WHERE tags MATCH EXCEPT ('a', 'b');")
.expect("MATCH EXCEPT parses");
let body = to_rest_route(&plan(&stmt).expect("MATCH EXCEPT plans"))
.expect("MATCH EXCEPT routes")
.body_json()
.expect("MATCH EXCEPT has a body");
let filter = body.get("filter").expect("route body carries a filter");
let norm_filter = if filter.get("must").is_none()
&& filter.get("should").is_none()
&& filter.get("must_not").is_none()
{
serde_json::json!({ "must": [filter] })
} else {
filter.clone()
};
assert_eq!(
norm_filter["must"][0]["match"]["except"],
serde_json::json!(["a", "b"])
);
validate_ref(&openapi, "Filter", &norm_filter);
use qql_plan::{FieldCondition, FilterClause, MatchValue};
let clause = FilterClause::Field(Box::new(FieldCondition {
key: "tag".into(),
r#match: Some(MatchValue::Except {
except: vec![
qql_core::ast::Value::Str("a".into()),
qql_core::ast::Value::Str("b".into()),
],
}),
..Default::default()
}));
let filter = serde_json::json!({
"must": [serde_json::to_value(&clause).expect("except clause serializes")],
});
validate_ref(&openapi, "Filter", &filter);
}
#[test]
fn grpc_range_rejects_non_numeric_bounds() {
let stmt =
Parser::parse("QUERY TEXT 'x' MODEL 'test-model' FROM docs WHERE age >= 21 LIMIT 5;")
.unwrap();
let op = plan(&stmt).unwrap();
let PlannedOperation::Query {
collection,
request,
} = &op
else {
panic!("expected Query");
};
let qp = test_api::to_query_points(request, collection).unwrap();
let range = qp
.filter
.expect("filter converts")
.must
.swap_remove(0)
.condition_one_of
.and_then(|c| match c {
qdrant::condition::ConditionOneOf::Field(f) => f.range,
_ => None,
})
.expect("numeric bound converts to a proto range");
assert_eq!(range.gte, Some(21.0));
for sql in [
"QUERY TEXT 'x' MODEL 'test-model' FROM docs WHERE created_at >= '2024-01-01T00:00:00Z' LIMIT 5;",
"QUERY TEXT 'x' MODEL 'test-model' FROM docs WHERE name > 'm' LIMIT 5;",
] {
let stmt = Parser::parse(sql).unwrap();
let op = plan(&stmt).unwrap();
let body = to_rest_route(&op).expect("rest route").body_json().unwrap();
assert!(
body["filter"]["must"][0]["range"].is_object(),
"REST keeps the string bound: {body}"
);
let PlannedOperation::Query {
collection,
request,
} = &op
else {
panic!("expected Query");
};
let err = test_api::to_query_points(request, collection).unwrap_err();
assert_eq!(err.code, "QQL-GRPC-RANGE-TYPE");
}
}
#[test]
fn rest_grpc_hybrid_prefetch_parity() {
let stmt = Parser::parse(
"QUERY HYBRID TEXT 'search' MODEL 'bge' DENSE dense SPARSE sparse FUSION RRF FROM docs LIMIT 10;",
)
.unwrap();
let op = plan(&stmt).unwrap();
let PlannedOperation::Query {
collection,
request,
} = &op
else {
panic!("expected Query");
};
let body = to_rest_route(&op).expect("rest route").body_json().unwrap();
assert_eq!(body["query"]["fusion"], "rrf");
assert_eq!(body["prefetch"].as_array().unwrap().len(), 2);
assert_eq!(body["prefetch"][0]["using"], "dense");
assert_eq!(body["prefetch"][1]["using"], "sparse");
let grpc = test_api::to_query_points(request, collection).unwrap();
assert_eq!(grpc.prefetch.len(), 2);
assert_eq!(grpc.prefetch[0].using.as_deref(), Some("dense"));
assert_eq!(grpc.prefetch[1].using.as_deref(), Some("sparse"));
use qdrant::query::Variant as Qv;
match grpc.query.as_ref().and_then(|q| q.variant.as_ref()) {
Some(Qv::Fusion(f)) => {
let _ = f;
}
other => panic!("expected Fusion query variant, got {other:?}"),
}
}