From 9ffd92bf44d98efee076138b0afee750c3d0c002 Mon Sep 17 00:00:00 2001 From: Julia Signell Date: Fri, 7 Feb 2025 15:39:34 -0500 Subject: [PATCH] More tests, fix docs --- docs/conf.py | 1 + pystac/collection.py | 9 ++++--- tests/test_collection.py | 55 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 4 deletions(-) diff --git a/docs/conf.py b/docs/conf.py index d6a3a667d..3c219eb28 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -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"), diff --git a/pystac/collection.py b/pystac/collection.py index d68a97b4d..d144e807d 100644 --- a/pystac/collection.py +++ b/pystac/collection.py @@ -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 diff --git a/tests/test_collection.py b/tests/test_collection.py index 170520772..2279e21ce 100644 --- a/tests/test_collection.py +++ b/tests/test_collection.py @@ -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"