Skip to content

Commit

Permalink
Handle garbage admstate from server
Browse files Browse the repository at this point in the history
Convert it to AdmState.UNKNOWN
  • Loading branch information
hmpf authored May 24, 2024
1 parent d9d187f commit 41270bb
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/zinolib/event_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ class AdmState(StrEnum):
OPEN = "open"
WAITING = "waiting"
WORKING = "working"
UNKNOWN = "unknown"

@classmethod
def _missing_(cls, value):
# Python 3.12: "if not value in cls:"
if not value in cls._value2member_map_:
return cls.UNKNOWN


class FlapState(StrEnum):
Expand Down
12 changes: 12 additions & 0 deletions tests/test_zinolib_controllers_zino1.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,18 @@ def init_manager(self):
zino1 = FakeZino1EventManager.configure(None)
return zino1

def test_create_event_from_id_receiving_garbage_admstate_is_safely_handled(self):
global raw_attrlist
zino1 = self.init_manager()
good_attrlist = raw_attrlist[:] # copy
try:
raw_attrlist[0] = "state: garbage admstate"
event = zino1.create_event_from_id(139110)
self.assertEqual(event.adm_state, AdmState.UNKNOWN)
finally:
# reset to known good attrlist for other tests
raw_attrlist = good_attrlist

def test_create_event_from_id_may_get_garabage_data(self):
def falsey(_):
return False
Expand Down
12 changes: 12 additions & 0 deletions tests/test_zinolib_event_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,3 +252,15 @@ def test_set_log_for_event(self):
}]
updated_event = event_manager.set_log_for_event(event, log_list)
self.assertTrue(event_manager.events[event.id].log)


class AdmStateTest(unittest.TestCase):

def test_golden_path(self):
for state in AdmState:
enum_state = AdmState(state)
self.assertEqual(enum_state.value, state)

def test_garbage_input_should_be_converted_to_UNKNOWN(self):
value = AdmState("random garbage")
self.assertEqual(value, AdmState.UNKNOWN)

0 comments on commit 41270bb

Please sign in to comment.