|
| 1 | +from __future__ import annotations |
| 2 | + |
1 | 3 | """Basic tests for the FastAPI search endpoint.""" |
2 | 4 |
|
3 | 5 | import json |
| 6 | +from typing import get_args |
4 | 7 | from urllib.parse import urlencode |
5 | 8 |
|
6 | 9 | import pytest |
7 | 10 |
|
8 | | -from openlibrary.fastapi.search import PublicQueryOptions |
| 11 | +from openlibrary.fastapi.search import FacetField, PublicQueryOptions |
9 | 12 | from openlibrary.plugins.worksearch.code import WorkSearchScheme |
10 | 13 |
|
11 | 14 |
|
@@ -391,3 +394,69 @@ def test_debug_openapi_structure(self, client): |
391 | 394 |
|
392 | 395 | # This test always passes - it's just for debug output |
393 | 396 | assert True |
| 397 | + |
| 398 | + |
| 399 | +class TestSearchFacetsEndpoint: |
| 400 | + """Tests for the /search/facets.json endpoint.""" |
| 401 | + |
| 402 | + def test_requires_field_param(self, fastapi_client, mock_run_solr_query_async): |
| 403 | + response = fastapi_client.get("/search/facets.json?q=tolkien") |
| 404 | + # FastAPI enforces required + min_length=1 on `field`, returns 422 when absent |
| 405 | + assert response.status_code == 422 |
| 406 | + detail = response.json()["detail"] |
| 407 | + assert any("field" in err["loc"] for err in detail) |
| 408 | + |
| 409 | + def test_rejects_invalid_field(self, fastapi_client, mock_run_solr_query_async): |
| 410 | + response = fastapi_client.get("/search/facets.json?field=notafield&q=tolkien") |
| 411 | + # FastAPI validates FacetField Literal and returns 422 for unknown values |
| 412 | + assert response.status_code == 422 |
| 413 | + detail = response.json()["detail"] |
| 414 | + assert any(err.get("input") == "notafield" for err in detail) |
| 415 | + |
| 416 | + def test_returns_facet_values_for_single_field(self, fastapi_client, mock_run_solr_query_async): |
| 417 | + response = fastapi_client.get("/search/facets.json?field=language&q=lord+of+the+rings") |
| 418 | + assert response.status_code == 200 |
| 419 | + data = response.json() |
| 420 | + assert "language" in data |
| 421 | + values = data["language"] |
| 422 | + assert len(values) > 0 |
| 423 | + assert all("value" in v and "count" in v and "label" in v for v in values) |
| 424 | + assert values[0] == {"value": "eng", "label": "English", "count": 665} |
| 425 | + |
| 426 | + def test_filters_zero_count_values(self, fastapi_client, mock_run_solr_query_async): |
| 427 | + response = fastapi_client.get("/search/facets.json?field=language&q=tolkien") |
| 428 | + assert response.status_code == 200 |
| 429 | + data = response.json() |
| 430 | + # "Latin" (code "lat") has count=0 in the mock — must not appear |
| 431 | + assert not any(v["value"] == "lat" for v in data["language"]) |
| 432 | + assert all(v["count"] > 0 for v in data["language"]) |
| 433 | + |
| 434 | + def test_returns_multiple_fields(self, fastapi_client, mock_run_solr_query_async): |
| 435 | + response = fastapi_client.get("/search/facets.json?field=language&field=subject_facet&q=tolkien") |
| 436 | + assert response.status_code == 200 |
| 437 | + data = response.json() |
| 438 | + assert "language" in data |
| 439 | + assert "subject_facet" in data |
| 440 | + |
| 441 | + def test_author_facet_key_maps_to_author_facet(self, fastapi_client, mock_run_solr_query_async): |
| 442 | + """Response key should be 'author_facet' even though Solr returns 'author_key' internally.""" |
| 443 | + response = fastapi_client.get("/search/facets.json?field=author_facet&q=tolkien") |
| 444 | + assert response.status_code == 200 |
| 445 | + data = response.json() |
| 446 | + assert "author_facet" in data |
| 447 | + assert "author_key" not in data |
| 448 | + assert data["author_facet"] == [{"value": "OL9A", "label": "J.R.R. Tolkien", "count": 123}] |
| 449 | + |
| 450 | + def test_empty_query_returns_valid_response(self, fastapi_client, mock_run_solr_query_async): |
| 451 | + """No q param should still return a valid (unfiltered) facet list.""" |
| 452 | + response = fastapi_client.get("/search/facets.json?field=language") |
| 453 | + assert response.status_code == 200 |
| 454 | + assert "language" in response.json() |
| 455 | + |
| 456 | + def test_facet_field_literal_matches_scheme(self): |
| 457 | + """FacetField Literal must stay in sync with WorkSearchScheme.facet_fields. |
| 458 | +
|
| 459 | + Guards against drift: if a facet field is added/removed upstream, this |
| 460 | + fails so the endpoint doesn't silently 422 on (or miss) it. |
| 461 | + """ |
| 462 | + assert set(get_args(FacetField)) == WorkSearchScheme.facet_fields |
0 commit comments