Przeglądaj źródła

[tests] Complete first step of the unit testing of OrderAnalyser +
[Fix] Renaming neurons module name to match new instanciation requirements

monf 8 lat temu
rodzic
commit
2ccb86ebb0

+ 3 - 2
brains/openweathermap.yml

@@ -1,13 +1,14 @@
 ---
   - name: "get-the-weather"
     signals:
-      - order: "quel temps fait-il"
+      - order: "quel temps fait-il {{ location }}"
     neurons:
       - openweathermap:
           api_key: "your-api"
           lang: "fr"
           temp_unit: "celsius"
-          location : "grenoble"
           country: "FR"
+          args:
+            - location
           say_template:
           - "Aujourd'hui a {{ location }} le temps est {{ weather_today }} avec une température de {{ temp_today_temp }} degrés et demain le temps sera {{ weather_tomorrow }} avec une température de {{ temp_tomorrow_temp }} degrés"

+ 10 - 6
core/OrderAnalyser.py

@@ -50,7 +50,7 @@ class OrderAnalyser:
                         # if the order contains bracket, we get parameters said by the user
                         params = None
                         if self._is_containing_bracket(signal.sentence):
-                            params = self._associate_order_params_to_values(signal.sentence)
+                            params = self._associate_order_params_to_values(self.order, signal.sentence)
                             logger.debug("Parameters for order: %s" % params)
 
                         for neuron in synapse.neurons:
@@ -90,10 +90,14 @@ class OrderAnalyser:
         # return the list of launched synapse
         return launched_synapses
 
-    def _associate_order_params_to_values(self, order_to_check):
+    @classmethod
+    def _associate_order_params_to_values(cls, order, order_to_check):
         """
         Associate the variables from the order to the incoming user order
-        :param order_to_check: the order to check
+        :param order_to_check: the order to check incoming from the brain
+        :type order_to_check: str
+        :param order: the order from user
+        :type order: str
         :return: the dict corresponding to the key / value of the params
         """
         pattern = '\s+(?=[^\{\{\}\}]*\}\})'
@@ -105,16 +109,16 @@ class OrderAnalyser:
         the_order = order_to_check[:order_to_check.find('{{')]
 
         # remove sentence before order which are sentences not matching anyway
-        truncate_user_sentence = self.order[self.order.find(the_order):]
+        truncate_user_sentence = order[order.find(the_order):]
         truncate_list_word_said = truncate_user_sentence.split()
 
         # make dict var:value
         dict_var = {}
         for idx, ow in enumerate(list_word_in_order):
-            if self._is_containing_bracket(ow):
+            if cls._is_containing_bracket(ow):
                 # remove bracket and grab the next value / stop value
                 var_name = ow.replace("{{", "").replace("}}", "")
-                stop_value = self._get_next_value_list(list_word_in_order[idx:])
+                stop_value = cls._get_next_value_list(list_word_in_order[idx:])
                 if stop_value is None:
                     dict_var[var_name] = " ".join(truncate_list_word_said)
                     break

+ 99 - 0
core/Tests/test_order_analyser.py

@@ -1,4 +1,6 @@
 import unittest
+from mock import patch, Mock, MagicMock
+
 
 from core.OrderAnalyser import OrderAnalyser
 
@@ -92,5 +94,102 @@ class TestOrderAnalyser(unittest.TestCase):
         self.assertEqual(OrderAnalyser._get_split_order_without_bracket(order_to_test), expected_result,
                          "No space brackets Fails to return the expected list")
 
