mirror of
https://github.com/Kaelio/ktx.git
synced 2026-06-10 08:05:14 +02:00
Initial open-source release
This commit is contained in:
commit
1a42152e6f
1199 changed files with 257054 additions and 0 deletions
64
python/klo-daemon/tests/test_semantic_layer.py
Normal file
64
python/klo-daemon/tests/test_semantic_layer.py
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from klo_daemon.semantic_layer import (
|
||||
SemanticLayerQueryRequest,
|
||||
ValidateSourcesRequest,
|
||||
query_semantic_layer,
|
||||
validate_semantic_layer,
|
||||
)
|
||||
|
||||
|
||||
ORDERS_SOURCE = {
|
||||
"name": "orders",
|
||||
"table": "public.orders",
|
||||
"grain": ["id"],
|
||||
"columns": [
|
||||
{"name": "id", "type": "number"},
|
||||
{"name": "status", "type": "string"},
|
||||
{"name": "amount", "type": "number"},
|
||||
],
|
||||
"joins": [],
|
||||
"measures": [
|
||||
{"name": "order_count", "expr": "count(*)"},
|
||||
{"name": "revenue", "expr": "sum(amount)"},
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
def test_query_semantic_layer_generates_sql_and_plan() -> None:
|
||||
response = query_semantic_layer(
|
||||
SemanticLayerQueryRequest(
|
||||
sources=[ORDERS_SOURCE],
|
||||
dialect="postgres",
|
||||
query={
|
||||
"measures": ["orders.order_count"],
|
||||
"dimensions": ["orders.status"],
|
||||
"limit": 25,
|
||||
},
|
||||
)
|
||||
)
|
||||
|
||||
assert response.dialect == "postgres"
|
||||
assert "public.orders" in response.sql
|
||||
assert "orders.status" in response.sql
|
||||
assert response.columns[0]["name"] == "orders.status"
|
||||
assert response.columns[1]["name"] == "orders.order_count"
|
||||
assert response.plan["sources_used"] == ["orders"]
|
||||
|
||||
|
||||
def test_validate_semantic_layer_reports_duplicate_measure_names() -> None:
|
||||
invalid_source = {
|
||||
**ORDERS_SOURCE,
|
||||
"measures": [
|
||||
{"name": "revenue", "expr": "sum(amount)"},
|
||||
{"name": "revenue", "expr": "sum(amount)"},
|
||||
],
|
||||
}
|
||||
|
||||
response = validate_semantic_layer(
|
||||
ValidateSourcesRequest(sources=[invalid_source], dialect="postgres")
|
||||
)
|
||||
|
||||
assert response.valid is False
|
||||
assert any("Duplicate measure" in error for error in response.errors)
|
||||
assert response.warnings == []
|
||||
Loading…
Add table
Add a link
Reference in a new issue