66 lines
1.8 KiB
Python
66 lines
1.8 KiB
Python
from __future__ import annotations
|
|
|
|
import httpx
|
|
|
|
from bib.regulations_gov import Client
|
|
|
|
|
|
def _row(cid: str, lm: str) -> dict:
|
|
return {
|
|
"id": cid,
|
|
"attributes": {
|
|
"lastModifiedDate": lm,
|
|
"postedDate": lm,
|
|
"docketId": "D",
|
|
"commentOnId": "x",
|
|
},
|
|
}
|
|
|
|
|
|
def _client(handler) -> Client:
|
|
http = httpx.Client(
|
|
transport=httpx.MockTransport(handler), headers={"X-Api-Key": "k"}
|
|
)
|
|
return Client(api_key="k", sleep=0, client=http)
|
|
|
|
|
|
def test_since_seeds_the_date_filter():
|
|
seen: list[dict] = []
|
|
|
|
def handler(req: httpx.Request) -> httpx.Response:
|
|
seen.append(dict(req.url.params))
|
|
return httpx.Response(
|
|
200,
|
|
json={
|
|
"data": [_row("D-1", "2026-09-08T12:00:00Z")],
|
|
"meta": {"totalPages": 1},
|
|
},
|
|
)
|
|
|
|
api = _client(handler)
|
|
out = list(api.iter_comments("obj", since="2026-09-01T00:00:00Z"))
|
|
assert [c.id for c in out] == ["D-1"]
|
|
assert seen[0]["filter[lastModifiedDate][ge]"] == "2026-09-01 00:00:00"
|
|
|
|
|
|
def test_no_since_means_no_date_filter():
|
|
seen: list[dict] = []
|
|
|
|
def handler(req: httpx.Request) -> httpx.Response:
|
|
seen.append(dict(req.url.params))
|
|
return httpx.Response(200, json={"data": [], "meta": {"totalPages": 1}})
|
|
|
|
list(_client(handler).iter_comments("obj"))
|
|
assert "filter[lastModifiedDate][ge]" not in seen[0]
|
|
|
|
|
|
def test_on_error_called_when_page_fails():
|
|
errors: list[Exception] = []
|
|
|
|
def handler(req: httpx.Request) -> httpx.Response:
|
|
return httpx.Response(500, json={})
|
|
|
|
out = list(_client(handler).iter_comments("obj", on_error=errors.append))
|
|
assert out == []
|
|
assert len(errors) == 1 and isinstance(errors[0], httpx.HTTPStatusError)
|