2026-06-14 23:00:30 +00:00
|
|
|
# retoor <retoor@molodetz.nl>
|
|
|
|
|
|
2026-11-05 17:59:21 +01:00
|
|
|
import asyncio
|
|
|
|
|
|
2026-06-14 23:00:30 +00:00
|
|
|
from tests.conftest import run_async
|
|
|
|
|
|
2026-11-05 17:59:21 +01:00
|
|
|
from devplacepy.services.dbapi.progress import hub
|
2026-06-14 23:00:30 +00:00
|
|
|
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
|
2026-11-05 17:59:21 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|