-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add test --------- Co-authored-by: yinjiaqi <yinjiaqi@baidu.com>
- Loading branch information
Showing
30 changed files
with
407 additions
and
244 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
appbuilder/tests/test_appbuilder_core_components_retriever.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# Copyright (c) 2023 Baidu, Inc. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
import os | ||
import unittest | ||
import subprocess | ||
import sys | ||
import time | ||
|
||
from appbuilder.core.components.retriever.baidu_vdb.baiduvdb_retriever import _try_import,BaiduVDBVectorStoreIndex | ||
|
||
|
||
@unittest.skipUnless(os.getenv("TEST_CASE", "UNKNOWN") == "CPU_SERIAL", "") | ||
class TestAppbuilderCoreComponentsRetriever__try_import(unittest.TestCase): | ||
def test_baidu_vdb_baiduvdb_retriever_try_import(self): | ||
subprocess.check_call([sys.executable, "-m", "pip", "uninstall", "-y", "pymochow"]) | ||
with self.assertRaises(ImportError): | ||
_try_import() | ||
subprocess.check_call([sys.executable, "-m", "pip", "install", "pymochow"]) | ||
_try_import() | ||
|
||
|
||
def test_baidu_vdb_baiduvdb_retriever_BaiduVDBVectorStoreIndex(self): | ||
# test not isinstance(instance_id, str): | ||
with self.assertRaises(TypeError): | ||
BaiduVDBVectorStoreIndex( | ||
instance_id = 1, | ||
api_key="key", | ||
) | ||
# test not isinstance(api_key, str): | ||
with self.assertRaises(TypeError): | ||
BaiduVDBVectorStoreIndex( | ||
instance_id = "test", | ||
api_key=1, | ||
) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Copyright (c) 2023 Baidu, Inc. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
import os | ||
import unittest | ||
|
||
from appbuilder.core.console.base import ConsoleLLMMessage,ConsoleCompletionResponse | ||
from appbuilder.core._exception import AppBuilderServerException | ||
from appbuilder.core.components.llms.base import CompletionResponse, LLMMessage | ||
from appbuilder.core.message import Message | ||
|
||
|
||
# @unittest.skipUnless(os.getenv("TEST_CASE", "UNKNOWN") == "CPU_PARALLEL", "") | ||
@unittest.skip(reason="暂时跳过") | ||
class TestCoreConsoleBase(unittest.TestCase): | ||
def test_ConsoleLLMMessage_init(self): | ||
message=Message() | ||
llmMessage=LLMMessage(message) | ||
clm=ConsoleLLMMessage(llmMessage) | ||
self.assertIsInstance(clm,ConsoleLLMMessage) | ||
|
||
def test_ConsoleCompletionResponse_init(self): | ||
# 测试stream=True | ||
cr=CompletionResponse() | ||
ccr=ConsoleCompletionResponse(response=cr,stream=True) | ||
|
||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
# Copyright (c) 2023 Baidu, Inc. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
import unittest | ||
import os | ||
import uuid | ||
|
||
from appbuilder.core.user_session import UserSession,SessionMessage | ||
from appbuilder.core.context import init_context,_LOCAL_KEY,get_context,context_var | ||
from appbuilder.core.message import Message | ||
|
||
|
||
@unittest.skipUnless(os.getenv("TEST_CASE", "UNKNOWN") == "CPU_PARALLEL", "") | ||
class TestCoreUserSession(unittest.TestCase): | ||
def test_usersession_init(self): | ||
UserSession._instance = None | ||
UserSession._initialized = False | ||
with self.assertRaises(ValueError): | ||
user_session=UserSession(user_session_config=1234) | ||
UserSession._instance = None | ||
UserSession._initialized = False | ||
|
||
def test_get_context(self): | ||
context_var=0 | ||
ctx=get_context() | ||
|
||
# 测试not ctx.session_id.startswith(_LOCAL_KEY) | ||
def test_not_startswith_LOCAL_KEY(self): | ||
user_session = UserSession() | ||
init_context( | ||
session_id=str(uuid.uuid4()), | ||
request_id=str(uuid.uuid4()) | ||
) | ||
key = "test_key" | ||
message_dict = {key: Message(content={"key": "value"})} | ||
# if key not in ctx.session_vars_dict | ||
user_session.append(message_dict) | ||
history = user_session.get_history(key) | ||
self.assertEqual(len(history), 0) | ||
# if not isinstance(message, Message) | ||
with self.assertRaises(ValueError): | ||
user_session.append({key: "test"}) | ||
# 测试if key in ctx.session_vars_dict | ||
with self.assertRaises(KeyError): | ||
user_session.append(message_dict) | ||
# 测试_post_append(self)的try | ||
user_session._post_append() | ||
|
||
|
||
# 测试ctx.session_id.startswith(_LOCAL_KEY) | ||
def test_startswith_LOCAL_KEY(self): | ||
user_session = UserSession() | ||
init_context( | ||
session_id=_LOCAL_KEY+str(uuid.uuid4()), | ||
request_id=_LOCAL_KEY+str(uuid.uuid4()) | ||
) | ||
key = "test_key" | ||
message_dict = {key: Message(content={"key": "value"})} | ||
# if key not in ctx.session_vars_dict | ||
user_session.append(message_dict) | ||
history = user_session.get_history("test") | ||
self.assertEqual(len(history), 0) | ||
history = user_session.get_history(key) | ||
self.assertEqual(len(history), 1) | ||
# if not isinstance(message, Message) | ||
with self.assertRaises(ValueError): | ||
user_session.append({key: "test"}) | ||
# 测试_post_append(self)的异常 | ||
with self.assertRaises(Exception): | ||
user_session._post_append() | ||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# Copyright (c) 2023 Baidu, Inc. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
import unittest | ||
import os | ||
import base64 | ||
|
||
import appbuilder.core.utils as ut | ||
from appbuilder.utils.sse_util import SSEClient,Event | ||
|
||
|
||
|
||
@unittest.skipUnless(os.getenv("TEST_CASE", "UNKNOWN") == "CPU_PARALLEL", "") | ||
class TestCoreUtils(unittest.TestCase): | ||
def test_utils_get_user_agent(self): | ||
user_agent=ut.utils_get_user_agent() | ||
|
||
def test_get_model_list(self): | ||
from appbuilder.core._exception import TypeNotSupportedException | ||
with self.assertRaises(TypeNotSupportedException): | ||
model_list=ut.get_model_list(api_type_filter=["unknown"]) | ||
|
||
def test_convert_cloudhub_url(self): | ||
try: | ||
from appbuilder.core._client import HTTPClient | ||
except: | ||
pass | ||
with self.assertRaises(ValueError): | ||
url=ut.convert_cloudhub_url(client=HTTPClient,qianfan_url="unknown") | ||
|
||
def test_is_url(self): | ||
is_url=ut.is_url("unknown") | ||
|
||
# def test_utils_sse_uti_events(self): | ||
# binary_data = os.urandom(100) # 生成100字节的随机数据 | ||
# encoded_data = base64.b64encode(binary_data).decode('utf-8') | ||
# sseClient=SSEClient(event_source=encoded_data) | ||
# sseClient.events() | ||
|
||
# def test_utils_sse_uti_events(self): | ||
# binary_data = os.urandom(100) # 生成100字节的随机数据 | ||
# encoded_data = base64.b64encode(binary_data).decode('utf-8') | ||
# sseClient=SSEClient(event_source=encoded_data) | ||
# sseClient.close() | ||
|
||
def test_utils_sse_uti_Events(self): | ||
event_null_data=Event(id="id") | ||
s=event_null_data.debug_str | ||
event=Event(id="id",data='data',retry="retry") | ||
s=event.debug_str | ||
event.raw="raw" | ||
s_change_raw=event.debug_str | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.