|
1 | 1 | from collections.abc import AsyncIterator, Iterator |
2 | 2 | from contextlib import asynccontextmanager |
3 | | -from datetime import datetime, timedelta, timezone |
4 | 3 | from typing import Any, Callable |
5 | 4 | from urllib.parse import urljoin |
6 | | -from uuid import uuid4 |
7 | 5 |
|
8 | 6 | import pytest |
9 | | -from fastapi import FastAPI, status |
| 7 | +from fastapi import FastAPI |
10 | 8 | from fastapi.testclient import TestClient |
11 | | -from geojson_pydantic import Point |
12 | | -from geojson_pydantic.types import Position2D |
13 | | -from httpx import Response |
14 | | -from pytest import fail |
15 | 9 |
|
16 | 10 | from stapi_fastapi.models.opportunity import ( |
17 | 11 | Opportunity, |
|
21 | 15 | ) |
22 | 16 | from stapi_fastapi.routers.root_router import RootRouter |
23 | 17 |
|
24 | | -from .application import ( |
25 | | - InMemoryOrderDB, |
26 | | - MyOpportunityProperties, |
27 | | - MyOrderParameters, |
28 | | - MyProductConstraints, |
29 | | - OffNadirRange, |
30 | | - mock_create_order, |
| 18 | +from .backends import ( |
31 | 19 | mock_get_order, |
32 | 20 | mock_get_order_statuses, |
33 | 21 | mock_get_orders, |
| 22 | +) |
| 23 | +from .shared import ( |
| 24 | + InMemoryOrderDB, |
| 25 | + create_mock_opportunity, |
| 26 | + find_link, |
| 27 | + mock_product_test_satellite_provider, |
34 | 28 | mock_product_test_spotlight, |
35 | | - mock_search_opportunities, |
36 | | - provider, |
37 | 29 | ) |
38 | | -from .shared import find_link |
39 | 30 |
|
40 | 31 |
|
41 | 32 | @pytest.fixture(scope="session") |
42 | 33 | def base_url() -> Iterator[str]: |
43 | 34 | yield "http://stapiserver" |
44 | 35 |
|
45 | 36 |
|
46 | | -mock_product_test_satellite_provider = Product( |
47 | | - id="test-satellite-provider", |
48 | | - title="Satellite Product", |
49 | | - description="A product by a satellite provider", |
50 | | - license="CC-BY-4.0", |
51 | | - keywords=["test", "satellite", "provider"], |
52 | | - providers=[provider], |
53 | | - links=[], |
54 | | - create_order=mock_create_order, |
55 | | - search_opportunities=mock_search_opportunities, |
56 | | - constraints=MyProductConstraints, |
57 | | - opportunity_properties=MyOpportunityProperties, |
58 | | - order_parameters=MyOrderParameters, |
59 | | -) |
60 | | - |
61 | | - |
62 | 37 | @pytest.fixture |
63 | 38 | def mock_products() -> list[Product]: |
64 | 39 | return [mock_product_test_spotlight, mock_product_test_satellite_provider] |
65 | 40 |
|
66 | 41 |
|
| 42 | +@pytest.fixture |
| 43 | +def mock_opportunities() -> list[Opportunity]: |
| 44 | + return [create_mock_opportunity()] |
| 45 | + |
| 46 | + |
67 | 47 | @pytest.fixture |
68 | 48 | def stapi_client( |
69 | 49 | mock_products: list[Product], |
@@ -134,99 +114,3 @@ def _assert_link( |
134 | 114 | assert link["href"] == url_for(path) |
135 | 115 |
|
136 | 116 | return _assert_link |
137 | | - |
138 | | - |
139 | | -@pytest.fixture |
140 | | -def mock_opportunities() -> list[Opportunity]: |
141 | | - """Fixture to create mock data for Opportunities for `test-spotlight-1`.""" |
142 | | - now = datetime.now(timezone.utc) # Use timezone-aware datetime |
143 | | - start = now |
144 | | - end = start + timedelta(days=5) |
145 | | - |
146 | | - # Create a list of mock opportunities for the given product |
147 | | - return [ |
148 | | - Opportunity( |
149 | | - id=str(uuid4()), |
150 | | - type="Feature", |
151 | | - geometry=Point( |
152 | | - type="Point", |
153 | | - coordinates=Position2D(longitude=0.0, latitude=0.0), |
154 | | - ), |
155 | | - properties=MyOpportunityProperties( |
156 | | - product_id="xyz123", |
157 | | - datetime=(start, end), |
158 | | - off_nadir=OffNadirRange(minimum=20, maximum=22), |
159 | | - vehicle_id=[1], |
160 | | - platform="platform_id", |
161 | | - other_thing="abcd1234", |
162 | | - ), |
163 | | - ), |
164 | | - ] |
165 | | - |
166 | | - |
167 | | -def pagination_tester( |
168 | | - stapi_client: TestClient, |
169 | | - endpoint: str, |
170 | | - method: str, |
171 | | - limit: int, |
172 | | - target: str, |
173 | | - expected_returns: list, |
174 | | - body: dict | None = None, |
175 | | -) -> None: |
176 | | - retrieved = [] |
177 | | - |
178 | | - res = make_request(stapi_client, endpoint, method, body, None, limit) |
179 | | - assert res.status_code == status.HTTP_200_OK |
180 | | - resp_body = res.json() |
181 | | - |
182 | | - assert len(resp_body[target]) <= limit |
183 | | - retrieved.extend(resp_body[target]) |
184 | | - next_url = next((d["href"] for d in resp_body["links"] if d["rel"] == "next"), None) |
185 | | - |
186 | | - while next_url: |
187 | | - url = next_url |
188 | | - if method == "POST": |
189 | | - body = next( |
190 | | - (d["body"] for d in resp_body["links"] if d["rel"] == "next"), None |
191 | | - ) |
192 | | - |
193 | | - res = make_request(stapi_client, url, method, body, next_url, limit) |
194 | | - assert res.status_code == status.HTTP_200_OK |
195 | | - assert len(resp_body[target]) <= limit |
196 | | - resp_body = res.json() |
197 | | - retrieved.extend(resp_body[target]) |
198 | | - |
199 | | - # get url w/ query params for next call if exists, and POST body if necessary |
200 | | - if resp_body["links"]: |
201 | | - next_url = next( |
202 | | - (d["href"] for d in resp_body["links"] if d["rel"] == "next"), None |
203 | | - ) |
204 | | - else: |
205 | | - next_url = None |
206 | | - |
207 | | - assert len(retrieved) == len(expected_returns) |
208 | | - assert retrieved == expected_returns |
209 | | - |
210 | | - |
211 | | -def make_request( |
212 | | - stapi_client: TestClient, |
213 | | - endpoint: str, |
214 | | - method: str, |
215 | | - body: dict | None, |
216 | | - next_token: str | None, |
217 | | - limit: int, |
218 | | -) -> Response: |
219 | | - """request wrapper for pagination tests""" |
220 | | - |
221 | | - match method: |
222 | | - case "GET": |
223 | | - if next_token: # extract pagination token |
224 | | - next_token = next_token.split("next=")[1] |
225 | | - params = {"next": next_token, "limit": limit} |
226 | | - res = stapi_client.get(endpoint, params=params) |
227 | | - case "POST": |
228 | | - res = stapi_client.post(endpoint, json=body) |
229 | | - case _: |
230 | | - fail(f"method {method} not supported in make request") |
231 | | - |
232 | | - return res |
0 commit comments