Skip to content

Commit

Permalink
hard code versions and address other comments
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-maschler committed Apr 4, 2024
1 parent e24351a commit 5bdd615
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 114 deletions.
11 changes: 2 additions & 9 deletions stac_fastapi/api/setup.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
"""stac_fastapi: api module."""

from distutils.util import convert_path

from setuptools import find_namespace_packages, setup

main_ns = {}
ver_path = convert_path("stac_fastapi/api/version.py")
with open(ver_path) as ver_file:
exec(ver_file.read(), main_ns)

with open("README.md") as f:
desc = f.read()

install_requires = [
"brotli_asgi",
f"stac-fastapi.types=={main_ns['__version__']}",
"stac-fastapi.types==2.4.9",
]

extra_reqs = {
Expand Down Expand Up @@ -58,5 +51,5 @@
install_requires=install_requires,
tests_require=extra_reqs["dev"],
extras_require=extra_reqs,
version=main_ns["__version__"],
version="2.4.9",
)
67 changes: 67 additions & 0 deletions stac_fastapi/api/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
from datetime import datetime
from typing import List, Optional, Union

import pytest
from stac_pydantic import Collection, Item
from stac_pydantic.api.utils import link_factory

from stac_fastapi.types import core, response_model
from stac_fastapi.types.core import NumType
from stac_fastapi.types.search import BaseSearchPostRequest

collection_links = link_factory.CollectionLinks("/", "test").create_links()
item_links = link_factory.ItemLinks("/", "test", "test").create_links()

Expand Down Expand Up @@ -53,3 +60,63 @@ def item(_item: Item):
@pytest.fixture
def item_dict(_item: Item):
return _item.model_dump(mode="json")


@pytest.fixture
def TestCoreClient(collection_dict, item_dict):
class CoreClient(core.BaseCoreClient):
def post_search(
self, search_request: BaseSearchPostRequest, **kwargs
) -> response_model.ItemCollection:
return response_model.ItemCollection(
type="FeatureCollection", features=[response_model.Item(**item_dict)]
)

def get_search(
self,
collections: Optional[List[str]] = None,
ids: Optional[List[str]] = None,
bbox: Optional[List[NumType]] = None,
intersects: Optional[str] = None,
datetime: Optional[Union[str, datetime]] = None,
limit: Optional[int] = 10,
**kwargs,
) -> response_model.ItemCollection:
return response_model.ItemCollection(
type="FeatureCollection", features=[response_model.Item(**item_dict)]
)

def get_item(
self, item_id: str, collection_id: str, **kwargs
) -> response_model.Item:
return response_model.Item(**item_dict)

def all_collections(self, **kwargs) -> response_model.Collections:
return response_model.Collections(
collections=[response_model.Collection(**collection_dict)],
links=[
{"href": "test", "rel": "root"},
{"href": "test", "rel": "self"},
{"href": "test", "rel": "parent"},
],
)

def get_collection(
self, collection_id: str, **kwargs
) -> response_model.Collection:
return response_model.Collection(**collection_dict)

def item_collection(
self,
collection_id: str,
bbox: Optional[List[Union[float, int]]] = None,
datetime: Optional[Union[str, datetime]] = None,
limit: int = 10,
token: str = None,
**kwargs,
) -> response_model.ItemCollection:
return response_model.ItemCollection(
type="FeatureCollection", features=[response_model.Item(**item_dict)]
)

return CoreClient
140 changes: 65 additions & 75 deletions stac_fastapi/api/tests/test_app.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
"""Implement all read_only methods of BaseCoreClient
and test all GET endpoints of the API"""

import importlib
import os
from datetime import datetime
from typing import List, Optional, Union

Expand All @@ -15,34 +11,59 @@
from stac_fastapi.extensions.core.filter.filter import FilterExtension
from stac_fastapi.types import core, response_model, search
from stac_fastapi.types.config import ApiSettings
from stac_fastapi.types.core import NumType
from stac_fastapi.types.search import BaseSearchPostRequest


@pytest.fixture
def cleanup():
old_environ = dict(os.environ)
yield
os.environ.clear()
os.environ.update(old_environ)


