Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit test part 1 #198

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add files via upload
19916302128 authored Sep 3, 2024
commit eba44ffb458e0c0053f529e41637415666140582
Empty file added 1hunyuan/__init__.py
Empty file.
62 changes: 62 additions & 0 deletions 1hunyuan/test_chat_completions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import json
import os
import types

import pytest

from tencentcloud.common import credential
from tencentcloud.common.exception import TencentCloudSDKException
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.hunyuan.v20230901 import hunyuan_client, models


def test_chat_completions():
cred = credential.Credential(
os.environ.get("TENCENTCLOUD_SECRET_ID"),
os.environ.get("TENCENTCLOUD_SECRET_KEY"))
httpProfile = HttpProfile()
httpProfile.endpoint = "hunyuan.tencentcloudapi.com"

clientProfile = ClientProfile()
clientProfile.httpProfile = httpProfile
client = hunyuan_client.HunyuanClient(cred, "", clientProfile)

req = models.ChatCompletionsRequest()
params = {
"Model": "hunyuan-pro",
"Messages": [
{
"Role": "user",
"Content": "你好啊,早上好"
}
]
}
req.from_json_string(json.dumps(params))

# 返回的resp是一个ChatCompletionsResponse的实例,与请求对象对应
resp = client.ChatCompletions(req)
assert isinstance(resp, types.GeneratorType) is False

req1 = models.ChatCompletionsRequest()
params = {
"Model": "hunyuan-pro",
"Messages": [
{
"Role": "user",
"Content": "你好啊,早上好"
}
],
"Stream": True
}
req1.from_json_string(json.dumps(params))

resp = client.ChatCompletions(req1)
assert isinstance(resp, types.GeneratorType) is True


def test_chat_completions_with_exception():
with pytest.raises(TencentCloudSDKException):
client = hunyuan_client.HunyuanClient(None, "", None)
params = {}
client.ChatCompletions(params)
67 changes: 67 additions & 0 deletions 1hunyuan/test_chat_completions_request.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import warnings

from tencentcloud.hunyuan.v20230901.models import ChatCompletionsRequest


def test_chat_completions_request_init():
request = ChatCompletionsRequest()
request.Model = 'gpt-3.5-turbo-0613'
assert request.Model == 'gpt-3.5-turbo-0613'

request.Messages = [
{
'role': 'user',
'content': 'Hello, world!'
}
]
assert request.Messages[0]['role'] == 'user'
assert request.Messages[0]['content'] == 'Hello, world!'

request.Stream = True
assert request.Stream is True

request.StreamModeration = True
assert request.StreamModeration is True

request.TopP = 0.9
assert request.TopP == 0.9

request.Temperature = 0.9
assert request.Temperature == 0.9

request.EnableEnhancement = True
assert request.EnableEnhancement is True

request.Tools = ['gpt-3.5-turbo-0613', '']
assert request.Tools == ['gpt-3.5-turbo-0613', '']

request.ToolChoice = 'gpt-3.5-turbo-0613'
assert request.ToolChoice == 'gpt-3.5-turbo-0613'

request.CustomTool = ['gpt-3.5-turbo']
assert request.CustomTool == ['gpt-3.5-turbo']


def test_chat_completions_request_deserialize(mocker):
request = ChatCompletionsRequest()
params = {
"Model": "TestModel",
"Messages": [{"message_id": 1, "content": "Test message"}],
"Stream": "TestStream",
"StreamModeration": "TestStreamModeration",
"TopP": "TestTopP",
"Temperature": "TestTemperature",
"EnableEnhancement": "TestEnableEnhancement",
"Tools": [1, 2, 3],
"ToolChoice": "TestToolChoice",
"CustomTool": "111",
"test": "test"
}

with mocker.patch('tencentcloud.hunyuan.v20230901.models.Message._deserialize', return_value=111) as mock_Message, \
mocker.patch('tencentcloud.hunyuan.v20230901.models.Tool._deserialize', return_value=222) as mock_Tool, \
warnings.catch_warnings(record=True) as w:
request._deserialize(params)
assert request._Model == 'TestModel'
assert len(request.Messages) == 1
assert str(w.pop().message) == "test fileds are useless."
47 changes: 47 additions & 0 deletions 1hunyuan/test_chat_completions_response.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from datetime import datetime

