Skip to content

Commit 47479d5

Browse files
author
SQLumAI Bot
committed
fix: map API Rule to engine Rule filtering extras; make metrics_prom tests robust across starlette shims
1 parent 85f1a18 commit 47479d5

3 files changed

Lines changed: 18 additions & 6 deletions

File tree

src/api.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,20 @@ class _TestEvent(BaseModel):
316316

317317
@app.post("/rules/test")
318318
def rules_test(ev: _TestEvent):
319-
rules = [_PRule(**r.model_dump()) for r in _read_rules()]
319+
# Convert API Rule models to engine Rules, preserving known fields and optional extras
320+
rules = []
321+
for r in _read_rules():
322+
raw = r.model_dump()
323+
base = {k: raw.get(k) for k in ("id", "target", "selector", "action", "reason", "confidence", "enabled") if k in raw}
324+
rule = _PRule(**base)
325+
# Attach optional fields used by engine heuristics, if present
326+
for extra in ("apply_in_envs", "min_hits_to_enforce"):
327+
if extra in raw:
328+
try:
329+
setattr(rule, extra, raw[extra])
330+
except Exception:
331+
pass
332+
rules.append(rule)
320333
pe = _PE(rules)
321334
dec = pe.decide(_PEvent(database=None, user=None, sql_text=ev.sql_text, table=ev.table, column=ev.column, value=ev.value))
322335
return dec.__dict__

tests/test_metrics_policy_coverage.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def test_metrics_prom_fallback_lines(tmp_path, monkeypatch):
8383
api = importlib.import_module("src.api")
8484
importlib.reload(api)
8585
resp = api.metrics_prom()
86-
content = resp.content if hasattr(resp, "content") else resp # shim in minimal env
87-
text = content.decode("utf-8") if hasattr(content, "decode") else str(content)
86+
payload = getattr(resp, "body", None) or getattr(resp, "content", None) or resp
87+
text = payload.decode("utf-8") if hasattr(payload, "decode") else str(payload)
8888
assert "sqlumai_metric{key=\"allowed\"} 5" in text
8989
assert "sqlumai_metric{key=\"rule\",rule=\"rX\",action=\"block\"} 2" in text

tests/test_policy_more_coverage.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,8 @@ def test_metrics_prom_success_branch(tmp_path, monkeypatch):
4646
)
4747
monkeypatch.setitem(sys.modules, "prometheus_client", fake) # type: ignore[arg-type]
4848
resp = api.metrics_prom()
49-
# In shim Response, .content is set
50-
content = getattr(resp, "content", b"")
51-
assert b"metric 1" in content or "metric 1" in str(content)
49+
payload = getattr(resp, "body", None) or getattr(resp, "content", None) or b""
50+
assert (hasattr(payload, "decode") and b"metric 1" in payload) or "metric 1" in str(payload)
5251

5352

5453
def test_rules_delete_success(tmp_path, monkeypatch):

0 commit comments

Comments
 (0)