|
1 | | -from datetime import datetime, timezone |
| 1 | +from datetime import UTC, datetime, timedelta, timezone |
2 | 2 | from typing import Any, Callable |
3 | 3 | from uuid import uuid4 |
4 | 4 |
|
5 | 5 | import pytest |
| 6 | +from fastapi import status |
6 | 7 | from fastapi.testclient import TestClient |
7 | 8 |
|
8 | 9 | from stapi_fastapi.models.opportunity import ( |
|
17 | 18 | from .shared import ( |
18 | 19 | create_mock_opportunity, |
19 | 20 | find_link, |
| 21 | + pagination_tester, |
20 | 22 | product_test_spotlight, |
21 | 23 | product_test_spotlight_async_opportunity, |
22 | 24 | product_test_spotlight_sync_async_opportunity, |
23 | 25 | product_test_spotlight_sync_opportunity, |
24 | 26 | ) |
| 27 | +from .test_datetime_interval import rfc3339_strftime |
25 | 28 |
|
26 | 29 |
|
27 | 30 | @pytest.mark.parametrize("mock_products", [[product_test_spotlight]]) |
@@ -297,5 +300,79 @@ def test_new_search_location_header_matches_self_link( |
297 | 300 | assert search_response.headers["Location"] == str(link["href"]) |
298 | 301 |
|
299 | 302 |
|
300 | | -# Pagination test for the OpportunitySearchRecrods returned from the /searches/opportunities |
301 | | -# endpoint on the root router |
| 303 | +@pytest.mark.parametrize("mock_products", [[product_test_spotlight_async_opportunity]]) |
| 304 | +def test_bad_ids(stapi_client_async_opportunity: TestClient) -> None: |
| 305 | + search_record_id = "bad_id" |
| 306 | + res = stapi_client_async_opportunity.get( |
| 307 | + f"/searches/opportunities/{search_record_id}" |
| 308 | + ) |
| 309 | + assert res.status_code == status.HTTP_404_NOT_FOUND |
| 310 | + |
| 311 | + product_id = "test-spotlight" |
| 312 | + opportunity_collection_id = "bad_id" |
| 313 | + res = stapi_client_async_opportunity.get( |
| 314 | + f"/products/{product_id}/opportunities/{opportunity_collection_id}" |
| 315 | + ) |
| 316 | + assert res.status_code == status.HTTP_404_NOT_FOUND |
| 317 | + |
| 318 | + |
| 319 | +@pytest.fixture |
| 320 | +def setup_search_record_pagination( |
| 321 | + stapi_client_async_opportunity: TestClient, |
| 322 | + mock_products: list[Product], |
| 323 | +) -> list[dict[str, Any]]: |
| 324 | + product_id = "test-spotlight" |
| 325 | + search_records = [] |
| 326 | + for _ in range(3): |
| 327 | + now = datetime.now(UTC) |
| 328 | + end = now + timedelta(days=5) |
| 329 | + format = "%Y-%m-%dT%H:%M:%S.%f%z" |
| 330 | + start_string = rfc3339_strftime(now, format) |
| 331 | + end_string = rfc3339_strftime(end, format) |
| 332 | + |
| 333 | + opportunity_request = { |
| 334 | + "geometry": { |
| 335 | + "type": "Point", |
| 336 | + "coordinates": [0, 0], |
| 337 | + }, |
| 338 | + "datetime": f"{start_string}/{end_string}", |
| 339 | + "filter": { |
| 340 | + "op": "and", |
| 341 | + "args": [ |
| 342 | + {"op": ">", "args": [{"property": "off_nadir"}, 0]}, |
| 343 | + {"op": "<", "args": [{"property": "off_nadir"}, 45]}, |
| 344 | + ], |
| 345 | + }, |
| 346 | + } |
| 347 | + |
| 348 | + response = stapi_client_async_opportunity.post( |
| 349 | + f"/products/{product_id}/opportunities", json=opportunity_request |
| 350 | + ) |
| 351 | + assert response.status_code == 201 |
| 352 | + |
| 353 | + body = response.json() |
| 354 | + search_records.append(body) |
| 355 | + |
| 356 | + return search_records |
| 357 | + |
| 358 | + |
| 359 | +@pytest.mark.parametrize("limit", [0, 1, 2, 4]) |
| 360 | +@pytest.mark.parametrize("mock_products", [[product_test_spotlight_async_opportunity]]) |
| 361 | +def test_get_search_records_pagination( |
| 362 | + stapi_client_async_opportunity: TestClient, |
| 363 | + mock_products: list[Product], |
| 364 | + setup_search_record_pagination: list[dict[str, Any]], |
| 365 | + limit: int, |
| 366 | +) -> None: |
| 367 | + expected_returns = [] |
| 368 | + if limit > 0: |
| 369 | + expected_returns = setup_search_record_pagination |
| 370 | + |
| 371 | + pagination_tester( |
| 372 | + stapi_client=stapi_client_async_opportunity, |
| 373 | + url="/searches/opportunities", |
| 374 | + method="GET", |
| 375 | + limit=limit, |
| 376 | + target="search_records", |
| 377 | + expected_returns=expected_returns, |
| 378 | + ) |
0 commit comments