from tencentcloud.hunyuan.v20230901.models import ChatCompletionsResponse


def test_chat_completions_response():
response = ChatCompletionsResponse()

response.Created = datetime(year=2020, month=1, day=1)
assert response.Created == datetime(year=2020, month=1, day=1)

response.Usage = 10
assert response.Usage == 10

response.Note = "test"
assert response.Note == "test"

response.Id = 123456
assert response.Id == 123456

response.Choices = 1
assert response.Choices == 1

response.RequestId = 'a-dm338d-s3ms3k4s-sa'
assert response.RequestId == 'a-dm338d-s3ms3k4s-sa'

response.ErrorMsg = "test"
assert response.ErrorMsg == "test"


def test_chat_completions_response_deserialize(mocker):
response = ChatCompletionsResponse()
params = {
"Created": "2020-01-01",
"Usage": 10,
"Note": "test",
"Id": 123456,
"Choices": [1, 2, 3],
"RequestId": "a-dm338d-s3ms3k4s-sa",
"ErrorMsg": "test"
}
with mocker.patch('tencentcloud.hunyuan.v20230901.models.Usage._deserialize', return_value=111) as mock_usage, \
mocker.patch('tencentcloud.hunyuan.v20230901.models.Choice._deserialize', return_value=222) as mock_Choice, \
mocker.patch('tencentcloud.hunyuan.v20230901.models.ErrorMsg._deserialize',
return_value=333) as mock_ErrorMsg:
response._deserialize(params)
assert response._Created == "2020-01-01"
28 changes: 28 additions & 0 deletions 1hunyuan/test_hunyuan_choice.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import warnings
import unittest
from tencentcloud.hunyuan.v20230901.models import Choice


def test_hunyuan_choice():
choice = Choice()

choice.FinishReason = "test"
assert choice.FinishReason == "test"

choice.Delta = "test"
assert choice.Delta == "test"

choice.Message = "test"
assert choice.Message == "test"


def test_hunyuan_choice_deserialize(mocker):
choice = Choice()
params = {"Delta": "111", "Message": "222", "FinishReason": "333", "fake": "444"}
with mocker.patch('tencentcloud.hunyuan.v20230901.models.Message._deserialize', return_value=111) as mock_Message, \
mocker.patch('tencentcloud.hunyuan.v20230901.models.Delta._deserialize', return_value=222) as mock_Delta, \
warnings.catch_warnings(record=True) as captured_warnings:
choice._deserialize(params)
assert choice._FinishReason == "333"
assert len(captured_warnings) == 1
assert 'fake fileds are useless.' in str(captured_warnings[0].message)
30 changes: 30 additions & 0 deletions 1hunyuan/test_hunyuan_content.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import warnings

from tencentcloud.hunyuan.v20230901.models import Content


def test_hunyuan_content():
content = Content()

content.Type = 1
content.Text = "111.text"
content.ImageUrl = "https://hunyuan.com"
assert content.Type == 1
assert content.Text == "111.text"
assert content.ImageUrl == "https://hunyuan.com"


def test_hunyuan_deserialize(mocker):
content = Content()
params = {
"Type": 1,
"Text": "111.text",
"ImageUrl": "https://hunyuan.com",
"test": "test"
}
with mocker.patch('tencentcloud.hunyuan.v20230901.models.ImageUrl._deserialize', return_value=111), \
warnings.catch_warnings(record=True) as captured_warnings:
content._deserialize(params)
assert content._Type == 1
assert content._Text == "111.text"
assert str(captured_warnings.pop().message) == "test fileds are useless."
27 changes: 27 additions & 0 deletions 1hunyuan/test_hunyuan_delta.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import warnings

from tencentcloud.hunyuan.v20230901.models import Delta, Tool


def test_hunyuan_delta():
delta = Delta()

delta.Role = 'normal_user'
assert delta.Role == 'normal_user'

delta.Content = 'hello world'
assert delta.Content == 'hello world'

delta.ToolCalls = "tool test"
assert delta.ToolCalls == "tool test"


