|
19 | 19 | Collection, |
20 | 20 | Extent, |
21 | 21 | Item, |
| 22 | + ItemCollection, |
22 | 23 | Provider, |
23 | 24 | SpatialExtent, |
24 | 25 | TemporalExtent, |
@@ -711,3 +712,58 @@ def test_permissive_temporal_extent_deserialization(collection: Collection) -> N |
711 | 712 | ]["interval"][0] |
712 | 713 | with pytest.warns(UserWarning): |
713 | 714 | Collection.from_dict(collection_dict) |
| 715 | + |
| 716 | + |
| 717 | +@pytest.mark.parametrize("fixture_name", ("sample_item_collection", "sample_items")) |
| 718 | +def test_from_items(fixture_name: str, request: pytest.FixtureRequest) -> None: |
| 719 | + items = request.getfixturevalue(fixture_name) |
| 720 | + collection = Collection.from_items(items) |
| 721 | + |
| 722 | + for item in items: |
| 723 | + assert collection.id == item.collection_id |
| 724 | + assert collection.extent.spatial.bboxes[0][0] <= item.bbox[0] |
| 725 | + assert collection.extent.spatial.bboxes[0][1] <= item.bbox[1] |
| 726 | + assert collection.extent.spatial.bboxes[0][2] >= item.bbox[2] |
| 727 | + assert collection.extent.spatial.bboxes[0][3] >= item.bbox[3] |
| 728 | + |
| 729 | + start = collection.extent.temporal.intervals[0][0] |
| 730 | + end = collection.extent.temporal.intervals[0][1] |
| 731 | + assert start and start <= datetime.fromisoformat( |
| 732 | + item.properties["start_datetime"] |
| 733 | + ) |
| 734 | + assert end and end >= datetime.fromisoformat(item.properties["end_datetime"]) |
| 735 | + |
| 736 | + if isinstance(items, ItemCollection): |
| 737 | + expected = {(link["rel"], link["href"]) for link in items.extra_fields["links"]} |
| 738 | + actual = {(link.rel, link.href) for link in collection.links} |
| 739 | + assert expected.issubset(actual) |
| 740 | + |
| 741 | + |
| 742 | +def test_from_items_pulls_from_properties() -> None: |
| 743 | + item1 = Item( |
| 744 | + id="test-item-1", |
| 745 | + geometry=ARBITRARY_GEOM, |
| 746 | + bbox=[-10, -20, 0, -10], |
| 747 | + datetime=datetime(2000, 2, 1, 12, 0, 0, 0, tzinfo=tz.UTC), |
| 748 | + collection="test-collection-1", |
| 749 | + properties={"title": "Test Item", "description": "Extra words describing"}, |
| 750 | + ) |
| 751 | + collection = Collection.from_items([item1]) |
| 752 | + assert collection.id == item1.collection_id |
| 753 | + assert collection.title == item1.properties["title"] |
| 754 | + assert collection.description == item1.properties["description"] |
| 755 | + |
| 756 | + |
| 757 | +def test_from_items_without_collection_id() -> None: |
| 758 | + item1 = Item( |
| 759 | + id="test-item-1", |
| 760 | + geometry=ARBITRARY_GEOM, |
| 761 | + bbox=[-10, -20, 0, -10], |
| 762 | + datetime=datetime(2000, 2, 1, 12, 0, 0, 0, tzinfo=tz.UTC), |
| 763 | + properties={}, |
| 764 | + ) |
| 765 | + with pytest.raises(ValueError, match="Collection id must be defined."): |
| 766 | + Collection.from_items([item1]) |
| 767 | + |
| 768 | + collection = Collection.from_items([item1], id="test-collection") |
| 769 | + assert collection.id == "test-collection" |
0 commit comments