Skip to content

Commit

Permalink
Add setitem method
Browse files Browse the repository at this point in the history
  • Loading branch information
JimMadge committed Apr 18, 2024
1 parent 7ec97e4 commit 5a38b8f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
11 changes: 11 additions & 0 deletions data_safe_haven/config/pulumi.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ def __getitem__(self, key: str):
msg = f"No configuration for Pulumi stack {key}."
raise IndexError(msg)

def __setitem__(self, key: str, value: PulumiStack):
if not isinstance(key, str):
msg = "'key' must be a string."
raise TypeError(msg)

if key in self.stack_names:
msg = f"Stack {key} already exists."
raise ValueError(msg)

self.stacks.append(value)

def __delitem__(self, key: str):
if not isinstance(key, str):
msg = "'key' must be a string."
Expand Down
16 changes: 16 additions & 0 deletions tests/config/test_pulumi.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,22 @@ def test_delitem_index_error(self, pulumi_config):
with raises(IndexError, match="No configuration for Pulumi stack Ringo."):
del pulumi_config["Ringo"]

def test_setitem(self, pulumi_config, pulumi_stack):
del pulumi_config["my_stack"]
assert len(pulumi_config.stack_names) == 1
assert "my_stack" not in pulumi_config.stack_names
pulumi_config["my_stack"] = pulumi_stack
assert len(pulumi_config.stack_names) == 2
assert "my_stack" in pulumi_config.stack_names

def test_setitem_type_error(self, pulumi_config):
with raises(TypeError, match="'key' must be a string."):
pulumi_config[1] = 5

def test_setitem_value_error(self, pulumi_config):
with raises(ValueError, match="Stack other_stack already exists."):
pulumi_config["other_stack"] = 5

def test_stack_names(self, pulumi_config):
assert "my_stack" in pulumi_config.stack_names

Expand Down

0 comments on commit 5a38b8f

Please sign in to comment.