def test_is_request_type_not_match(): test_handler_input = HandlerInput( request_envelope=RequestEnvelope(request=SessionEndedRequest())) intent_name_wrapper = is_request_type("IntentRequest") assert not intent_name_wrapper(test_handler_input), ( "is_request_type matcher matched with the incorrect request type")
def test_is_request_type_match(): test_handler_input = HandlerInput( request_envelope=RequestEnvelope(request=IntentRequest())) request_type_wrapper = is_request_type("IntentRequest") assert request_type_wrapper(test_handler_input), ( "is_request_type matcher didn't match with the correct request type")
def test_get_handler_chain_registered_for_event_request(self): test_intent_request = mock.MagicMock(spec=IntentRequest) test_request_envelope = mock.MagicMock(spec=RequestEnvelope) test_request_envelope.request = test_intent_request test_handler_input = HandlerInput( request_envelope=test_request_envelope) test_intent_handler = mock.MagicMock(spec=AbstractRequestHandler) test_intent_handler.can_handle.return_value = False test_intent_request_handler_chain = RequestHandlerChain( request_handler=test_intent_handler) test_event_handler = mock.MagicMock(spec=AbstractRequestHandler) test_event_handler.can_handle.return_value = True test_event_request_handler_chain = RequestHandlerChain( request_handler=test_event_handler) test_request_mapper = RequestMapper( request_handler_chains=[ test_event_request_handler_chain, test_intent_request_handler_chain]) assert test_request_mapper.get_request_handler_chain( test_handler_input).request_handler == test_event_handler, ( "get_request_handler_chain in Request Mapper found incorrect " "request handler chain for " "intent request")
def test_get_handler_registered_for_custom_exception(self): test_intent_request = mock.MagicMock(spec=IntentRequest) test_request_envelope = mock.MagicMock(spec=RequestEnvelope) test_request_envelope.request = test_intent_request test_handler_input = HandlerInput( request_envelope=test_request_envelope) test_exception_handler_1 = mock.MagicMock( spec=AbstractExceptionHandler) test_exception_handler_1.can_handle.return_value = False test_exception_handler_2 = mock.MagicMock( spec=AbstractExceptionHandler) test_exception_handler_2.can_handle.return_value = True test_exception_mapper = ExceptionMapper(exception_handlers=[ test_exception_handler_1, test_exception_handler_2 ]) test_custom_exception = DispatchException("Test Custom Exception") assert test_exception_mapper.get_handler( handler_input=test_handler_input, exception=test_custom_exception ) == test_exception_handler_2, ( "get_handler in Default Exception Mapper found incorrect request " "exception handler for " "input and custom exception")
def setUp(self): test_request = mock.MagicMock(spec=IntentRequest) test_request_envelope = mock.MagicMock(RequestEnvelope) test_request_envelope.request = test_request self.valid_handler_input = HandlerInput( request_envelope=test_request_envelope) self.test_dispatcher = RequestDispatcher()
def test_is_new_session_throws_exception_if_session_not_exists(self): test_input = HandlerInput(request_envelope=RequestEnvelope()) with self.assertRaises( TypeError, msg="is_new_session method didn't throw TypeError when an " "input request without session is passed"): is_new_session(handler_input=test_input)
def test_is_intent_name_not_match(): test_intent_name = "TestIntent" test_handler_input = HandlerInput(request_envelope=RequestEnvelope( request=IntentRequest(intent=Intent(name=test_intent_name)))) intent_name_wrapper = is_intent_name("TestIntent1") assert not intent_name_wrapper( test_handler_input), "is_intent_name matcher matched with the " \ "incorrect intent name"
def test_is_intent_name_match(): test_intent_name = "TestIntent" test_handler_input = HandlerInput(request_envelope=RequestEnvelope( request=IntentRequest(intent=Intent(name=test_intent_name)))) intent_name_wrapper = is_intent_name(test_intent_name) assert intent_name_wrapper( test_handler_input), "is_intent_name matcher didn't match with the " \ "correct intent name"
def test_no_error_thrown_when_service_client_factory_getter_called_after_setting( self): test_input = HandlerInput(request_envelope=None, service_client_factory=mock.Mock()) test_client_factory = test_input.service_client_factory assert isinstance(test_client_factory, mock.Mock), ( "Handler Input service client factory getter returned incorrect " "value for client factory after setter")
def test_error_thrown_when_service_client_factory_getter_called_without_setting( self): test_input = HandlerInput(request_envelope=None) with self.assertRaises(ValueError) as exc: test_client_factory = test_input.service_client_factory assert "Attempting to use service client factory with no configured API client" in str( exc.exception), ( "Handler Input didn't raise Value Error when service client " "factory is not set and a get is called")
def test_is_intent_not_match_canfulfill_intent(): test_intent_name = "TestIntent" test_handler_input = HandlerInput( request_envelope=RequestEnvelope(request=IntentRequest( intent=Intent(name=test_intent_name)))) canfulfill_intent_name_wrapper = is_canfulfill_intent_name(test_intent_name) assert not canfulfill_intent_name_wrapper( test_handler_input), "is_canfulfill_intent_name matcher matched with the " \ "incorrect request type"
def setUp(self) -> None: skill = CustomSkill(skill_configuration=sb.skill_configuration) req = update_request(message_request, 'en-US') request_envelope = skill.serializer.deserialize( payload=json.dumps(req), obj_type=RequestEnvelope) attributes_manager = AttributesManager( request_envelope=request_envelope, persistence_adapter=skill.persistence_adapter) self.handler_input = HandlerInput(request_envelope, attributes_manager) self.handler = sb.lambda_handler()
def create_handler_input(slots: Dict[str, Slot], attributes: Dict) -> HandlerInput: request_envelope = RequestEnvelope(version="v1", session=Session(attributes=attributes), request=IntentRequest( request_id=str(uuid4()), intent=Intent(name="test intent", slots=slots))) attributes_manager = AttributesManager(request_envelope=request_envelope) handler_input = HandlerInput(request_envelope=request_envelope, attributes_manager=attributes_manager) return handler_input
def setUp(self, mock_cache, mock_enumerator): self.test_template_name = 'test' self.test_dir_path = '.' self.test_enumerator = mock_enumerator.return_value mock_cache.get.return_value = None self.cache = mock_cache self.test_handler_input = HandlerInput( request_envelope=RequestEnvelope(request=CanFulfillIntentRequest( locale='en-GB'))) self.test_loader = FileSystemTemplateLoader( dir_path=self.test_dir_path, enumerator=self.test_enumerator, cache=mock_cache, encoding='utf-8')
def test_no_handler_registered_for_event_request(self): test_event_request = mock.MagicMock(spec=SkillEnabledRequest) test_request_envelope = mock.MagicMock(spec=RequestEnvelope) test_request_envelope.request = test_event_request test_handler_input = HandlerInput( request_envelope=test_request_envelope) test_request_handler = mock.MagicMock(spec=AbstractRequestHandler) test_request_handler.can_handle.return_value = False test_request_handler_chain = RequestHandlerChain( request_handler=test_request_handler) test_request_mapper = RequestMapper( request_handler_chains=[test_request_handler_chain]) assert test_request_mapper.get_request_handler_chain(test_handler_input) is None, ( "get_request_handler_chain in Request Mapper found an unsupported " "request handler chain for " "event request")
def test_no_handler_registered_for_custom_exception(self): test_intent_request = mock.MagicMock(spec=IntentRequest) test_request_envelope = mock.MagicMock(spec=RequestEnvelope) test_request_envelope.request = test_intent_request test_handler_input = HandlerInput( request_envelope=test_request_envelope) test_exception_handler = mock.MagicMock(spec=AbstractExceptionHandler) test_exception_handler.can_handle.return_value = False test_exception_mapper = ExceptionMapper( exception_handlers=[test_exception_handler]) test_custom_exception = DispatchException("Test Custom Exception") assert test_exception_mapper.get_handler( handler_input=test_handler_input, exception=test_custom_exception) is None, ( "get_handler in Default Exception Mapper found an unsupported " "exception handler for " "handler input and custom exception")
def setUp(self): self.test_enumerator = LocaleTemplateEnumerator() self.test_template_name = 'test_template' self.test_handler_input = HandlerInput( request_envelope=RequestEnvelope(request=CanFulfillIntentRequest( locale='en-GB')))
def _create_handler_input(self, request): self.test_request_envelope.request = request return HandlerInput(request_envelope=self.test_request_envelope)
def handler_input(request) -> HandlerInput: event_name = request.param event = load_event(event_name, as_json=False) request_envelope = serializer.deserialize(event, RequestEnvelope) return HandlerInput(request_envelope)