Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_set_state_for_existing_state(self):
state_manager = ActorStateManager(self._fake_actor)
_run(state_manager.set_state('state1', 'value2'))
state = state_manager._state_change_tracker['state1']
self.assertEqual(StateChangeKind.update, state.change_kind)
self.assertEqual('value2', state.value)
def test_set_state_for_existing_state_only_in_mem(self):
state_manager = ActorStateManager(self._fake_actor)
_run(state_manager.set_state('state1', 'value1'))
state = state_manager._state_change_tracker['state1']
self.assertEqual(StateChangeKind.add, state.change_kind)
self.assertEqual('value1', state.value)
_run(state_manager.set_state('state1', 'value2'))
state = state_manager._state_change_tracker['state1']
self.assertEqual(StateChangeKind.add, state.change_kind)
self.assertEqual('value2', state.value)
def test_remove_state_twice_for_existing_state_in_mem(self):
state_manager = ActorStateManager(self._fake_actor)
_run(state_manager.set_state('state1', 'value1'))
removed = _run(state_manager.try_remove_state('state1'))
self.assertTrue(removed)
removed = _run(state_manager.try_remove_state('state1'))
self.assertFalse(removed)
def test_remove_state_for_existing_state(self):
state_manager = ActorStateManager(self._fake_actor)
removed = _run(state_manager.try_remove_state('state1'))
self.assertTrue(removed)
state = state_manager._state_change_tracker['state1']
self.assertEqual(StateChangeKind.remove, state.change_kind)
def test_get_or_add_state_for_removed_state(self):
state_manager = ActorStateManager(self._fake_actor)
_run(state_manager.remove_state('state1'))
state = state_manager._state_change_tracker['state1']
self.assertEqual(StateChangeKind.remove, state.change_kind)
val = _run(state_manager.get_or_add_state('state1', 'value2'))
state = state_manager._state_change_tracker['state1']
self.assertEqual(StateChangeKind.update, state.change_kind)
self.assertEqual('value2', val)
def test_get_state_for_removed_value(self):
state_manager = ActorStateManager(self._fake_actor)
removed = _run(state_manager.try_remove_state('state1'))
self.assertTrue(removed)
state = state_manager._state_change_tracker['state1']
self.assertEqual(StateChangeKind.remove, state.change_kind)
has_value, val = _run(state_manager.try_get_state('state1'))
self.assertFalse(has_value)
self.assertIsNone(val)
def test_add_or_update_state_for_removed_state(self):
"""add state value if state was removed."""
def test_update_value(name, value):
return f'{name}-{value}'
state_manager = ActorStateManager(self._fake_actor)
_run(state_manager.remove_state('state1'))
val = _run(state_manager.add_or_update_state('state1', 'value1', test_update_value))
self.assertEqual('value1', val)
def test_clear_cache(self):
state_manager = ActorStateManager(self._fake_actor)
_run(state_manager.set_state('state1', 'value1'))
_run(state_manager.set_state('state2', 'value2'))
_run(state_manager.set_state('state3', 'value3'))
_run(state_manager.clear_cache())
self.assertEqual(0, len(state_manager._state_change_tracker))
def test_save_state(self):
state_manager = ActorStateManager(self._fake_actor)
# set states which are StateChangeKind.add
_run(state_manager.set_state('state1', 'value1'))
_run(state_manager.set_state('state2', 'value2'))
has_value, val = _run(state_manager.try_get_state('state3'))
self.assertTrue(has_value)
self.assertEqual("value3", val)
# set state which is StateChangeKind.remove
_run(state_manager.remove_state('state4'))
# set state which is StateChangeKind.update
_run(state_manager.set_state('state5', 'value5'))
expected = b'[{"operation":"upsert","request":{"key":"state1","value":"value1"}},{"operation":"upsert","request":{"key":"state2","value":"value2"}},{"operation":"delete","request":{"key":"state4"}},{"operation":"upsert","request":{"key":"state5","value":"value5"}}]' # noqa: E501
# Save the state
def mock_save_state(actor_type, actor_id, data):
self.assertEqual(expected, data)
def test_get_or_add_state_for_non_existing_state(self):
state_manager = ActorStateManager(self._fake_actor)
val = _run(state_manager.get_or_add_state('state1', 'value2'))
state = state_manager._state_change_tracker['state1']
self.assertEqual(StateChangeKind.add, state.change_kind)
self.assertEqual('value2', val)
self._fake_client.get_state.mock.assert_called_once_with(
self._test_type_info._name,
self._test_actor_id.id, 'state1')