Skip to content

Commit a6a9d5b

Browse files
authored
Add the parameter name for Dataset.get_or_create (#353)
* allow names for get_or_create * Update tests/test_dataset.py
1 parent 1fa030f commit a6a9d5b

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

tests/test_dataset.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,10 +684,19 @@ def test_dataset_get_or_create() -> None:
684684
ds2 = Dataset.get_or_create(ds_path, scale=(1, 1, 1))
685685
assert "color" in ds2.layers.keys()
686686

687+
ds2 = Dataset.get_or_create(
688+
ds_path, scale=(1, 1, 1), name="wk_dataset_get_or_create"
689+
)
690+
assert "color" in ds2.layers.keys()
691+
687692
with pytest.raises(AssertionError):
688693
# dataset already exists, but with a different scale
689694
Dataset.get_or_create(ds_path, scale=(2, 2, 2))
690695

696+
with pytest.raises(AssertionError):
697+
# dataset already exists, but with a different name
698+
Dataset.get_or_create(ds_path, scale=(1, 1, 1), name="some different name")
699+
691700

692701
def test_changing_layer_bounding_box() -> None:
693702
delete_dir(TESTOUTPUT_DIR / "test_changing_layer_bounding_box")

wkcuber/api/dataset.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,10 @@ def create(
614614

615615
@classmethod
616616
def get_or_create(
617-
cls, dataset_path: Union[str, Path], scale: Tuple[float, float, float]
617+
cls,
618+
dataset_path: Union[str, Path],
619+
scale: Tuple[float, float, float],
620+
name: Optional[str] = None,
618621
) -> "Dataset":
619622
"""
620623
Creates a new `Dataset`, in case it did not exist before, and then returns it.
@@ -628,9 +631,13 @@ def get_or_create(
628631
assert tuple(ds.properties.scale) == tuple(
629632
scale
630633
), f"Cannot get_or_create Dataset: The dataset {dataset_path} already exists, but the scales do not match ({ds.properties.scale} != {scale})"
634+
if name is not None:
635+
assert (
636+
ds.name == name
637+
), f"Cannot get_or_create Dataset: The dataset {dataset_path} already exists, but the names do not match ({ds.name} != {name})"
631638
return ds
632639
else:
633-
return cls.create(dataset_path, scale)
640+
return cls.create(dataset_path, scale, name)
634641

635642
def _create_layer(
636643
self,

0 commit comments

Comments
 (0)