|
| 1 | +from uuid import uuid4 |
| 2 | + |
| 3 | +from fastapi import FastAPI, Request |
| 4 | +from stapi_fastapi.backends.product_backend import ProductBackend |
| 5 | +from stapi_fastapi.exceptions import ConstraintsException, NotFoundException |
| 6 | +from stapi_fastapi.models.opportunity import ( |
| 7 | + Opportunity, |
| 8 | + OpportunityPropertiesBase, |
| 9 | + OpportunityRequest, |
| 10 | +) |
| 11 | +from stapi_fastapi.models.order import Order |
| 12 | +from stapi_fastapi.models.product import ( |
| 13 | + Product, |
| 14 | + Provider, |
| 15 | + ProviderRole, |
| 16 | +) |
| 17 | +from stapi_fastapi.routers.root_router import RootRouter |
| 18 | + |
| 19 | + |
| 20 | +class MockOrderDB(dict[int | str, Order]): |
| 21 | + pass |
| 22 | + |
| 23 | + |
| 24 | +class MockRootBackend: |
| 25 | + def __init__(self, orders: MockOrderDB) -> None: |
| 26 | + self._orders: MockOrderDB = orders |
| 27 | + |
| 28 | + async def get_orders(self, request: Request) -> list[Order]: |
| 29 | + """ |
| 30 | + Show all orders. |
| 31 | + """ |
| 32 | + return list(self._orders.values()) |
| 33 | + |
| 34 | + async def get_order(self, order_id: str, request: Request) -> Order: |
| 35 | + """ |
| 36 | + Show details for order with `order_id`. |
| 37 | + """ |
| 38 | + try: |
| 39 | + return self._orders[order_id] |
| 40 | + except KeyError: |
| 41 | + raise NotFoundException() |
| 42 | + |
| 43 | + |
| 44 | +class MockProductBackend(ProductBackend): |
| 45 | + def __init__(self, orders: MockOrderDB) -> None: |
| 46 | + self._opportunities: list[Opportunity] = [] |
| 47 | + self._allowed_payloads: list[OpportunityRequest] = [] |
| 48 | + self._orders: MockOrderDB = orders |
| 49 | + |
| 50 | + async def search_opportunities( |
| 51 | + self, product: Product, search: OpportunityRequest, request: Request |
| 52 | + ) -> list[Opportunity]: |
| 53 | + return [o.model_copy(update=search.model_dump()) for o in self._opportunities] |
| 54 | + |
| 55 | + async def create_order( |
| 56 | + self, product: Product, payload: OpportunityRequest, request: Request |
| 57 | + ) -> Order: |
| 58 | + """ |
| 59 | + Create a new order. |
| 60 | + """ |
| 61 | + allowed: bool = any(allowed == payload for allowed in self._allowed_payloads) |
| 62 | + if allowed: |
| 63 | + order = Order( |
| 64 | + id=str(uuid4()), |
| 65 | + geometry=payload.geometry, |
| 66 | + properties={ |
| 67 | + "filter": payload.filter, |
| 68 | + "datetime": payload.datetime, |
| 69 | + "product_id": product.id, |
| 70 | + }, |
| 71 | + links=[], |
| 72 | + ) |
| 73 | + self._orders[order.id] = order |
| 74 | + return order |
| 75 | + raise ConstraintsException("not allowed") |
| 76 | + |
| 77 | + |
| 78 | +class TestSpotlightProperties(OpportunityPropertiesBase): |
| 79 | + off_nadir: int |
| 80 | + |
| 81 | + |
| 82 | +order_db = MockOrderDB() |
| 83 | +product_backend = MockProductBackend(order_db) |
| 84 | +root_backend = MockRootBackend(order_db) |
| 85 | + |
| 86 | +provider = Provider( |
| 87 | + name="Test Provider", |
| 88 | + description="A provider for Test data", |
| 89 | + roles=[ProviderRole.producer], # Example role |
| 90 | + url="https://test-provider.example.com", # Must be a valid URL |
| 91 | +) |
| 92 | + |
| 93 | +product = Product( |
| 94 | + id="test-spotlight", |
| 95 | + title="Test Spotlight Product", |
| 96 | + description="Test product for test spotlight", |
| 97 | + license="CC-BY-4.0", |
| 98 | + keywords=["test", "satellite"], |
| 99 | + providers=[provider], |
| 100 | + links=[], |
| 101 | + constraints=TestSpotlightProperties, |
| 102 | + backend=product_backend, |
| 103 | +) |
| 104 | + |
| 105 | +root_router = RootRouter(root_backend) |
| 106 | +root_router.add_product(product) |
| 107 | +app: FastAPI = FastAPI() |
| 108 | +app.include_router(root_router, prefix="") |
0 commit comments