Skip to content

Commit

Permalink
More tests, fix docs
Browse files Browse the repository at this point in the history
  • Loading branch information
jsignell committed Feb 7, 2025
1 parent f51b872 commit 9ffd92b
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 4 deletions.
1 change: 1 addition & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@
rst_epilog = f".. |stac_version| replace:: {STACVersion.DEFAULT_STAC_VERSION}"

nitpick_ignore = [
("py:class", "C"),
("py:class", "Datetime"),
("py:class", "L"),
("py:class", "pystac.summaries.T"),
Expand Down
9 changes: 5 additions & 4 deletions pystac/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -718,14 +718,15 @@ def from_items(
id: str | None = None,
strategy: HrefLayoutStrategy | None = None,
) -> C:
"""Create a :class:`Collection` from items or an :class:`ItemCollection`.
"""Create a :class:`Collection` from iterable of items or an
:class:`~pystac.ItemCollection`.
Will try to pull collection attributes from :attr:`ItemCollection.extra_fields`
and items when possible.
Will try to pull collection attributes from
:attr:`~pystac.ItemCollection.extra_fields` and items when possible.
Args:
items : Iterable of :class:`~pystac.Item` instances to include in the
:class:`ItemCollection`. This can be an :class:`pystac.ItemCollection`.
:class:`Collection`. This can be a :class:`~pystac.ItemCollection`.
id : Identifier for the collection. If not set, must be available on the
items and they must all match.
strategy : The layout strategy to use for setting the
Expand Down
55 changes: 55 additions & 0 deletions tests/test_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -765,3 +765,58 @@ def test_from_items_without_collection_id() -> None:

collection = Collection.from_items([item1], id="test-collection")
assert collection.id == "test-collection"


def test_from_items_with_collection_ids() -> None:
item1 = Item(
id="test-item-1",
geometry=ARBITRARY_GEOM,
bbox=[-10, -20, 0, -10],
datetime=datetime(2000, 2, 1, 12, 0, 0, 0, tzinfo=tz.UTC),
collection="test-collection-1",
properties={},
)
item2 = Item(
id="test-item-2",
geometry=ARBITRARY_GEOM,
bbox=[-15, -20, 0, -10],
datetime=datetime(2000, 2, 1, 13, 0, 0, 0, tzinfo=tz.UTC),
collection="test-collection-2",
properties={},
)

with pytest.raises(ValueError, match="Collection id must be defined."):
Collection.from_items([item1, item2])

collection = Collection.from_items([item1, item2], id="test-collection")
assert collection.id == "test-collection"


def test_from_items_with_different_values() -> None:
item1 = Item(
id="test-item-1",
geometry=ARBITRARY_GEOM,
bbox=[-10, -20, 0, -10],
datetime=datetime(2000, 2, 1, 12, 0, 0, 0, tzinfo=tz.UTC),
properties={"title": "Test Item 1"},
)
item2 = Item(
id="test-item-2",
geometry=ARBITRARY_GEOM,
bbox=[-15, -20, 0, -10],
datetime=datetime(2000, 2, 1, 13, 0, 0, 0, tzinfo=tz.UTC),
properties={"title": "Test Item 2"},
)

collection = Collection.from_items([item1, item2], id="test_collection")
assert collection.title is None


def test_from_items_with_providers(sample_item_collection: ItemCollection) -> None:
sample_item_collection.extra_fields["providers"] = [{"name": "pystac"}]

collection = Collection.from_items(sample_item_collection)
assert collection.providers and len(collection.providers) == 1

provider = collection.providers[0]
assert provider and provider.name == "pystac"

0 comments on commit 9ffd92b

Please sign in to comment.