@pytest.mark.parametrize(
"validate, response_type",
[
("True", BaseModel),
("False", dict),
],
)
def test_app(validate, response_type, collection_dict, item_dict, cleanup, monkeypatch):
def test_client_response_type(validate, response_type, TestCoreClient, monkeypatch):
"""Test for correct response type when VALIDATE_RESPONSE is set."""
monkeypatch.setenv("VALIDATE_RESPONSE", validate)

importlib.reload(response_model)
importlib.reload(core)

class MyCoreClient(core.BaseCoreClient):
test_app = StacApi(
settings=ApiSettings(),
client=TestCoreClient(),
)

class MockRequest:
base_url = "http://test"
app = test_app.app

assert isinstance(TestCoreClient().landing_page(request=MockRequest()), response_type)
assert isinstance(TestCoreClient().get_collection("test"), response_type)
assert isinstance(TestCoreClient().all_collections(), response_type)
assert isinstance(TestCoreClient().get_item("test", "test"), response_type)
assert isinstance(TestCoreClient().item_collection("test"), response_type)
assert isinstance(
TestCoreClient().post_search(search.BaseSearchPostRequest()), response_type
)
assert isinstance(
TestCoreClient().get_search(),
response_type,
)


def test_filter_extension(TestCoreClient, item_dict):
"""Test if Filter Parameters are passed correctly."""

class FilterClient(TestCoreClient):
def post_search(
self, search_request: BaseSearchPostRequest, **kwargs
) -> response_model.ItemCollection:
search_request.collections = ["test"]
search_request.filter = {}
search_request.filter_crs = "EPSG:4326"
search_request.filter_lang = "cql2-text"

return response_model.ItemCollection(
type="FeatureCollection", features=[response_model.Item(**item_dict)]
)
Expand All @@ -51,53 +72,28 @@ def get_search(
self,
collections: Optional[List[str]] = None,
ids: Optional[List[str]] = None,
bbox: Optional[List[Union[float, int]]] = None,
bbox: Optional[List[NumType]] = None,
intersects: Optional[str] = None,
datetime: Optional[Union[str, datetime]] = None,
limit: Optional[int] = 10,
query: Optional[str] = None,
token: Optional[str] = None,
fields: Optional[List[str]] = None,
sortby: Optional[str] = None,
intersects: Optional[str] = None,
filter: Optional[str] = None,
filter_crs: Optional[str] = None,
filter_lang: Optional[str] = None,
**kwargs,
) -> response_model.ItemCollection:
# FIXME: hyphen alias for filter_crs and filter_lang are currently not working
# assert kwargs.get("filter_crs") == "EPSG:4326"
# assert kwargs.get("filter_lang") == "cql-test"
# Check if all filter parameters are passed correctly

return response_model.ItemCollection(
type="FeatureCollection", features=[response_model.Item(**item_dict)]
)
assert filter == "TEST"

def get_item(
self, item_id: str, collection_id: str, **kwargs
) -> response_model.Item:
return response_model.Item(**item_dict)

def all_collections(self, **kwargs) -> response_model.Collections:
return response_model.Collections(
collections=[response_model.Collection(**collection_dict)],
links=[
{"href": "test", "rel": "root"},
{"href": "test", "rel": "self"},
{"href": "test", "rel": "parent"},
],
)
# FIXME: https://github.com/stac-utils/stac-fastapi/issues/638
# hyphen alias for filter_crs and filter_lang are currently not working
# Query parameters `filter-crs` and `filter-lang`
# should be recognized by the API
# They are present in the `request.query_params` but not in the `kwargs`

def get_collection(
self, collection_id: str, **kwargs
) -> response_model.Collection:
return response_model.Collection(**collection_dict)
# assert filter_crs == "EPSG:4326"
# assert filter_lang == "cql2-text"

