diff --git a/src/gulliver/checks.py b/src/gulliver/checks.py index f812769..d17f514 100644 --- a/src/gulliver/checks.py +++ b/src/gulliver/checks.py @@ -304,6 +304,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()]) diff --git a/tests/test_checks.py b/tests/test_checks.py index d041b00..243132c 100644 --- a/tests/test_checks.py +++ b/tests/test_checks.py @@ -7,12 +7,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'"): diff --git a/tests/test_cli_and_mcp.py b/tests/test_cli_and_mcp.py index 2c17334..c34cdf4 100644 --- a/tests/test_cli_and_mcp.py +++ b/tests/test_cli_and_mcp.py @@ -165,6 +165,24 @@ def test_cli_translate_output_file(tmp_path, capsys): 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 + + + @pytest.mark.parametrize("command", ["lint", "preflight", "rehearse", "run"]) @pytest.mark.parametrize("sql", ["SELECT 1 FROM", "SELECT a b c FROM t", "SELECT 'abc"]) def test_cli_syntax_errors_stay_blocked_with_actionable_advice(command, sql, demo_db, state, capsys): @@ -194,6 +212,7 @@ def test_cli_non_syntax_blocks_keep_existing_advice(sql, demo_db, state, capsys) ) + def test_mcp_check_by_name_and_status_checks(demo_db, state, tmp_path, monkeypatch): db_path = demo_db.replace("\\", "/") monkeypatch.chdir(tmp_path)