PyLimeRPC: package pylimerpc + pyproject + tests/examples (overname van minimalLS)
- nieuwe package `pylimerpc/` (rpc, session, surveys, tokens, responses, errors, utils, config) - pyproject.toml (name PyLimeRPC, versie 0.1.0) - tests/ (69 passed, 2 deselected) en examples/ - .env.example: TLS-verificatie documentatie (LIMESURVEY_VERIFY_SSL) - README/.gitignore bijgewerkt Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
This commit is contained in:
parent
30fc89e4e2
commit
d7d9dc61bf
1 changed files with 2853 additions and 14 deletions
129
tests/test_examples.py
Normal file
129
tests/test_examples.py
Normal file
|
|
@ -0,0 +1,129 @@
|
|||
"""Tests for the example scripts: ASGI app and CLI with a fake LsClient/LsConfig."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from pylimerpc.errors import LsError
|
||||
|
||||
|
||||
class FakeLs:
|
||||
"""Duck-typed replacement for LsClient with canned data."""
|
||||
|
||||
surveys = [
|
||||
{"sid": 1, "surveyls_title": "Test <b>", "active": "Y"},
|
||||
{"sid": 2, "surveyls_title": "Inactief", "active": "N"},
|
||||
]
|
||||
|
||||
def __init__(self, cfg: Any) -> None:
|
||||
pass
|
||||
|
||||
def __enter__(self) -> FakeLs:
|
||||
return self
|
||||
|
||||
def __exit__(self, *args: Any) -> None:
|
||||
return None
|
||||
|
||||
def list_surveys(self) -> list[dict[str, Any]]:
|
||||
return list(self.surveys)
|
||||
|
||||
def export_responses(self, sid: Any, doc_type: str = "csv", output_path: Any = None) -> Path:
|
||||
out = Path(output_path) if output_path else Path("output")
|
||||
if out.is_dir():
|
||||
out = out / f"survey_{sid}_responses.{doc_type}"
|
||||
out.parent.mkdir(parents=True, exist_ok=True)
|
||||
out.write_bytes(b"fake-export")
|
||||
return out
|
||||
|
||||
def list_participants(self, sid: Any, **kwargs: Any) -> list[dict[str, Any]]:
|
||||
return [{"tid": "1", "email": "a@b.c", "token": "tok", "completed": "N"}]
|
||||
|
||||
|
||||
class FakeCfg:
|
||||
@classmethod
|
||||
def from_dotenv(cls, path: Any) -> FakeCfg:
|
||||
return cls()
|
||||
|
||||
|
||||
def _patch(monkeypatch, module) -> None:
|
||||
monkeypatch.setattr(module, "LsClient", FakeLs)
|
||||
monkeypatch.setattr(module, "LsConfig", FakeCfg)
|
||||
|
||||
|
||||
def _run_asgi(app, path: str = "/") -> tuple[int, bytes]:
|
||||
sent: list[dict[str, Any]] = []
|
||||
|
||||
async def receive() -> dict[str, Any]:
|
||||
return {"type": "http.request", "body": b"", "more_body": False}
|
||||
|
||||
async def send(msg: dict[str, Any]) -> None:
|
||||
sent.append(msg)
|
||||
|
||||
asyncio.run(app({"type": "http", "method": "GET", "path": path, "headers": []}, receive, send))
|
||||
status = sent[0]["status"]
|
||||
body = b"".join(m.get("body", b"") for m in sent if m["type"] == "http.response.body")
|
||||
return status, body
|
||||
|
||||
|
||||
# -- ASGI -------------------------------------------------------------------
|
||||
|
||||
|
||||
def test_asgi_index_toont_alleen_actieve_surveys(monkeypatch):
|
||||
import examples.example_asgi as ex
|
||||
|
||||
_patch(monkeypatch, ex)
|
||||
status, body = _run_asgi(ex.app, "/")
|
||||
assert status == 200
|
||||
assert b"Test <b>" in body # HTML-ge-escaped
|
||||
assert b"Inactief" not in body # inactive survey filtered out
|
||||
assert b"<td>1</td>" in body
|
||||
|
||||
|
||||
def test_asgi_health_en_404(monkeypatch):
|
||||
import examples.example_asgi as ex
|
||||
|
||||
_patch(monkeypatch, ex)
|
||||
assert _run_asgi(ex.app, "/health") == (200, b"ok")
|
||||
status, _ = _run_asgi(ex.app, "/anders")
|
||||
assert status == 404
|
||||
|
||||
|
||||
def test_asgi_502_bij_ls_fout(monkeypatch):
|
||||
import examples.example_asgi as ex
|
||||
|
||||
class BrokenLs(FakeLs):
|
||||
def list_surveys(self):
|
||||
raise LsError("stuk")
|
||||
|
||||
monkeypatch.setattr(ex, "LsClient", BrokenLs)
|
||||
monkeypatch.setattr(ex, "LsConfig", FakeCfg)
|
||||
status, body = _run_asgi(ex.app, "/")
|
||||
assert status == 502
|
||||
assert b"stuk" in body
|
||||
|
||||
|
||||
# -- CLI ---------------------------------------------------------------------
|
||||
|
||||
|
||||
def test_cli_export_tokens_surveys(monkeypatch, tmp_path, capsys):
|
||||
import examples.example_cli as cli
|
||||
|
||||
_patch(monkeypatch, cli)
|
||||
|
||||
monkeypatch.setattr(sys, "argv", ["example_cli.py", "export", "--sid", "5", "--out", str(tmp_path)])
|
||||
assert cli.main() == 0
|
||||
assert (tmp_path / "survey_5_responses.csv").read_bytes() == b"fake-export"
|
||||
|
||||
monkeypatch.setattr(sys, "argv", ["example_cli.py", "tokens-lijst", "--sid", "5"])
|
||||
assert cli.main() == 0
|
||||
out = capsys.readouterr().out
|
||||
assert "a@b.c" in out and "tok" in out
|
||||
|
||||
monkeypatch.setattr(sys, "argv", ["example_cli.py", "surveys"])
|
||||
assert cli.main() == 0
|
||||
out = capsys.readouterr().out
|
||||
assert "Test <b>" in out
|
||||
assert "Inactief" not in out # active surveys only
|
||||
Loading…
Reference in a new issue