예제 #1
0
 def test___init__(self):
     test_name = "TestIntentName"
     test_parameters = {
         "TestParameter1": "Value1",
         "TestParameter2": "Value3"
     }
     intent = Intent(test_name, "Test", test_parameters)
     self.assertEqual(test_name, intent.name)
     self.assertEqual(test_parameters, intent.parameters)
    def test_get_number_of_notifications(self):
        speaker = Mock()
        speaker.speak_text = MagicMock()
        trigger_manager = TriggerManager(speaker)

        hold_notification_intent = Intent("HoldNotifications",
                                          "Hold all notifications", {})
        trigger_manager.handle_intent(hold_notification_intent)

        notification = Notification("Notification test")
        trigger_manager.trigger(notification)

        notification2 = Notification("Notification test 2")
        trigger_manager.trigger(notification2)

        speaker.speak_text.assert_not_called()
        response = trigger_manager.handle_intent(
            Intent("GetNumberOfNotifications", "Test", {}))
        self.assertEqual("There are 2 notifications in the queue", response)
예제 #3
0
    def test_dispatch_intent(self):
        mock_intent_handler = Mock()
        mock_intent_handler.intent_definitions = [IntentDefinition("test", "Test text")]
        mock_intent_handler.handle_intent = MagicMock()

        intent_manager = IntentHandlerManager()
        intent_manager.subscribe_intent_handler(mock_intent_handler)

        intent_manager.update(Intent('test', {}))

        mock_intent_handler.handle_intent.assert_called_once()
예제 #4
0
    def test_speaker_is_called_when_present(self):
        mock_intent_handler = Mock()
        mock_intent_handler.intent_definitions = [IntentDefinition("test", "Test text")]
        mock_intent_handler.handle_intent = Mock(return_value="Speak this text")

        speaker = Mock()

        intent_manager = IntentHandlerManager(speaker)
        intent_manager.subscribe_intent_handler(mock_intent_handler)

        intent_manager.update(Intent("test", {}))
        speaker.speak_text.assert_called_once()
예제 #5
0
    def test_handle_exception_in_intent_handler(self):
        mock_intent_handler = Mock()
        mock_intent_handler.intent_definitions = [IntentDefinition("test", "Test text")]
        mock_intent_handler.handle_intent = Mock(side_effect=KeyError("Shit happened"))

        intent_manager = IntentHandlerManager()

        intent_manager.subscribe_intent_handler(mock_intent_handler)

        try:
            intent_manager.update(Intent("test", {}))
        except KeyError:
            self.fail("IntentHandlerManager.update() unexpectedly raised a KeyError")
    def test_hold_and_resume_notifications(self):
        speaker = Mock()
        speaker.speak_text = MagicMock()
        trigger_manager = TriggerManager(speaker)

        hold_notification_intent = Intent("HoldNotifications",
                                          "Hold all notifications", {})
        trigger_manager.handle_intent(hold_notification_intent)

        notification = Notification("Notification test")
        trigger_manager.trigger(notification)

        speaker.speak_text.assert_not_called()

        resume_notification_intent = Intent("ResumeNotifications",
                                            "Resume all notifications", {})
        trigger_manager.handle_intent(resume_notification_intent)

        notification2 = Notification("Notification test 2")
        trigger_manager.trigger(notification2)

        speaker.speak_text.assert_called_once()
    def test_handle_intent(self):
        localtime = time.localtime()
        time_string = time.strftime("%H:%M", localtime)
        expected_response = "It is currently {} o'clock".format(
            time_string), False

        intent = Intent("GetTime", {})
        intent_manager = IntentHandlerManager()

        time_intent_handler = TimeIntentHandler()
        intent_manager.subscribe_intent_handler(time_intent_handler)

        response = time_intent_handler.handle_intent(intent)
        self.assertEqual(expected_response, response)
    def test_handle_intent(self, hue_manager):
        hue_manager_instance = hue_manager.return_value
        intent = Intent("TurnLightsInRoomOnOff",
                        "Turn the lights in the Living Room on",
                        parameters={
                            "Room": "Living room",
                            "OnOff": "On"
                        })
        room_on_off_handler = LightsInRoomOnOffHandler()
        hue_manager_instance.get_group_by_name = Mock(
            return_value=Group("Living room", 13))
        hue_manager_instance.send_room_command_to_bridge = MagicMock()

        room_on_off_handler.handle_intent(intent)
        calls = [call("Living room", """{"on": true}""")]
        hue_manager_instance.send_room_command_to_bridge.assert_has_calls(
            calls)
예제 #9
0
 def test_dispatch_unknown_intent(self):
     intent_manager = IntentHandlerManager()
     try:
         intent_manager.update(Intent("unknown", {}))
     except KeyError:
         self.fail("dispatch_intent raised KeyError unexpectedly")
예제 #10
0
 def _create_intent_object(raw_intent_string: str):
     intent_dict = json.loads(raw_intent_string)
     name = intent_dict["intent"]["name"]
     if "slots" in intent_dict:
         return Intent(name, intent_dict["text"], intent_dict["slots"])
     return Intent(name, intent_dict["text"])