File size: 4,713 Bytes
f6fdf6a 6d3bf74 f6fdf6a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
import pytest
from unittest.mock import patch, AsyncMock
from app.models.openai import (
Message, ChatCompletionRequest, ChoiceMessage,
Choice, Usage, ChatCompletionResponse
)
def test_message_model():
"""Test Message Pydantic model."""
message = Message(role="user", content="Hello")
assert message.role == "user"
assert message.content == "Hello"
def test_message_invalid_role():
"""Test Message with invalid role."""
with pytest.raises(ValueError):
Message(role="invalid", content="Hello")
def test_chat_completion_request_model():
"""Test ChatCompletionRequest Pydantic model."""
messages = [
Message(role="system", content="You are a helpful assistant"),
Message(role="user", content="Hello")
]
request = ChatCompletionRequest(
model="test-model",
messages=messages,
temperature=0.7,
max_tokens=100,
stream=False
)
assert request.model == "test-model"
assert len(request.messages) == 2
assert request.temperature == 0.7
assert request.max_tokens == 100
assert request.stream is False
def test_chat_completion_request_defaults():
"""Test ChatCompletionRequest with default values."""
messages = [Message(role="user", content="Hello")]
request = ChatCompletionRequest(
model="test-model",
messages=messages
)
assert request.model == "test-model"
assert request.temperature == 0.7 # Default temperature
assert request.max_tokens is None
assert request.stream is False
def test_choice_message_model():
"""Test ChoiceMessage Pydantic model."""
message = ChoiceMessage(role="assistant", content="Hi there!")
assert message.role == "assistant"
assert message.content == "Hi there!"
def test_choice_message_optional_content():
"""Test ChoiceMessage with optional content."""
message = ChoiceMessage(role="assistant")
assert message.role == "assistant"
assert message.content is None
def test_choice_model():
"""Test Choice Pydantic model."""
message = ChoiceMessage(role="assistant", content="Response")
choice = Choice(
index=0,
message=message,
finish_reason="stop"
)
assert choice.index == 0
assert choice.message == message
assert choice.finish_reason == "stop"
def test_choice_optional_finish_reason():
"""Test Choice with optional finish_reason."""
message = ChoiceMessage(role="assistant", content="Response")
choice = Choice(index=0, message=message)
assert choice.index == 0
assert choice.message == message
assert choice.finish_reason is None
def test_usage_model():
"""Test Usage Pydantic model."""
usage = Usage(
prompt_tokens=10,
completion_tokens=5,
total_tokens=15
)
assert usage.prompt_tokens == 10
assert usage.completion_tokens == 5
assert usage.total_tokens == 15
def test_chat_completion_response_model():
"""Test ChatCompletionResponse Pydantic model."""
message = ChoiceMessage(role="assistant", content="Response")
choice = Choice(index=0, message=message, finish_reason="stop")
usage = Usage(prompt_tokens=10, completion_tokens=5, total_tokens=15)
response = ChatCompletionResponse(
id="cmpl-123",
created=1234567890,
model="test-model",
choices=[choice],
usage=usage
)
assert response.id == "cmpl-123"
assert response.object == "chat.completion"
assert response.created == 1234567890
assert response.model == "test-model"
assert len(response.choices) == 1
assert response.usage == usage
def test_chat_completion_response_optional_usage():
"""Test ChatCompletionResponse with optional usage."""
message = ChoiceMessage(role="assistant", content="Response")
choice = Choice(index=0, message=message, finish_reason="stop")
response = ChatCompletionResponse(
id="cmpl-123",
created=1234567890,
model="test-model",
choices=[choice]
)
assert response.id == "cmpl-123"
assert response.usage is None
def test_model_serialization():
"""Test model serialization to dict."""
messages = [Message(role="user", content="Hello")]
request = ChatCompletionRequest(
model="test-model",
messages=messages,
temperature=0.5
)
data = request.model_dump()
assert data["model"] == "test-model"
assert len(data["messages"]) == 1
assert data["messages"][0]["role"] == "user"
assert data["messages"][0]["content"] == "Hello"
assert data["temperature"] == 0.5
|