PyLimeRPC/tests/test_utils.py
Tammo ter Hark d7d9dc61bf 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>
2026-11-19 12:26:35 +01:00

99 lines
3.1 KiB
Python

"""Unit tests for pylimerpc.utils (pytest)."""
from __future__ import annotations
import base64
import json
import pytest
from pylimerpc.errors import LsApiError, LsAuthError
from pylimerpc.utils import (
decode_export_json,
is_active_flag,
is_filled,
normalize_sid,
parse_responses_json,
raise_for_status,
summary_counts,
to_int,
)
def test_to_int():
assert to_int("42") == 42
assert to_int(7) == 7
assert to_int("") is None
assert to_int(None) is None
assert to_int("abc") is None
def test_is_active_flag():
assert is_active_flag("Y") is True
assert is_active_flag("1") is True
assert is_active_flag("true") is True
assert is_active_flag("N") is False
assert is_active_flag(None) is False
def test_is_filled():
assert is_filled("x") is True
assert is_filled("") is False
assert is_filled(" ") is False
assert is_filled(None) is False
assert is_filled("[]") is False
assert is_filled("[]x") is True
def test_normalize_sid():
assert normalize_sid("123") == 123
assert normalize_sid(5) == 5
assert normalize_sid("abc") == "abc"
def test_parse_responses_json_varianten():
assert parse_responses_json('{"responses": [{"id": "1"}]}') == [{"id": "1"}]
assert parse_responses_json('[{"id": "2"}]') == [{"id": "2"}]
assert parse_responses_json('{"id": "3"}') == [{"id": "3"}]
def test_decode_export_json_base64():
payload = json.dumps({"responses": [{"id": "1", "Q1": "a"}]}).encode("utf-8")
b64 = base64.b64encode(payload).decode("ascii")
assert decode_export_json(b64) == [{"id": "1", "Q1": "a"}]
assert decode_export_json("") == []
assert decode_export_json(None) == []
def test_summary_counts_key_varianten():
assert summary_counts({"completed_responses": "3", "responses": "10"}) == (3, 10)
assert summary_counts({"completed": 1, "full_responses": 5}) == (1, 5)
assert summary_counts({}) == (None, None)
def test_raise_for_status_auth():
with pytest.raises(LsAuthError):
raise_for_status({"status": "Invalid session key"}, "list_surveys")
def test_raise_for_status_api():
with pytest.raises(LsApiError) as excinfo:
raise_for_status({"status": "Survey not found"}, "get_survey_properties")
assert not isinstance(excinfo.value, LsAuthError)
def test_raise_for_status_met_error_code():
# LS returns e.g. {"status": "Error: No survey participant list", "error_code": "ERR_NO_PARTICIPANT_TABLE"}
with pytest.raises(LsApiError) as excinfo:
raise_for_status(
{"status": "Error: No survey participant list", "error_code": "ERR_NO_PARTICIPANT_TABLE"},
"list_participants",
)
assert not isinstance(excinfo.value, LsAuthError)
assert "No survey participant list" in str(excinfo.value.rpc_message)
def test_raise_for_status_passthrough():
props = {"sid": 1, "status": "Y", "surveyls_title": "X"} # status-key tussen andere keys = géén fout
assert raise_for_status(props, "get_survey_properties") == props
assert raise_for_status([{"sid": 1}], "list_surveys") == [{"sid": 1}]