def item_collection(
self,
collection_id: str,
bbox: Optional[List[Union[float, int]]] = None,
datetime: Optional[Union[str, datetime]] = None,
limit: int = 10,
token: str = None,
**kwargs,
) -> response_model.ItemCollection:
return response_model.ItemCollection(
type="FeatureCollection", features=[response_model.Item(**item_dict)]
)
Expand All @@ -106,30 +102,11 @@ def item_collection(

test_app = StacApi(
settings=ApiSettings(),
client=MyCoreClient(post_request_model=post_request_model),
client=FilterClient(post_request_model=post_request_model),
search_get_request_model=create_get_request_model([FilterExtension()]),
search_post_request_model=post_request_model,
)

class MockRequest:
base_url = "http://test"
app = test_app.app

assert isinstance(MyCoreClient().landing_page(request=MockRequest()), response_type)
assert isinstance(MyCoreClient().get_collection("test"), response_type)
assert isinstance(MyCoreClient().all_collections(), response_type)
assert isinstance(MyCoreClient().get_item("test", "test"), response_type)
assert isinstance(MyCoreClient().item_collection("test"), response_type)
assert isinstance(
MyCoreClient().post_search(search.BaseSearchPostRequest()), response_type
)
assert isinstance(
MyCoreClient().get_search(
**{"filter_crs": "EPSG:4326", "filter_lang": "cql-test"}
),
response_type,
)

with TestClient(test_app.app) as client:
landing = client.get("/")
collection = client.get("/collections/test")
Expand All @@ -140,9 +117,22 @@ class MockRequest:
params={"limit": 10},
)
get_search = client.get(
"/search", params={"filter-crs": "EPSG:4326", "filter-lang": "cql-test"}
"/search",
params={
"filter": "TEST",
"filter-crs": "EPSG:4326",
"filter-lang": "cql2-text",
},
)
post_search = client.post(
"/search",
json={
"collections": ["test"],
"filter": {},
"filter-crs": "EPSG:4326",
"filter-lang": "cql2-text",
},
)
post_search = client.post("/search", json={"collections": ["test"]})

assert landing.status_code == 200, landing.text
assert collection.status_code == 200, collection.text
Expand Down
3 changes: 2 additions & 1 deletion stac_fastapi/api/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ def test_create_get_request_model():
datetime="2020-01-01T00:00:00Z",
limit=10,
filter="test==test",
# FIXME: hyphen aliases are not properly working
# FIXME: https://github.com/stac-utils/stac-fastapi/issues/638
# hyphen aliases are not properly working
# **{"filter-crs": "epsg:4326", "filter-lang": "cql2-text"},
)

Expand Down
13 changes: 3 additions & 10 deletions stac_fastapi/extensions/setup.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
"""stac_fastapi: extensions module."""

from distutils.util import convert_path

from setuptools import find_namespace_packages, setup

main_ns = {}
ver_path = convert_path("stac_fastapi/extensions/version.py")
with open(ver_path) as ver_file:
exec(ver_file.read(), main_ns)


with open("README.md") as f:
desc = f.read()

install_requires = [
f"stac-fastapi.types=={main_ns['__version__']}",
f"stac-fastapi.api=={main_ns['__version__']}",
"stac-fastapi.types==2.4.9",
"stac-fastapi.api==2.4.9",
]

extra_reqs = {
Expand Down Expand Up @@ -57,5 +50,5 @@
install_requires=install_requires,
tests_require=extra_reqs["dev"],
extras_require=extra_reqs,
version=main_ns["__version__"],
version="2.4.9",
)
9 changes: 1 addition & 8 deletions stac_fastapi/types/setup.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
"""stac_fastapi: types module."""

from distutils.util import convert_path

from setuptools import find_namespace_packages, setup

main_ns = {}
ver_path = convert_path("stac_fastapi/types/version.py")
with open(ver_path) as ver_file:
exec(ver_file.read(), main_ns)

with open("README.md") as f:
desc = f.read()

Expand Down Expand Up @@ -60,5 +53,5 @@
install_requires=install_requires,
tests_require=extra_reqs["dev"],
extras_require=extra_reqs,
version=main_ns["__version__"],
version="2.4.9",
)
8 changes: 2 additions & 6 deletions stac_fastapi/types/stac_fastapi/types/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from typing import Optional, Set

from pydantic_settings import BaseSettings
from pydantic_settings import BaseSettings, SettingsConfigDict


class ApiSettings(BaseSettings):
Expand Down Expand Up @@ -33,11 +33,7 @@ class ApiSettings(BaseSettings):

validate_response: bool = False

class Config:
"""Model config (https://pydantic-docs.helpmanual.io/usage/model_config/)."""

extra = "allow"
env_file = ".env"
model_config = SettingsConfigDict(env_file=".env", extra="allow")


class Settings:
Expand Down
Loading

0 comments on commit 5bdd615

Please sign in to comment.