GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — xmlvalid-python/tests/conftest.py at main · xmlvalid/xmlvalid-python Expand file tree/ Copy pathconftest.pyMore file actions 121 lines (96 loc) · 3.84 KB / Copy pathconftest.pyFile metadata and controls121 lines (96 loc) · 3.84 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 from __future__ import annotations import io import json from collections.abc import Callable from pathlib import Path from typing import Any import httpx import pytest FIXTURES = Path(__file__).parent / "fixtures" def validation(status: str = "done", valid: bool = True, errors: list[dict[str, Any]] | None = None) -> dict[str, Any]: """A validation as the API returns it.""" errors = errors or [] finished = status not in ("queued", "running") return { "id": "val_1", "status": status, "progress": 100 if finished else 0, "created_at": "2026-10-18T10:00:00+00:00", "finished_at": "2026-10-18T10:00:01+00:00" if finished else None, "document": {"source": "upload", "name": "invoice.xml", "size": 12}, "schema": {"kind": "xsd", "catalog": "peppol-bis-3-invoice", "name": "Peppol BIS Billing 3.0 Invoice"}, "result": { "valid": valid, "outcome": "valid" if valid else "invalid", "errors_total": len(errors), "warnings_total": 0, "errors_truncated": False, "errors": errors, "warnings": [], "bytes": 12, "duration_ms": 5, } if finished else None, "error": None, "links": {"report": "https://xmlvalid.com/api/v1/validations/val_1/report"}, } class Api: """Answers requests from a queue and keeps them, bodies read, for assertions.""" def __init__(self) -> None: self.queue: list[httpx.Response | Exception] = [] self.requests: list[httpx.Request] = [] self.bodies: list[bytes] = [] def json(self, status: int, body: Any, headers: dict[str, str] | None = None) -> Api: self.queue.append( httpx.Response( status, content=json.dumps(body).encode(), headers={"Content-Type": "application/json", **(headers or {})}, ) ) return self def raw(self, status: int, body: str, headers: dict[str, str] | None = None) -> Api: self.queue.append(httpx.Response(status, text=body, headers=headers)) return self def fail(self, error: Exception) -> Api: self.queue.append(error) return self def _answer(self, request: httpx.Request) -> httpx.Response: self.requests.append(request) if not self.queue: raise AssertionError(f"Unexpected request {request.method} {request.url}") answer = self.queue.pop(0) if isinstance(answer, Exception): raise answer return answer def handler(self, request: httpx.Request) -> httpx.Response: self.bodies.append(request.read()) return self._answer(request) async def async_handler(self, request: httpx.Request) -> httpx.Response: self.bodies.append(await request.aread()) return self._answer(request) @pytest.fixture def api() -> Api: return Api() @pytest.fixture def sleeps(monkeypatch: pytest.MonkeyPatch) -> list[float]: """Records the client's sleeps instead of sleeping.""" slept: list[float] = [] async def asleep(seconds: float) -> None: slept.append(seconds) record: Callable[[float], None] = slept.append monkeypatch.setattr("xmlvalid._client.time.sleep", record) monkeypatch.setattr("xmlvalid._client.anyio.sleep", asleep) return slept class Reads(io.BytesIO): """Remembers the sizes it was read with: a streamed upload never reads all at once.""" def __init__(self, data: bytes) -> None: super().__init__(data) self.sizes: list[int | None] = [] def read(self, size: int | None = -1, /) -> bytes: self.sizes.append(size) return super().read(size) @property def streamed(self) -> bool: return bool(self.sizes) and all(size is not None and size > 0 for size in self.sizes) |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |