Skip to content

Commit

Permalink
Fixing couple of things
Browse files Browse the repository at this point in the history
  • Loading branch information
Psychokiller1888 committed Mar 10, 2021
1 parent dcd6956 commit 18eeb19
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 8 deletions.
5 changes: 2 additions & 3 deletions core/base/StateManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,17 @@ def register(self, statePath: str, initialState: StateType = StateType.BORN) ->

try:
state = State(statePath.split('.')[-1], initialState)
self._buildDict(statePath, state, initialState)
self._buildDict(statePath, state)
return state
except StateAlreadyRegistered:
return None


def _buildDict(self, statePath: str, state: State, initialState: StateType):
def _buildDict(self, statePath: str, state: State):
"""
Generates a dict from a dotted string
:param statePath: dotted string
:param state: state name
:param initialState: initialstate
"""
track = self._states
parts = statePath.split('.')
Expand Down
5 changes: 5 additions & 0 deletions core/base/model/State.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,8 @@ def setState(self, newState: StateType):
callback(oldState, newState)
except:
self.logger.logWarning(f'Failed callback for state {self.name}')


def __repr__(self) -> str:
return f'State "{self.name}" Current state "{self.currentState.value}"'

5 changes: 4 additions & 1 deletion core/util/model/Logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ def logCritical(self, msg: str, plural: Union[list, str] = None):


def doLog(self, function: callable, msg: str, printStack = True, plural: Union[list, str] = None):
if not msg:
return

if plural:
msg = self.doPlural(string=msg, word=plural)

Expand Down Expand Up @@ -79,6 +82,6 @@ def plural(match: Match) -> str:
words = [word]

for word in words:
string = re.sub('([\d]+)[* ]+?({})'.format(word), plural, string)
string = re.sub(r'([\d]+)[* ]+?({})'.format(word), plural, string)

return string
3 changes: 0 additions & 3 deletions requirements_test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@ esptool~=3.0
pyserial~=3.5
pydub~=0.25.0
terminaltables~=3.1.0
click~=7.0
PyYAML~=5.4.1
flask~=1.1.2
flask-classful~=0.14.2
flask-login~=0.5.0
pympler~=0.9
Flask-Cors~=3.0.10
googletrans~=3.0.0
Expand All @@ -25,6 +23,5 @@ importlib_metadata~=3.7.2
langdetect~=1.0.8
webrtcvad~=2.0.10
pyjwt~=2.0.1
toml~=0.10.2
markdown~=3.3.4
sounddevice==0.4.1
16 changes: 16 additions & 0 deletions tests/base/test_StateManager.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import unittest
from unittest.mock import MagicMock, patch

from core.ProjectAliceExceptions import StateAlreadyRegistered
from core.base.StateManager import StateManager
from core.base.model.State import State
from core.base.model.StateType import StateType
Expand Down Expand Up @@ -37,13 +38,25 @@ def test_register(self, mock_superManager):
self.assertIsInstance(stateManager.register('unit.tests'), State)


@patch('core.base.SuperManager.SuperManager')
def test__build_dict(self, mock_superManager):
mock_instance = MagicMock()
mock_superManager.getInstance.return_value = mock_instance
mock_instance.commonsManager.getFunctionCaller.return_value = 'unittest'
stateManager = StateManager()

self.assertIsNone(stateManager._buildDict('unit.test.is.awesome', State('awesome')))
self.assertRaises(StateAlreadyRegistered, stateManager._buildDict, statePath='unit.test.is.awesome', state=State('awesome'))


@patch('core.base.SuperManager.SuperManager')
def test_get_state(self, mock_superManager):
mock_instance = MagicMock()
mock_superManager.getInstance.return_value = mock_instance
mock_instance.commonsManager.getFunctionCaller.return_value = 'unittest'
stateManager = StateManager()

self.assertIsNone(stateManager.getState(''))
self.assertIsNone(stateManager.getState('unittest'))

stateManager.register('unit.test.is.awesome')
Expand Down Expand Up @@ -74,6 +87,9 @@ def test_set_state(self, mock_superManager):
state = stateManager.getState('unit.test.is.awesome')
self.assertEqual(state, mockState)

state = stateManager.setState('unit.test.is.crappy', StateType.WAITING)
self.assertEqual(state, False)

stateManager.setState('unit.test.is.awesome', StateType.WAITING)
state = stateManager.getState('unit.test.is.awesome')
self.assertEqual(state.currentState, StateType.WAITING)
Expand Down

0 comments on commit 18eeb19

Please sign in to comment.