PyLimeRPC/tests/test_utils.py

118 lines
3.9 KiB
Python
Raw Permalink Normal View History

"""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,
summary_counts_full,
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_summary_counts_full_basis():
summary = {"completed_responses": 14, "incomplete_responses": 41, "full_responses": 55}
assert summary_counts_full(summary) == (14, 41, 55)
def test_summary_counts_full_fallback_keys_and_total_sum():
# without full_responses → total = completed + incomplete
assert summary_counts_full({"responses_completed": "3", "in_progress_responses": 2}) == (3, 2, 5)
assert summary_counts_full({"completed": 1}) == (1, 0, 1)
# full_responses wins when present
assert summary_counts_full({"completed_responses": 2, "incomplete_responses": 3, "full_responses": 99}) == (2, 3, 99)
def test_summary_counts_full_missing_or_invalid_is_zero():
assert summary_counts_full({}) == (0, 0, 0)
assert summary_counts_full({"completed_responses": "abc", "incomplete_responses": None}) == (0, 0, 0)
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}]