Skip to content

Commit

Permalink
Allow root to be set during to_dict
Browse files Browse the repository at this point in the history
There was existing unused arguments for seting the root on
to_dict. This commit implements and tests that the root is set to the
parameter if present, and adds the parameter to ItemCollection, which
itself does not have a root but instead passes the root onto each of
the Items parsed during the to_dict call.

Related to #546
  • Loading branch information
lossyrob committed Jul 9, 2021
1 parent 12eff70 commit 28c282e
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 2 deletions.
3 changes: 3 additions & 0 deletions pystac/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,9 @@ def from_dict(
if link["rel"] != pystac.RelType.SELF or href is None:
cat.add_link(Link.from_dict(link))

if root:
cat.set_root(root)

return cat

def full_copy(
Expand Down
3 changes: 3 additions & 0 deletions pystac/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,9 @@ def from_dict(
for asset_key, asset_dict in assets.items():
collection.add_asset(asset_key, Asset.from_dict(asset_dict))

if root:
collection.set_root(root)

return collection

def get_assets(self) -> Dict[str, Asset]:
Expand Down
3 changes: 3 additions & 0 deletions pystac/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -970,6 +970,9 @@ def from_dict(
asset.set_owner(item)
item.assets[k] = asset

if root:
item.set_root(root)

return item

@property
Expand Down
7 changes: 5 additions & 2 deletions pystac/item_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,10 @@ def clone(self) -> "ItemCollection":

@classmethod
def from_dict(
cls, d: Dict[str, Any], preserve_dict: bool = True
cls,
d: Dict[str, Any],
preserve_dict: bool = True,
root: Optional[pystac.Catalog] = None,
) -> "ItemCollection":
"""Creates a :class:`ItemCollection` instance from a dictionary.
Expand All @@ -151,7 +154,7 @@ def from_dict(
raise STACTypeError("Dict is not a valid ItemCollection")

items = [
pystac.Item.from_dict(item, preserve_dict=preserve_dict)
pystac.Item.from_dict(item, preserve_dict=preserve_dict, root=root)
for item in d.get("features", [])
]
extra_fields = {k: v for k, v in d.items() if k not in ("features", "type")}
Expand Down
8 changes: 8 additions & 0 deletions tests/test_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,14 @@ def test_from_dict_preserves_dict(self) -> None:
_ = Catalog.from_dict(param_dict, preserve_dict=False)
self.assertNotEqual(param_dict, catalog_dict)

def test_from_dict_set_root(self) -> None:
path = TestCases.get_path("data-files/catalogs/test-case-1/catalog.json")
with open(path) as f:
cat_dict = json.load(f)
root_cat = pystac.Catalog(id="test", description="test desc")
collection = Catalog.from_dict(cat_dict, root=root_cat)
self.assertIs(collection.get_root(), root_cat)

def test_read_remote(self) -> None:
# TODO: Move this URL to the main stac-spec repo once the example JSON is fixed.
catalog_url = (
Expand Down
8 changes: 8 additions & 0 deletions tests/test_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,14 @@ def test_from_dict_preserves_dict(self) -> None:
_ = Collection.from_dict(param_dict, preserve_dict=False)
self.assertNotEqual(param_dict, collection_dict)

def test_from_dict_set_root(self) -> None:
path = TestCases.get_path("data-files/examples/hand-0.8.1/collection.json")
with open(path) as f:
collection_dict = json.load(f)
catalog = pystac.Catalog(id="test", description="test desc")
collection = Collection.from_dict(collection_dict, root=catalog)
self.assertIs(collection.get_root(), catalog)

def test_schema_summary(self) -> None:
collection = pystac.Collection.from_file(
TestCases.get_path(
Expand Down
6 changes: 6 additions & 0 deletions tests/test_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ def test_to_from_dict(self) -> None:
_ = Item.from_dict(param_dict, preserve_dict=False)
self.assertNotEqual(param_dict, item_dict)

def test_from_dict_set_root(self) -> None:
item_dict = self.get_example_item_dict()
catalog = pystac.Catalog(id="test", description="test desc")
item = Item.from_dict(item_dict, root=catalog)
self.assertIs(item.get_root(), catalog)

def test_set_self_href_does_not_break_asset_hrefs(self) -> None:
cat = TestCases.test_case_2()
for item in cat.get_all_items():
Expand Down
7 changes: 7 additions & 0 deletions tests/test_item_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,10 @@ def test_from_dict_preserves_dict(self) -> None:
# non-default parameter
_ = ItemCollection.from_dict(param_dict, preserve_dict=False)
self.assertNotEqual(param_dict, self.item_collection_dict)

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)

0 comments on commit 28c282e

Please sign in to comment.