Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

993 progress: convert 3 more files to pytest #1520

Merged
merged 17 commits into from
Feb 6, 2025
Merged
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
1,003 changes: 506 additions & 497 deletions tests/test_collection.py

Large diffs are not rendered by default.

110 changes: 49 additions & 61 deletions tests/test_item_assets.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import unittest

import pytest

from pystac import Collection
@@ -13,43 +11,18 @@
)


class TestItemAssets(unittest.TestCase):
def setUp(self) -> None:
self.maxDiff = None
self.collection = Collection.from_file(
TestCases.get_path("data-files/item-assets/example-landsat8.json")
)

def test_example(self) -> None:
collection = self.collection.clone()

self.assertEqual(len(collection.item_assets), 13)

self.assertEqual(
collection.item_assets["B1"],
ItemAssetDefinition(
{
"type": "image/tiff; application=geotiff",
"eo:bands": [
{
"name": "B1",
"common_name": "coastal",
"center_wavelength": 0.44,
"full_width_half_max": 0.02,
}
],
"title": "Coastal Band (B1)",
"description": "Coastal Band Top Of the Atmosphere",
}
),
)
@pytest.fixture
def landsat8_collection() -> Collection:
return Collection.from_file(
TestCases.get_path("data-files/item-assets/example-landsat8.json")
)

def test_set_using_dict(self) -> None:
collection = self.collection.clone()

self.assertEqual(len(collection.item_assets), 13)
def test_example(landsat8_collection: Collection) -> None:
assert len(landsat8_collection.item_assets) == 13

