# retoor <retoor@molodetz.nl>

from tests.conftest import run_async

from devplacepy.services.dbapi.service import DbApiJobService


def test_process_runs_select_and_writes_result(local_db):
    svc = DbApiJobService()
    job = {"uid": "unit-dbquery-1", "payload": {"sql": "SELECT 1 AS n"}}
    try:
        result = run_async(svc.process(job))
        assert result["row_count"] == 1
        assert result["truncated"] is False
        assert result["result_url"].endswith("/result")
        assert (svc.result_dir("unit-dbquery-1") / "result.json").is_file()
    finally:
        svc.cleanup(job)
        assert not svc.result_dir("unit-dbquery-1").exists()


def test_process_rejects_non_select(local_db):
    svc = DbApiJobService()
    job = {"uid": "unit-dbquery-2", "payload": {"sql": "DELETE FROM posts"}}
    raised = False
    try:
        run_async(svc.process(job))
    except Exception:
        raised = True
    finally:
        svc.cleanup(job)
    assert raised
