PyLimeRPC/tests/test_integration.py
Tammo ter Hark 6822eab467 PyLimeRPC 0.2.0 — prompts.md Prompt 1-4
- responses.py: get_summary() (raw RPC get_summary), count_responses() ->
  frozen ResponseCounts(completed, incomplete, total), export_responses_auto()
  with automatic format fallback (xlsx -> xls -> csv) on IWriter errors
- utils.py: summary_counts_full() -> (completed, incomplete, total) with
  per-LS-version key fallbacks; summary_counts left untouched
- surveys.py: get_survey_by_sid() (dict | None), get_survey_info() ->
  frozen SurveyInfo(sid, title, active)
- tokens.py: documented that extra/custom token attributes (attribute_1, ...)
  pass through unchanged in add_participants/list_participants
- LsClient: get_summary, count_responses, get_survey_by_sid, get_survey_info,
  export_responses_auto; __all__ + re-exports updated
- README: new "Response counts & summary" section + usage examples
- .gitignore: prompts.md added (local-only docs, like werkdocument.md)
- version 0.1.0 -> 0.2.0 (__init__.py + pyproject.toml)
- tests: +27 FakeRpc unit tests (96 passed, 2 deselected)

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
2026-11-20 11:56:03 +01:00

78 lines
3.3 KiB
Python

"""Integration tests against a real (local) LimeSurvey instance.
Skipped by default (pyproject: addopts = -m "not integration").
Run them manually with the local endpoint via environment variables:
LIMESURVEY_BASE_URL='https://localhost/limesurvey/index.php?r=admin/remotecontrol' \
LIMESURVEY_USERNAME='...' LIMESURVEY_PASSWORD='...' \
.venv/bin/python -m pytest -m integration -v
Without those variables the tests are skipped.
Optional: LIMESURVEY_VERIFY_SSL=false for a test instance with a wrong certificate.
"""
from __future__ import annotations
import os
import pytest
from pylimerpc import LsClient, LsConfig
from pylimerpc.errors import LsApiError
from pylimerpc.utils import parse_responses_json
pytestmark = pytest.mark.integration
def _config_from_env() -> LsConfig:
base_url = os.environ.get("LIMESURVEY_BASE_URL", "").strip()
username = os.environ.get("LIMESURVEY_USERNAME", "").strip()
password = os.environ.get("LIMESURVEY_PASSWORD", "").strip()
if not (base_url and username and password):
pytest.skip(
"Set LIMESURVEY_BASE_URL, LIMESURVEY_USERNAME and LIMESURVEY_PASSWORD "
"(local LimeSurvey endpoint) to run the integration tests"
)
timeout = int(os.environ.get("LIMESURVEY_TIMEOUT_SECONDS", "30") or 30)
verify_ssl = os.environ.get("LIMESURVEY_VERIFY_SSL", "true").strip().lower() not in ("0", "false", "no", "off")
return LsConfig(base_url=base_url, username=username, password=password, timeout=timeout, verify_ssl=verify_ssl)
def test_sessie_openen_list_surveys_eigenschappen_en_sluiten():
cfg = _config_from_env()
with LsClient(cfg) as ls:
assert ls.session_key, "no session key received"
surveys = ls.list_surveys()
assert isinstance(surveys, list)
print(f"\n{len(surveys)} surveys gevonden:")
for s in surveys[:10]:
print(f" sid={s.get('sid')} | active={s.get('active')} | {s.get('surveyls_title')}")
if surveys:
sid = surveys[0].get("sid")
props = ls.get_survey_properties(sid)
assert isinstance(props, dict)
assert props.get("sid") in (sid, str(sid), int(str(sid)) if str(sid).isdigit() else sid)
print(f" properties of sid={sid}: {sorted(props)[:10]} ...")
# context manager released the session cleanly
assert ls.session_key is None
def test_export_responses_en_statistieken_readonly():
"""Read-only export against the real instance (no data modified)."""
cfg = _config_from_env()
with LsClient(cfg) as ls:
surveys = ls.list_surveys()
if not surveys:
pytest.skip("no surveys on this instance")
sid = surveys[0]["sid"]
data = ls.export_responses(sid, doc_type="json")
assert isinstance(data, bytes)
rows = parse_responses_json(data.decode("utf-8")) if data else []
print(f"\nexport_responses sid={sid}: {len(rows)} response-rows, {len(data)} bytes")
try:
stats = ls.export_statistics(sid, doc_type="json")
except LsApiError as exc:
# server may return an empty statistics export (statistics off / no permission)
pytest.skip(f"export_statistics returned an empty export on this instance: {exc}")
assert isinstance(stats, bytes)
print(f"export_statistics sid={sid}: {len(stats)} bytes")