File: test_unit_models.py

package info (click to toggle)
python-azure 20251014%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 766,472 kB
  • sloc: python: 6,314,744; ansic: 804; javascript: 287; makefile: 198; sh: 198; xml: 109
file content (293 lines) | stat: -rw-r--r-- 10,982 bytes parent folder | download
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# coding=utf-8
# --------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
# --------------------------------------------------------------------------

from azure.ai.voicelive.models import (
    AssistantMessageItem,
    AzureCustomVoice,
    AzurePersonalVoice,
    AzureStandardVoice,
    AzureVoiceType,
    InputAudioContentPart,
    InputTextContentPart,
    MessageContentPart,
    MessageItem,
    MessageRole,
    OpenAIVoice,
    OutputTextContentPart,
    PersonalVoiceModels,
    RequestSession,
    ResponseSession,
    SystemMessageItem,
    UserMessageItem,
    ItemParamStatus,
    OpenAIVoiceName,
)


class TestAzureVoiceModels:
    """Test Azure voice model classes."""

    def test_azure_custom_voice(self):
        """Test AzureCustomVoice model."""
        voice = AzureCustomVoice(name="custom-voice", endpoint_id="endpoint-123")

        assert voice.type == AzureVoiceType.AZURE_CUSTOM
        assert voice.name == "custom-voice"
        assert voice.endpoint_id == "endpoint-123"

    def test_azure_standard_voice(self):
        """Test AzureStandardVoice model."""
        voice = AzureStandardVoice(name="standard-voice")

        assert voice.type == AzureVoiceType.AZURE_STANDARD
        assert voice.name == "standard-voice"
        assert voice.temperature is None

    def test_azure_standard_voice_with_params(self):
        """Test AzureStandardVoice with optional parameters."""
        voice = AzureStandardVoice(
            name="standard-voice", temperature=0.7, style="friendly", pitch="+10%", rate="+20%", volume="-5%"
        )

        assert voice.temperature == 0.7
        assert voice.style == "friendly"
        assert voice.pitch == "+10%"
        assert voice.rate == "+20%"
        assert voice.volume == "-5%"

    def test_azure_personal_voice(self):
        """Test AzurePersonalVoice model."""
        voice = AzurePersonalVoice(name="personal-voice", model=PersonalVoiceModels.PHOENIX_LATEST_NEURAL)

        assert voice.type == AzureVoiceType.AZURE_PERSONAL
        assert voice.name == "personal-voice"
        assert voice.model == PersonalVoiceModels.PHOENIX_LATEST_NEURAL

    def test_azure_personal_voice_with_temperature(self):
        """Test AzurePersonalVoice with temperature parameter."""
        voice = AzurePersonalVoice(
            name="personal-voice", temperature=0.5, model=PersonalVoiceModels.DRAGON_LATEST_NEURAL
        )

        assert voice.temperature == 0.5
        assert voice.model == PersonalVoiceModels.DRAGON_LATEST_NEURAL


class TestOpenAIVoice:
    """Test OpenAIVoice model."""

    def test_openai_voice_creation(self):
        """Test creating OpenAI voice model."""
        voice = OpenAIVoice(name=OpenAIVoiceName.ALLOY)

        assert voice.type == "openai"
        assert voice.name == OpenAIVoiceName.ALLOY

    def test_openai_voice_with_string(self):
        """Test creating OpenAI voice with string name."""
        voice = OpenAIVoice(name="shimmer")

        assert voice.type == "openai"
        assert voice.name == "shimmer"


class TestMessageContentParts:
    """Test message content part models."""

    def test_input_text_content_part(self):
        """Test InputTextContentPart model."""
        content = InputTextContentPart(text="Hello, world!")

        assert content.type == "input_text"
        assert content.text == "Hello, world!"

    def test_input_audio_content_part(self):
        """Test InputAudioContentPart model."""
        audio_data = b"fake audio data"
        content = InputAudioContentPart(audio=audio_data)

        assert content.type == "input_audio"
        # Audio data gets base64 encoded
        import base64

        expected_audio = base64.b64encode(audio_data).decode("utf-8")
        assert content.audio == expected_audio

    def test_output_text_content_part(self):
        """Test OutputTextContentPart model."""
        content = OutputTextContentPart(text="Response text")

        assert content.type == "text"
        assert content.text == "Response text"

    def test_message_content_part_inheritance(self):
        """Test that content parts inherit from MessageContentPart."""
        text_content = InputTextContentPart(text="test")
        audio_content = InputAudioContentPart(audio=b"test")
        output_content = OutputTextContentPart(text="test")

        assert isinstance(text_content, MessageContentPart)
        assert isinstance(audio_content, MessageContentPart)
        assert isinstance(output_content, MessageContentPart)


