openai/openai-dotnet

Public

mirrored from https://github.com/openai/openai-dotnetAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
joseharriaga/MessageResponseItem

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

tests/Files/FilesTests.cs

391lines · modecode

1using Microsoft.ClientModel.TestFramework;
2using NUnit.Framework;
3using OpenAI.Files;
4using OpenAI.Tests.Utility;
5using System;
6using System.ClientModel;
7using System.ClientModel.Primitives;
8using System.Collections.Generic;
9using System.IO;
10using System.Linq;
11using System.Threading.Tasks;
12using static OpenAI.Tests.TestHelpers;
13
14namespace OpenAI.Tests.Files;
15
16[Category("Files")]
17public class FilesTests : OpenAIRecordedTestBase
18{
19 public enum FileSourceKind
20 {
21 UsingStream,
22 UsingFilePath,
23 UsingBinaryData
24 }
25
26 public FilesTests(bool isAsync) : base(isAsync)
27 {
28 }
29
30 [OneTimeTearDown]
31 public void TearDown()
32 {
33 OpenAIFileClient client = GetTestClient();
34
35 RequestOptions noThrowOptions = new() { ErrorOptions = ClientErrorBehaviors.NoThrow };
36 foreach (string fileId in FileIdsForCleanup)
37 {
38 _ = client.DeleteFile(fileId, noThrowOptions);
39 }
40 }
41
42 [RecordedTest]
43 public async Task ListFiles()
44 {
45 using (Recording.DisableRequestBodyRecording()) // Temp pending https://github.com/Azure/azure-sdk-tools/issues/11901
46 {
47 OpenAIFileClient client = GetTestClient();
48 using Stream file1 = BinaryData.FromString("Hello! This is a test text file. Please delete me.").ToStream();
49 using Stream file2 = BinaryData.FromString("Hello! This is another test text file. Please delete me.").ToStream();
50 string filename = "test-file-delete-me.txt";
51 string visionFilename = "images_dog_and_cat.png";
52 string visionFilePath = Path.Combine("Assets", visionFilename);
53
54 OpenAIFile uploadedFile1 = null;
55 OpenAIFile uploadedFile2 = null;
56 OpenAIFile uploadedVisionFile = null;
57 OpenAIFileCollection fileInfoCollection;
58
59 try
60 {
61 uploadedFile1 = await client.UploadFileAsync(file1, filename, FileUploadPurpose.Assistants);
62 Validate(uploadedFile1);
63 uploadedFile2 = await client.UploadFileAsync(file2, filename, FileUploadPurpose.Assistants);
64 Validate(uploadedFile2);
65 uploadedVisionFile = await client.UploadFileAsync(visionFilePath, FileUploadPurpose.Vision);
66 Validate(uploadedVisionFile);
67
68 fileInfoCollection = await client.GetFilesAsync(FilePurpose.Assistants);
69 }
70 finally
71 {
72 if (uploadedVisionFile != null)
73 {
74 await client.DeleteFileAsync(uploadedVisionFile.Id);
75 }
76
77 if (uploadedFile2 != null)
78 {
79 await client.DeleteFileAsync(uploadedFile2.Id);
80 }
81
82 if (uploadedFile1 != null)
83 {
84 await client.DeleteFileAsync(uploadedFile1.Id);
85 }
86 }
87
88 OpenAIFile fileInfo1 = null;
89 OpenAIFile fileInfo2 = null;
90 OpenAIFile visionFileInfo = null;
91
92 foreach (OpenAIFile item in fileInfoCollection)
93 {
94 if (item.Id == uploadedFile1.Id)
95 {
96 fileInfo1 = item;
97 }
98 else if (item.Id == uploadedFile2.Id)
99 {
100 fileInfo2 = item;
101 }
102 else if (item.Id == uploadedVisionFile.Id)
103 {
104 visionFileInfo = item;
105 }
106 }
107
108 Assert.That(fileInfo1, Is.Not.Null);
109 Assert.That(fileInfo1.SizeInBytes, Is.EqualTo(uploadedFile1.SizeInBytes));
110 Assert.That(fileInfo1.CreatedAt, Is.EqualTo(uploadedFile1.CreatedAt));
111 Assert.That(fileInfo1.Filename, Is.EqualTo(uploadedFile1.Filename));
112 Assert.That(fileInfo1.Purpose, Is.EqualTo(uploadedFile1.Purpose));
113#pragma warning disable CS0618
114 Assert.That(fileInfo1.Status, Is.EqualTo(uploadedFile1.Status));
115 Assert.That(fileInfo1.StatusDetails, Is.EqualTo(uploadedFile1.StatusDetails));
116#pragma warning restore CS0618
117
118 Assert.That(fileInfo2, Is.Not.Null);
119 Assert.That(fileInfo2.SizeInBytes, Is.EqualTo(uploadedFile2.SizeInBytes));
120 Assert.That(fileInfo2.CreatedAt, Is.EqualTo(uploadedFile2.CreatedAt));
121 Assert.That(fileInfo2.Filename, Is.EqualTo(uploadedFile2.Filename));
122 Assert.That(fileInfo2.Purpose, Is.EqualTo(uploadedFile2.Purpose));
123#pragma warning disable CS0618
124 Assert.That(fileInfo2.Status, Is.EqualTo(uploadedFile2.Status));
125 Assert.That(fileInfo2.StatusDetails, Is.EqualTo(uploadedFile2.StatusDetails));
126#pragma warning restore CS0618
127
128 Assert.That(visionFileInfo, Is.Null);
129 }
130 }
131
132 [RecordedTest]
133 [TestCaseSource(nameof(s_fileSourceKindSource))]
134 public async Task UploadFile(FileSourceKind fileSourceKind)
135 {
136 using (Recording.DisableRequestBodyRecording()) // Temp pending https://github.com/Azure/azure-sdk-tools/issues/11901
137 {
138 OpenAIFileClient client = GetTestClient();
139 string filename = "images_dog_and_cat.png";
140 string path = Path.Combine("Assets", filename);
141 OpenAIFile fileInfo = null;
142
143 try
144 {
145 if (fileSourceKind == FileSourceKind.UsingStream)
146 {
147 using Stream file = File.OpenRead(path);
148
149 fileInfo = await client.UploadFileAsync(file, filename, FileUploadPurpose.Vision);
150 Validate(fileInfo);
151 }
152 else if (fileSourceKind == FileSourceKind.UsingFilePath)
153 {
154 fileInfo = await client.UploadFileAsync(path, FileUploadPurpose.Vision);
155 Validate(fileInfo);
156 }
157 else if (fileSourceKind == FileSourceKind.UsingBinaryData)
158 {
159 using Stream file = File.OpenRead(path);
160 BinaryData content = BinaryData.FromStream(file);
161
162 fileInfo = await client.UploadFileAsync(content, filename, FileUploadPurpose.Vision);
163 Validate(fileInfo);
164 }
165 else
166 {
167 Assert.Fail("Invalid source kind.");
168 }
169 }
170 finally
171 {
172 if (fileInfo != null)
173 {
174 await client.DeleteFileAsync(fileInfo.Id);
175 }
176 }
177
178 long expectedSize = new FileInfo(path).Length;
179 long unixTime2024 = (new DateTimeOffset(2024, 01, 01, 0, 0, 0, TimeSpan.Zero)).ToUnixTimeSeconds();
180 string expectedFilename = (fileSourceKind == FileSourceKind.UsingFilePath) ? path : filename;
181
182 Assert.That(fileInfo, Is.Not.Null);
183 Assert.That(fileInfo.Id, Is.Not.Null.And.Not.Empty);
184 Assert.That(fileInfo.SizeInBytes, Is.EqualTo(expectedSize));
185 Assert.That(fileInfo.CreatedAt.ToUnixTimeSeconds(), Is.GreaterThan(unixTime2024));
186 Assert.That(fileInfo.Filename, Is.EqualTo(expectedFilename));
187 Assert.That(fileInfo.Purpose, Is.EqualTo(FilePurpose.Vision));
188#pragma warning disable CS0618
189 Assert.That(fileInfo.Status, Is.Not.EqualTo(default(FileStatus)));
190#pragma warning restore CS0618
191 }
192 }
193
194 [RecordedTest]
195 public void UploadFileCanParseServiceError()
196 {
197 using (Recording.DisableRequestBodyRecording()) // Temp pending https://github.com/Azure/azure-sdk-tools/issues/11901
198 {
199 OpenAIFileClient client = GetTestClient();
200 string filename = "images_dog_and_cat.png";
201 string path = Path.Combine("Assets", filename);
202 FileUploadPurpose fakePurpose = new FileUploadPurpose("world_domination");
203 ClientResultException ex = Assert.ThrowsAsync<ClientResultException>(async () => await client.UploadFileAsync(path, fakePurpose));
204
205 Assert.That(ex.Status, Is.EqualTo(400));
206 }
207 }
208
209 [RecordedTest]
210 [TestCase(true)]
211 [TestCase(false)]
212 public async Task DeleteFile(bool useFileInfoOverload)
213 {
214 using (Recording.DisableRequestBodyRecording()) // Temp pending https://github.com/Azure/azure-sdk-tools/issues/11901
215 {
216 OpenAIFileClient client = GetTestClient();
217 string fileContent = "Hello! This is a test text file. Please delete me.";
218 using Stream file = BinaryData.FromString(fileContent).ToStream();
219 string filename = "test-file-delete-me.txt";
220
221 OpenAIFile uploadedFile = await client.UploadFileAsync(file, filename, FileUploadPurpose.Assistants);
222 Validate(uploadedFile);
223 FileDeletionResult result;
224
225 if (useFileInfoOverload)
226 {
227 result = await client.DeleteFileAsync(uploadedFile.Id);
228 }
229 else
230 {
231 result = await client.DeleteFileAsync(uploadedFile.Id);
232 }
233
234 Assert.That(result.FileId, Is.EqualTo(uploadedFile.Id));
235 Assert.That(result.Deleted, Is.True);
236 }
237 }
238
239 [RecordedTest]
240 public void DeleteFileCanParseServiceError()
241 {
242 OpenAIFileClient client = GetTestClient();
243 ClientResultException ex = Assert.ThrowsAsync<ClientResultException>(async () => await client.DeleteFileAsync("fake_id"));
244
245 Assert.That(ex.Status, Is.EqualTo(404));
246 }
247
248 [RecordedTest]
249 public async Task GetFile()
250 {
251 using (Recording.DisableRequestBodyRecording()) // Temp pending https://github.com/Azure/azure-sdk-tools/issues/11901
252 {
253 OpenAIFileClient client = GetTestClient();
254 using Stream file = BinaryData.FromString("Hello! This is a test text file. Please delete me.").ToStream();
255 string filename = "test-file-delete-me.txt";
256 OpenAIFile uploadedFile = null;
257 OpenAIFile fileInfo;
258
259 try
260 {
261 uploadedFile = await client.UploadFileAsync(file, filename, FileUploadPurpose.Assistants);
262 Validate(uploadedFile);
263
264 fileInfo = await client.GetFileAsync(uploadedFile.Id);
265 }
266 finally
267 {
268 if (uploadedFile != null)
269 {
270 await client.DeleteFileAsync(uploadedFile.Id);
271 }
272 }
273
274 Assert.That(fileInfo, Is.Not.Null);
275 Assert.That(fileInfo.Id, Is.EqualTo(uploadedFile.Id));
276 Assert.That(fileInfo.SizeInBytes, Is.EqualTo(uploadedFile.SizeInBytes));
277 Assert.That(fileInfo.CreatedAt, Is.EqualTo(uploadedFile.CreatedAt));
278 Assert.That(fileInfo.Filename, Is.EqualTo(uploadedFile.Filename));
279 Assert.That(fileInfo.Purpose, Is.EqualTo(uploadedFile.Purpose));
280#pragma warning disable CS0618
281 Assert.That(fileInfo.Status, Is.EqualTo(uploadedFile.Status));
282 Assert.That(fileInfo.StatusDetails, Is.EqualTo(uploadedFile.StatusDetails));
283#pragma warning restore CS0618
284 }
285 }
286
287 [RecordedTest]
288 public void GetFileCanParseServiceError()
289 {
290 OpenAIFileClient client = GetTestClient();
291 ClientResultException ex = Assert.ThrowsAsync<ClientResultException>(async () => await client.GetFileAsync("fake_id"));
292
293 Assert.That(ex.Status, Is.EqualTo(404));
294 }
295
296 [RecordedTest]
297 public async Task DownloadContent()
298 {
299 using (Recording.DisableRequestBodyRecording()) // Temp pending https://github.com/Azure/azure-sdk-tools/issues/11901
300 {
301 OpenAIFileClient client = GetTestClient();
302 string filename = "images_dog_and_cat.png";
303 string path = Path.Combine("Assets", filename);
304 using Stream file = File.OpenRead(path);
305 OpenAIFile uploadedFile = null;
306 BinaryData downloadedContent;
307
308 try
309 {
310 uploadedFile = await client.UploadFileAsync(file, filename, FileUploadPurpose.Vision);
311 Validate(uploadedFile);
312
313 downloadedContent = await client.DownloadFileAsync(uploadedFile.Id);
314 }
315 finally
316 {
317 if (uploadedFile != null)
318 {
319 await client.DeleteFileAsync(uploadedFile.Id);
320 }
321 }
322
323 byte[] originalFileBytes = File.ReadAllBytes(path);
324 byte[] downloadedBytes = downloadedContent.ToArray();
325
326 Assert.That(downloadedBytes.SequenceEqual(originalFileBytes));
327 }
328 }
329
330 [RecordedTest]
331 public void DownloadFileCanParseServiceError()
332 {
333 OpenAIFileClient client = GetTestClient();
334 ClientResultException ex = Assert.ThrowsAsync<ClientResultException>(async () => await client.DownloadFileAsync("fake_id"));
335
336 Assert.That(ex.Status, Is.EqualTo(404));
337 }
338
339 [RecordedTest]
340 public void SerializeFileCollection()
341 {
342 // TODO: Add this test.
343 }
344
345 [RecordedTest]
346 public async Task NonAsciiFilename()
347 {
348 using (Recording.DisableRequestBodyRecording()) // Temp pending https://github.com/Azure/azure-sdk-tools/issues/11901
349 {
350 OpenAIFileClient client = GetTestClient();
351 string filename = "你好.txt";
352 BinaryData fileContent = BinaryData.FromString("世界您好!这是个测试。");
353 OpenAIFile uploadedFile = await client.UploadFileAsync(fileContent, filename, FileUploadPurpose.Assistants);
354 Validate(uploadedFile);
355 Assert.That(uploadedFile?.Filename, Is.EqualTo(filename));
356 }
357 }
358
359 [RecordedTest]
360 public async Task UserDataPurpose()
361 {
362 using (Recording.DisableRequestBodyRecording()) // Temp pending https://github.com/Azure/azure-sdk-tools/issues/11901
363 {
364 OpenAIFileClient client = GetTestClient();
365
366 BinaryData fileContent = BinaryData.FromString("Hello, world!");
367 OpenAIFile uploadedFile = await client.UploadFileAsync(fileContent, "test_hello_world.txt", FileUploadPurpose.UserData);
368 Validate(uploadedFile);
369 Assert.That(uploadedFile.Purpose, Is.EqualTo(FilePurpose.UserData));
370 }
371 }
372
373 private static Array s_fileSourceKindSource = Enum.GetValues(typeof(FileSourceKind));
374
375 private readonly List<string> FileIdsForCleanup = [];
376
377 private void Validate<T>(T instance)
378 {
379 if (instance is OpenAIFile file)
380 {
381 Assert.That(file?.Id, Is.Not.Null.And.Not.Empty);
382 FileIdsForCleanup.Add(file.Id);
383 }
384 else
385 {
386 throw new NotImplementedException();
387 }
388 }
389
390 private OpenAIFileClient GetTestClient() => GetProxiedOpenAIClient<OpenAIFileClient>(TestScenario.Files);
391}