Skip to content

Commit

Permalink
[issue-47] drop element from resource tree
Browse files Browse the repository at this point in the history
This commit adds a "drop_element" function to the Resource class. As the dataclass is frozen we need to return a new instance of Resource without the specified path. If the specified element has children or if it doesn't exist in the Resource a ValueError is thrown.

Signed-off-by: Meret Behrens <[email protected]>
  • Loading branch information
meretp committed Jun 22, 2023
1 parent 2032e3f commit 0f27be9
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/opossum_lib/opossum_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,25 @@ def element_exists_but_resource_type_differs(
return self.children[element].type != resource_type
return False

def drop_element(
self, path_to_element_to_drop: List[Tuple[str, ResourceType]]
) -> Resource:
paths_in_resource = self.get_paths_with_resource_types()
if path_to_element_to_drop not in paths_in_resource:
raise ValueError(
f"Element {path_to_element_to_drop} doesn't exist in resource!"
)

else:
resource = Resource(ResourceType.TOP_LEVEL)
paths_in_resource.remove(path_to_element_to_drop)
paths_in_resource.append(path_to_element_to_drop[:-1])

for path_to_element_to_drop in paths_in_resource:
resource.add_path(path_to_element_to_drop)

return resource

def to_dict(self) -> Union[int, Dict]:
if not self.has_children():
if self.type == ResourceType.FOLDER:
Expand Down
65 changes: 65 additions & 0 deletions tests/test_opossum_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,68 @@ def test_resource_add_path_throws_err_if_element_exists_with_different_type() ->
("C", ResourceType.FILE),
]
)


def test_resource_drop_element_error() -> None:
resource = Resource(ResourceType.TOP_LEVEL)
resource.add_path(
[
("A", ResourceType.FOLDER),
("B", ResourceType.FILE),
("E", ResourceType.FOLDER),
("D", ResourceType.FILE),
]
)

with pytest.raises(ValueError):
resource.drop_element(
[
("A", ResourceType.FOLDER),
("B", ResourceType.FILE),
("C", ResourceType.FILE),
]
)


def test_resource_drop_element_error_not_leaf() -> None:
resource = Resource(ResourceType.TOP_LEVEL)
resource.add_path(
[
("A", ResourceType.FOLDER),
("B", ResourceType.FILE),
("C", ResourceType.FOLDER),
("D", ResourceType.FILE),
]
)

with pytest.raises(ValueError):
resource.drop_element(
[
("A", ResourceType.FOLDER),
("B", ResourceType.FILE),
("C", ResourceType.FILE),
]
)


def test_resource_drop_element() -> None:
resource = Resource(ResourceType.TOP_LEVEL)
resource.add_path(
[
("A", ResourceType.FOLDER),
("B", ResourceType.FILE),
("C", ResourceType.FOLDER),
]
)

resource_without_element = resource.drop_element(
[
("A", ResourceType.FOLDER),
("B", ResourceType.FILE),
("C", ResourceType.FOLDER),
]
)

assert resource_without_element.get_paths_with_resource_types() == [
[("A", ResourceType.FOLDER), ("B", ResourceType.FILE)]
]

0 comments on commit 0f27be9

Please sign in to comment.