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:
Tammo ter Hark 2026-11-19 12:26:35 +01:00
commit d7d9dc61bf

View file

@ -0,0 +1,167 @@
"""Unit tests for responses.py and tokens.py (FakeRpc, no server) + LsClient wiring."""
from __future__ import annotations
import base64
import json
from pathlib import Path
from typing import Any
import pytest
from pylimerpc import LsClient, LsConfig
from pylimerpc.errors import LsApiError, LsAuthError
from pylimerpc.responses import export_responses, export_responses_json, export_statistics
from pylimerpc.tokens import add_participants, delete_participants, list_participants
class FakeRpc:
def __init__(self, *responses: Any) -> None:
self.responses = list(responses)
self.calls: list[tuple[str, list[Any]]] = []
def call(self, method: str, params: list[Any]) -> Any:
self.calls.append((method, list(params)))
item = self.responses.pop(0)
if isinstance(item, BaseException):
raise item
return item
CFG = LsConfig(base_url="https://ls.example.com/index.php?r=admin/remotecontrol", username="u", password="p")
def b64(payload: bytes) -> str:
return base64.b64encode(payload).decode("ascii")
# -- responses --------------------------------------------------------------
def test_export_responses_params_en_base64_decode():
raw = json.dumps({"responses": [{"id": "1"}]}).encode("utf-8")
fake = FakeRpc(b64(raw))
data = export_responses(fake, "KEY", "123") # type: ignore[arg-type]
assert data == raw
# LS7 export_responses: [session_key, sid, doc_type, language, completion, field_map, header]
assert fake.calls == [("export_responses", ["KEY", 123, "json", None, "all", "code", "short"])]
def test_export_responses_doc_type_csv_en_output_path(tmp_path):
fake = FakeRpc(b64(b"a,b\n1,2\n"))
out = export_responses(fake, "KEY", 5, doc_type="csv", output_path=tmp_path / "export.csv") # type: ignore[arg-type]
assert isinstance(out, Path)
assert out.read_bytes() == b"a,b\n1,2\n"
assert fake.calls[0][1][2] == "csv"
def test_export_responses_output_path_is_directory(tmp_path):
fake = FakeRpc(b64(b"{}"))
out = export_responses(fake, "KEY", 9, doc_type="json", output_path=tmp_path) # type: ignore[arg-type]
assert out == tmp_path / "survey_9_responses.json"
assert out.exists()
def test_export_responses_json_wrapper():
payload = {"responses": [{"id": "1", "Q1": "ja"}, {"id": "2", "Q1": "nee"}]}
fake = FakeRpc(b64(json.dumps(payload).encode("utf-8")))
rows = export_responses_json(fake, "KEY", 5) # type: ignore[arg-type]
assert rows == payload["responses"]
def test_export_responses_status_fout_is_lsautherror():
fake = FakeRpc({"status": "Invalid session key"})
with pytest.raises(LsAuthError):
export_responses(fake, "OUDE-KEY", 5) # type: ignore[arg-type]
def test_export_responses_leeg_resultaat_is_lsapierror():
fake = FakeRpc("")
with pytest.raises(LsApiError):
export_responses(fake, "KEY", 5) # type: ignore[arg-type]
def test_export_statistics_params_en_decode():
raw = json.dumps({"statistics": []}).encode("utf-8")
fake = FakeRpc(b64(raw))
data = export_statistics(fake, "KEY", 7, doc_type="json") # type: ignore[arg-type]
assert data == raw
# LS7 export_statistics: [session_key, sid, doc_type, language]
assert fake.calls == [("export_statistics", ["KEY", 7, "json", None])]
# -- tokens -----------------------------------------------------------------
def test_add_participants_params():
participants = [{"email": "a@b.c", "firstname": "A", "lastname": "B"}]
fake = FakeRpc([{"tid": "1", "token": "tok123", "email": "a@b.c"}])
out = add_participants(fake, "KEY", 5, participants) # type: ignore[arg-type]
assert out[0]["token"] == "tok123"
assert fake.calls == [("add_participants", ["KEY", 5, participants])]
def test_add_participants_create_token_param_en_typecheck():
fake = FakeRpc([])
add_participants(fake, "KEY", 5, [], create_token=False) # type: ignore[arg-type]
assert fake.calls == [("add_participants", ["KEY", 5, [], False])]
with pytest.raises(TypeError):
add_participants(fake, "KEY", 5, "not-a-list") # type: ignore[arg-type]
def test_list_participants_normaliseert_en_params():
fake = FakeRpc({"participants": [{"tid": "1", "email": "a@b.c"}]})
out = list_participants(fake, "KEY", 5) # type: ignore[arg-type]
assert out == [{"tid": "1", "email": "a@b.c"}]
# LS7 list_participants: [session_key, sid, start, limit] (+ conditions, language)
assert fake.calls == [("list_participants", ["KEY", 5, 0, 100000])]
fake2 = FakeRpc([{"tid": "2"}])
out2 = list_participants(fake2, "KEY", 5, start=10, limit=50, conditions={"email": "x"}, language="nl") # type: ignore[arg-type]
assert out2 == [{"tid": "2"}]
assert fake2.calls == [("list_participants", ["KEY", 5, 10, 50, {"email": "x"}, "nl"])]
def test_delete_participants_params_en_coercitie():
fake = FakeRpc({"1": "OK", "2": "OK"})
out = delete_participants(fake, "KEY", 5, [1, "2"]) # type: ignore[arg-type]
assert out == {"1": "OK", "2": "OK"}
# LS7 delete_participants: [session_key, sid, [token_ids]]
assert fake.calls == [("delete_participants", ["KEY", 5, [1, 2]])]
with pytest.raises(TypeError):
delete_participants(fake, "KEY", 5, "not-a-list") # type: ignore[arg-type]
# -- LsClient wiring ----------------------------------------------------------
def test_lsclient_responses_en_tokens_delegeren_met_session_key():
raw = b64(json.dumps({"responses": []}).encode("utf-8"))
fake = FakeRpc(
"KEY", # get_session_key
raw, # export_responses
raw, # export_statistics
[{"tid": "1"}], # add_participants
{"participants": [{"tid": "1"}]}, # list_participants
{"1": "OK"}, # delete_participants
True, # release_session_key
)
with LsClient(CFG, rpc=fake) as ls: # type: ignore[arg-type]
assert isinstance(ls.export_responses(5), bytes)
assert isinstance(ls.export_statistics(5), bytes)
ls.add_participants(5, [{"email": "a@b.c"}])
assert ls.list_participants(5) == [{"tid": "1"}]
assert ls.delete_participants(5, [1]) == {"1": "OK"}
methods = [c[0] for c in fake.calls]
assert methods == [
"get_session_key",
"export_responses",
"export_statistics",
"add_participants",
"list_participants",
"delete_participants",
"release_session_key",
]
# session key is in every call
assert fake.calls[1][1][0] == "KEY"
assert fake.calls[3][1][1] == 5