def test_get_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 = Order(sentence="test_order") signal2 = Order(sentence="test_order_2") signal3 = Order(sentence="test_order_3") signal4 = Order(sentence="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 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 = 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_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 LIFOBuffer.lifo_list = list()
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 = Order(sentence="this is the sentence") signal2 = Order(sentence="this is the second sentence") signal3 = Order(sentence="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_signals(self): signals = [{'order': 'test_order'}] signal = Order(sentence='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_Order(self): order1 = Order(sentence="this is an order") order2 = Order(sentence="this is an other order") order3 = Order(sentence="this is an order") expected_result_serialize = {'order': 'this is an order'} expected_result_str = "{'order': 'this is an order'}" self.assertEqual(expected_result_serialize, order1.serialize()) self.assertEqual(expected_result_str, order1.__str__()) self.assertTrue(order1.__eq__(order3)) self.assertFalse(order1.__eq__(order2))
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 = Order(sentence="this is the sentence") signal2 = Order(sentence="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': [ { 'order': 'this is the sentence' } ], 'neurons': [ { 'name': 'neurone1', 'parameters': { 'var1': 'val1' } }, { 'name': 'neurone2', 'parameters': { 'var2': 'val2' } } ], 'name': 'Synapse1' } self.assertDictEqual(expected_result_serialize, synapse1.serialize()) self.assertTrue(synapse1.__eq__(synapse3)) self.assertFalse(synapse1.__eq__(synapse2))
def test_get_event_or_order_from_dict(self): order_object = Order(sentence="test_order") event_object = Event(hour="7") dict_order = {'order': 'test_order'} dict_event = {'event': {'hour': '7'}} bl = BrainLoader(file_path=self.brain_to_test) order_from_bl = bl._get_event_or_order_from_dict(dict_order) event_from_bl = bl._get_event_or_order_from_dict(dict_event) self.assertEqual(order_from_bl, order_object) self.assertEqual(event_from_bl, event_object)
def test_get_event_or_order_from_dict(self): order_object = Order(sentence="test_order") event_object = Event(period="0 7 * * *") dict_order = {'order': 'test_order'} dict_event = {'event': '0 7 * * *'} bl = BrainLoader.Instance(file_path=self.brain_to_test) order_from_bl = bl._get_event_or_order_from_dict(dict_order) event_from_bl = bl._get_event_or_order_from_dict(dict_event) self.assertEqual(order_from_bl, order_object) self.assertEqual(event_from_bl, event_object) del bl