|
@@ -5,6 +5,7 @@ from kalliope.core.OrderAnalyser import OrderAnalyser
|
|
|
from kalliope.core.Models.Neuron import Neuron
|
|
|
from kalliope.core.Models.Synapse import Synapse
|
|
|
from kalliope.core.Models.Brain import Brain
|
|
|
+from kalliope.core.Models.Settings import Settings
|
|
|
from kalliope.core.Models.Order import Order
|
|
|
|
|
|
|
|
@@ -21,7 +22,9 @@ class TestOrderAnalyser(unittest.TestCase):
|
|
|
Scenarii :
|
|
|
- Order matchs a synapse and the synapse has been launched.
|
|
|
- Order does not match but have a default synapse.
|
|
|
- - Order does not match and does not ahve default synapse.
|
|
|
+ - Order does not match and does not have default synapse.
|
|
|
+ - Provide synapse without any external orders
|
|
|
+ - Provide synapse with any external orders
|
|
|
"""
|
|
|
# Init
|
|
|
neuron1 = Neuron(name='neurone1', parameters={'var1': 'val1'})
|
|
@@ -43,9 +46,6 @@ class TestOrderAnalyser(unittest.TestCase):
|
|
|
|
|
|
br = Brain(synapses=all_synapse_list)
|
|
|
|
|
|
- def _start_neuron_mock(cls, neuron, params):
|
|
|
- pass
|
|
|
-
|
|
|
with mock.patch("kalliope.core.OrderAnalyser._start_neuron") as mock_start_neuron_method:
|
|
|
# assert synapses have been launched
|
|
|
order_to_match = "this is the sentence"
|
|
@@ -81,6 +81,35 @@ class TestOrderAnalyser(unittest.TestCase):
|
|
|
expected_result,
|
|
|
"Fail to no synapse because no synapse matchs and no default defined")
|
|
|
|
|
|
+ # Provide synapse to run
|
|
|
+ order_to_match = "this is the sentence"
|
|
|
+ oa = OrderAnalyser(order=order_to_match,
|
|
|
+ brain=br)
|
|
|
+ expected_result = [synapse1]
|
|
|
+ synapses_to_run = [synapse1]
|
|
|
+
|
|
|
+ self.assertEquals(oa.start(synapses_to_run=synapses_to_run),
|
|
|
+ expected_result,
|
|
|
+ "Fail to run the provided synapse to run")
|
|
|
+ calls = [mock.call(neuron1, {}), mock.call(neuron2, {})]
|
|
|
+ mock_start_neuron_method.assert_has_calls(calls=calls)
|
|
|
+ mock_start_neuron_method.reset_mock()
|
|
|
+
|
|
|
+ # Provide synapse and external orders
|
|
|
+ order_to_match = "this is an external sentence"
|
|
|
+ oa = OrderAnalyser(order=order_to_match,
|
|
|
+ brain=br)
|
|
|
+ external_orders = "this is an external {{ order }}"
|
|
|
+ synapses_to_run = [synapse2]
|
|
|
+ expected_result = [synapse2]
|
|
|
+
|
|
|
+ self.assertEquals(oa.start(synapses_to_run=synapses_to_run, external_order=external_orders),
|
|
|
+ expected_result,
|
|
|
+ "Fail to run a provided synapse with external order")
|
|
|
+ calls = [mock.call(neuron3, {"order":u"sentence"}), mock.call(neuron4, {"order":u"sentence"})]
|
|
|
+ mock_start_neuron_method.assert_has_calls(calls=calls)
|
|
|
+ mock_start_neuron_method.reset_mock()
|
|
|
+
|
|
|
def test_start_neuron(self):
|
|
|
"""
|
|
|
Testing params association and starting a Neuron
|
|
@@ -181,12 +210,12 @@ class TestOrderAnalyser(unittest.TestCase):
|
|
|
sentence_to_test = "this is the order"
|
|
|
|
|
|
# Success
|
|
|
- self.assertTrue(OrderAnalyser._spelt_order_match_brain_order_via_table(order_to_test, sentence_to_test),
|
|
|
+ self.assertTrue(OrderAnalyser.spelt_order_match_brain_order_via_table(order_to_test, sentence_to_test),
|
|
|
"Fail matching order with the expected sentence")
|
|
|
|
|
|
# Failure
|
|
|
sentence_to_test = "unexpected sentence"
|
|
|
- self.assertFalse(OrderAnalyser._spelt_order_match_brain_order_via_table(order_to_test, sentence_to_test),
|
|
|
+ self.assertFalse(OrderAnalyser.spelt_order_match_brain_order_via_table(order_to_test, sentence_to_test),
|
|
|
"Fail to ensure the expected sentence is not matching the order")
|
|
|
|
|
|
def test_get_split_order_without_bracket(self):
|
|
@@ -389,83 +418,67 @@ class TestOrderAnalyser(unittest.TestCase):
|
|
|
expected_result,
|
|
|
"Fail matching 'synapse with all key worlds' from the complete synapse list and the order")
|
|
|
|
|
|
- def test_get_synapse_params(self):
|
|
|
- # Init
|
|
|
- neuron1 = Neuron(name='neurone1', parameters={'var1': 'val1'})
|
|
|
- neuron2 = Neuron(name='neurone2', parameters={'var2': 'val2'})
|
|
|
-
|
|
|
- signal1 = Order(sentence="this is the {{ sentence }}")
|
|
|
-
|
|
|
- synapse1 = Synapse(name="Synapse1", neurons=[neuron1, neuron2], signals=[signal1])
|
|
|
+ def test_get_params_from_order(self):
|
|
|
|
|
|
+ string_order = "this is the {{ sentence }}"
|
|
|
order_to_check = "this is the value"
|
|
|
expected_result = {'sentence': 'value'}
|
|
|
|
|
|
- self.assertEquals(OrderAnalyser._get_synapse_params(synapse=synapse1, order_to_check=order_to_check),
|
|
|
+ self.assertEquals(OrderAnalyser._get_params_from_order(string_order=string_order, order_to_check=order_to_check),
|
|
|
expected_result,
|
|
|
- "Fail to retrieve 'the params' of the synapse from the order")
|
|
|
+ "Fail to retrieve 'the params' of the string_order from the order")
|
|
|
|
|
|
# Multiple match
|
|
|
- signal1 = Order(sentence="this is the {{ sentence }}")
|
|
|
-
|
|
|
- synapse1 = Synapse(name="Synapse1", neurons=[neuron1, neuron2], signals=[signal1])
|
|
|
+ string_order = "this is the {{ sentence }}"
|
|
|
|
|
|
order_to_check = "this is the value with multiple words"
|
|
|
expected_result = {'sentence': 'value with multiple words'}
|
|
|
|
|
|
- self.assertEqual(OrderAnalyser._get_synapse_params(synapse=synapse1, order_to_check=order_to_check),
|
|
|
+ self.assertEqual(OrderAnalyser._get_params_from_order(string_order=string_order, order_to_check=order_to_check),
|
|
|
expected_result,
|
|
|
- "Fail to retrieve the 'multiple words params' of the synapse from the order")
|
|
|
+ "Fail to retrieve the 'multiple words params' of the string_order from the order")
|
|
|
|
|
|
# Multiple params
|
|
|
- signal1 = Order(sentence="this is the {{ sentence }} with multiple {{ params }}")
|
|
|
-
|
|
|
- synapse1 = Synapse(name="Synapse1", neurons=[neuron1, neuron2], signals=[signal1])
|
|
|
+ string_order = "this is the {{ sentence }} with multiple {{ params }}"
|
|
|
|
|
|
order_to_check = "this is the value with multiple words"
|
|
|
expected_result = {'sentence': 'value',
|
|
|
'params':'words'}
|
|
|
|
|
|
- self.assertEqual(OrderAnalyser._get_synapse_params(synapse=synapse1, order_to_check=order_to_check),
|
|
|
+ self.assertEqual(OrderAnalyser._get_params_from_order(string_order=string_order, order_to_check=order_to_check),
|
|
|
expected_result,
|
|
|
- "Fail to retrieve the 'multiple params' of the synapse from the order")
|
|
|
+ "Fail to retrieve the 'multiple params' of the string_order from the order")
|
|
|
|
|
|
# Multiple params with multiple words
|
|
|
- signal1 = Order(sentence="this is the {{ sentence }} with multiple {{ params }}")
|
|
|
-
|
|
|
- synapse1 = Synapse(name="Synapse1", neurons=[neuron1, neuron2], signals=[signal1])
|
|
|
+ string_order = "this is the {{ sentence }} with multiple {{ params }}"
|
|
|
|
|
|
order_to_check = "this is the multiple values with multiple values as words"
|
|
|
expected_result = {'sentence': 'multiple values',
|
|
|
'params': 'values as words'}
|
|
|
|
|
|
- self.assertEqual(OrderAnalyser._get_synapse_params(synapse=synapse1, order_to_check=order_to_check),
|
|
|
+ self.assertEqual(OrderAnalyser._get_params_from_order(string_order=string_order, order_to_check=order_to_check),
|
|
|
expected_result,
|
|
|
- "Fail to retrieve the 'multiple params with multiple words' of the synapse from the order")
|
|
|
+ "Fail to retrieve the 'multiple params with multiple words' of the string_order from the order")
|
|
|
|
|
|
# params at the begining of the sentence
|
|
|
- signal1 = Order(sentence="{{ sentence }} this is the sentence")
|
|
|
-
|
|
|
- synapse1 = Synapse(name="Synapse1", neurons=[neuron1, neuron2], signals=[signal1])
|
|
|
+ string_order = "{{ sentence }} this is the sentence"
|
|
|
|
|
|
order_to_check = "hello world this is the multiple values with multiple values as words"
|
|
|
expected_result = {'sentence': 'hello world'}
|
|
|
|
|
|
- self.assertEqual(OrderAnalyser._get_synapse_params(synapse=synapse1, order_to_check=order_to_check),
|
|
|
+ self.assertEqual(OrderAnalyser._get_params_from_order(string_order=string_order, order_to_check=order_to_check),
|
|
|
expected_result,
|
|
|
- "Fail to retrieve the 'params at the begining of the sentence' of the synapse from the order")
|
|
|
+ "Fail to retrieve the 'params at the begining of the sentence' of the string_order from the order")
|
|
|
|
|
|
# all of the sentence is a variable
|
|
|
- signal1 = Order(sentence="{{ sentence }}")
|
|
|
-
|
|
|
- synapse1 = Synapse(name="Synapse1", neurons=[neuron1, neuron2], signals=[signal1])
|
|
|
+ string_order = "{{ sentence }}"
|
|
|
|
|
|
order_to_check = "this is the all sentence is a variable"
|
|
|
expected_result = {'sentence': 'this is the all sentence is a variable'}
|
|
|
|
|
|
- self.assertEqual(OrderAnalyser._get_synapse_params(synapse=synapse1, order_to_check=order_to_check),
|
|
|
+ self.assertEqual(OrderAnalyser._get_params_from_order(string_order=string_order, order_to_check=order_to_check),
|
|
|
expected_result,
|
|
|
- "Fail to retrieve the 'all of the sentence is a variable' of the synapse from the order")
|
|
|
+ "Fail to retrieve the 'all of the sentence is a variable' of the string_order from the order")
|
|
|
|
|
|
def test_get_default_synapse_from_sysnapses_list(self):
|
|
|
# Init
|
|
@@ -494,6 +507,56 @@ class TestOrderAnalyser(unittest.TestCase):
|
|
|
expected_result,
|
|
|
"Fail to match the expected default Synapse")
|
|
|
|
|
|
+ def test_find_synapse_to_run(self):
|
|
|
+ """
|
|
|
+ Test to find the good synapse to run
|
|
|
+ Scenarii:
|
|
|
+ - Find the synapse
|
|
|
+ - No synpase found, no default synapse
|
|
|
+ - No synapse found, run the default synapse
|
|
|
+ """
|
|
|
+ # 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)
|
|
|
+ st = Settings()
|
|
|
+ # Find synapse
|
|
|
+ order = "this is the sentence"
|
|
|
+ expected_result = [synapse1]
|
|
|
+ self.assertEquals(OrderAnalyser._find_synapse_to_run(brain=br,settings=st, order=order),
|
|
|
+ expected_result,
|
|
|
+ "Fail to run the proper synapse matching the order")
|
|
|
+
|
|
|
+ # No Default synapse
|
|
|
+ order = "No default synapse"
|
|
|
+ expected_result = []
|
|
|
+ self.assertEquals(OrderAnalyser._find_synapse_to_run(brain=br,settings=st, order=order),
|
|
|
+ expected_result,
|
|
|
+ "Fail to run no synapse, when no default is defined")
|
|
|
+
|
|
|
+ # Default synapse
|
|
|
+ st = Settings(default_synapse="Synapse2")
|
|
|
+ order = "default synapse"
|
|
|
+ expected_result = [synapse2]
|
|
|
+ self.assertEquals(OrderAnalyser._find_synapse_to_run(brain=br, settings=st, order=order),
|
|
|
+ expected_result,
|
|
|
+ "Fail to run the default synapse")
|
|
|
+
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
unittest.main()
|