+    def test_associate_order_params_to_values(self):
+       ##
+       # Testing the brackets position behaviour
+       ##
+
+       # Success
+       order_brain = "This is the {{ variable }}"
+       order_user = "This is the value"
+       expected_result = {'variable': 'value'}
+       self.assertEqual(OrderAnalyser._associate_order_params_to_values(order_user,order_brain), expected_result,
+                        "Fail to match the order_brain {{ variable }} to the 'value'")
+
+       # Success
+       order_brain = "This is the {{variable }}"
+       order_user = "This is the value"
+       expected_result = {'variable': 'value'}
+       self.assertEqual(OrderAnalyser._associate_order_params_to_values(order_user, order_brain), expected_result,
+                        "Fail to match the order_brain {{variable }} to the 'value'")
+
+       # Success
+       order_brain = "This is the {{ variable}}"
+       order_user = "This is the value"
+       expected_result = {'variable': 'value'}
+       self.assertEqual(OrderAnalyser._associate_order_params_to_values(order_user, order_brain), expected_result,
+                        "Fail to match the order_brain {{ variable}} to the 'value'")
+
+       # Success
+       order_brain = "This is the {{variable}}"
+       order_user = "This is the value"
+       expected_result = {'variable': 'value'}
+       self.assertEqual(OrderAnalyser._associate_order_params_to_values(order_user, order_brain), expected_result,
+                        "Fail to match the order_brain {{variable}} to the 'value'")
+
+       # Fail
+       order_brain = "This is the {variable}"
+       order_user = "This is the value"
+       expected_result = {'variable': 'value'}
+       self.assertNotEquals(OrderAnalyser._associate_order_params_to_values(order_user, order_brain), expected_result,
+                        "Should not match the order_brain {variable} to the 'value'")
+
+       # Fail
+       order_brain = "This is the { variable}}"
+       order_user = "This is the value"
+       expected_result = {'variable': 'value'}
+       self.assertNotEquals(OrderAnalyser._associate_order_params_to_values(order_user, order_brain), expected_result,
+                        "Should not match the order_brain { variable}} to the 'value'")
+
+       ##
+       # Testing the brackets position in the sentence
+       ##
+
+       # Success
+       order_brain = "{{ variable }} This is the"
+       order_user = "value This is the"
+       expected_result = {'variable': 'value'}
+       self.assertEqual(OrderAnalyser._associate_order_params_to_values(order_user, order_brain), expected_result,
+                         "Fail to match the order_brain {{ variable }} in first position ins the sentence to the 'value'")
+
+       # Success
+       order_brain = "This is {{ variable }} the"
+       order_user = " This is value the"
+       expected_result = {'variable': 'value'}
+       self.assertEqual(OrderAnalyser._associate_order_params_to_values(order_user, order_brain), expected_result,
+                         "Fail to match the order_brain {{ variable }} in middle position ins the sentence to the 'value'")
+
+
+       ##
+       # Testing multi variables
+       ##
+
+       # Success
+       order_brain = "This is {{ variable }} the {{ variable2 }}"
+       order_user = "This is value the value2"
+       expected_result = {'variable': 'value',
+                          'variable2': 'value2'}
+       self.assertEqual(OrderAnalyser._associate_order_params_to_values(order_user, order_brain), expected_result,
+                        "Fail to match the order_brain multi variable to the multi values")
+
+       ##
+       # Testing multi words in variable
+       ##
+
+       # Success
+       order_brain = "This is the {{ variable }}"
+       order_user = "This is the value with multiple words"
+       expected_result = {'variable': 'value with multiple words'}
+       self.assertEqual(OrderAnalyser._associate_order_params_to_values(order_user,order_brain), expected_result,
+                        "Fail to match the order_brain {{ variable }} to the 'value with multiple words'")
+
+       # Success
+       order_brain = "This is the {{ variable }} and  {{ variable2 }}"
+       order_user = "This is the value with multiple words and second value multiple"
+       expected_result = {'variable': 'value with multiple words',
+                          'variable2': 'second value multiple'}
+       self.assertEqual(OrderAnalyser._associate_order_params_to_values(order_user,order_brain), expected_result,
+                        "Fail to match the order_brain multiple variables with multiple words as values'")
+
 if __name__ == '__main__':
     unittest.main()

+ 1 - 1
neurons/openweathermap/__init__.py

@@ -1 +1 @@
-from Openweathermap import Openweathermap
+from openweathermap import Openweathermap

+ 0 - 0
neurons/openweathermap/Openweathermap.py → neurons/openweathermap/openweathermap.py


+ 1 - 1
neurons/push_message/__init__.py

@@ -1,2 +1,2 @@
-from Push_message import Push_message
+from push_message import Push_message
 

+ 0 - 0
neurons/push_message/Push_message.py → neurons/push_message/push_message.py


+ 1 - 1
neurons/twitter/__init__.py

@@ -1 +1 @@
-from Twitter import Twitter
+from twitter import Twitter

+ 0 - 0
neurons/twitter/Twitter.py → neurons/twitter/twitter.py


+ 1 - 1
neurons/wake_on_lan/__init__.py

@@ -1 +1 @@
-from Wake_on_lan import Wake_on_lan
+from wake_on_lan import Wake_on_lan

+ 0 - 0
neurons/wake_on_lan/Wake_on_lan.py → neurons/wake_on_lan/wake_on_lan.py


+ 1 - 1
neurons/wikipedia/__init__.py

@@ -1 +1 @@
-from Wikipedia import Wikipedia
+from wikipedia import Wikipedia

+ 0 - 0
neurons/wikipedia/Wikipedia.py → neurons/wikipedia/wikipedia.py