1
1
import pytest
2
2
3
- from pydantic_ai .messages import BinaryContent , DocumentUrl , ImageUrl , VideoUrl
3
+ from pydantic_ai .messages import AudioUrl , BinaryContent , DocumentUrl , ImageUrl , VideoUrl
4
4
5
5
6
6
def test_image_url ():
@@ -20,7 +20,7 @@ def test_video_url():
20
20
21
21
22
22
def test_document_url ():
23
- with pytest .raises (RuntimeError , match = 'Unknown document file extension: https://example.com/document.potato' ):
23
+ with pytest .raises (ValueError , match = 'Unknown document file extension: https://example.com/document.potato' ):
24
24
document_url = DocumentUrl (url = 'https://example.com/document.potato' )
25
25
document_url .media_type
26
26
@@ -93,3 +93,163 @@ def test_binary_content_document(media_type: str, format: str):
93
93
binary_content = BinaryContent (data = b'Hello, world!' , media_type = media_type )
94
94
assert binary_content .is_document
95
95
assert binary_content .format == format
96
+
97
+
98
+ @pytest .mark .parametrize (
99
+ 'audio_url,media_type,format' ,
100
+ [
101
+ pytest .param (AudioUrl ('foobar.mp3' ), 'audio/mpeg' , 'mp3' , id = 'mp3' ),
102
+ pytest .param (AudioUrl ('foobar.wav' ), 'audio/wav' , 'wav' , id = 'wav' ),
103
+ ],
104
+ )
105
+ def test_audio_url (audio_url : AudioUrl , media_type : str , format : str ):
106
+ assert audio_url .media_type == media_type
107
+ assert audio_url .format == format
108
+
109
+
110
+ def test_audio_url_invalid ():
111
+ with pytest .raises (ValueError , match = 'Unknown audio file extension: foobar.potato' ):
112
+ AudioUrl ('foobar.potato' ).media_type
113
+
114
+
115
+ @pytest .mark .parametrize (
116
+ 'image_url,media_type,format' ,
117
+ [
118
+ pytest .param (ImageUrl ('foobar.jpg' ), 'image/jpeg' , 'jpeg' , id = 'jpg' ),
119
+ pytest .param (ImageUrl ('foobar.jpeg' ), 'image/jpeg' , 'jpeg' , id = 'jpeg' ),
120
+ pytest .param (ImageUrl ('foobar.png' ), 'image/png' , 'png' , id = 'png' ),
121
+ pytest .param (ImageUrl ('foobar.gif' ), 'image/gif' , 'gif' , id = 'gif' ),
122
+ pytest .param (ImageUrl ('foobar.webp' ), 'image/webp' , 'webp' , id = 'webp' ),
123
+ ],
124
+ )
125
+ def test_image_url_formats (image_url : ImageUrl , media_type : str , format : str ):
126
+ assert image_url .media_type == media_type
127
+ assert image_url .format == format
128
+
129
+
130
+ def test_image_url_invalid ():
131
+ with pytest .raises (ValueError , match = 'Unknown image file extension: foobar.potato' ):
132
+ ImageUrl ('foobar.potato' ).media_type
133
+
134
+ with pytest .raises (ValueError , match = 'Unknown image file extension: foobar.potato' ):
135
+ ImageUrl ('foobar.potato' ).format
136
+
137
+
138
+ @pytest .mark .parametrize (
139
+ 'document_url,media_type,format' ,
140
+ [
141
+ pytest .param (DocumentUrl ('foobar.pdf' ), 'application/pdf' , 'pdf' , id = 'pdf' ),
142
+ pytest .param (DocumentUrl ('foobar.txt' ), 'text/plain' , 'txt' , id = 'txt' ),
143
+ pytest .param (DocumentUrl ('foobar.csv' ), 'text/csv' , 'csv' , id = 'csv' ),
144
+ pytest .param (
145
+ DocumentUrl ('foobar.docx' ),
146
+ 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' ,
147
+ 'docx' ,
148
+ id = 'docx' ,
149
+ ),
150
+ pytest .param (
151
+ DocumentUrl ('foobar.xlsx' ),
152
+ 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' ,
153
+ 'xlsx' ,
154
+ id = 'xlsx' ,
155
+ ),
156
+ pytest .param (DocumentUrl ('foobar.html' ), 'text/html' , 'html' , id = 'html' ),
157
+ pytest .param (DocumentUrl ('foobar.md' ), 'text/markdown' , 'md' , id = 'md' ),
158
+ pytest .param (DocumentUrl ('foobar.xls' ), 'application/vnd.ms-excel' , 'xls' , id = 'xls' ),
159
+ ],
160
+ )
161
+ def test_document_url_formats (document_url : DocumentUrl , media_type : str , format : str ):
162
+ assert document_url .media_type == media_type
163
+ assert document_url .format == format
164
+
165
+
166
+ def test_document_url_invalid ():
167
+ with pytest .raises (ValueError , match = 'Unknown document file extension: foobar.potato' ):
168
+ DocumentUrl ('foobar.potato' ).media_type
169
+
170
+ with pytest .raises (ValueError , match = 'Unknown document media type: text/x-python' ):
171
+ DocumentUrl ('foobar.py' ).format
172
+
173
+
174
+ def test_binary_content_unknown_media_type ():
175
+ with pytest .raises (ValueError , match = 'Unknown media type: application/custom' ):
176
+ binary_content = BinaryContent (data = b'Hello, world!' , media_type = 'application/custom' )
177
+ binary_content .format
178
+
179
+
180
+ def test_binary_content_is_methods ():
181
+ # Test that is_X returns False for non-matching media types
182
+ audio_content = BinaryContent (data = b'Hello, world!' , media_type = 'audio/wav' )
183
+ assert audio_content .is_audio is True
184
+ assert audio_content .is_image is False
185
+ assert audio_content .is_video is False
186
+ assert audio_content .is_document is False
187
+ assert audio_content .format == 'wav'
188
+
189
+ audio_content = BinaryContent (data = b'Hello, world!' , media_type = 'audio/wrong' )
190
+ assert audio_content .is_audio is True
191
+ assert audio_content .is_image is False
192
+ assert audio_content .is_video is False
193
+ assert audio_content .is_document is False
194
+ with pytest .raises (ValueError , match = 'Unknown media type: audio/wrong' ):
195
+ audio_content .format
196
+
197
+ audio_content = BinaryContent (data = b'Hello, world!' , media_type = 'image/wrong' )
198
+ assert audio_content .is_audio is False
199
+ assert audio_content .is_image is True
200
+ assert audio_content .is_video is False
201
+ assert audio_content .is_document is False
202
+ with pytest .raises (ValueError , match = 'Unknown media type: image/wrong' ):
203
+ audio_content .format
204
+
205
+ image_content = BinaryContent (data = b'Hello, world!' , media_type = 'image/jpeg' )
206
+ assert image_content .is_audio is False
207
+ assert image_content .is_image is True
208
+ assert image_content .is_video is False
209
+ assert image_content .is_document is False
210
+ assert image_content .format == 'jpeg'
211
+
212
+ video_content = BinaryContent (data = b'Hello, world!' , media_type = 'video/mp4' )
213
+ assert video_content .is_audio is False
214
+ assert video_content .is_image is False
215
+ assert video_content .is_video is True
216
+ assert video_content .is_document is False
217
+ assert video_content .format == 'mp4'
218
+
219
+ video_content = BinaryContent (data = b'Hello, world!' , media_type = 'video/wrong' )
220
+ assert video_content .is_audio is False
221
+ assert video_content .is_image is False
222
+ assert video_content .is_video is True
223
+ assert video_content .is_document is False
224
+ with pytest .raises (ValueError , match = 'Unknown media type: video/wrong' ):
225
+ video_content .format
226
+
227
+ document_content = BinaryContent (data = b'Hello, world!' , media_type = 'application/pdf' )
228
+ assert document_content .is_audio is False
229
+ assert document_content .is_image is False
230
+ assert document_content .is_video is False
231
+ assert document_content .is_document is True
232
+ assert document_content .format == 'pdf'
233
+
234
+
235
+ @pytest .mark .parametrize (
236
+ 'video_url,media_type,format' ,
237
+ [
238
+ pytest .param (VideoUrl ('foobar.mp4' ), 'video/mp4' , 'mp4' , id = 'mp4' ),
239
+ pytest .param (VideoUrl ('foobar.mov' ), 'video/quicktime' , 'mov' , id = 'mov' ),
240
+ pytest .param (VideoUrl ('foobar.mkv' ), 'video/x-matroska' , 'mkv' , id = 'mkv' ),
241
+ pytest .param (VideoUrl ('foobar.webm' ), 'video/webm' , 'webm' , id = 'webm' ),
242
+ pytest .param (VideoUrl ('foobar.flv' ), 'video/x-flv' , 'flv' , id = 'flv' ),
243
+ pytest .param (VideoUrl ('foobar.mpeg' ), 'video/mpeg' , 'mpeg' , id = 'mpeg' ),
244
+ pytest .param (VideoUrl ('foobar.wmv' ), 'video/x-ms-wmv' , 'wmv' , id = 'wmv' ),
245
+ pytest .param (VideoUrl ('foobar.three_gp' ), 'video/3gpp' , 'three_gp' , id = 'three_gp' ),
246
+ ],
247
+ )
248
+ def test_video_url_formats (video_url : VideoUrl , media_type : str , format : str ):
249
+ assert video_url .media_type == media_type
250
+ assert video_url .format == format
251
+
252
+
253
+ def test_video_url_invalid ():
254
+ with pytest .raises (ValueError , match = 'Unknown video file extension: foobar.potato' ):
255
+ VideoUrl ('foobar.potato' ).media_type
0 commit comments