def test_hunyuan_delta_deserialize(mocker):
delta = Delta()
params = {'Role': 'normal_user', 'Content': 'hello world', 'ToolCalls': [1, 2, 3], "test": "test"}
with mocker.patch('tencentcloud.hunyuan.v20230901.models.ToolCall._deserialize', return_value=111), \
warnings.catch_warnings(record=True) as captured_warnings:
delta._deserialize(params)
assert delta.Role == 'normal_user'
assert delta.Content == 'hello world'
assert str(captured_warnings.pop().message) == "test fileds are useless."
31 changes: 31 additions & 0 deletions 1hunyuan/test_hunyuan_embedding_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import warnings

from tencentcloud.hunyuan.v20230901.models import EmbeddingData


def test_hunyuan_embedding_data():
embedding = EmbeddingData()
embedding.Embedding = 'test/embedding/embedding'
assert embedding.Embedding == 'test/embedding/embedding'

embedding.Index = 'test/embedding/index'
assert embedding.Index == 'test/embedding/index'

embedding.Object = True
assert embedding.Object is True


def test_hunyuan_embedding_data_deserialize():
embedding = EmbeddingData()
params = {
'Embedding': 'test/embedding/embedding',
'Index': 'test/embedding/index',
'Object': True,
"test": "test"
}
with warnings.catch_warnings(record=True) as w:
embedding._deserialize(params)
assert embedding.Embedding == 'test/embedding/embedding'
assert embedding.Index == 'test/embedding/index'
assert embedding.Object is True
assert str(w.pop().message) == "test fileds are useless."
25 changes: 25 additions & 0 deletions 1hunyuan/test_hunyuan_embedding_usage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import warnings

from tencentcloud.hunyuan.v20230901.models import EmbeddingUsage


def test_hunyuan_embedding_usage():
embedding = EmbeddingUsage()
embedding.PromptTokens = 100
assert embedding.PromptTokens == 100
embedding.TotalTokens = 1000
assert embedding.TotalTokens == 1000


def test_hunyuan_embedding_usage_deserialize():
embedding = EmbeddingUsage()
params = {
"PromptTokens": 100,
"TotalTokens": 1000,
"test": "test"
}
with warnings.catch_warnings(record=True) as w:
embedding._deserialize(params)
assert embedding.PromptTokens == 100
assert embedding.TotalTokens == 1000
assert str(w.pop().message) == "test fileds are useless."
26 changes: 26 additions & 0 deletions 1hunyuan/test_hunyuan_error_msg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import warnings

from tencentcloud.hunyuan.v20230901.models import ErrorMsg


def test_hunyuan_error_msg():
msg = ErrorMsg()
msg.Msg = "Hello World!"
assert msg.Msg == "Hello World!"

msg.Code = "InternalError"
assert msg.Code == "InternalError"


def test_hunyuan_error_msg_deserialize():
msg = ErrorMsg()
params = {
"Msg": "Hello World!",
"Code": "InternalError",
"test": "test"
}
with warnings.catch_warnings(record=True) as w:
msg._deserialize(params)
assert msg.Msg == "Hello World!"
assert msg.Code == "InternalError"
assert str(w.pop().message) == "test fileds are useless."
39 changes: 39 additions & 0 deletions 1hunyuan/test_hunyuan_get_embedding.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import json
import os

import pytest

from tencentcloud.common import credential
from tencentcloud.common.exception import TencentCloudSDKException
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.hunyuan.v20230901 import hunyuan_client, models


def test_hunyuan_get_embedding():
cred = credential.Credential(
os.environ.get("TENCENTCLOUD_SECRET_ID"),
os.environ.get("TENCENTCLOUD_SECRET_KEY"))

httpProfile = HttpProfile()
httpProfile.endpoint = "hunyuan.tencentcloudapi.com"

clientProfile = ClientProfile()
clientProfile.httpProfile = httpProfile
client = hunyuan_client.HunyuanClient(cred, "", clientProfile)

req = models.GetEmbeddingRequest()
params = {
"Input": "你好"
}
req.from_json_string(json.dumps(params))

resp = client.GetEmbedding(req)
assert resp is not None


def test_hunyuan_get_embedding_with_exception():
with pytest.raises(TencentCloudSDKException):
client = hunyuan_client.HunyuanClient(None, "", None)
params = {}
client.GetEmbedding(params)
Loading