feat(radiobrowser): add search, paging and favourite lookup
This commit is contained in:
1 parent
f96923b5b7
commit
638cc9eddb
8 files changed
+1018
-45
No files matched your search
+115
-4
@@ -66,25 +66,136 @@ def test_hls_streams_survive_as_direct_urls() -> None:
|
||||
assert station.format_label == "aac · 64k"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_search_stations_queries_all_searchable_fields() -> None:
|
||||
entries = {
|
||||
"name": {**ENTRY, "stationuuid": "name", "name": "Jazz Name"},
|
||||
"tag": {**ENTRY, "stationuuid": "tag", "name": "Zed Jazz Tag"},
|
||||
"country": {**ENTRY, "stationuuid": "country", "name": "Country Jazz"},
|
||||
"language": {**ENTRY, "stationuuid": "language", "name": "Language Jazz"},
|
||||
}
|
||||
seen: list[dict[str, str]] = []
|
||||
|
||||
def handler(request: httpx.Request) -> httpx.Response:
|
||||
params = dict(request.url.params)
|
||||
seen.append(params)
|
||||
field = next(name for name in api._SEARCH_FIELDS if name in params)
|
||||
payload = [entries[field]]
|
||||
if field in ("name", "tag"):
|
||||
payload.append(ENTRY) # the same station may match twice
|
||||
return httpx.Response(200, json=payload)
|
||||
|
||||
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
|
||||
stations = await api.search_stations(client, "jazz")
|
||||
|
||||
assert {station.id for station in stations} == {
|
||||
ENTRY["stationuuid"],
|
||||
"name",
|
||||
"tag",
|
||||
"country",
|
||||
"language",
|
||||
}
|
||||
assert [params["limit"] for params in seen] == [
|
||||
str(api.SEARCH_LIMIT)
|
||||
] * len(api._SEARCH_FIELDS)
|
||||
for params in seen:
|
||||
assert params["hidebroken"] == "true"
|
||||
assert params["order"] == "clickcount"
|
||||
assert params["reverse"] == "true"
|
||||
assert {next(name for name in api._SEARCH_FIELDS if name in params) for params in seen} == set(
|
||||
api._SEARCH_FIELDS
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_search_stations_ignores_a_failed_field(monkeypatch) -> None:
|
||||
async def fake_fetch_json(client, url):
|
||||
if "name=jazz" in url:
|
||||
raise RuntimeError("name search is down")
|
||||
return [ENTRY]
|
||||
|
||||
monkeypatch.setattr(api, "fetch_json", fake_fetch_json)
|
||||
async with httpx.AsyncClient() as client:
|
||||
stations = await api.search_stations(client, "jazz")
|
||||
assert [station.id for station in stations] == [ENTRY["stationuuid"]]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_an_empty_search_does_not_hit_the_api() -> None:
|
||||
def handler(request: httpx.Request) -> httpx.Response:
|
||||
raise AssertionError("empty queries must not make a request")
|
||||
|
||||
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
|
||||
assert await api.search_stations(client, " ") == []
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_fetch_stations_caches_the_directory() -> None:
|
||||
payload = [ENTRY, {**ENTRY, "stationuuid": "x", "name": "Another"}]
|
||||
|
||||
def handler(request: httpx.Request) -> httpx.Response:
|
||||
assert "hidebroken=true" in str(request.url)
|
||||
assert "limit=300" in str(request.url)
|
||||
params = dict(request.url.params)
|
||||
assert params["hidebroken"] == "true"
|
||||
assert params["limit"] == str(api.PAGE_SIZE)
|
||||
assert params["offset"] == "0"
|
||||
return httpx.Response(200, json=payload)
|
||||
|
||||
provider = RadioBrowserProvider()
|
||||
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
|
||||
stations = await provider.fetch_stations(client)
|
||||
assert [station.title for station in stations] == ["Another", "France Info"]
|
||||
assert [station.title for station in stations] == ["France Info", "Another"]
|
||||
assert [station.title for station in provider.load_cached_stations()] == [
|
||||
"Another",
|
||||
"France Info",
|
||||
"Another",
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_fetch_station_page_continues_in_catalogue_order() -> None:
|
||||
first = [
|
||||
{**ENTRY, "stationuuid": f"id-{index}", "name": f"Station {index}"}
|
||||
for index in range(api.PAGE_SIZE)
|
||||
]
|
||||
last = [{**ENTRY, "stationuuid": "last", "name": "Last Station"}]
|
||||
seen: list[dict[str, str]] = []
|
||||
|
||||
def handler(request: httpx.Request) -> httpx.Response:
|
||||
params = dict(request.url.params)
|
||||
seen.append(params)
|
||||
return httpx.Response(200, json=first if params["offset"] == "100" else last)
|
||||
|
||||
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
|
||||
page = await api.fetch_station_page(client, 100)
|
||||
end = await api.fetch_station_page(client, 200)
|
||||
|
||||
assert [station.id for station in page.stations] == [
|
||||
f"id-{index}" for index in range(api.PAGE_SIZE)
|
||||
]
|
||||
assert page.next_offset == 200
|
||||
assert end.next_offset is None
|
||||
assert [params["offset"] for params in seen] == ["100", "200"]
|
||||
assert [params["limit"] for params in seen] == [str(api.PAGE_SIZE)] * 2
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_fetch_stations_by_ids_uses_the_uuid_endpoint() -> None:
|
||||
seen: list[dict[str, str]] = []
|
||||
|
||||
def handler(request: httpx.Request) -> httpx.Response:
|
||||
seen.append(dict(request.url.params))
|
||||
return httpx.Response(200, json=[ENTRY])
|
||||
|
||||
station_id = ENTRY["stationuuid"]
|
||||
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
|
||||
stations = await api.fetch_stations_by_ids(
|
||||
client, ["defcon", station_id, station_id]
|
||||
)
|
||||
|
||||
assert [station.id for station in stations] == [station_id]
|
||||
assert len(seen) == 1
|
||||
assert seen[0]["uuids"] == station_id
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_a_failed_fetch_does_not_kill_the_catalogue() -> None:
|
||||
def handler(request: httpx.Request) -> httpx.Response:
|
||||
|
||||
Reference in new issue
Block a user