Skip to content

Commit a2a8e5b

Browse files
jsignellgadomski
andauthored
Make get_all_collections properly recursive (#1361)
* Make `get_all_collections` properly recursive * Update changelog --------- Co-authored-by: Pete Gadomski <[email protected]>
1 parent 52d59b3 commit a2a8e5b

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
- Allow object ID as input for getting APILayoutStrategy hrefs and add `items`, `collections`, `search`, `conformance`, `service_desc` and `service_doc` href methods. ([#1335](https://github.com/stac-utils/pystac/pull/1335))
88
- Update docstring of `name` argument to `Classification.apply` and `Classification.create` to agree with extension specification. ([#1356](https://github.com/stac-utils/pystac/pull/1356))
99

10+
### Fixed
11+
12+
- Make `get_all_collections` properly recursive ([#1361](https://github.com/stac-utils/pystac/pull/1361))
13+
1014
## [v1.10.1] - 2024-05-03
1115

1216
### Fixed

pystac/catalog.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ def get_all_collections(self) -> Iterable[Collection]:
457457
any subcatalogs recursively."""
458458
yield from self.get_collections()
459459
for child in self.get_children():
460-
yield from child.get_collections()
460+
yield from child.get_all_collections()
461461

462462
def get_child_links(self) -> list[Link]:
463463
"""Return all child links of this catalog.

tests/test_catalog.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,13 @@
3535
make_posix_style,
3636
make_relative_href,
3737
)
38-
from tests.utils import ARBITRARY_BBOX, ARBITRARY_GEOM, MockStacIO, TestCases
38+
from tests.utils import (
39+
ARBITRARY_BBOX,
40+
ARBITRARY_EXTENT,
41+
ARBITRARY_GEOM,
42+
MockStacIO,
43+
TestCases,
44+
)
3945

4046

4147
class CatalogTypeTest(unittest.TestCase):
@@ -1350,7 +1356,7 @@ def test_get_all_collections(self) -> None:
13501356
catalog = TestCases.case_1()
13511357
all_collections = list(catalog.get_all_collections())
13521358

1353-
assert len(all_collections) > 0
1359+
assert len(all_collections) == 4
13541360
assert all(isinstance(c, pystac.Collection) for c in all_collections)
13551361

13561362
def test_get_single_links_media_type(self) -> None:
@@ -1639,7 +1645,9 @@ def nested_catalog() -> pystac.Catalog:
16391645
└── variables
16401646
├── catalog.json
16411647
└── variable_a
1642-
├── catalog.json
1648+
└── catalog.json
1649+
└── variable_a_1
1650+
└── collection.json
16431651
"""
16441652
root = pystac.Catalog("root", "root")
16451653
variables = pystac.Catalog("variables", "variables")
@@ -1654,9 +1662,22 @@ def nested_catalog() -> pystac.Catalog:
16541662
variables.add_child(variable_a)
16551663
products.add_child(product_a)
16561664

1665+
variable_a_1 = pystac.Collection(
1666+
"variable_a_1", "variable_a_1", extent=ARBITRARY_EXTENT
1667+
)
1668+
variable_a.add_child(variable_a_1)
1669+
16571670
return root
16581671

16591672

1673+
def test_get_all_collections_deeply_nested(nested_catalog: pystac.Catalog) -> None:
1674+
catalog = nested_catalog
1675+
all_collections = list(catalog.get_all_collections())
1676+
1677+
assert len(all_collections) == 1
1678+
assert all(isinstance(c, pystac.Collection) for c in all_collections)
1679+
1680+
16601681
def test_set_parent_false_stores_in_proper_place_on_normalize_and_save(
16611682
nested_catalog: pystac.Catalog, tmp_path: Path
16621683
) -> None:

0 commit comments

Comments
 (0)