class TestMessageItems:
    """Test message item models."""

    def test_user_message_item(self):
        """Test UserMessageItem model."""
        content = [InputTextContentPart(text="Hello")]
        message = UserMessageItem(content=content)

        assert message.role == MessageRole.USER
        assert message.type == "message"
        assert len(message.content) == 1
        assert message.content[0].text == "Hello"

    def test_user_message_item_with_id(self):
        """Test UserMessageItem with optional ID."""
        content = [InputTextContentPart(text="Hello")]
        message = UserMessageItem(id="msg-123", content=content, status=ItemParamStatus.COMPLETED)

        assert message.id == "msg-123"
        assert message.status == ItemParamStatus.COMPLETED

    def test_assistant_message_item(self):
        """Test AssistantMessageItem model."""
        content = [OutputTextContentPart(text="Hi there!")]
        message = AssistantMessageItem(content=content)

        assert message.role == MessageRole.ASSISTANT
        assert message.type == "message"
        assert len(message.content) == 1
        assert message.content[0].text == "Hi there!"

    def test_system_message_item(self):
        """Test SystemMessageItem model."""
        content = [InputTextContentPart(text="You are a helpful assistant.")]
        message = SystemMessageItem(content=content)

        assert message.role == MessageRole.SYSTEM
        assert message.type == "message"
        assert len(message.content) == 1
        assert message.content[0].text == "You are a helpful assistant."

    def test_message_item_polymorphism(self):
        """Test that all message items are instances of MessageItem."""
        user_content = [InputTextContentPart(text="User message")]
        assistant_content = [OutputTextContentPart(text="Assistant response")]
        system_content = [InputTextContentPart(text="System prompt")]

        user_msg = UserMessageItem(content=user_content)
        assistant_msg = AssistantMessageItem(content=assistant_content)
        system_msg = SystemMessageItem(content=system_content)

        assert isinstance(user_msg, MessageItem)
        assert isinstance(assistant_msg, MessageItem)
        assert isinstance(system_msg, MessageItem)

    def test_mixed_content_types(self):
        """Test message with mixed content types."""
        mixed_content = [InputTextContentPart(text="Text content"), InputAudioContentPart(audio=b"audio data")]
        message = UserMessageItem(content=mixed_content)

        assert len(message.content) == 2
        assert message.content[0].type == "input_text"
        assert message.content[1].type == "input_audio"


class TestRequestSession:
    """Test RequestSession model."""

    def test_basic_request_session(self):
        """Test creating a basic request session."""
        session = RequestSession(model="gpt-4o-realtime-preview")

        assert session.model == "gpt-4o-realtime-preview"
        assert session.modalities is None
        assert session.voice is None

    def test_request_session_with_voice(self):
        """Test request session with voice configuration."""
        voice = OpenAIVoice(name=OpenAIVoiceName.ALLOY)
        session = RequestSession(model="gpt-4o-realtime-preview", voice=voice, instructions="Be helpful and concise")

        assert session.voice == voice
        assert session.instructions == "Be helpful and concise"

    def test_request_session_with_modalities(self):
        """Test request session with modalities."""
        from azure.ai.voicelive.models import Modality

        session = RequestSession(model="gpt-4o-realtime-preview", modalities=[Modality.TEXT, Modality.AUDIO])

        assert len(session.modalities) == 2
        assert Modality.TEXT in session.modalities
        assert Modality.AUDIO in session.modalities

    def test_request_session_with_temperature(self):
        """Test request session with temperature settings."""
        session = RequestSession(model="gpt-4o-realtime-preview", temperature=0.7, max_response_output_tokens=1000)

        assert session.temperature == 0.7
        assert session.max_response_output_tokens == 1000


class TestResponseSession:
    """Test ResponseSession model."""

    def test_response_session_with_agent(self):
        """Test response session with agent configuration."""
        session = ResponseSession(model="gpt-4o-realtime-preview", id="session-789")

        assert session.id == "session-789"
        assert session.model == "gpt-4o-realtime-preview"


class TestModelValidation:
    """Test model validation and error cases."""

    def test_enum_values_work_correctly(self):
        """Test that enum values work correctly in models."""
        # Test that voice configurations work with proper enums
        from azure.ai.voicelive.models import AzureStandardVoice, AzureVoiceType

        voice = AzureStandardVoice(name="test-voice")
        assert voice.type == AzureVoiceType.AZURE_STANDARD


class TestModelSerialization:
    """Test model serialization capabilities."""

    def test_voice_model_dict_conversion(self):
        """Test converting voice models to dictionaries."""
        voice = AzureStandardVoice(name="test-voice", temperature=0.7)

        # Test that the model has serialization capabilities
        assert hasattr(voice, "__dict__")
        assert voice.name == "test-voice"
        assert voice.temperature == 0.7

    def test_message_item_dict_conversion(self):
        """Test converting message items to dictionaries."""
        content = [InputTextContentPart(text="Test message")]
        message = UserMessageItem(content=content)

        # Test that the model has expected attributes
        assert hasattr(message, "__dict__")
        assert message.role == MessageRole.USER
        assert len(message.content) == 1

    def test_complex_model_structure(self):
        """Test complex model with nested objects."""

        voice = AzurePersonalVoice(name="personal-voice", model=PersonalVoiceModels.PHOENIX_LATEST_NEURAL)

        session = ResponseSession(model="gpt-4o-realtime-preview", voice=voice, id="complex-session")

        # Verify the nested structure
        assert session.voice.name == "personal-voice"
        assert session.voice.model == PersonalVoiceModels.PHOENIX_LATEST_NEURAL