浏览代码

add test for rest api. not working with discover mode

nico 8 年之前
父节点
当前提交
2e88df9860
共有 2 个文件被更改,包括 23 次插入40 次删除
  1. 23 38
      Tests/test_rest_api.py
  2. 0 2
      kalliope/core/OrderAnalyser.py

+ 23 - 38
Tests/test_rest_api.py

@@ -3,8 +3,9 @@ import os
 import unittest
 
 import requests
-import time
 from flask import Flask
+from flask_testing import LiveServerTestCase
+
 from kalliope.core.Models import Singleton
 
 from kalliope.core.ConfigurationManager import BrainLoader
@@ -12,56 +13,39 @@ from kalliope.core.ConfigurationManager import SettingLoader
 from kalliope.core.RestAPI.FlaskAPI import FlaskAPI
 
 
-class TestRestAPI(unittest.TestCase):
+class TestRestAPI(LiveServerTestCase):
 
-    @classmethod
-    def setUpClass(cls):
+    def create_app(self):
         """
         executed once at the beginning of the test
         """
-        super(TestRestAPI, cls).setUpClass()
+        # be sure that the singleton haven't been loaded before
+        Singleton._instances = {}
         current_path = os.getcwd()
         full_path_brain_to_test = current_path + os.sep + "Tests/brains/brain_test.yml"
         print full_path_brain_to_test
+
         # rest api config
         sl = SettingLoader()
         sl.settings.rest_api.password_protected = False
         sl.settings.active = True
         sl.settings.port = 5000
 
-        print sl.settings.rest_api.password_protected
-
-        # be sure that the singleton haven't been loaded before
-        Singleton._instances = {}
         # prepare a test brain
         brain_to_test = full_path_brain_to_test
         brain_loader = BrainLoader(file_path=brain_to_test)
         brain = brain_loader.brain
 
-        print brain_loader.yaml_config
-
-        app = Flask(__name__)
-        cls.flask_api = FlaskAPI(app, port=5000, brain=brain)
-        cls.flask_api.start()
-        time.sleep(1)
+        self.app = Flask(__name__)
+        self.flask_api = FlaskAPI(self.app, port=5000, brain=brain)
+        self.flask_api.app.config['TESTING'] = True
+        return self.flask_api.app
 
-    # @classmethod
-    # def tearDownClass(cls):
-    #     """
-    #     executed once at the end of the test
-    #     """
-    #     url = "http://127.0.0.1:5000/shutdown/"
-    #     requests.post(url=url)
-
-    def setUp(self):
-        self.base_url = "http://127.0.0.1:5000"
-
-    # def test_get_synapses(self):
-    #     url = self.base_url+"/synapses/"
-    #     result = requests.get(url=url)
-    #
-    #     print result.content
+    # TODO all following test passes with 'python -m unittest Tests.TestRestAPI' but not with discover
+    # def test_get_all_synapses(self):
+    #     url = "http://127.0.0.1:5000/synapses"
     #
+    #     result = requests.get(url=url)
     #     expected_content = {
     #         "synapses": [
     #             {
@@ -123,10 +107,11 @@ class TestRestAPI(unittest.TestCase):
     #         ]
     #     }
     #
+    #     self.assertEqual(result.status_code, 200)
     #     self.assertEqual(expected_content, json.loads(result.content))
-
+    #
     # def test_get_one_synapse(self):
-    #     url = self.base_url+"/synapses/test"
+    #     url = "http://127.0.0.1:5000/synapses/test"
     #     result = requests.get(url=url)
     #
     #     expected_content = {
@@ -152,7 +137,7 @@ class TestRestAPI(unittest.TestCase):
     #     self.assertEqual(expected_content, json.loads(result.content))
     #
     # def test_get_synapse_not_found(self):
-    #     url = self.base_url + "/synapses/test-none"
+    #     url = "http://127.0.0.1:5000/synapses/test-none"
     #     result = requests.get(url=url)
     #
     #     expected_content = {
@@ -165,7 +150,7 @@ class TestRestAPI(unittest.TestCase):
     #     self.assertEqual(result.status_code, 404)
     #
     # def test_run_synapse_by_name(self):
-    #     url = self.base_url + "/synapses/test"
+    #     url = "http://127.0.0.1:5000/synapses/test"
     #     result = requests.post(url=url)
     #
     #     expected_content = {
@@ -192,7 +177,7 @@ class TestRestAPI(unittest.TestCase):
     #     self.assertEqual(result.status_code, 201)
     #
     # def test_post_synapse_not_found(self):
-    #     url = self.base_url + "/synapses/test-none"
+    #     url = "http://127.0.0.1:5000/synapses/test-none"
     #     result = requests.post(url=url)
     #
     #     expected_content = {
@@ -205,7 +190,7 @@ class TestRestAPI(unittest.TestCase):
     #     self.assertEqual(result.status_code, 404)
     #
     # def test_run_synapse_with_order(self):
-    #     url = self.base_url + "/order/"
+    #     url = "http://127.0.0.1:5000/order/"
     #     headers = {"Content-Type": "application/json"}
     #     data = {"order": "test_order"}
     #     result = requests.post(url=url, headers=headers, json=data)
@@ -233,7 +218,7 @@ class TestRestAPI(unittest.TestCase):
     #     self.assertEqual(result.status_code, 201)
     #
     # def test_post_synapse_by_order_not_found(self):
-    #     url = self.base_url + "/order/"
+    #     url = "http://127.0.0.1:5000/order/"
     #     data = {"order": "non existing order"}
     #     headers = {"Content-Type": "application/json"}
     #     result = requests.post(url=url, headers=headers, json=data)

+ 0 - 2
kalliope/core/OrderAnalyser.py

@@ -21,7 +21,6 @@ class OrderAnalyser:
         """
         Class used to load brain and run neuron attached to the received order
         :param order: spelt order
-        :param main_controller
         :param brain: loaded brain
         """
         sl = SettingLoader()
@@ -265,4 +264,3 @@ class OrderAnalyser:
             logger.debug("Default synapse not found")
             Utils.print_warning("Default synapse not found")
         return default_synapse
-