def setUp(self): # Kill the singleton Singleton._instances = dict() # Init neuron1 = Neuron(name='neurone1', parameters={'var1': 'val1'}) neuron2 = Neuron(name='neurone2', parameters={'var2': 'val2'}) neuron3 = Neuron(name='neurone3', parameters={'var3': 'val3'}) neuron4 = Neuron(name='neurone4', parameters={'var4': 'val4'}) signal1 = Order(sentence="this is the sentence") signal2 = Order(sentence="this is the second sentence") signal3 = Order(sentence="that is part of the third sentence") self.synapse1 = Synapse(name="Synapse1", neurons=[neuron1, neuron2], signals=[signal1]) self.synapse2 = Synapse(name="Synapse2", neurons=[neuron3, neuron4], signals=[signal2]) self.synapse3 = Synapse(name="Synapse3", neurons=[neuron2, neuron4], signals=[signal3]) self.all_synapse_list1 = [self.synapse1, self.synapse2, self.synapse3] self.all_synapse_list2 = [self.synapse2, self.synapse3] self.brain_test1 = Brain(synapses=self.all_synapse_list1) self.brain_test2 = Brain(synapses=self.all_synapse_list2) # this brain is the same as the first one self.brain_test3 = Brain(synapses=self.all_synapse_list1) self.settings_test = Settings(default_synapse="Synapse3") # clean the LiFO LIFOBuffer.lifo_list = list()
def test_get_list_synapse_with_raise_missing_parameters(self): # Init neuron1 = Neuron(name='neurone1', parameters={'var1': 'val1'}) neuron2 = Neuron(name='neurone2', parameters={'var2': 'val2'}) neuron3 = Neuron(name='neurone3', parameters={'var3': 'val3'}) neuron4 = Neuron(name='neurone4', parameters={'var4': 'val4'}) fake_geolocation_parameters = { "longitude": 66, "radius": 66, } signal1 = Signal(name="geolocation", parameters=fake_geolocation_parameters) signal2 = Signal(name="order", parameters="this is the second sentence") synapse1 = Synapse(name="Synapse1", neurons=[neuron1, neuron2], signals=[signal1]) synapse2 = Synapse(name="Synapse2", neurons=[neuron3, neuron4], signals=[signal2]) synapses_list = [synapse1, synapse2] br = Brain(synapses=synapses_list) # Stubbing the Geolocation Signal with the brain geo = Geolocation() geo.brain = br with self.assertRaises(MissingParameter): geo.run()
def test_get_neurons(self): """ Test to get neurons from the brainLoader scenarii: - 1/ get a simple neuron from the brainloader - 2/ get a neuron with global variables as parameters """ # 1/ get a simple neuron from the brainloader st = Settings() neuron_list = [{'say': {'message': ['test message']}}] neuron = Neuron(name='say', parameters={'message': ['test message']}) bl = BrainLoader(file_path=self.brain_to_test) neurons_from_brain_loader = bl._get_neurons(neuron_list, settings=st) self.assertEqual([neuron], neurons_from_brain_loader) # 2/ get a neuron with global variables as parameters neuron_list = [{'say': {'message': ['bonjour {{name}}']}}] variables = {"author": "Lamonf", "test_number": 60, "name": "kalliope"} st = Settings(variables=variables) bl = BrainLoader(file_path=self.brain_to_test) neurons_from_brain_loader = bl._get_neurons(neuron_list, settings=st) neuron = Neuron(name='say', parameters={'message': ['bonjour kalliope']}) self.assertEqual([neuron], neurons_from_brain_loader)
def setUp(self): # Init neuron1 = Neuron(name='neurone1', parameters={'var1': 'val1'}) neuron2 = Neuron(name='neurone2', parameters={'var2': 'val2'}) neuron3 = Neuron(name='neurone3', parameters={'var3': 'val3'}) neuron4 = Neuron(name='neurone4', parameters={'var4': 'val4'}) signal1 = Signal(name="order", parameters="this is the sentence") signal2 = Signal(name="order", parameters="this is the second sentence") signal3 = Signal(name="order", parameters="that is part of the third sentence") self.synapse1 = Synapse(name="Synapse1", neurons=[neuron1, neuron2], signals=[signal1]) self.synapse2 = Synapse(name="Synapse2", neurons=[neuron3, neuron4], signals=[signal2]) self.synapse3 = Synapse(name="Synapse3", neurons=[neuron2, neuron4], signals=[signal3]) self.all_synapse_list = [self.synapse1, self.synapse2, self.synapse3] self.brain_test = Brain(synapses=self.all_synapse_list) self.settings_test = Settings(default_synapse="Synapse3") # clean the LiFO Singleton._instances = dict()
def test_load_brain(self): """ Test the class return a valid brain object """ neuron = Neuron(name='say', parameters={'message': ['test message']}) neuron2 = Neuron(name='sleep', parameters={'seconds': 60}) signal1 = Signal(name="order", parameters="test_order") signal2 = Signal(name="order", parameters="test_order_2") signal3 = Signal(name="order", parameters="test_order_3") signal4 = Signal(name="order", parameters="order_for_int") synapse1 = Synapse(name="test", neurons=[neuron], signals=[signal1]) synapse2 = Synapse(name="test2", neurons=[neuron], signals=[signal2]) synapse3 = Synapse(name="test3", neurons=[neuron], signals=[signal3]) synapse4 = Synapse(name="testint", neurons=[neuron2], signals=[signal4]) synapses = [synapse1, synapse2, synapse4, synapse3] brain = Brain() brain.synapses = synapses brain.brain_file = self.brain_to_test brain.brain_yaml = self.expected_result brain_loader = BrainLoader(file_path=self.brain_to_test) self.assertEqual(brain, brain_loader.brain)
def test_password_parameter(self): neuron_name = "test" neuron_parameters = {"password": "******", "parameter": "test"} neuron = Neuron() neuron.name = neuron_name neuron.parameters = neuron_parameters expected_result = "Neuron: name: test, parameters: {'password': '******', 'parameter': 'test'}" self.assertEqual(neuron.__str__(), expected_result)
def test_get_matching_synapse(self): # Init neuron1 = Neuron(name='neurone1', parameters={'var1': 'val1'}) neuron2 = Neuron(name='neurone2', parameters={'var2': 'val2'}) neuron3 = Neuron(name='neurone3', parameters={'var3': 'val3'}) neuron4 = Neuron(name='neurone4', parameters={'var4': 'val4'}) signal1 = Signal(name="order", parameters="this is the sentence") signal2 = Signal(name="order", parameters="this is the second sentence") signal3 = Signal(name="order", parameters="that is part of the third sentence") synapse1 = Synapse(name="Synapse1", neurons=[neuron1, neuron2], signals=[signal1]) synapse2 = Synapse(name="Synapse2", neurons=[neuron3, neuron4], signals=[signal2]) synapse3 = Synapse(name="Synapse3", neurons=[neuron2, neuron4], signals=[signal3]) all_synapse_list = [synapse1, synapse2, synapse3] br = Brain(synapses=all_synapse_list) # TEST1: should return synapse1 spoken_order = "this is the sentence" # Create the matched synapse matched_synapse_1 = MatchedSynapse(matched_synapse=synapse1, matched_order=spoken_order, user_order=spoken_order) matched_synapses = OrderAnalyser.get_matching_synapse( order=spoken_order, brain=br) self.assertEqual(len(matched_synapses), 1) self.assertTrue(matched_synapse_1 in matched_synapses) # TEST2: should return synapse1 and 2 spoken_order = "this is the second sentence" matched_synapses = OrderAnalyser.get_matching_synapse( order=spoken_order, brain=br) self.assertEqual(len(matched_synapses), 2) self.assertTrue(synapse1, synapse2 in matched_synapses) # TEST3: should empty spoken_order = "not a valid order" matched_synapses = OrderAnalyser.get_matching_synapse( order=spoken_order, brain=br) self.assertFalse(matched_synapses)
def test_get_list_synapse_with_mqtt_subscriber(self): # test with one signal mqtt neuron = Neuron(name='say', parameters={'message': ['test message']}) signal1 = Signal(name="mqtt_subscriber", parameters={ "topic": "test", "broker_ip": "192.168.0.1" }) synapse1 = Synapse(name="synapse1", neurons=[neuron], signals=[signal1]) synapses = [synapse1] brain = Brain() brain.synapses = synapses expected_result = synapses mq = Mqtt_subscriber() mq.brain = brain generator = mq.get_list_synapse() self.assertEqual(expected_result, list(generator)) # test with two synapse neuron = Neuron(name='say', parameters={'message': ['test message']}) signal1 = Signal(name="order", parameters="test_order") signal2 = Signal(name="mqtt_subscriber", parameters={ "topic": "test", "broker_ip": "192.168.0.1" }) synapse1 = Synapse(name="synapse1", neurons=[neuron], signals=[signal1]) synapse2 = Synapse(name="synapse2", neurons=[neuron], signals=[signal1, signal2]) synapses = [synapse1, synapse2] brain = Brain() brain.synapses = synapses expected_result = [synapse2] mq = Mqtt_subscriber() mq.brain = brain generator = mq.get_list_synapse() self.assertEqual(expected_result, list(generator))
def run_ansible_playbook_module(self, install_file_path): """ Run the install.yml file through an Ansible playbook using the dedicated neuron ! :param sudo_password: local machine sudo password required to install libraries :param install_file_path: the path of the Ansible playbook to run. :return: """ logger.debug("[ResourcesManager] Run ansible playbook") Utils.print_info("Starting neuron installation") # ask the sudo password if self.sudo_password is not None: pswd = self.sudo_password else: pswd = getpass.getpass('Sudo password:') if not pswd or pswd == "": Utils.print_warning("You must enter a sudo password") return False else: ansible_neuron_parameters = { "task_file": install_file_path, "sudo": True, "sudo_user": "******", "sudo_password": pswd } neuron = Neuron(name="ansible_playbook", parameters=ansible_neuron_parameters) NeuronLauncher.start_neuron(neuron) return True
def test_Synapse(self): neuron1 = Neuron(name='neurone1', parameters={'var1': 'val1'}) neuron2 = Neuron(name='neurone2', parameters={'var2': 'val2'}) neuron3 = Neuron(name='neurone3', parameters={'var3': 'val3'}) neuron4 = Neuron(name='neurone4', parameters={'var4': 'val4'}) signal1 = Signal(name="order", parameters="this is the sentence") signal2 = Signal(name="order", parameters="this is the second sentence") synapse1 = Synapse(name="Synapse1", neurons=[neuron1, neuron2], signals=[signal1]) synapse2 = Synapse(name="Synapse2", neurons=[neuron3, neuron4], signals=[signal2]) synapse3 = Synapse(name="Synapse1", neurons=[neuron1, neuron2], signals=[signal1]) expected_result_serialize = { 'signals': [{ 'name': 'order', 'parameters': 'this is the sentence' }], 'neurons': [{ 'name': 'neurone1', 'parameters': { 'var1': 'val1' } }, { 'name': 'neurone2', 'parameters': { 'var2': 'val2' } }], 'name': 'Synapse1', 'enabled': True } self.assertDictEqual(expected_result_serialize, synapse1.serialize()) self.assertTrue(synapse1.__eq__(synapse3)) self.assertFalse(synapse1.__eq__(synapse2))
def test_get_neurons(self): neuron_list = [{'say': {'message': ['test message']}}] neuron = Neuron(name='say', parameters={'message': ['test message']}) bl = BrainLoader(file_path=self.brain_to_test) neurons_from_brain_loader = bl._get_neurons(neuron_list) self.assertEqual([neuron], neurons_from_brain_loader)
def test_get_neurons(self): """ Test to get neurons from the brainLoader scenarii: - 1/ get a simple neuron from the brainloader - 2/ get a neuron with brackets - 3/ get a neuron with int as parameters """ # 1/ get a simple neuron from the brainloader st = Settings() neuron_list = [{'say': {'message': ['test message']}}] neuron = Neuron(name='say', parameters={'message': ['test message']}) bl = BrainLoader(file_path=self.brain_to_test) neurons_from_brain_loader = bl.get_neurons(neuron_list, settings=st) self.assertEqual([neuron], neurons_from_brain_loader) # 2/ get a neuron with global variables as parameters neuron_list = [{'say': {'message': ['bonjour {{name}}']}}] st = Settings() bl = BrainLoader(file_path=self.brain_to_test) neurons_from_brain_loader = bl.get_neurons(neuron_list, settings=st) neuron = Neuron(name='say', parameters={'message': ['bonjour {{name}}']}) self.assertEqual([neuron], neurons_from_brain_loader) # 3/ get a neuron with int as parameters st = Settings() neuron_list = [{'sleep': {'seconds': 60}}] neuron = Neuron(name='sleep', parameters={'seconds': 60}) bl = BrainLoader(file_path=self.brain_to_test) neurons_from_brain_loader = bl.get_neurons(neuron_list, settings=st) self.assertEqual([neuron], neurons_from_brain_loader)
def run_ansible_playbook_module(install_file_path): """ Run the install.yml file through an Ansible playbook using the dedicated neuron ! :param install_file_path: the path of the Ansible playbook to run. :return: """ logger.debug("[ResourcesManager] Run ansible playbook") Utils.print_info("Starting neuron installation") # ask the sudo password pswd = getpass.getpass('Sudo password:') ansible_neuron_parameters = { "task_file": install_file_path, "sudo": True, "sudo_user": "******", "sudo_password": pswd } neuron = Neuron(name="ansible_playbook", parameters=ansible_neuron_parameters) NeuronLauncher.start_neuron(neuron)
def test_get_brain(self): """ Test the class return a valid brain object """ neuron = Neuron(name='say', parameters={'message': ['test message']}) signal1 = Order(sentence="test_order") signal2 = Order(sentence="test_order_2") signal3 = Order(sentence="test_order_3") synapse1 = Synapse(name="test", neurons=[neuron], signals=[signal1]) synapse2 = Synapse(name="test2", neurons=[neuron], signals=[signal2]) synapse3 = Synapse(name="test3", neurons=[neuron], signals=[signal3]) synapses = [synapse1, synapse2, synapse3] brain = Brain() brain.synapses = synapses brain.brain_file = self.brain_to_test brain.brain_yaml = self.expected_result brain_loader = BrainLoader(file_path=self.brain_to_test) self.assertEqual(brain, brain_loader.brain)
def test_get_list_broker_to_instantiate(self): # ---------------- # only one synapse # ---------------- neuron = Neuron(name='say', parameters={'message': ['test message']}) signal1 = Signal(name="mqtt_subscriber", parameters={ "topic": "topic1", "broker_ip": "192.168.0.1" }) synapse1 = Synapse(name="synapse1", neurons=[neuron], signals=[signal1]) brain = Brain() brain.synapses = [synapse1] list_synapse_with_mqtt_subscriber = [synapse1] expected_broker = Broker() expected_broker.broker_ip = "192.168.0.1" expected_broker.topics = list() expected_topic = Topic() expected_topic.name = "topic1" # add the current synapse to the topic expected_topic.synapses = list() expected_topic.synapses.append(synapse1) expected_broker.topics.append(expected_topic) expected_retuned_list = [expected_broker] mq = Mqtt_subscriber(brain=brain) self.assertListEqual( expected_retuned_list, mq.get_list_broker_to_instantiate( list_synapse_with_mqtt_subscriber)) # ---------------- # one synapse, two different broker # ---------------- neuron = Neuron(name='say', parameters={'message': ['test message']}) signal1 = Signal(name="mqtt_subscriber", parameters={ "topic": "topic1", "broker_ip": "192.168.0.1", "is_json": False }) signal2 = Signal(name="mqtt_subscriber", parameters={ "topic": "topic2", "broker_ip": "172.16.0.1", "is_json": False }) synapse1 = Synapse(name="synapse1", neurons=[neuron], signals=[signal1, signal2]) brain = Brain() brain.synapses = [synapse1] list_synapse_with_mqtt_subscriber = [synapse1] expected_broker1 = Broker() expected_broker1.broker_ip = "192.168.0.1" expected_broker1.topics = list() expected_topic = Topic() expected_topic.name = "topic1" # add the current synapse to the topic expected_topic.synapses = list() expected_topic.synapses.append(synapse1) expected_broker1.topics.append(expected_topic) expected_broker2 = Broker() expected_broker2.broker_ip = "172.16.0.1" expected_broker2.topics = list() expected_topic = Topic() expected_topic.name = "topic2" # add the current synapse to the topic expected_topic.synapses = list() expected_topic.synapses.append(synapse1) expected_broker2.topics.append(expected_topic) expected_retuned_list = [expected_broker1, expected_broker2] mq = Mqtt_subscriber(brain=brain) self.assertEqual( expected_retuned_list, mq.get_list_broker_to_instantiate( list_synapse_with_mqtt_subscriber)) # ---------------- # two synapse, same broker, different topics # ---------------- # synapse 1 neuron1 = Neuron(name='say', parameters={'message': ['test message']}) signal1 = Signal(name="mqtt_subscriber", parameters={ "topic": "topic1", "broker_ip": "192.168.0.1" }) synapse1 = Synapse(name="synapse1", neurons=[neuron1], signals=[signal1]) # synapse 2 neuron2 = Neuron(name='say', parameters={'message': ['test message']}) signal2 = Signal(name="mqtt_subscriber", parameters={ "topic": "topic2", "broker_ip": "192.168.0.1" }) synapse2 = Synapse(name="synapse2", neurons=[neuron2], signals=[signal2]) brain = Brain() brain.synapses = [synapse1, synapse2] list_synapse_with_mqtt_subscriber = [synapse1, synapse2] expected_broker1 = Broker() expected_broker1.broker_ip = "192.168.0.1" expected_broker1.topics = list() expected_topic1 = Topic() expected_topic1.name = "topic1" expected_topic2 = Topic() expected_topic2.name = "topic2" # add the current synapse to the topic expected_topic1.synapses = [synapse1] expected_topic2.synapses = [synapse2] # add both topic to the broker expected_broker1.topics.append(expected_topic1) expected_broker1.topics.append(expected_topic2) expected_retuned_list = [expected_broker1] mq = Mqtt_subscriber(brain=brain) self.assertEqual( expected_retuned_list, mq.get_list_broker_to_instantiate( list_synapse_with_mqtt_subscriber)) # ---------------- # two synapse, same broker, same topic # ---------------- # synapse 1 neuron1 = Neuron(name='say', parameters={'message': ['test message']}) signal1 = Signal(name="mqtt_subscriber", parameters={ "topic": "topic1", "broker_ip": "192.168.0.1" }) synapse1 = Synapse(name="synapse1", neurons=[neuron1], signals=[signal1]) # synapse 2 neuron2 = Neuron(name='say', parameters={'message': ['test message']}) signal2 = Signal(name="mqtt_subscriber", parameters={ "topic": "topic1", "broker_ip": "192.168.0.1" }) synapse2 = Synapse(name="synapse2", neurons=[neuron2], signals=[signal2]) brain = Brain() brain.synapses = [synapse1, synapse2] list_synapse_with_mqtt_subscriber = [synapse1, synapse2] expected_broker1 = Broker() expected_broker1.broker_ip = "192.168.0.1" expected_broker1.topics = list() expected_topic1 = Topic() expected_topic1.name = "topic1" # add both synapses to the topic expected_topic1.synapses = [synapse1, synapse2] # add the topic to the broker expected_broker1.topics.append(expected_topic1) expected_retuned_list = [expected_broker1] mq = Mqtt_subscriber(brain=brain) self.assertEqual( expected_retuned_list, mq.get_list_broker_to_instantiate( list_synapse_with_mqtt_subscriber))
def test_get_matching_synapse(self): # Init neuron1 = Neuron(name='neurone1', parameters={'var1': 'val1'}) neuron2 = Neuron(name='neurone2', parameters={'var2': 'val2'}) neuron3 = Neuron(name='neurone3', parameters={'var3': 'val3'}) neuron4 = Neuron(name='neurone4', parameters={'var4': 'val4'}) signal1 = Signal(name="order", parameters="this is the sentence") signal2 = Signal(name="order", parameters="this is the second sentence") signal3 = Signal(name="order", parameters="that is part of the third sentence") signal4 = Signal(name="order", parameters={ "matching-type": "strict", "text": "that is part of the fourth sentence" }) signal5 = Signal(name="order", parameters={ "matching-type": "ordered-strict", "text": "sentence 5 with specific order" }) signal6 = Signal(name="order", parameters={ "matching-type": "normal", "text": "matching type normal" }) signal7 = Signal(name="order", parameters={ "matching-type": "non-existing", "text": "matching type non existing" }) signal8 = Signal(name="order", parameters={ "matching-type": "non-existing", "non-existing-parameter": "will not match order" }) signal9 = Signal( name="order", parameters= "order that should be triggered because synapse is disabled") signal10 = Signal(name="order", parameters="i say this") signal11 = Signal(name="order", parameters="and then that") signal12 = Signal(name="order", parameters={ "matching-type": "strict", "text": "just 1 test", "stt-correction": [{ "input": "one", "output": "1" }] }) synapse1 = Synapse(name="Synapse1", neurons=[neuron1, neuron2], signals=[signal1]) synapse2 = Synapse(name="Synapse2", neurons=[neuron3, neuron4], signals=[signal2]) synapse3 = Synapse(name="Synapse3", neurons=[neuron2, neuron4], signals=[signal3]) synapse4 = Synapse(name="Synapse4", neurons=[neuron2, neuron4], signals=[signal4]) synapse5 = Synapse(name="Synapse5", neurons=[neuron1, neuron2], signals=[signal5]) synapse6 = Synapse(name="Synapse6", neurons=[neuron1, neuron2], signals=[signal6]) synapse7 = Synapse(name="Synapse7", neurons=[neuron1, neuron2], signals=[signal7]) synapse8 = Synapse(name="Synapse8", neurons=[neuron1, neuron2], signals=[signal8]) synapse9 = Synapse(name="Synapse9", enabled=False, neurons=[neuron1, neuron2], signals=[signal9]) synapse10 = Synapse(name="Synapse10", neurons=[neuron1], signals=[signal10, signal11]) synapse11 = Synapse(name="Synapse11", neurons=[neuron1], signals=[signal12]) all_synapse_list = [ synapse1, synapse2, synapse3, synapse4, synapse5, synapse6, synapse7, synapse8, synapse9, synapse10, synapse11 ] br = Brain(synapses=all_synapse_list) # TEST1: should return synapse1 spoken_order = "this is the sentence" # Create the matched synapse expected_matched_synapse_1 = MatchedSynapse(matched_synapse=synapse1, matched_order=spoken_order, user_order=spoken_order) matched_synapses = OrderAnalyser.get_matching_synapse( order=spoken_order, brain=br) self.assertEqual(len(matched_synapses), 1) self.assertTrue(expected_matched_synapse_1 in matched_synapses) # with defined normal matching type spoken_order = "matching type normal" expected_matched_synapse_5 = MatchedSynapse(matched_synapse=synapse6, matched_order=spoken_order, user_order=spoken_order) matched_synapses = OrderAnalyser.get_matching_synapse( order=spoken_order, brain=br) self.assertEqual(len(matched_synapses), 1) self.assertTrue(expected_matched_synapse_5 in matched_synapses) # TEST2: should return synapse1 and 2 spoken_order = "this is the second sentence" expected_matched_synapse_2 = MatchedSynapse(matched_synapse=synapse1, matched_order=spoken_order, user_order=spoken_order) matched_synapses = OrderAnalyser.get_matching_synapse( order=spoken_order, brain=br) self.assertEqual(len(matched_synapses), 2) self.assertTrue(expected_matched_synapse_1, expected_matched_synapse_2 in matched_synapses) # TEST3: should empty spoken_order = "not a valid order" matched_synapses = OrderAnalyser.get_matching_synapse( order=spoken_order, brain=br) self.assertFalse(matched_synapses) # TEST4: with matching type strict spoken_order = "that is part of the fourth sentence" expected_matched_synapse_3 = MatchedSynapse(matched_synapse=synapse4, matched_order=spoken_order, user_order=spoken_order) matched_synapses = OrderAnalyser.get_matching_synapse( order=spoken_order, brain=br) self.assertTrue(expected_matched_synapse_3 in matched_synapses) spoken_order = "that is part of the fourth sentence with more word" matched_synapses = OrderAnalyser.get_matching_synapse( order=spoken_order, brain=br) self.assertFalse(matched_synapses) # TEST5: with matching type ordered strict spoken_order = "sentence 5 with specific order" expected_matched_synapse_4 = MatchedSynapse(matched_synapse=synapse5, matched_order=spoken_order, user_order=spoken_order) matched_synapses = OrderAnalyser.get_matching_synapse( order=spoken_order, brain=br) self.assertEqual(len(matched_synapses), 1) self.assertTrue(expected_matched_synapse_4 in matched_synapses) spoken_order = "order specific with 5 sentence" matched_synapses = OrderAnalyser.get_matching_synapse( order=spoken_order, brain=br) self.assertFalse(matched_synapses) # TEST6: non supported type of matching. should fallback to normal spoken_order = "matching type non existing" expected_matched_synapse_5 = MatchedSynapse(matched_synapse=synapse7, matched_order=spoken_order, user_order=spoken_order) matched_synapses = OrderAnalyser.get_matching_synapse( order=spoken_order, brain=br) self.assertTrue(expected_matched_synapse_5 in matched_synapses) # TEST7: should not match the disabled synapse spoken_order = "order that should be triggered because synapse is disabled" # we expect an empty list matched_synapses = OrderAnalyser.get_matching_synapse( order=spoken_order, brain=br) self.assertTrue(len(matched_synapses) == 0) # TEST8: a spoken order that match multiple time the same synapse should return the synapse only once spoken_order = "i say this and then that" matched_synapses = OrderAnalyser.get_matching_synapse( order=spoken_order, brain=br) self.assertTrue("Synapse10" in matched_synapse.synapse.name for matched_synapse in matched_synapses) self.assertTrue(len(matched_synapses) == 1) # TEST9: with STT correction spoken_order = "just one test" matched_synapses = OrderAnalyser.get_matching_synapse( order=spoken_order, brain=br) self.assertTrue("Synapse11" in matched_synapse.synapse.name for matched_synapse in matched_synapses) self.assertTrue(spoken_order in matched_synapse.user_order for matched_synapse in matched_synapses) self.assertTrue(len(matched_synapses) == 1)
def test_Neuron(self): neuron1 = Neuron(name="test", parameters={"key1": "val1", "key2": "val2"}) neuron2 = Neuron(name="test", parameters={"key3": "val3", "key4": "val4"}) neuron3 = Neuron(name="test", parameters={"key1": "val1", "key2": "val2"}) expected_result_serialize = {'name': 'test', 'parameters': {'key2': 'val2', 'key1': 'val1'}} self.assertDictEqual(expected_result_serialize, neuron1.serialize()) self.assertTrue(neuron1.__eq__(neuron3)) self.assertFalse(neuron1.__eq__(neuron2)) # test password neuron_name = "test" neuron_parameters = { "password": "******", "parameter": "test" } neuron = Neuron() neuron.name = neuron_name neuron.parameters = neuron_parameters expected_result_str = "{'name': 'test', 'parameters': {'password': '******', 'parameter': 'test'}}" self.assertDictEqual(ast.literal_eval(neuron.__str__()), ast.literal_eval(expected_result_str)) neuron_name = "test" neuron_parameters = { "password_parameter": "my secret", "parameter": "test" } neuron = Neuron() neuron.name = neuron_name neuron.parameters = neuron_parameters expected_result_str = "{'name': 'test', 'parameters': {'parameter': 'test', 'password_parameter': '*****'}}" self.assertDictEqual(ast.literal_eval(neuron.__str__()), ast.literal_eval(expected_result_str))
def test_Neuron(self): neuron1 = Neuron(name="test", parameters={ "key1": "val1", "key2": "val2" }) neuron2 = Neuron(name="test", parameters={ "key3": "val3", "key4": "val4" }) neuron3 = Neuron(name="test", parameters={ "key1": "val1", "key2": "val2" }) expected_result_serialize = { 'name': 'test', 'parameters': { 'key2': 'val2', 'key1': 'val1' } } self.assertDictEqual(expected_result_serialize, neuron1.serialize()) self.assertTrue(neuron1.__eq__(neuron3)) self.assertFalse(neuron1.__eq__(neuron2)) # test password neuron_name = "test" neuron_parameters = {"password": "******", "parameter": "test"} neuron = Neuron() neuron.name = neuron_name neuron.parameters = neuron_parameters expected_result_str = "{'name': 'test', 'parameters': {'password': '******', 'parameter': 'test'}}" self.assertDictEqual(ast.literal_eval(neuron.__str__()), ast.literal_eval(expected_result_str)) neuron_name = "test" neuron_parameters = { "password_parameter": "my secret", "parameter": "test" } neuron = Neuron() neuron.name = neuron_name neuron.parameters = neuron_parameters expected_result_str = "{'name': 'test', 'parameters': {'parameter': 'test', 'password_parameter': '*****'}}" self.assertDictEqual(ast.literal_eval(neuron.__str__()), ast.literal_eval(expected_result_str))
def test_get_matching_synapse(self): # Init neuron1 = Neuron(name='neurone1', parameters={'var1': 'val1'}) neuron2 = Neuron(name='neurone2', parameters={'var2': 'val2'}) neuron3 = Neuron(name='neurone3', parameters={'var3': 'val3'}) neuron4 = Neuron(name='neurone4', parameters={'var4': 'val4'}) signal1 = Signal(name="order", parameters="this is the sentence") signal2 = Signal(name="order", parameters="this is the second sentence") signal3 = Signal(name="order", parameters="that is part of the third sentence") signal4 = Signal(name="order", parameters={ "matching-type": "strict", "text": "that is part of the fourth sentence" }) signal5 = Signal(name="order", parameters={ "matching-type": "ordered-strict", "text": "sentence 5 with specific order" }) signal6 = Signal(name="order", parameters={ "matching-type": "normal", "text": "matching type normal" }) signal7 = Signal(name="order", parameters={ "matching-type": "non-existing", "text": "matching type non existing" }) signal8 = Signal(name="order", parameters={ "matching-type": "non-existing", "non-existing-parameter": "will not match order" }) synapse1 = Synapse(name="Synapse1", neurons=[neuron1, neuron2], signals=[signal1]) synapse2 = Synapse(name="Synapse2", neurons=[neuron3, neuron4], signals=[signal2]) synapse3 = Synapse(name="Synapse3", neurons=[neuron2, neuron4], signals=[signal3]) synapse4 = Synapse(name="Synapse4", neurons=[neuron2, neuron4], signals=[signal4]) synapse5 = Synapse(name="Synapse5", neurons=[neuron1, neuron2], signals=[signal5]) synapse6 = Synapse(name="Synapse6", neurons=[neuron1, neuron2], signals=[signal6]) synapse7 = Synapse(name="Synapse6", neurons=[neuron1, neuron2], signals=[signal7]) synapse8 = Synapse(name="Synapse6", neurons=[neuron1, neuron2], signals=[signal8]) all_synapse_list = [ synapse1, synapse2, synapse3, synapse4, synapse5, synapse6, synapse7, synapse8 ] br = Brain(synapses=all_synapse_list) # TEST1: should return synapse1 spoken_order = "this is the sentence" # Create the matched synapse expected_matched_synapse_1 = MatchedSynapse(matched_synapse=synapse1, matched_order=spoken_order, user_order=spoken_order) matched_synapses = OrderAnalyser.get_matching_synapse( order=spoken_order, brain=br) self.assertEqual(len(matched_synapses), 1) self.assertTrue(expected_matched_synapse_1 in matched_synapses) # with defined normal matching type spoken_order = "matching type normal" expected_matched_synapse_5 = MatchedSynapse(matched_synapse=synapse6, matched_order=spoken_order, user_order=spoken_order) matched_synapses = OrderAnalyser.get_matching_synapse( order=spoken_order, brain=br) self.assertEqual(len(matched_synapses), 1) self.assertTrue(expected_matched_synapse_5 in matched_synapses) # TEST2: should return synapse1 and 2 spoken_order = "this is the second sentence" expected_matched_synapse_2 = MatchedSynapse(matched_synapse=synapse1, matched_order=spoken_order, user_order=spoken_order) matched_synapses = OrderAnalyser.get_matching_synapse( order=spoken_order, brain=br) self.assertEqual(len(matched_synapses), 2) self.assertTrue(expected_matched_synapse_1, expected_matched_synapse_2 in matched_synapses) # TEST3: should empty spoken_order = "not a valid order" matched_synapses = OrderAnalyser.get_matching_synapse( order=spoken_order, brain=br) self.assertFalse(matched_synapses) # TEST4: with matching type strict spoken_order = "that is part of the fourth sentence" expected_matched_synapse_3 = MatchedSynapse(matched_synapse=synapse4, matched_order=spoken_order, user_order=spoken_order) matched_synapses = OrderAnalyser.get_matching_synapse( order=spoken_order, brain=br) self.assertTrue(expected_matched_synapse_3 in matched_synapses) spoken_order = "that is part of the fourth sentence with more word" matched_synapses = OrderAnalyser.get_matching_synapse( order=spoken_order, brain=br) self.assertFalse(matched_synapses) # TEST5: with matching type ordered strict spoken_order = "sentence 5 with specific order" expected_matched_synapse_4 = MatchedSynapse(matched_synapse=synapse5, matched_order=spoken_order, user_order=spoken_order) matched_synapses = OrderAnalyser.get_matching_synapse( order=spoken_order, brain=br) self.assertEqual(len(matched_synapses), 1) self.assertTrue(expected_matched_synapse_4 in matched_synapses) spoken_order = "order specific with 5 sentence" matched_synapses = OrderAnalyser.get_matching_synapse( order=spoken_order, brain=br) self.assertFalse(matched_synapses) # TEST6: non supported type of matching. should fallback to normal spoken_order = "matching type non existing" expected_matched_synapse_5 = MatchedSynapse(matched_synapse=synapse7, matched_order=spoken_order, user_order=spoken_order) matched_synapses = OrderAnalyser.get_matching_synapse( order=spoken_order, brain=br) self.assertTrue(expected_matched_synapse_5 in matched_synapses)