Testing¶
liquipydia.testing lets you test code that uses liquipydia without network access or an API key.
FakeLpdb is an in-memory LPDB: it serves the records you give it to a real
LiquipediaClient, whose network layer only is replaced. Keyword filters, query
validation, pagination, retries, error mapping and model parsing therefore behave exactly as in production.
from liquipydia import Match
from liquipydia.testing import FakeLpdb
def latest_matches(client):
return [Match.model_validate(r) for r in client.matches.paginate("rocketleague", page_size=2)]
def test_latest_matches():
fake = FakeLpdb({"match": [{"match2id": "m1"}, {"match2id": "m2"}, {"match2id": "m3"}]})
with fake.client() as client:
matches = latest_matches(client)
assert [m.match2id for m in matches] == ["m1", "m2", "m3"]
assert [r.params["offset"] for r in fake.requests] == ["0", "2", "4"]
What the fake reproduces¶
Records per endpoint, keyed by LPDB endpoint name (
"match","player","teamtemplate"…); an endpoint without records answers an empty page.Paging: results are sliced by
limit/offset, sopaginate()walks the pages.Field selection: with
query, records keep only the named fields (case-insensitive) pluswiki.Failures:
fail()makes an endpoint answer an HTTP error or time out, optionally for a limited number of requests, so the client raises its real errors (RateLimitError,NetworkError,NotFoundError…), after its retries.A request log:
requestslists every request with its endpoint and parameters, so tests can check what was asked.
It does not evaluate conditions or keyword filters (every record of the endpoint is returned; check the
request log instead), nor compute query functions such as sum::prizemoney (those items are left out).
Reference¶
- class liquipydia.testing.FakeLpdb¶
Bases:
objectAn in-memory LPDB serving canned records to real
LiquipediaClientinstances.Records are keyed by LPDB endpoint name (
"match","player","teamtemplate"…). Each request returns the endpoint’s records, sliced byoffset/limitso pagination works, and reduced to the fields named inquerywhen given.conditionsand keyword filters are not evaluated: every record of the endpoint is returned, andrequestsshows what was asked.queryfunctions (sum::prizemoney) are not computed either; such items are left out of the returned records.- Parameters:
records (
Mapping[str,Iterable[dict[str,Any]]] |None(default:None)) – Initial records per endpoint name.- Raises:
ValueError – If an endpoint name is unknown.
- __init__(records=None)¶
Create the fake, optionally with initial records per endpoint name.
- add(endpoint, *records)¶
Add records served by an endpoint, after any it already serves.
- fail(endpoint, *, status=None, timeout=False, times=None)¶
Make an endpoint fail, with an HTTP status or a timeout.
The client then raises what it raises against the real API:
RateLimitErrorfor429(after its retries),AuthErrorfor403,NotFoundErrorfor404,LiquipediaErrorfor other statuses, andNetworkErrorfor a timeout (after its retries).- Parameters:
endpoint (
str) – LPDB endpoint name.status (
int|None(default:None)) – HTTP status code to answer with (>= 400).timeout (
bool(default:False)) – Make requests time out instead.times (
int|None(default:None)) – Fail only the nexttimesrequests, then serve records again;Nonefails every request.
- Raises:
ValueError – If the endpoint is unknown, or not exactly one of
status/timeoutis given.- Return type:
- client(app_name='liquipydia-fake', *, max_retries=3, retry_backoff_factor=0.0)¶
Create a real
LiquipediaClientwhose requests are served by this fake (no network, no API key).- Parameters:
app_name (
str(default:'liquipydia-fake')) – Application name used in the User-Agent header.max_retries (
int(default:3)) – Retries on simulated HTTP 429 responses and timeouts, as for a real client.retry_backoff_factor (
float(default:0.0)) – Backoff base between those retries;0.0by default so tests do not wait.
- Return type:
- Returns:
The client; use it as a context manager.
- property requests: list[RecordedRequest]¶
Every request received so far, in order (a copy).