This repository was archived by the owner on Apr 2, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathconftest.py
More file actions
185 lines (152 loc) · 5.09 KB
/
conftest.py
File metadata and controls
185 lines (152 loc) · 5.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
from collections.abc import AsyncIterator, Generator, Iterator
from contextlib import asynccontextmanager
from datetime import UTC, datetime, timedelta
from typing import Any, Callable
from urllib.parse import urljoin
import pytest
from fastapi import FastAPI
from fastapi.testclient import TestClient
from stapi_fastapi.models.conformance import ASYNC_OPPORTUNITIES, CORE, OPPORTUNITIES
from stapi_fastapi.models.opportunity import (
Opportunity,
)
from stapi_fastapi.models.product import (
Product,
)
from stapi_fastapi.routers.root_router import RootRouter
from .backends import (
mock_get_opportunity_search_record,
mock_get_opportunity_search_records,
mock_get_order,
mock_get_order_statuses,
mock_get_orders,
)
from .shared import (
InMemoryOpportunityDB,
InMemoryOrderDB,
create_mock_opportunity,
find_link,
product_test_satellite_provider_sync_opportunity,
product_test_spotlight_sync_opportunity,
)
from .test_datetime_interval import rfc3339_strftime
@pytest.fixture(scope="session")
def base_url() -> Iterator[str]:
yield "http://stapiserver"
@pytest.fixture
def mock_products(request) -> list[Product]:
if request.node.get_closest_marker("mock_products") is not None:
return request.node.get_closest_marker("mock_products").args[0]
return [
product_test_spotlight_sync_opportunity,
product_test_satellite_provider_sync_opportunity,
]
@pytest.fixture
def mock_opportunities() -> list[Opportunity]:
return [create_mock_opportunity()]
@pytest.fixture
def stapi_client(
mock_products: list[Product],
base_url: str,
mock_opportunities: list[Opportunity],
) -> Generator[TestClient, None, None]:
@asynccontextmanager
async def lifespan(app: FastAPI) -> AsyncIterator[dict[str, Any]]:
try:
yield {
"_orders_db": InMemoryOrderDB(),
"_opportunities": mock_opportunities,
}
finally:
pass
root_router = RootRouter(
get_orders=mock_get_orders,
get_order=mock_get_order,
get_order_statuses=mock_get_order_statuses,
conformances=[CORE],
)
for mock_product in mock_products:
root_router.add_product(mock_product)
app = FastAPI(lifespan=lifespan)
app.include_router(root_router, prefix="")
with TestClient(app, base_url=f"{base_url}") as client:
yield client
@pytest.fixture
def stapi_client_async_opportunity(
mock_products: list[Product],
base_url: str,
mock_opportunities: list[Opportunity],
) -> Generator[TestClient, None, None]:
@asynccontextmanager
async def lifespan(app: FastAPI) -> AsyncIterator[dict[str, Any]]:
try:
yield {
"_orders_db": InMemoryOrderDB(),
"_opportunities_db": InMemoryOpportunityDB(),
"_opportunities": mock_opportunities,
}
finally:
pass
root_router = RootRouter(
get_orders=mock_get_orders,
get_order=mock_get_order,
get_order_statuses=mock_get_order_statuses,
get_opportunity_search_records=mock_get_opportunity_search_records,
get_opportunity_search_record=mock_get_opportunity_search_record,
conformances=[CORE, OPPORTUNITIES, ASYNC_OPPORTUNITIES],
)
for mock_product in mock_products:
root_router.add_product(mock_product)
app = FastAPI(lifespan=lifespan)
app.include_router(root_router, prefix="")
with TestClient(app, base_url=f"{base_url}") as client:
yield client
@pytest.fixture(scope="session")
def url_for(base_url: str) -> Iterator[Callable[[str], str]]:
def with_trailing_slash(value: str) -> str:
return value if value.endswith("/") else f"{value}/"
def url_for(value: str) -> str:
return urljoin(with_trailing_slash(base_url), f"./{value.lstrip('/')}")
yield url_for
@pytest.fixture
def assert_link(url_for) -> Callable:
def _assert_link(
req: str,
body: dict[str, Any],
rel: str,
path: str,
media_type: str = "application/json",
method: str | None = None,
):
link = find_link(body["links"], rel)
assert link, f"{req} Link[rel={rel}] should exist"
assert link["type"] == media_type
assert link["href"] == url_for(path)
if method:
assert link["method"] == method
return _assert_link
@pytest.fixture
def limit() -> int:
return 10
@pytest.fixture
def opportunity_search(limit) -> dict[str, Any]:
now = datetime.now(UTC)
end = now + timedelta(days=5)
format = "%Y-%m-%dT%H:%M:%S.%f%z"
start_string = rfc3339_strftime(now, format)
end_string = rfc3339_strftime(end, format)
return {
"geometry": {
"type": "Point",
"coordinates": [0, 0],
},
"datetime": f"{start_string}/{end_string}",
"filter": {
"op": "and",
"args": [
{"op": ">", "args": [{"property": "off_nadir"}, 0]},
{"op": "<", "args": [{"property": "off_nadir"}, 45]},
],
},
"limit": limit,
}