# retoor import asyncio from tests.conftest import run_async from devplacepy.services.dbapi.progress import hub 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 def test_stream_publishes_progress_to_subscriber(local_db): svc = DbApiJobService() uid = "unit-dbquery-progress" queue = hub.subscribe(uid) try: rows, truncated = run_async(svc._stream(uid, "SELECT 1 AS n", 5000)) assert rows == [{"n": 1}] assert truncated is False frames = [] while True: try: frames.append(queue.get_nowait()) except asyncio.QueueEmpty: break assert any(frame.get("type") == "progress" for frame in frames) finally: hub.unsubscribe(uid, queue) hub.clear(uid)