Skip to content

monte-carlo-data/mc-agent-toolkit / skills/prevent

monte-carlo-prevent

Verified

Shift-left safety net for dbt/SQL model edits. Runs change impact assessment before edits, generates SQL validation queries after, and executes them via `/mc-validate run`. Delegates health and monitor creation to peer skills.

scripts/sandbox/tests/test_readonly_check.py

View on GitHub
"""Tests for readonly_check.py."""
import json
import subprocess
import sys
from pathlib import Path


SCRIPT = Path(__file__).resolve().parents[1] / "readonly_check.py"


def _run(path: Path) -> tuple[int, dict]:
    out = subprocess.run(
        [sys.executable, str(SCRIPT), str(path)],
        capture_output=True, text=True,
    )
    return out.returncode, json.loads(out.stdout or "{}")


OK_SINGLE_SELECT = "SELECT 1\n"
OK_WITH_CTE = "WITH a AS (SELECT 1) SELECT * FROM a;\n"
OK_SHOW = "SHOW TABLES IN SCHEMA prod;\n"
OK_COMMENT_THEN_SELECT = "-- comment\n/* block */\nSELECT 1;\n"

BAD_INSERT = "INSERT INTO t VALUES (1);\n"
BAD_UPDATE = "-- lead comment\nUPDATE t SET x = 1;\n"
BAD_MERGE = "merge into t using s on s.id = t.id when matched then update set x = 1;"
BAD_CREATE = "CREATE TABLE x (id INT);\n"
BAD_DROP = "DROP TABLE x;\n"
BAD_CALL = "CALL sp_do_thing();\n"
BAD_USE = "USE DATABASE raw;\nSELECT 1;\n"
OK_MULTI_SELECT = "SELECT 1;\nSELECT 2;\nSELECT 3;\n"
MIXED_WRITE_IN_MULTI = "SELECT 1;\nDROP TABLE x;\nSELECT 2;\n"


def test_ok_select(tmp_path):
    f = tmp_path / "q.sql"
    f.write_text(OK_SINGLE_SELECT)
    code, data = _run(f)
    assert code == 0
    assert data == {"ok": True, "rejected": None}


def test_ok_with_cte(tmp_path):
    f = tmp_path / "q.sql"
    f.write_text(OK_WITH_CTE)
    assert _run(f) == (0, {"ok": True, "rejected": None})


def test_ok_show(tmp_path):
    f = tmp_path / "q.sql"
    f.write_text(OK_SHOW)
    assert _run(f) == (0, {"ok": True, "rejected": None})


def test_ok_comment_then_select(tmp_path):
    f = tmp_path / "q.sql"
    f.write_text(OK_COMMENT_THEN_SELECT)
    assert _run(f) == (0, {"ok": True, "rejected": None})


def test_rejects_insert(tmp_path):
    f = tmp_path / "q.sql"
    f.write_text(BAD_INSERT)
    code, data = _run(f)
    assert code == 1
    assert data["ok"] is False
    assert data["rejected"] == "INSERT"


def test_rejects_update(tmp_path):
    f = tmp_path / "q.sql"
    f.write_text(BAD_UPDATE)
    assert _run(f)[1]["rejected"] == "UPDATE"


def test_rejects_merge_lowercase(tmp_path):
    f = tmp_path / "q.sql"
    f.write_text(BAD_MERGE)
    assert _run(f)[1]["rejected"] == "MERGE"


def test_rejects_create(tmp_path):
    f = tmp_path / "q.sql"
    f.write_text(BAD_CREATE)
    assert _run(f)[1]["rejected"] == "CREATE"


def test_rejects_drop(tmp_path):
    f = tmp_path / "q.sql"
    f.write_text(BAD_DROP)
    assert _run(f)[1]["rejected"] == "DROP"


def test_rejects_call(tmp_path):
    f = tmp_path / "q.sql"
    f.write_text(BAD_CALL)
    assert _run(f)[1]["rejected"] == "CALL"


def test_rejects_use(tmp_path):
    f = tmp_path / "q.sql"
    f.write_text(BAD_USE)
    assert _run(f)[1]["rejected"] == "USE"


def test_accepts_multi_select(tmp_path):
    """Multiple SELECT statements in one file are fine — Workflow 4.2 output shape."""
    f = tmp_path / "q.sql"
    f.write_text(OK_MULTI_SELECT)
    code, data = _run(f)
    assert code == 0
    assert data == {"ok": True, "rejected": None}


def test_rejects_write_among_multi_statement(tmp_path):
    """A write statement anywhere in the file still gets rejected, even if
    other statements are pure reads."""
    f = tmp_path / "q.sql"
    f.write_text(MIXED_WRITE_IN_MULTI)
    code, data = _run(f)
    assert code == 1
    assert data["rejected"] == "DROP"


def test_rejects_get_stage(tmp_path):
    f = tmp_path / "q.sql"
    f.write_text("GET @stage FILE 'out.csv';\n")
    assert _run(f)[1]["rejected"] == "GET"


def test_rejects_put(tmp_path):
    f = tmp_path / "q.sql"
    f.write_text("PUT file:///tmp/x.csv @stage;\n")
    assert _run(f)[1]["rejected"] == "PUT"


def test_rejects_unload(tmp_path):
    f = tmp_path / "q.sql"
    f.write_text("UNLOAD ('SELECT 1') TO '@stage';\n")
    assert _run(f)[1]["rejected"] == "UNLOAD"


def test_rejects_set_session_var(tmp_path):
    f = tmp_path / "q.sql"
    f.write_text("SET v = 1;\nSELECT 1;\n")
    assert _run(f)[1]["rejected"] == "SET"
deprecation-and-migrationaddyosmani/agent-skillsManages deprecation and migration. Use when removing old systems, APIs, or features. Use when migrating users from one implementation to another. Use when migrating a database schema in production, such as renaming or dropping a column without downtime (expand/contract). Use when deciding whether to maintain or sunset existing code.Stars: 119kUpdated Claude CodeCodexVerifiedInstall
performance-optimizationaddyosmani/agent-skillsOptimizes application performance across frontend, backend, queries, and databases. Use when performance requirements exist, when you suspect performance regressions, when Core Web Vitals or load times need improvement, when N+1 query patterns need fixing, or when profiling reveals bottlenecks.Stars: 119kUpdated Claude CodeCodexVerifiedInstall
agent-cost-reportthedotmack/claude-memBelievable agent cost report for any period, default the last 7 full days PT, not counting today. Measured tokens from Claude Code transcripts priced at OpenRouter list prices (ESTIMATED), measured provider spend when a sanctioned source exists, note-taker cost separate, Timing-style HTML/PDF plus report.json, line-items.csv, evidence.json.Stars: 118kUpdated Claude CodeCodexVerifiedInstall
agent-cost-reportthedotmack/claude-memBelievable agent cost report for any period, default the last 7 full days PT, not counting today. Measured tokens from Claude Code transcripts priced at OpenRouter list prices (ESTIMATED), measured provider spend when a sanctioned source exists, note-taker cost separate, Timing-style HTML/PDF plus report.json, line-items.csv, evidence.json.Stars: 118kUpdated Claude CodeCodexVerifiedInstall