Przeglądaj źródła

Merge pull request #60 from kalliope-project/tests

Tests
Nicolas Marcq 8 lat temu
rodzic
commit
d887ee3f80

+ 15 - 1
brains/openweathermap.yml

@@ -1,5 +1,19 @@
 ---
   - name: "get-the-weather"
+    signals:
+      - order: "quel temps fait-il {{ location }}"
+    neurons:
+      - openweathermap:
+          api_key: "your-api"
+          lang: "fr"
+          temp_unit: "celsius"
+          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"
+
+  - name: "get-the-weather-2"
     signals:
       - order: "quel temps fait-il"
     neurons:
@@ -7,7 +21,7 @@
           api_key: "your-api"
           lang: "fr"
           temp_unit: "celsius"
-          location : "grenoble"
           country: "FR"
+          location: "grenoble"          
           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"

+ 14 - 9
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
@@ -150,7 +154,8 @@ class OrderAnalyser:
         next(ite, None)
         return next(ite, None)
 
-    def _spelt_order_match_brain_order_via_table(self, order_to_analyse, user_said):
+    @classmethod
+    def _spelt_order_match_brain_order_via_table(cls, order_to_analyse, user_said):
         """
         return true if all string that are in the sentence are present in the order to test
         :param order_to_analyse: String order to test
@@ -158,10 +163,10 @@ class OrderAnalyser:
         :return: True if all string are present in the order
         """
         list_word_user_said = user_said.split()
-        split_order_without_bracket = self._get_split_order_without_bracket(order_to_analyse)
+        split_order_without_bracket = cls._get_split_order_without_bracket(order_to_analyse)
 
         # if all words in the list of what the user said in in the list of word in the order
-        return self._counter_subset(split_order_without_bracket, list_word_user_said)
+        return cls._counter_subset(split_order_without_bracket, list_word_user_said)
 
     @staticmethod
     def _get_split_order_without_bracket(order):

+ 1 - 0
core/Tests/__init__.py

@@ -0,0 +1 @@
+from test_order_analyser import TestOrderAnalyser

+ 195 - 0
core/Tests/test_order_analyser.py

@@ -0,0 +1,195 @@
+import unittest
+from mock import patch, Mock, MagicMock
+
+
+from core.OrderAnalyser import OrderAnalyser
+
+
+class TestOrderAnalyser(unittest.TestCase):
+
+    """Test case for the OrderAnalyser Class"""
+
+    def test_is_containing_bracket(self):
+        #  Success
+        order_to_test = "This test contains {{ bracket }}"
+        self.assertTrue(OrderAnalyser._is_containing_bracket(order_to_test),
+                        "Fail returning True when order contains spaced brackets")
+
+        order_to_test = "This test contains {{bracket }}"
+        self.assertTrue(OrderAnalyser._is_containing_bracket(order_to_test),
+                        "Fail returning True when order contains right spaced bracket")
+
+        order_to_test = "This test contains {{ bracket}}"
+        self.assertTrue(OrderAnalyser._is_containing_bracket(order_to_test),
+                        "Fail returning True when order contains left spaced bracket")
+
+        order_to_test = "This test contains {{bracket}}"
+        self.assertTrue(OrderAnalyser._is_containing_bracket(order_to_test),
+                        "Fail returning True when order contains no spaced bracket")
+
+        #  Failure
+        order_to_test = "This test does not contain bracket"
+        self.assertFalse(OrderAnalyser._is_containing_bracket(order_to_test),
+                        "Fail returning False when order has no brackets")
+
+        #  Behaviour
+        order_to_test = ""
+        self.assertFalse(OrderAnalyser._is_containing_bracket(order_to_test),
+                        "Fail returning False when no order")
+
+    def test_get_next_value_list(self):
+        # Success
+        list_to_test = {1, 2, 3}
+        self.assertEqual(OrderAnalyser._get_next_value_list(list_to_test),2,
+                         "Fail to match the expected next value from the list")
+
+        # Failure
+        list_to_test = {1}
+        self.assertEqual(OrderAnalyser._get_next_value_list(list_to_test), None,
+                         "Fail to ensure there is no next value from the list")
+
+        # Behaviour
+        list_to_test = {}
+        self.assertEqual(OrderAnalyser._get_next_value_list(list_to_test), None,
+                         "Fail to ensure the empty list return None value")
+
+    def test_spelt_order_match_brain_order_via_table(self):
+        order_to_test = "this is the order"
+        sentence_to_test = "this is the order"
+
+        # Success
+        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),
+                         "Fail to ensure the expected sentence is not matching the order")
+
+    def test_get_split_order_without_bracket(self):
+
+        # Success
+        order_to_test = "this is the order"
+        expected_result = ["this", "is", "the", "order"]
+        self.assertEqual(OrderAnalyser._get_split_order_without_bracket(order_to_test),expected_result,
+                         "No brackets Fails to return the expected list")
+
+        order_to_test = "this is the {{ order }}"
+        expected_result = ["this", "is", "the"]
+        self.assertEqual(OrderAnalyser._get_split_order_without_bracket(order_to_test), expected_result,
+                         "With spaced brackets Fails to return the expected list")
+
+        order_to_test = "this is the {{order }}" # left bracket without space
+        expected_result = ["this", "is", "the"]
+        self.assertEqual(OrderAnalyser._get_split_order_without_bracket(order_to_test), expected_result,
+                         "Left brackets Fails to return the expected list")
+
+        order_to_test = "this is the {{ order}}" # right bracket without space
+        expected_result = ["this", "is", "the"]
+        self.assertEqual(OrderAnalyser._get_split_order_without_bracket(order_to_test), expected_result,
+                         "Right brackets Fails to return the expected list")
+
+        order_to_test = "this is the {{order}}"  # bracket without space
+        expected_result = ["this", "is", "the"]
+        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