Skip to content

Commit

Permalink
Merge pull request #191 from raphaelrpl/latest
Browse files Browse the repository at this point in the history
⬆️ Update bdc-catalog to 1.0.2 and 🧪 unittest code coverage
  • Loading branch information
raphaelrpl authored May 17, 2023
2 parents c9462ca + b2d0d30 commit 64fe96b
Show file tree
Hide file tree
Showing 7 changed files with 259 additions and 21 deletions.
4 changes: 3 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ Version 1.0.2
-------------

- Review way to represent deprecated collections and versioning (`#88 <https://github.com/brazil-data-cube/bdc-stac/issues/88>`_)
- Add fixture for tests
- Add fixture for tests and remove warnings PEP8
- Upgrade Docker base image for python:3.11
- Upgrade bdc-catalog to 1.0.2 to improve test coverage related successor/predecessor
- Add option ``bdc:public`` to display collection availability


Version 1.0.1 (2022-12-06)
Expand Down
17 changes: 10 additions & 7 deletions bdc_stac/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ def get_collection_items(

@lru_cache()
def get_collection_eo(collection_id):
"""Get Collection Eletro-Optical properties.
"""Get Collection Electro-Optical properties.
.. note::
Expand Down Expand Up @@ -347,7 +347,9 @@ def get_collections(collection_id=None, roles=None, assets_kwargs=None):

if collection_id and result: # shallow query property to generate traceability
where = [Collection.is_available.is_(True), _add_roles_constraint(roles)]
_collection_relation = db.session.query(Collection.id, Collection.identifier, Collection.title).filter(*where).all()
_collection_relation = (
db.session.query(Collection.id, Collection.identifier, Collection.title).filter(*where).all()
)
collection_map = {row.id: row for row in _collection_relation}

collections = list()
Expand Down Expand Up @@ -379,7 +381,7 @@ def get_collections(collection_id=None, roles=None, assets_kwargs=None):
extra_links.append(_collection_link(predecessor, rel="predecessor-version", qs=assets_kwargs))

meta = r.Collection.metadata_
deprecated = successor is not None or (meta and meta.get("deprecated", False))
deprecated = successor is not None or bool(meta and meta.get("deprecated", False))

collection = {
"id": r.Collection.identifier,
Expand All @@ -396,6 +398,7 @@ def get_collections(collection_id=None, roles=None, assets_kwargs=None):
"item_assets": r.Collection.item_assets,
"properties": r.Collection.properties or {},
"bdc:type": r.Collection.collection_type,
"bdc:public": r.Collection.is_public,
}
collection["properties"]["created"] = r.Collection.created.strftime(DATETIME_RFC339)
collection["properties"]["updated"] = r.Collection.updated.strftime(DATETIME_RFC339)
Expand Down Expand Up @@ -669,7 +672,7 @@ def create_query_filter(query):
def parse_fields_parameter(fields: Optional[str] = None):
"""Parse the string parameter `fields` to include/exclude certain fields in response.
Follow the `STAC API Fields Fragment <https://github.com/radiantearth/stac-api-spec/blob/v1.0.0-rc.1/fragments/fields/README.md>`.
Follow the `STAC API Fields Fragment <https://github.com/radiantearth/stac-api-spec/blob/v1.0.0-rc.1/fragments/fields/README.md>`_.
"""
if fields is None:
return [], []
Expand Down Expand Up @@ -710,17 +713,17 @@ def resolve_base_file_root_url() -> str:
"""Retrieve base URL used as STAC BASE URL ROOT for items from HTTP header.
Note:
This method uses ``flask.request`` object to check for X-Script-Name in header.
This method uses ``flask.request`` object to check for ``X-Script-Name`` in header.
Make sure you are inside flask app context.
"""
return request.headers.get("X-Script-Name", BDC_STAC_FILE_ROOT)


def resolve_stac_url() -> str:
"""Retrieve base URL used as STAC BASE URL ROOT for items from HTTP header.
"""Retrieve base URL used as STAC URL for items from HTTP header.
Note:
This method uses ``flask.request`` object to check for X-Stac-Url in header.
This method uses ``flask.request`` object to check for ``X-Stac-Url`` in header.
Make sure you are inside flask app context.
"""
return request.headers.get("X-Stac-Url", BDC_STAC_BASE_URL).rstrip("/")
Expand Down
6 changes: 5 additions & 1 deletion bdc_stac/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ def before_request():
request.assets_kwargs = ""

if config.BDC_STAC_ASSETS_ARGS:
assets_kwargs = {arg: request.args.get(arg) for arg in config.BDC_STAC_ASSETS_ARGS.split(",") if request.args.get(arg) is not None}
assets_kwargs = {
arg: request.args.get(arg)
for arg in config.BDC_STAC_ASSETS_ARGS.split(",")
if request.args.get(arg) is not None
}
if "access_token" in request.args:
assets_kwargs["access_token"] = request.args.get("access_token")
assets_kwargs = "?" + urlencode(assets_kwargs) if urlencode(assets_kwargs) else ""
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"coveralls>=1.8",
"pytest>=5.2",
"pytest-cov>=2.8",
"pytest-pep8>=1.0",
"pytest-pycodestyle>=2.3",
"pydocstyle>=4.0",
"isort>4.3",
"check-manifest>=0.40",
Expand Down Expand Up @@ -66,7 +66,7 @@
"Shapely>=1.6",
"packaging>=20.4",
"psycopg2-binary>=2.8.4",
"bdc-catalog @ git+https://github.com/brazil-data-cube/[email protected].1",
"bdc-catalog @ git+https://github.com/brazil-data-cube/[email protected].2",
"bdc-auth-client @ git+https://github.com/brazil-data-cube/[email protected]",
]

Expand Down
33 changes: 29 additions & 4 deletions tests/bdc_stac_tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import gzip
import json
import os
from unittest import mock

from packaging import version

Expand Down Expand Up @@ -57,10 +58,7 @@ def test_collections(self, client):
assert "links" in data

def test_collection(self, client):
response = client.get("/collections/S2-16D-2")
assert response.status_code == 200

data = response.get_json()
data = self._get_collection("S2-16D-2", client)

assert data["type"] == "Collection"
assert data["id"] == "S2-16D-2"
Expand All @@ -70,6 +68,19 @@ def test_collection(self, client):
eo_uri = config.get_stac_extensions("eo")[0]
assert eo_uri in data["stac_extensions"]

def test_collection_successor_predecessor(self, client):
def _assert_collection_versioning(collection_name, kind):
collection = self._get_collection(collection_name, client)
found = None
for link in collection["links"]:
if link["rel"] == kind:
found = link

assert found is not None

_assert_collection_versioning("S2-16D-2", "predecessor-version")
_assert_collection_versioning("S2-16D-1", "successor-version")

def test_collection_items(self, client):
response = client.get("/collections/S2-16D-2/items?limit=20")

Expand Down Expand Up @@ -226,3 +237,17 @@ def test_search_pagination_get(self, client):
assert "page=3" in link["href"]
elif link["rel"] == "prev":
assert "page=1" in link["href"]

def _get_collection(self, name, client):
response = client.get(f"/collections/{name}")
assert response.status_code == 200

data = response.get_json()
return data

@mock.patch("bdc_stac.controller.session")
def test_internal_server_error(self, mock_db, client):
mock_db.query.side_effect = RuntimeError("Unknown error")

response = client.get(f"/collections/S2-16D-2")
assert response.status_code == 500
33 changes: 27 additions & 6 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
"""Define the configuration for pytest and fixtures."""

import json
import os

import shapely
from bdc_catalog.models import GridRefSys, Item, Tile, db
from bdc_catalog.models import Collection, GridRefSys, Item, Tile, db
from bdc_catalog.utils import create_collection, create_item, geom_to_wkb
from pytest import fixture

Expand Down Expand Up @@ -71,10 +72,7 @@ def _setup_grid():
db.session.commit()


def _setup_initial_data():
with open("tests/fixtures/S2-16D-2.json") as fd:
data = json.load(fd)

def _setup_collection(data) -> Collection:
items = data.pop("items", [])
collection, _ = create_collection(**data)
item_map = [i.name for i in db.session.query(Item.name).filter(Item.collection_id == collection.id).all()]
Expand All @@ -94,4 +92,27 @@ def _setup_initial_data():
item["footprint"] = shapely.geometry.shape(footprint)
tile_id = tile_map.get(item.get("tile_id"))
item["tile_id"] = tile_id
_ = create_item(collection_id=collection.id, **item)
item_obj = create_item(collection_id=collection.id, **item)
item_obj.metadata_ = item.get("metadata", {})
item_obj.save()

return collection


def _setup_initial_data():
def _read(filename):
with open(filename) as fd:
data = json.load(fd)
return data

fixture_dir = "tests/fixtures"
json_files = {filename: _read(os.path.join(fixture_dir, filename)) for filename in os.listdir(fixture_dir)}

collection_v1 = _setup_collection(json_files["S2-16D-1.json"])
collection_v2 = _setup_collection(json_files["S2-16D-2.json"])

with db.session.begin_nested():
collection_v1.version_successor = collection_v2.id
collection_v2.version_predecessor = collection_v1.id

db.session.commit()
183 changes: 183 additions & 0 deletions tests/fixtures/S2-16D-1.json

Large diffs are not rendered by default.

0 comments on commit 64fe96b

Please sign in to comment.