Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions marble_api/versions/v1/data_request/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from pydantic.json_schema import SkipJsonSchema
from stac_pydantic.item import Item
from stac_pydantic.links import Links
from stac_pydantic.shared import Asset
from typing_extensions import Annotated

from marble_api.utils.geojson import (
Expand Down Expand Up @@ -59,14 +60,12 @@ class DataRequest(BaseModel):
temporal: Temporal
tz_offset: SkipJsonSchema[list[float] | None] = Field(default=None, exclude=True)
links: Links
path: str
assets: dict[str, Asset]
contact: EmailStr
additional_paths: list[str] = []
variables: list[str] = []
extra_properties: dict[str, str] = {}
model_config = ConfigDict(populate_by_name=True, arbitrary_types_allowed=True)

@field_validator("title", "description", "authors", "path", "contact")
@field_validator("title", "description", "authors", "assets", "contact")
@classmethod
def min_length_if_set(cls, value: Sized | None, info: ValidationInfo) -> Sized | None:
"""Raise an error if the value is not None and is empty."""
Expand Down Expand Up @@ -138,7 +137,7 @@ def stac_item(self) -> Item:
"bbox": None,
"properties": dict(self.extra_properties), # TODO: add more
"links": self.links.model_dump(),
"assets": {}, # TODO: determine assets from other fields
"assets": {key: asset.model_dump() for key, asset in self.assets.items()},
}

# STAC spec recommends including datetime even if using start_datetime and end_datetime
Expand Down
20 changes: 16 additions & 4 deletions test/faker_providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,15 @@ def temporal(self):
def link(self):
return {"href": self.generator.uri(), "rel": self.generator.word(), "type": self.generator.mime_type()}

def asset(self):
return {
"href": self.generator.uri(),
"type": self.generator.mime_type(),
"title": self.generator.word(),
"description": self.generator.sentence(),
"roles": self.generator.get_words_list(),
}

def _data_request_inputs(self, unset=None):
inputs = dict(
id=bson.ObjectId(),
Expand All @@ -220,11 +229,14 @@ def _data_request_inputs(self, unset=None):
geometry=self.collapsible_geojson(),
temporal=self.temporal(),
links=[self.link() for _ in range(self.generator.random.randint(0, 10))],
path=self.generator.file_path(),
assets={
key: self.asset()
for key in self.generator.pylist(
nb_elements=self.generator.pyint(1, 10), variable_nb_elements=False, value_types=[str]
)
},
contact=self.generator.email(),
additional_paths=[self.generator.file_path() for _ in range(self.generator.random.randint(0, 10))],
variables=([] if self.generator.pybool(10) else self.generator.pylist(allowed_types=[str])),
extra_properties=({} if self.generator.pybool(10) else self.generator.pydict(allowed_types=[str])),
extra_properties=({} if self.generator.pybool(10) else self.generator.pydict(value_types=[str])),
)
if unset:
for field in unset:
Expand Down
8 changes: 2 additions & 6 deletions test/unit/versions/v1/data_request/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def fake_class(self, fake):
def test_id_dumped(self, fake_class):
assert "id" not in fake_class().model_dump()

@pytest.mark.parametrize("field", ["title", "description", "authors", "path", "contact"])
@pytest.mark.parametrize("field", ["title", "description", "authors", "assets", "contact"])
def test_text_fields_not_empty(self, fake_class, field):
with pytest.raises(ValidationError):
fake_class(**{field: ""})
Expand All @@ -46,10 +46,8 @@ def test_text_fields_not_empty(self, fake_class, field):
"authors",
"temporal",
"links",
"path",
"assets",
"contact",
"additional_paths",
"variables",
"extra_properties",
],
)
Expand All @@ -68,8 +66,6 @@ def test_user_field_present_when_serialized(self, fake_class, value):
"field",
[
"description",
"additional_paths",
"variables",
"extra_properties",
],
)
Expand Down
Loading