예제 #1
0
    def test_process_debug_request_not_server_mode(self):
        home_dir = os.path.dirname(__file__) + os.sep + "testdata"
        tmp_dir = home_dir + os.sep + "tmp"
        if os.path.exists(tmp_dir):
            shutil.rmtree(tmp_dir)
        self.assertFalse(os.path.exists(tmp_dir))

        config_file = home_dir + os.sep + "config.yaml"
        arguments = MockArgumentParser(config=config_file)
        client = SanicYadlanClient("testrest", arguments)
        client._server_mode = False
        self.assertIsNotNone(client)

        request = unittest.mock.Mock()
        request.body = b'{"userId": "1234567890", "utterance": "Hello"}'
        client.process_request(request)

        request = unittest.mock.Mock()
        request.body = b'{"userId": "1234567890", "variables": [{"type": "name", "key": "testname", "value": "value1"}]}'

        debugInfo, status = client.process_debug_request(request)
        self.assertEqual(3, len(debugInfo))
        self.assertEqual(200, status)

        if os.path.exists(tmp_dir):
            shutil.rmtree(tmp_dir)
        self.assertFalse(os.path.exists(tmp_dir))
예제 #2
0
    def test_process_debug_request_invalid_variables_parameter(self):
        arguments = MockArgumentParser()
        client = SanicYadlanClient("yadlan", arguments)
        self.assertIsNotNone(client)

        request = unittest.mock.Mock()
        request.body = b'{"userId": "1234567890", "variables": [{"var": "test", "value": "value1"}]}'

        debugInfo, status = client.process_debug_request(request)
        self.assertEqual('Invalid variables list format', debugInfo['error'])
        self.assertEqual(400, status)
예제 #3
0
    def test_process_debug_request_no_conversation(self):
        arguments = MockArgumentParser()
        client = SanicYadlanClient("yadlan", arguments)
        self.assertIsNotNone(client)

        request = unittest.mock.Mock()
        request.body = b'{"userId": "1234567890", "variables": [{"type": "name", "key": "testname", "value": "value1"}]}'

        debugInfo, status = client.process_debug_request(request)
        self.assertEqual(0, len(debugInfo))
        self.assertEqual(200, status)
예제 #4
0
    def test_process_debug_request_with_variables(self):
        home_dir = os.path.dirname(__file__) + os.sep + "testdata"
        tmp_dir = home_dir + os.sep + "tmp"
        errors_file = tmp_dir + os.sep + "errors.txt"
        duplicates_file = tmp_dir + os.sep + "duplicates.txt"
        conversation_file = tmp_dir + os.sep + "testrest_testUser.conv"
        logs_file = tmp_dir + os.sep + "testrest_testUser.log"
        if os.path.exists(tmp_dir):
            shutil.rmtree(tmp_dir)
        self.assertFalse(os.path.exists(tmp_dir))

        config_file = home_dir + os.sep + "config.yaml"
        arguments = MockArgumentParser(config=config_file)
        client = SanicYadlanClient("testrest", arguments)
        self.assertIsNotNone(client)

        request = unittest.mock.Mock()
        request.body = b'{"userId": "testUser", "utterance": "VARIABLES"}'

        response, _ = client.process_request(request)
        self.assertIsNotNone(response)
        self.assertEqual("testUser", response['userId'])
        self.assertEqual("VARIABLES.", response['utterance'])
        self.assertEqual("Variable datas.", response['response'])

        self.assertTrue(os.path.exists(errors_file))
        self.assertTrue(os.path.exists(duplicates_file))
        self.assertTrue(os.path.exists(conversation_file))
        self.assertTrue(os.path.exists(logs_file))

        request = unittest.mock.Mock()
        request.body = b'{"userId": "testUser"}'

        debug_info, _ = client.process_debug_request(request)
        self.assertTrue('errors' in debug_info)
        self.assertTrue('duplicates' in debug_info)
        self.assertTrue('conversations' in debug_info)
        self.assertTrue('logs' in debug_info)
        self.assertTrue('current_conversation' in debug_info)

        self.assertEqual("name_value",
                         debug_info['conversations']['properties']['name_key'])
        self.assertEqual(
            "data_value",
            debug_info['conversations']['data_properties']['data_key'])
        question = debug_info['conversations']['questions'][0]
        self.assertEqual("var_value", question['var_properties']['var_key'])

        if os.path.exists(tmp_dir):
            shutil.rmtree(tmp_dir)
        self.assertFalse(os.path.exists(tmp_dir))
예제 #5
0
    def test_process_debug_request_no_userid(self):
        home_dir = os.path.dirname(__file__) + os.sep + "testdata"
        tmp_dir = home_dir + os.sep + "tmp"
        errors_file = tmp_dir + os.sep + "errors.txt"
        duplicates_file = tmp_dir + os.sep + "duplicates.txt"
        conversation_file = tmp_dir + os.sep + "testrest_testUser.conv"
        logs_file = tmp_dir + os.sep + "testrest_testUser.log"
        if os.path.exists(tmp_dir):
            shutil.rmtree(tmp_dir)
        self.assertFalse(os.path.exists(tmp_dir))

        config_file = home_dir + os.sep + "config.yaml"
        arguments = MockArgumentParser(config=config_file)
        client = SanicYadlanClient("testrest", arguments)
        self.assertIsNotNone(client)

        request = unittest.mock.Mock()
        request.body = b'{"userId": "testUser", "utterance": "Hello"}'

        response, _ = client.process_request(request)
        self.assertIsNotNone(response)
        self.assertEqual("testUser", response['userId'])
        self.assertEqual("Hello.", response['utterance'])
        self.assertEqual("HELLO, WORLD.", response['response'])

        self.assertTrue(os.path.exists(errors_file))
        self.assertTrue(os.path.exists(duplicates_file))
        self.assertTrue(os.path.exists(conversation_file))
        self.assertTrue(os.path.exists(logs_file))

        request = unittest.mock.Mock()
        request.body = b'{}'

        debug_info, _ = client.process_debug_request(request)
        self.assertTrue('errors' in debug_info)
        self.assertTrue('duplicates' in debug_info)
        self.assertFalse('conversations' in debug_info)
        self.assertFalse('logs' in debug_info)
        self.assertFalse('current_conversation' in debug_info)

        if os.path.exists(tmp_dir):
            shutil.rmtree(tmp_dir)
        self.assertFalse(os.path.exists(tmp_dir))