def test_create_multiple_parameters(self, requests_mock): values1 = ["One", "Two", "Three", "Four", "Five", "Six"] values2 = ["One", "Two", "Three", "Four", "Five", "Seven"] parameter1 = SetParameter("Test1", True, values1) parameter2 = SetParameter("Test2", True, values2) sentence = Sentence() sentence.add_parameter(parameter1) sentence.add_parameter(parameter2) intent_definition = IntentDefinition("TestIntent") intent_definition.add_sentence(sentence) intent_definition_source_mock = Mock() intent_definition_source_mock.get_intent_definitions.return_value = [ intent_definition ] updater = RhasspyUpdater(intent_definition_source_mock) expected_json = { "Test1": ["One", "Two", "Three", "Four", "Five", "Six"], "Test2": ["One", "Two", "Three", "Four", "Five", "Seven"] } updater.update_rhasspy() calls = [ mock.call(self.SLOTS_URL, json.dumps(expected_json)), mock.call(self.SENTENCES_URL, "[TestIntent]\n$Test1{Test1} $Test2{Test2}") ] self.assertEqual(2, requests_mock.call_count) requests_mock.assert_has_calls(calls, any_order=True)
def test_create_parameter_string_without_return(self, requests_mock): values = ["One", "Two", "Three", "Four", "Five", "Six"] parameter = SetParameter("Test", possible_values=values) sentence = Sentence() sentence.add_parameter(parameter) intent_definition = IntentDefinition("TestIntent") intent_definition.add_sentence(sentence) intent_definition_source_mock = Mock() intent_definition_source_mock.get_intent_definitions.return_value = [ intent_definition ] updater = RhasspyUpdater(intent_definition_source_mock) expected_json = { "Test": ["One", "Two", "Three", "Four", "Five", "Six"] } updater.update_rhasspy() calls = [ mock.call(self.SLOTS_URL, json.dumps(expected_json)), mock.call(self.SENTENCES_URL, "[TestIntent]\n$Test") ] self.assertEqual(2, requests_mock.call_count) requests_mock.assert_has_calls(calls, any_order=True)
def _create_one_string_sentence_intent_definition(intent_name: str, sentence_string: str) -> \ IntentDefinition: """Creates the most basic of IntentDefinitions, those consisting of only one single string""" intent_definition = IntentDefinition(intent_name) sentence = Sentence() sentence.add_string(sentence_string) intent_definition.add_sentence(sentence) return intent_definition
def test_two_slots_same_name_same_values(self, requests_mock): slot_values = ["One", "Two", "Three", "Four", "Five", "Six"] parameter1 = SetParameter("Test", True, possible_values=slot_values) sentence1 = Sentence() sentence1.add_parameter(parameter1) intent_definition1 = IntentDefinition("TestIntent1") intent_definition1.add_sentence(sentence1) parameter2 = SetParameter("Test", True, possible_values=slot_values) sentence2 = Sentence() sentence2.add_parameter(parameter2) intent_definition2 = IntentDefinition("TestIntent2") intent_definition2.add_sentence(sentence2) intent_definition_source_mock = Mock() intent_definition_source_mock.get_intent_definitions.return_value = [ intent_definition1, intent_definition2 ] updater = RhasspyUpdater(intent_definition_source_mock) expected_json = { "Test": ["One", "Two", "Three", "Four", "Five", "Six"] } updater.update_rhasspy() expected_intents = "[TestIntent1]\n$Test{Test}\n\n[TestIntent2]\n$Test{Test}" calls = [ mock.call(self.SLOTS_URL, json.dumps(expected_json)), mock.call(self.SENTENCES_URL, expected_intents) ] self.assertEqual(2, requests_mock.call_count) requests_mock.assert_has_calls(calls, any_order=True)
def test_optional_string(self, requests_mock): requests_mock.return_value.status_code = 200 sentence = Sentence() sentence.add_string("Some test text", True) intent_definition = IntentDefinition("TestIntent") intent_definition.add_sentence(sentence) intent_definition_source_mock = Mock() intent_definition_source_mock.get_intent_definitions.return_value = [ intent_definition ] updater = RhasspyUpdater(intent_definition_source_mock) updater.update_rhasspy() requests_mock.assert_called_once() requests_mock.assert_called_with(self.SENTENCES_URL, "[TestIntent]\n[Some test text]")
def test_create_option_string_without_return(self, requests_mock): values = ["One", "Two", "Three", "Four", "Five"] parameter = SetParameter("Test", possible_values=values) sentence = Sentence() sentence.add_parameter(parameter) intent_definition = IntentDefinition("TestIntent") intent_definition.add_sentence(sentence) intent_definition_source_mock = Mock() intent_definition_source_mock.get_intent_definitions.return_value = [ intent_definition ] updater = RhasspyUpdater(intent_definition_source_mock) updater.update_rhasspy() requests_mock.assert_called_once() expected_intent_in_call = "[TestIntent]\n(One | Two | Three | Four | Five)" requests_mock.assert_called_with(self.SENTENCES_URL, expected_intent_in_call)
def test_number_range_parameter_without_step(self, requests_mock): parameter = NumberRangeParameter("Test", True, -5, 31) sentence = Sentence() sentence.add_parameter(parameter) intent_definition = IntentDefinition("TestIntent") intent_definition.add_sentence(sentence) intent_definition_source_mock = Mock() intent_definition_source_mock.get_intent_definitions.return_value = [ intent_definition ] updater = RhasspyUpdater(intent_definition_source_mock) updater.update_rhasspy() requests_mock.assert_called_once() expected_intent_in_call = "[TestIntent]\n(-5..31){Test!int}" requests_mock.assert_has_calls( [mock.call(self.SENTENCES_URL, expected_intent_in_call)], any_order=True)
def test_create_option_string_with_return(self, requests_mock): parameter = SetParameter("Test", True, ["One", "Two", "Three", "Four", "Five"]) sentence = Sentence() sentence.add_parameter(parameter) intent_definition = IntentDefinition("TestIntent") intent_definition.add_sentence(sentence) intent_definition_source_mock = Mock() intent_definition_source_mock.get_intent_definitions.return_value = [ intent_definition ] updater = RhasspyUpdater(intent_definition_source_mock) updater.update_rhasspy() requests_mock.assert_called_once() expected_intent_in_call = "[TestIntent]\n(One | Two | Three | Four | Five){Test}" requests_mock.assert_has_calls( [mock.call(self.SENTENCES_URL, expected_intent_in_call)], any_order=True)
def _create_intent_definition() -> IntentDefinition: start_stop_server_definition = IntentDefinition("StartStopTeamspeakServer") sentence = Sentence() sentence.add_parameter(SetParameter(TeamspeakIntentHandler.ACTION, True, possible_values=["Start", "Stop"])) sentence.add_string("teamspeak server") start_stop_server_definition.add_sentence(sentence) return start_stop_server_definition
def test_one_intent_two_sentence(self, requests_mock): sentence = Sentence() sentence.add_string("Some test text") sentence2 = Sentence() sentence2.add_string("Some other test text") intent_definition = IntentDefinition("TestIntent") intent_definition.add_sentence(sentence) intent_definition.add_sentence(sentence2) intent_definition_source_mock = Mock() intent_definition_source_mock.get_intent_definitions.return_value = [ intent_definition ] updater = RhasspyUpdater(intent_definition_source_mock) updater.update_rhasspy() requests_mock.assert_called_once() expected_intent_in_call = "[TestIntent]\nSome test text\nSome other test text" requests_mock.assert_called_with(self.SENTENCES_URL, expected_intent_in_call)