Skip to content

Commit

Permalink
option to access config command state by label
Browse files Browse the repository at this point in the history
  • Loading branch information
amyasnikov committed Jan 21, 2024
1 parent 3c7d92c commit 4261dc3
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
4 changes: 3 additions & 1 deletion validity/compliance/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from django.utils.translation import gettext_lazy as _

from validity.compliance.serialization import Serializable
from ..utils.misc import reraise
from validity.utils.misc import reraise
from .exceptions import NoComponentError, SerializationError, StateKeyError


Expand Down Expand Up @@ -82,6 +82,8 @@ def __getattr__(self, key):
return self[key]

def __getitem__(self, key):
if self.config_command_label and key == self.config_command_label:
key = "config"
with reraise(KeyError, StateKeyError):
state_item = super().__getitem__(key)
return state_item.serialized
Expand Down
6 changes: 3 additions & 3 deletions validity/tests/test_compliance/test_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ class TestState:
@pytest.mark.django_db
def test_get_item(self):
item1 = state_item("item1", {"k1": "v1"})
item2 = state_item("item2", {"k2": "v2"})
item2 = state_item("item2", {"k2": "v2"}, command=CommandFactory(retrieves_config=True))
item_err = state_item("item_err", {}, data_file=None)
del item_err.__dict__["serialized"]
state = State({item1.name: item1, item2.name: item2, item_err.name: item_err})
state = State({"item1": item1, "config": item2, "item_err": item_err}, config_command_label="item2")
assert state["item1"] == state.item1 == {"k1": "v1"}
assert state["item2"] == state.item2 == {"k2": "v2"}
assert state["item2"] == state.item2 == state.config == state["config"] == {"k2": "v2"}
assert state.get_full_item("item1") == item1
assert state.get("item3") is None
assert state.get("item_err", ignore_errors=True) is None
Expand Down

0 comments on commit 4261dc3

Please sign in to comment.