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, so paginate() walks the pages.

  • Field selection: with query, records keep only the named fields (case-insensitive) plus wiki.

  • 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: requests lists 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: object

An in-memory LPDB serving canned records to real LiquipediaClient instances.

Records are keyed by LPDB endpoint name ("match", "player", "teamtemplate"…). Each request returns the endpoint’s records, sliced by offset / limit so pagination works, and reduced to the fields named in query when given. conditions and keyword filters are not evaluated: every record of the endpoint is returned, and requests shows what was asked. query functions (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.

Parameters:
  • endpoint (str) – LPDB endpoint name (e.g. "match").

  • *records (dict[str, Any]) – Records as returned by the API (plain dicts).

Raises:

ValueError – If the endpoint name is unknown.

Return type:

None

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: RateLimitError for 429 (after its retries), AuthError for 403, NotFoundError for 404, LiquipediaError for other statuses, and NetworkError for 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 next times requests, then serve records again; None fails every request.

Raises:

ValueError – If the endpoint is unknown, or not exactly one of status / timeout is given.

Return type:

None

client(app_name='liquipydia-fake', *, max_retries=3, retry_backoff_factor=0.0)

Create a real LiquipediaClient whose 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.0 by default so tests do not wait.

Return type:

LiquipediaClient

Returns:

The client; use it as a context manager.

property requests: list[RecordedRequest]

Every request received so far, in order (a copy).

class liquipydia.testing.RecordedRequest

Bases: object

One request received by a FakeLpdb.

endpoint

The LPDB endpoint called (e.g. "match").

Type:

str

params

The query parameters sent, as strings (e.g. {"wiki": "dota2", "limit": "50", ...}).

Type:

dict[str, str]

__init__(endpoint, params)
classmethod __new__(*args, **kwargs)