Example #1
0
    def test_load_services(self):

        service_config = BrainServiceConfiguration("mock")
        service_config._classname = 'programytest.services.test_service.MockService'

        brain_config = BrainConfiguration()
        brain_config.services._services['mock'] = service_config

        ServiceFactory.preload_services(brain_config.services)

        self.assertIsNotNone(ServiceFactory.get_service("mock"))
        self.assertIsInstance(ServiceFactory.get_service("mock"), MockService)
Example #2
0
    def load_config_section(self, configuration_file, configuration, bot_root):
        services = configuration_file.get_section("services", configuration)
        if services is not None:
            service_keys = configuration_file.get_keys(services)

            for name in service_keys:
                service = BrainServiceConfiguration(name)
                service.load_config_section(configuration_file, services, bot_root)
                self._services[name] = service

        else:
            YLogger.warning(self, "Config section [services] missing from Brain, no services loaded")
Example #3
0
    def test_call_service(self):

        service_config = BrainServiceConfiguration("mock")
        service_config._classname = 'programytest.services.test_service.MockService'

        brain_config = BrainConfiguration()
        brain_config.services._services['mock'] = service_config

        ServiceFactory.preload_services(brain_config.services)

        root = TemplateNode()

        node = TemplateSRAIXNode()
        node.service = "mock"
        root.append(node)
        node.append(TemplateWordNode("Hello"))

        self.assertEqual("asked", node.resolve(self._client_context))
    def test_format_get_url(self):
        client = TestClient()
        client_context = client.create_client_context("testid")

        config = BrainServiceConfiguration("rest")
        config._classname = "programy.testclass"
        config._method = "GET"
        config._host = "localhost"
        config._port = 8080
        config._url = "/api/v1.0/ask"

        service = ProgramyRESTService(config, api=None)
        self.assertEqual(
            "/api/v1.0/ask?question=Hello&userid=testid",
            service._format_get_url("/api/v1.0/ask", client_context, "Hello"))
Example #5
0
    def test_parse_response(self):
        client = TestClient()
        client_context = client.create_client_context("testid")

        config = BrainServiceConfiguration("rest")
        config._classname = "programy.testclass"
        config._method = "GET"
        config._host = "localhost"
        config._port = 8080
        config._url = "/api/v1.0/ask"

        service = ProgramyRESTService(config, api=None)
        self.assertEquals(
            "Hello",
            service._parse_response('[{"response": {"answer": "Hello"}}]'))
Example #6
0
    def test_format_payload(self):
        client = TestClient()
        client_context = client.create_client_context("testid")

        config = BrainServiceConfiguration("rest")
        config._classname = "programy.testclass"
        config._method = "GET"
        config._host = "localhost"
        config._port = 8080
        config._url = "/api/v1.0/ask"

        service = ProgramyRESTService(config, api=None)
        self.assertEquals({
            'question': 'Hello',
            'userid': 'testid'
        }, service._format_payload(client_context, "Hello"))
Example #7
0
    def test_parse_response(self):
        client = TestClient()
        client_context = client.create_client_context("testid")

        config = BrainServiceConfiguration("rest")
        config._classname = "programy.testclass"
        config._method = "GET"
        config._host = "localhost"
        config._port = 8080
        config._url = "/api/v1.0/ask"

        service = ProgramyRESTService(config, api=None)
        self.assertEquals("Hello", service._parse_response('[{"response": {"answer": "Hello"}}]'))
Example #8
0
    def test_format_get_url(self):
        client = TestClient()
        client_context = client.create_client_context("testid")

        config = BrainServiceConfiguration("rest")
        config._classname = "programy.testclass"
        config._method = "GET"
        config._host = "localhost"
        config._port = 8080
        config._url = "/api/v1.0/ask"

        service = ProgramyRESTService(config, api=None)
        self.assertEquals("/api/v1.0/ask?question=Hello&userid=testid", service._format_get_url("/api/v1.0/ask", client_context, "Hello"))
Example #9
0
    def test_ask_question_get(self):
        client = TestClient()
        client.add_license_keys_store()
        self._client_context = client.create_client_context("testid")

        self._client_context.brain._openchatbots._openchatbots['CHATBOT1'] = OpenChatBot("openchat1",
                                                                                         "http://localhost:5959/api/rest/v2.0/ask",
                                                                                         "GET")

        mock_data = {"response": {
                            "text": "Hi there from chatbot1",
                        },
                        "status": {"code": 200, "text": "success"}
                     }
        mock_response = json.dumps(mock_data)

        service = OpenChatRESTService(BrainServiceConfiguration("openchatbot"), api=MockRestAPI(200, mock_response))

        response = service.ask_question(self._client_context, "chatbot1 Hello")
        self.assertIsNotNone(response)
        self.assertEqual("Hi there from chatbot1", response)
Example #10
0
    def test_defaults(self):
        service_config = BrainServiceConfiguration("REST")
        data = {}
        service_config.to_yaml(data, True)

        BrainServiceConfigurationTests.assert_defaults(self, data)
 def test_authorisor(self):
     service = PassThroughAuthorisationService(
         BrainServiceConfiguration("authorisation"))
     self.assertIsNotNone(service)
     self.assertTrue(service.authorise("console", "sysadmin"))
     self.assertTrue(service.authorise("anyone", "sysadmin"))
Example #12
0
                                               question)
        if found:
            value = get_result(searchable_name, "de")
            if value is not None:
                return value
        question = question.title()
        languages = ['de', 'en']
        # loops through german results, then for english results is the former is not found.
        for lang in languages:
            found, searchable_name = query_factory(get_query(question, "de"),
                                                   question)
            if found:
                value = get_result(searchable_name, lang)
                if value is None:
                    found, searchable_name = query_factory(
                        get_query(question, "en"), question)
                    value = get_result(searchable_name, lang)
            else:
                found, searchable_name = query_factory(
                    get_query(question, "en"), question)
                value = get_result(searchable_name, lang)
            if value is not None:
                return value if (
                    lang == "de"
                ) else "Die Seite is leider nicht auf Deutsch verfügbar\n" + value
        return "No entry found try again"


a = MyService(BrainServiceConfiguration(""))
print(a.ask_question("", "Stephen Curry"))