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_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 test_get_signals(self): signals = [{'order': 'test_order'}] signal = Signal(name="order", parameters="test_order") bl = BrainLoader(file_path=self.brain_to_test) signals_from_brain_loader = bl.get_signals(signals) self.assertEqual([signal], signals_from_brain_loader)
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))