Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
- @abu-tahir-0 for five fixes on their first day: strict `check --range` bounds (#85), one row-count history per connection (#86), whole-number and non-boolean budget limits (#84), refusing unknown `gulliver.toml` sections (#83) and GV013 for leading `_` (#82).

### Fixed
- `check` now refuses a negative `--min-rows` and a negative, NaN orinfinite `--max-change` instead of silently passing or failing (#106).
- `spend --days N`, `calibration --days N` and `top --limit N` refuse a value below 1 with `status: error` and a `next_step`, and exit 1 instead of quietly reporting an empty or off-by-one window. `top --analyze` refuses a negative value too, while `--analyze 0` still lists the groups without analysing them (#92).
- `check --range` refuses `nan`, infinite and non-numeric bounds and an inverted `min > max` before any query runs, naming the column. Before, `nan` / `inf` reached the engine as column names and an inverted range silently failed every row (#74, #85).
- `check --max-change` keys the row-count history on the resolved connection name, so a check with and without `-c default` shares one history. Histories saved under the old key are still read and migrated (#75, #86).
Expand Down
4 changes: 4 additions & 0 deletions src/gulliver/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ def parse_spec(unique: list[str] | None = None, not_null: list[str] | None = Non
max_change_pct: float | None = None) -> CheckSpec:
"""Parse CLI-style strings: unique 'id' or 'a,b'; accepted 'status=new|paid'; range 'amount=0:1000';
fresh 'ts=24h'."""
if min_rows is not None and min_rows < 0:
raise ValueError(f"min_rows needs 0 or more, got {min_rows!r}")
if max_change_pct is not None and not (math.isfinite(max_change_pct) and max_change_pct >= 0):
raise ValueError(f"max_change needs a finite number, 0 or more, got {max_change_pct!r}")
spec = CheckSpec(min_rows=min_rows, max_change_pct=max_change_pct)
for u in unique or []:
spec.unique.append([c.strip() for c in u.split(",") if c.strip()])
Expand Down
15 changes: 15 additions & 0 deletions tests/test_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,27 @@
def _bounds(text):
return parse_spec(ranges=[f"amount={text}"]).ranges["amount"]

@pytest.mark.parametrize("value", [-1, -5])
def test_min_rows_refuses_negative(value):
with pytest.raises(ValueError, match=r"min_rows needs 0 or more"):
parse_spec(min_rows=value)

@pytest.mark.parametrize("value", [-5.0, float("nan"), float("inf"), float("-inf")])
def test_max_change_refuses_negative_or_non_finite(value):
with pytest.raises(ValueError, match=r"max_change needs a finite number"):
parse_spec(max_change_pct=value)



@pytest.mark.parametrize("text", ["nan:", ":inf", "-inf:", ":nan"])
def test_range_refuses_non_finite_bounds(text):
with pytest.raises(ValueError, match="finite"):
parse_spec(ranges=[f"amount={text}"])

def test_zero_thresholds_still_parse():
spec = parse_spec(min_rows=0, max_change_pct=0.0)
assert spec.min_rows == 0
assert spec.max_change_pct == 0.0

def test_range_refuses_a_non_number():
with pytest.raises(ValueError, match=r"range for 'amount' needs numbers, got 'abc'"):
Expand Down
17 changes: 17 additions & 0 deletions tests/test_cli_and_mcp.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import asyncio
import json

import pytest

from gulliver.cli import main
from gulliver.mcp_server import build_server, export_tools

Expand Down Expand Up @@ -97,3 +99,18 @@ def test_cli_translate_output_file(tmp_path, capsys):
data = json.loads(capsys.readouterr().out)
assert data["warnings"] # still printed
assert out.read_text(encoding="utf-8") == data["sql"]

@pytest.mark.parametrize("extra, message", [
(["--min-rows", "-5"], "min_rows"),
(["--max-change", "-5"], "max_change"),
(["--max-change", "nan"], "max_change"),
(["--max-change", "inf"], "max_change"),
])
def test_check_refuses_invalid_thresholds(demo_db, capsys, extra, message):
code = main(["--dsn", demo_db, "--json", "check", "events", "--not-null", "event_id", *extra])
assert code == 1
assert message in capsys.readouterr().out


def test_check_accepts_zero_min_rows(demo_db):
assert main(["--dsn", demo_db, "--json", "check", "events", "--not-null", "event_id", "--min-rows", "0"]) == 0