Testing¶
Public test helpers. FakeSession / FakeResponse are the
recommended seam for downstream integrators driving the Polaris client
without real HTTP.
iceberg_ops.testing¶
Public test helpers for tools that integrate with iceberg-ops.
Today the module exposes FakeSession and
FakeResponse - a lightweight stand-in for a
requests.Session and requests.Response pair. The
experimental iceberg-ops Polaris client
(PolarisClient) takes an injectable
session= kwarg so that downstream consumers - and our own test
suite - can drive it against this in-process fake instead of a live
HTTP service. Its endpoint paths do not match the Polaris 1.x policy
API yet, so the fake is for exercising the client, not for modelling
a real Polaris server.
Usage¶
from iceberg_ops.testing import FakeResponse, FakeSession
sess = FakeSession()
sess.register("GET", "/policies", FakeResponse(json_body={"policies": []}))
client = PolarisClient(
"https://polaris.example.com",
catalog="mycat",
token="t",
session=sess,
)
client.list_policies("ns")
# Inspect what the client actually sent.
assert sess.calls[0]["method"] == "GET"
assert "/policies" in sess.calls[0]["url"]
Behaviour¶
FakeSessionrecords everyrequest()/post()call inFakeSession.calls.Responses are matched by
(method, path_substring)and consumed on first match (subsequent calls to the same endpoint require anotherFakeSession.register()).An unmatched call raises
AssertionErrorso tests fail loudly rather than silently producing surprising output.
Stability¶
Both classes are part of the documented public API. Their attributes / methods follow semantic versioning: additions are non-breaking; renames or removals are breaking and bump the major version. Code outside this repository may rely on:
FakeSession.__init__()signatureFakeSession.register(),FakeSession.request(),FakeSession.post(),FakeSession.callsFakeResponse.__init__keyword-only signature, plus the attributesstatus_codeandtextand the methodsjsonandraise_for_status.
- class iceberg_ops.testing.FakeResponse(*, status_code=200, json_body=None, text=None)[source]¶
Bases:
objectMinimal stand-in for
requests.Response.Exposes the four attributes a typical REST client touches:
status_code,text,json(), andraise_for_status()(the last is a no-op here, kept for forward-compatibility if a client adopts it).- Parameters:
status_code (
int, default:200) – HTTP status code. Default200.json_body (
Any|None, default:None) – Body returned byjson().Nonemeans the response carries no JSON body andjson()raises.text (
str|None, default:None) – Raw response text. Defaults to a JSON-serialisedjson_bodywhenjson_bodyis given, otherwise the empty string.
- json()[source]¶
Return the response body decoded as JSON.
- Raises:
ValueError – If no
json_bodywas supplied at construction.- Return type:
- raise_for_status()[source]¶
No-op stand-in for
requests.Response.raise_for_status().- Return type:
- class iceberg_ops.testing.FakeSession[source]¶
Bases:
objectRecords every request and returns canned responses.
Each call inspects the
(method, path_substring)pair and returns the matching response. Tests register response sequences viaregister(). Unmatched calls raiseAssertionError- keeps tests honest about which calls they expect.- Variables:
calls (list[dict]) – Every call this session has serviced, in chronological order. Each dict carries the keys
method,url, plus the per-call kwargs (params,json,headers,timeoutforrequest();dataandtimeoutforpost()).
- register(method, path_substring, response)[source]¶
Queue response for the next
method/path_substringmatch.Each registration is consumed on first match. To service the same endpoint twice, call
register()twice.- Parameters:
method (
str) – HTTP method (case-insensitive).path_substring (
str) – Substring matched against the request URL. Useful when a client builds its own full URL from a base + path.response (
FakeResponse) – The response to return when the next call matches.
- Return type:
- request(method, url, *, params=None, json=None, headers=None, timeout=None)[source]¶
Mock for
requests.Session.request().Records the call and returns the next registered response whose
(method, path_substring)matches.
- post(url, *, data=None, timeout=None)[source]¶
Mock for
requests.Session.post().Some clients call
session.post(url, data=...)directly for OAuth2 token requests instead of going throughrequest(). Both paths land here in the right place.- Parameters:
- Return type: