def map_calling_args(calling_args: dict, action): args = {} for arg, cls in calling_args.items(): if issubclass(cls, StateNode): args[arg] = STATES[get_full_path_to_class(cls)] if issubclass(cls, Action): args[arg] = action return args
async def test_call_handler(mocker, dummy_action, dummy_state): mocker.patch('meru.handlers.HANDLERS', {}) states = mocker.patch('meru.handlers.STATES', {}) async def dummy_handler(action: dummy_action, state: dummy_state): pass stub = create_autospec(dummy_handler) register_action_handler(stub) await handle_action(dummy_action()).__anext__() stub.assert_awaited_once_with( action=dummy_action(), state=states[get_full_path_to_class(dummy_state)], )
async def request_state(): from meru.sockets import StateConsumerSocket state_consumer = StateConsumerSocket() states_to_request = list(STATES.keys()) logging.debug(states_to_request) action = RequireState(states_to_request) await state_consumer.request_state(action) state = await state_consumer.receive_state() for node in state.nodes: class_path = get_full_path_to_class(node.__class__) STATES[class_path] = node return STATES
def test_register_handler(mocker, dummy_action, dummy_state): handlers = mocker.patch('meru.handlers.HANDLERS', {}) states = mocker.patch('meru.handlers.STATES', {}) def dummy_handler(action: dummy_action, state: dummy_state): pass register_action_handler(dummy_handler) assert len(handlers) == 1 assert dummy_action in handlers assert handlers[dummy_action] == ActionHandler(dummy_handler, { 'action': dummy_action, 'state': dummy_state, }) assert len(states) == 1 assert get_full_path_to_class(dummy_state) in states
def get_state(state_cls): return STATES[get_full_path_to_class(state_cls)]
def register_state(state_cls): cls_path = get_full_path_to_class(state_cls) if state_cls not in STATES: STATES[cls_path] = state_cls()