collection.item_assets["Bx"] = {
assert landsat8_collection.item_assets["B1"] == ItemAssetDefinition(
{
"type": "image/tiff; application=geotiff",
"eo:bands": [
{
@@ -61,20 +34,35 @@ def test_set_using_dict(self) -> None:
],
"title": "Coastal Band (B1)",
"description": "Coastal Band Top Of the Atmosphere",
} # type:ignore
}
)

self.assertEqual(collection.item_assets["B1"], collection.item_assets["Bx"])

def test_set_using_dict(landsat8_collection: Collection) -> None:
assert len(landsat8_collection.item_assets) == 13

class TestAssetDefinition(unittest.TestCase):
def setUp(self) -> None:
self.maxDiff = None
self.collection = Collection.from_file(
TestCases.get_path("data-files/item-assets/example-landsat8.json")
)
landsat8_collection.item_assets["Bx"] = {
"type": "image/tiff; application=geotiff",
"eo:bands": [
{
"name": "B1",
"common_name": "coastal",
"center_wavelength": 0.44,
"full_width_half_max": 0.02,
}
],
"title": "Coastal Band (B1)",
"description": "Coastal Band Top Of the Atmosphere",
} # type:ignore

assert (
landsat8_collection.item_assets["B1"] == landsat8_collection.item_assets["Bx"]
)

def test_eq(self) -> None:
assert self.collection.item_assets["B1"] != {"title": "Coastal Band (B1)"}

class TestAssetDefinition:
def test_eq(self, landsat8_collection: Collection) -> None:
assert landsat8_collection.item_assets["B1"] != {"title": "Coastal Band (B1)"}

def test_create(self) -> None:
title = "Coastal Band (B1)"
@@ -84,48 +72,48 @@ def test_create(self) -> None:
asset_defn = ItemAssetDefinition.create(
title=title, description=description, media_type=media_type, roles=roles
)
self.assertEqual(asset_defn.title, title)
self.assertEqual(asset_defn.description, description)
self.assertEqual(asset_defn.media_type, media_type)
self.assertEqual(asset_defn.roles, roles)
assert (
asset_defn.title,
asset_defn.description,
asset_defn.media_type,
asset_defn.roles,
) == (title, description, media_type, roles)

def test_title(self) -> None:
asset_defn = ItemAssetDefinition({})
title = "Very Important Asset"

asset_defn.title = title

self.assertEqual(asset_defn.title, title)
self.assertEqual(asset_defn.to_dict()["title"], title)
assert asset_defn.title == asset_defn.to_dict()["title"] == title

def test_description(self) -> None:
asset_defn = ItemAssetDefinition({})
description = "What an incredibly important asset this is!"

asset_defn.description = description

self.assertEqual(asset_defn.description, description)
self.assertEqual(asset_defn.to_dict()["description"], description)
assert (
asset_defn.description == asset_defn.to_dict()["description"] == description
)

def test_media_type(self) -> None:
asset_defn = ItemAssetDefinition({})
media_type = "application/json"

asset_defn.media_type = media_type

self.assertEqual(asset_defn.media_type, media_type)
self.assertEqual(asset_defn.to_dict()["type"], media_type)
assert asset_defn.media_type == asset_defn.to_dict()["type"] == media_type

def test_roles(self) -> None:
asset_defn = ItemAssetDefinition({})
roles = ["thumbnail"]

asset_defn.roles = roles

self.assertEqual(asset_defn.roles, roles)
self.assertEqual(asset_defn.to_dict()["roles"], roles)
assert asset_defn.roles == asset_defn.to_dict()["roles"] == roles

def test_set_owner(self) -> None:
def test_set_owner(self, landsat8_collection: Collection) -> None:
asset_definition = ItemAssetDefinition(
{
"type": "image/tiff; application=geotiff",
@@ -141,8 +129,8 @@ def test_set_owner(self) -> None:
"description": "Coastal Band Top Of the Atmosphere",
}
)
asset_definition.set_owner(self.collection)
assert asset_definition.owner == self.collection
asset_definition.set_owner(landsat8_collection)
assert asset_definition.owner == landsat8_collection


def test_extra_fields(collection: Collection) -> None:
300 changes: 161 additions & 139 deletions tests/test_item_collection.py
Original file line number Diff line number Diff line change
@@ -1,186 +1,208 @@
import json
import unittest
from copy import deepcopy
from os.path import relpath
from typing import Any, cast

import pytest

import pystac
from pystac import Item, StacIO
from pystac.item_collection import ItemCollection
from tests.utils import TestCases
from tests.utils.stac_io_mock import MockDefaultStacIO

SIMPLE_ITEM = TestCases.get_path("data-files/examples/1.0.0-RC1/simple-item.json")
CORE_ITEM = TestCases.get_path("data-files/examples/1.0.0-RC1/core-item.json")
EXTENDED_ITEM = TestCases.get_path("data-files/examples/1.0.0-RC1/extended-item.json")

ITEM_COLLECTION = TestCases.get_path(
"data-files/item-collection/sample-item-collection.json"
)


@pytest.fixture
def item_collection_dict() -> dict[str, Any]:
with open(ITEM_COLLECTION) as src:
return cast(dict[str, Any], json.load(src))


@pytest.fixture
def items(item_collection_dict: dict[str, Any]) -> list[Item]:
return [Item.from_dict(f) for f in item_collection_dict["features"]]


@pytest.fixture
def stac_io() -> StacIO:
return StacIO.default()


def test_item_collection_length(
item_collection_dict: dict[str, Any], items: list[Item]
) -> None:
item_collection = pystac.ItemCollection(items=items)

assert len(item_collection) == len(items)


def test_item_collection_iter(items: list[Item]) -> None:
expected_ids = [item.id for item in items]
item_collection = pystac.ItemCollection(items=items)

actual_ids = [item.id for item in item_collection]

assert expected_ids == actual_ids


def test_item_collection_get_item_by_index(items: list[Item]) -> None:
expected_id = items[0].id
item_collection = pystac.ItemCollection(items=items)

assert item_collection[0].id == expected_id


def test_item_collection_contains() -> None:
item = pystac.Item.from_file(SIMPLE_ITEM)
item_collection = pystac.ItemCollection(items=[item], clone_items=False)

class TestItemCollection(unittest.TestCase):
SIMPLE_ITEM = TestCases.get_path("data-files/examples/1.0.0-RC1/simple-item.json")
CORE_ITEM = TestCases.get_path("data-files/examples/1.0.0-RC1/core-item.json")
EXTENDED_ITEM = TestCases.get_path(
"data-files/examples/1.0.0-RC1/extended-item.json"
assert item in item_collection


def test_item_collection_extra_fields(items: list[Item]) -> None:
item_collection = pystac.ItemCollection(
items=items, extra_fields={"custom_field": "My value"}
)

ITEM_COLLECTION = TestCases.get_path(
"data-files/item-collection/sample-item-collection.json"
assert item_collection.extra_fields.get("custom_field") == "My value"


def test_item_collection_to_dict(items: list[Item]) -> None:
item_collection = pystac.ItemCollection(
items=items, extra_fields={"custom_field": "My value"}
)

def setUp(self) -> None:
self.maxDiff = None
with open(self.ITEM_COLLECTION) as src:
self.item_collection_dict = json.load(src)
self.items = [
pystac.Item.from_dict(f) for f in self.item_collection_dict["features"]
]
self.stac_io = pystac.StacIO.default()
d = item_collection.to_dict()

def test_item_collection_length(self) -> None:
item_collection = pystac.ItemCollection(items=self.items)
assert len(d["features"]) == len(items)
assert d.get("custom_field") == "My value"

self.assertEqual(len(item_collection), len(self.items))

def test_item_collection_iter(self) -> None:
expected_ids = [item.id for item in self.items]
item_collection = pystac.ItemCollection(items=self.items)
def test_item_collection_from_dict(items: list[Item]) -> None:
features = [item.to_dict(transform_hrefs=False) for item in items]
d = {
"type": "FeatureCollection",
"features": features,
"custom_field": "My value",
}
item_collection = pystac.ItemCollection.from_dict(d)
expected = len(features)
assert expected == len(item_collection.items)
assert item_collection.extra_fields.get("custom_field") == "My value"

actual_ids = [item.id for item in item_collection]

self.assertListEqual(expected_ids, actual_ids)
def test_clone_item_collection() -> None:
item_collection_1 = pystac.ItemCollection.from_file(ITEM_COLLECTION)
item_collection_2 = item_collection_1.clone()

def test_item_collection_get_item_by_index(self) -> None:
expected_id = self.items[0].id
item_collection = pystac.ItemCollection(items=self.items)
item_ids_1 = [item.id for item in item_collection_1]
item_ids_2 = [item.id for item in item_collection_2]

self.assertEqual(item_collection[0].id, expected_id)
# All items from the original collection should be in the clone...
assert item_ids_1 == item_ids_2
# ... but they should not be the same objects
assert item_collection_1[0] is not item_collection_2[0]

def test_item_collection_contains(self) -> None:
item = pystac.Item.from_file(self.SIMPLE_ITEM)
item_collection = pystac.ItemCollection(items=[item], clone_items=False)

self.assertIn(item, item_collection)
def test_raise_error_for_invalid_object(stac_io: StacIO) -> None:
item_dict = stac_io.read_json(SIMPLE_ITEM)

def test_item_collection_extra_fields(self) -> None:
item_collection = pystac.ItemCollection(
items=self.items, extra_fields={"custom_field": "My value"}
)
with pytest.raises(pystac.STACTypeError):
_ = pystac.ItemCollection.from_dict(item_dict)

self.assertEqual(item_collection.extra_fields.get("custom_field"), "My value")

def test_item_collection_to_dict(self) -> None:
item_collection = pystac.ItemCollection(
items=self.items, extra_fields={"custom_field": "My value"}
def test_from_relative_path() -> None:
_ = pystac.ItemCollection.from_file(
relpath(
TestCases.get_path("data-files/item-collection/sample-item-collection.json")
)
)

d = item_collection.to_dict()

self.assertEqual(len(d["features"]), len(self.items))
self.assertEqual(d.get("custom_field"), "My value")

def test_item_collection_from_dict(self) -> None:
features = [item.to_dict(transform_hrefs=False) for item in self.items]
d = {
"type": "FeatureCollection",
"features": features,
"custom_field": "My value",
}
item_collection = pystac.ItemCollection.from_dict(d)
expected = len(features)
self.assertEqual(expected, len(item_collection.items))
self.assertEqual(item_collection.extra_fields.get("custom_field"), "My value")

def test_clone_item_collection(self) -> None:
item_collection_1 = pystac.ItemCollection.from_file(self.ITEM_COLLECTION)
item_collection_2 = item_collection_1.clone()

item_ids_1 = [item.id for item in item_collection_1]
item_ids_2 = [item.id for item in item_collection_2]

# All items from the original collection should be in the clone...
self.assertListEqual(item_ids_1, item_ids_2)
# ... but they should not be the same objects
self.assertIsNot(item_collection_1[0], item_collection_2[0])

def test_raise_error_for_invalid_object(self) -> None:
item_dict = self.stac_io.read_json(self.SIMPLE_ITEM)

with self.assertRaises(pystac.STACTypeError):
_ = pystac.ItemCollection.from_dict(item_dict)

def test_from_relative_path(self) -> None:
_ = pystac.ItemCollection.from_file(
relpath(
TestCases.get_path(
"data-files/item-collection/sample-item-collection.json"
)
)
)

def test_from_list_of_dicts(self) -> None:
item_dict = self.stac_io.read_json(self.SIMPLE_ITEM)
item_collection = pystac.ItemCollection(items=[item_dict], clone_items=True)
def test_from_list_of_dicts(stac_io: StacIO) -> None:
item_dict = stac_io.read_json(SIMPLE_ITEM)
item_collection = pystac.ItemCollection(items=[item_dict], clone_items=True)

self.assertEqual(item_collection[0].id, item_dict.get("id"))
assert item_collection[0].id == item_dict.get("id")

def test_add_item_collections(self) -> None:
item_1 = pystac.Item.from_file(self.SIMPLE_ITEM)
item_2 = pystac.Item.from_file(self.EXTENDED_ITEM)
item_3 = pystac.Item.from_file(self.CORE_ITEM)

item_collection_1 = pystac.ItemCollection(items=[item_1, item_2])
item_collection_2 = pystac.ItemCollection(items=[item_2, item_3])
def test_add_item_collections() -> None:
item_1 = pystac.Item.from_file(SIMPLE_ITEM)
item_2 = pystac.Item.from_file(EXTENDED_ITEM)
item_3 = pystac.Item.from_file(CORE_ITEM)

combined = item_collection_1 + item_collection_2
item_collection_1 = pystac.ItemCollection(items=[item_1, item_2])
item_collection_2 = pystac.ItemCollection(items=[item_2, item_3])

self.assertEqual(len(combined), 4)
combined = item_collection_1 + item_collection_2

def test_add_other_raises_error(self) -> None:
item_collection = pystac.ItemCollection.from_file(self.ITEM_COLLECTION)
assert len(combined) == 4

with self.assertRaises(TypeError):
_ = item_collection + 2

def test_identify_0_8_itemcollection_type(self) -> None:
itemcollection_path = TestCases.get_path(
"data-files/examples/0.8.1/item-spec/"
"examples/itemcollection-sample-full.json"
)
itemcollection_dict = pystac.StacIO.default().read_json(itemcollection_path)
def test_add_other_raises_error() -> None:
item_collection = pystac.ItemCollection.from_file(ITEM_COLLECTION)

self.assertTrue(
pystac.ItemCollection.is_item_collection(itemcollection_dict),
msg="Did not correctly identify valid STAC 0.8 ItemCollection.",
)
with pytest.raises(TypeError):
_ = item_collection + 2

def test_identify_0_9_itemcollection(self) -> None:
itemcollection_path = TestCases.get_path(
"data-files/examples/0.9.0/item-spec/"
"examples/itemcollection-sample-full.json"
)
itemcollection_dict = pystac.StacIO.default().read_json(itemcollection_path)

self.assertTrue(
pystac.ItemCollection.is_item_collection(itemcollection_dict),
msg="Did not correctly identify valid STAC 0.9 ItemCollection.",
)
def test_identify_0_8_itemcollection_type(stac_io: StacIO) -> None:
itemcollection_path = TestCases.get_path(
"data-files/examples/0.8.1/item-spec/"
"examples/itemcollection-sample-full.json"
)
itemcollection_dict = stac_io.read_json(itemcollection_path)

assert pystac.ItemCollection.is_item_collection(
itemcollection_dict
), "Did not correctly identify valid STAC 0.8 ItemCollection."


def test_identify_0_9_itemcollection(stac_io: StacIO) -> None:
itemcollection_path = TestCases.get_path(
"data-files/examples/0.9.0/item-spec/"
"examples/itemcollection-sample-full.json"
)
itemcollection_dict = stac_io.read_json(itemcollection_path)

assert pystac.ItemCollection.is_item_collection(
itemcollection_dict
), "Did not correctly identify valid STAC 0.9 ItemCollection."


def test_from_dict_preserves_dict(item_collection_dict: dict[str, Any]) -> None:
param_dict = deepcopy(item_collection_dict)

# test that the parameter is preserved
_ = ItemCollection.from_dict(param_dict)
assert param_dict == item_collection_dict

def test_from_dict_preserves_dict(self) -> None:
param_dict = deepcopy(self.item_collection_dict)
# assert that the parameter is preserved regardless of
# preserve_dict
_ = ItemCollection.from_dict(param_dict, preserve_dict=False)
assert param_dict == item_collection_dict

# test that the parameter is preserved
_ = ItemCollection.from_dict(param_dict)
self.assertEqual(param_dict, self.item_collection_dict)

# assert that the parameter is preserved regardless of
# preserve_dict
_ = ItemCollection.from_dict(param_dict, preserve_dict=False)
self.assertEqual(param_dict, self.item_collection_dict)
def test_from_dict_sets_root(item_collection_dict: dict[str, Any]) -> None:
param_dict = deepcopy(item_collection_dict)
catalog = pystac.Catalog(id="test", description="test desc")
item_collection = ItemCollection.from_dict(param_dict, root=catalog)
for item in item_collection.items:
assert item.get_root() == catalog

def test_from_dict_sets_root(self) -> None:
param_dict = deepcopy(self.item_collection_dict)
catalog = pystac.Catalog(id="test", description="test desc")
item_collection = ItemCollection.from_dict(param_dict, root=catalog)
for item in item_collection.items:
self.assertEqual(item.get_root(), catalog)

def test_to_dict_does_not_read_root_link_of_items(self) -> None:
with MockDefaultStacIO() as mock_stac_io:
item_collection = pystac.ItemCollection.from_file(self.ITEM_COLLECTION)
def test_to_dict_does_not_read_root_link_of_items() -> None:
with MockDefaultStacIO() as mock_stac_io:
item_collection = pystac.ItemCollection.from_file(ITEM_COLLECTION)

item_collection.to_dict()
item_collection.to_dict()

self.assertEqual(mock_stac_io.mock.read_text.call_count, 1)
assert mock_stac_io.mock.read_text.call_count == 1