openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
tests/Files/FilesMockTests.cs
430lines · modecode
| 1 | using Microsoft.ClientModel.TestFramework; |
| 2 | using Microsoft.ClientModel.TestFramework.Mocks; |
| 3 | using NUnit.Framework; |
| 4 | using OpenAI.Files; |
| 5 | using OpenAI.Tests.Utility; |
| 6 | using System; |
| 7 | using System.ClientModel; |
| 8 | using System.IO; |
| 9 | using System.Linq; |
| 10 | using System.Threading; |
| 11 | using System.Threading.Tasks; |
| 12 | |
| 13 | namespace OpenAI.Tests.Files; |
| 14 | |
| 15 | [Parallelizable(ParallelScope.All)] |
| 16 | [Category("Files")] |
| 17 | [Category("Smoke")] |
| 18 | public class FilesMockTests : ClientTestBase |
| 19 | { |
| 20 | private static readonly ApiKeyCredential s_fakeCredential = new ApiKeyCredential("key"); |
| 21 | |
| 22 | public FilesMockTests(bool isAsync) |
| 23 | : base(isAsync) |
| 24 | { |
| 25 | } |
| 26 | |
| 27 | public enum FileSourceKind |
| 28 | { |
| 29 | UsingStream, |
| 30 | UsingFilePath, |
| 31 | UsingBinaryData |
| 32 | } |
| 33 | |
| 34 | private static Array s_fileSourceKindSource = Enum.GetValues(typeof(FileSourceKind)); |
| 35 | |
| 36 | private static object[] s_purposeSource = |
| 37 | { |
| 38 | ("assistants", FilePurpose.Assistants), |
| 39 | ("assistants_output", FilePurpose.AssistantsOutput), |
| 40 | ("batch", FilePurpose.Batch), |
| 41 | ("batch_output", FilePurpose.BatchOutput), |
| 42 | ("fine-tune", FilePurpose.FineTune), |
| 43 | ("fine-tune-results", FilePurpose.FineTuneResults), |
| 44 | ("vision", FilePurpose.Vision) |
| 45 | }; |
| 46 | |
| 47 | #pragma warning disable CS0618 |
| 48 | private static object[] s_statusSource = |
| 49 | { |
| 50 | ("uploaded", FileStatus.Uploaded), |
| 51 | ("processed", FileStatus.Processed), |
| 52 | ("error", FileStatus.Error) |
| 53 | }; |
| 54 | #pragma warning restore CS0618 |
| 55 | |
| 56 | [Test] |
| 57 | public async Task GetFileDeserializesId() |
| 58 | { |
| 59 | OpenAIClientOptions clientOptions = GetClientOptionsWithMockResponse(200, """ |
| 60 | { |
| 61 | "id": "returned_file_id" |
| 62 | } |
| 63 | """); |
| 64 | OpenAIFileClient client = CreateProxyFromClient(new OpenAIFileClient(s_fakeCredential, clientOptions)); |
| 65 | |
| 66 | OpenAIFile fileInfo = await client.GetFileAsync("file_id"); |
| 67 | Assert.That(fileInfo.Id, Is.EqualTo("returned_file_id")); |
| 68 | } |
| 69 | |
| 70 | [Test] |
| 71 | public async Task GetFileDeserializesCreatedAt() |
| 72 | { |
| 73 | OpenAIClientOptions clientOptions = GetClientOptionsWithMockResponse(200, """ |
| 74 | { |
| 75 | "created_at": 1704096000 |
| 76 | } |
| 77 | """); |
| 78 | OpenAIFileClient client = CreateProxyFromClient(new OpenAIFileClient(s_fakeCredential, clientOptions)); |
| 79 | |
| 80 | OpenAIFile fileInfo = await client.GetFileAsync("file_id"); |
| 81 | Assert.That(fileInfo.CreatedAt.ToUnixTimeSeconds(), Is.EqualTo(1704096000)); |
| 82 | } |
| 83 | |
| 84 | [Test] |
| 85 | [TestCaseSource(nameof(s_purposeSource))] |
| 86 | public async Task GetFileDeserializesPurpose((string stringValue, FilePurpose expectedValue) purpose) |
| 87 | { |
| 88 | OpenAIClientOptions clientOptions = GetClientOptionsWithMockResponse(200, $$""" |
| 89 | { |
| 90 | "purpose": "{{purpose.stringValue}}" |
| 91 | } |
| 92 | """); |
| 93 | OpenAIFileClient client = CreateProxyFromClient(new OpenAIFileClient(s_fakeCredential, clientOptions)); |
| 94 | OpenAIFile fileInfo = await client.GetFileAsync("file_id"); |
| 95 | |
| 96 | Assert.That(fileInfo.Purpose, Is.EqualTo(purpose.expectedValue)); |
| 97 | } |
| 98 | |
| 99 | |
| 100 | #pragma warning disable CS0618 |
| 101 | [Test] |
| 102 | [TestCaseSource(nameof(s_statusSource))] |
| 103 | public async Task GetFileDeserializesStatus((string stringValue, FileStatus expectedValue) status) |
| 104 | { |
| 105 | OpenAIClientOptions clientOptions = GetClientOptionsWithMockResponse(200, $$""" |
| 106 | { |
| 107 | "status": "{{status.stringValue}}" |
| 108 | } |
| 109 | """); |
| 110 | OpenAIFileClient client = CreateProxyFromClient(new OpenAIFileClient(s_fakeCredential, clientOptions)); |
| 111 | OpenAIFile fileInfo = await client.GetFileAsync("file_id"); |
| 112 | Assert.That(fileInfo.Status, Is.EqualTo(status.expectedValue)); |
| 113 | } |
| 114 | #pragma warning restore CS0618 |
| 115 | |
| 116 | #pragma warning disable CS0618 |
| 117 | [Test] |
| 118 | public async Task GetFileDeserializesStatusDetails() |
| 119 | { |
| 120 | OpenAIClientOptions clientOptions = GetClientOptionsWithMockResponse(200, """ |
| 121 | { |
| 122 | "status_details": "This is definitely an error." |
| 123 | } |
| 124 | """); |
| 125 | OpenAIFileClient client = CreateProxyFromClient(new OpenAIFileClient(s_fakeCredential, clientOptions)); |
| 126 | OpenAIFile fileInfo = await client.GetFileAsync("file_id"); |
| 127 | Assert.That(fileInfo.StatusDetails, Is.EqualTo("This is definitely an error.")); |
| 128 | } |
| 129 | #pragma warning restore CS0618 |
| 130 | |
| 131 | [Test] |
| 132 | public void GetFileRespectsTheCancellationToken() |
| 133 | { |
| 134 | OpenAIFileClient client = CreateProxyFromClient(new OpenAIFileClient(s_fakeCredential)); |
| 135 | using CancellationTokenSource cancellationSource = new(); |
| 136 | cancellationSource.Cancel(); |
| 137 | |
| 138 | Assert.That(async () => await client.GetFileAsync("fileId", cancellationSource.Token), |
| 139 | Throws.InstanceOf<OperationCanceledException>()); |
| 140 | } |
| 141 | |
| 142 | [Test] |
| 143 | [TestCaseSource(nameof(s_fileSourceKindSource))] |
| 144 | public async Task UploadFileDeserializesId(FileSourceKind fileSourceKind) |
| 145 | { |
| 146 | OpenAIClientOptions clientOptions = GetClientOptionsWithMockResponse(200, """ |
| 147 | { |
| 148 | "id": "returned_file_id" |
| 149 | } |
| 150 | """); |
| 151 | OpenAIFile fileInfo = await InvokeUploadFileSyncOrAsync(clientOptions, fileSourceKind); |
| 152 | |
| 153 | Assert.That(fileInfo.Id, Is.EqualTo("returned_file_id")); |
| 154 | } |
| 155 | |
| 156 | [Test] |
| 157 | [TestCaseSource(nameof(s_fileSourceKindSource))] |
| 158 | public async Task UploadFileDeserializesCreatedAt(FileSourceKind fileSourceKind) |
| 159 | { |
| 160 | OpenAIClientOptions clientOptions = GetClientOptionsWithMockResponse(200, """ |
| 161 | { |
| 162 | "created_at": 1704096000 |
| 163 | } |
| 164 | """); |
| 165 | OpenAIFile fileInfo = await InvokeUploadFileSyncOrAsync(clientOptions, fileSourceKind); |
| 166 | |
| 167 | Assert.That(fileInfo.CreatedAt.ToUnixTimeSeconds(), Is.EqualTo(1704096000)); |
| 168 | } |
| 169 | |
| 170 | [Test] |
| 171 | public async Task UploadFileDeserializesPurpose( |
| 172 | [ValueSource(nameof(s_fileSourceKindSource))] FileSourceKind fileSourceKind, |
| 173 | [ValueSource(nameof(s_purposeSource))] (string stringValue, FilePurpose expectedValue) purpose) |
| 174 | { |
| 175 | OpenAIClientOptions clientOptions = GetClientOptionsWithMockResponse(200, $$""" |
| 176 | { |
| 177 | "purpose": "{{purpose.stringValue}}" |
| 178 | } |
| 179 | """); |
| 180 | OpenAIFile fileInfo = await InvokeUploadFileSyncOrAsync(clientOptions, fileSourceKind); |
| 181 | |
| 182 | Assert.That(fileInfo.Purpose, Is.EqualTo(purpose.expectedValue)); |
| 183 | } |
| 184 | |
| 185 | #pragma warning disable CS0618 |
| 186 | [Test] |
| 187 | public async Task UploadFileDeserializesStatus( |
| 188 | [ValueSource(nameof(s_fileSourceKindSource))] FileSourceKind fileSourceKind, |
| 189 | [ValueSource(nameof(s_statusSource))] (string stringValue, FileStatus expectedValue) status) |
| 190 | { |
| 191 | OpenAIClientOptions clientOptions = GetClientOptionsWithMockResponse(200, $$""" |
| 192 | { |
| 193 | "status": "{{status.stringValue}}" |
| 194 | } |
| 195 | """); |
| 196 | OpenAIFile fileInfo = await InvokeUploadFileSyncOrAsync(clientOptions, fileSourceKind); |
| 197 | |
| 198 | Assert.That(fileInfo.Status, Is.EqualTo(status.expectedValue)); |
| 199 | } |
| 200 | #pragma warning restore CS0618 |
| 201 | |
| 202 | #pragma warning disable CS0618 |
| 203 | [Test] |
| 204 | [TestCaseSource(nameof(s_fileSourceKindSource))] |
| 205 | public async Task UploadFileDeserializesStatusDetails(FileSourceKind fileSourceKind) |
| 206 | { |
| 207 | OpenAIClientOptions clientOptions = GetClientOptionsWithMockResponse(200, """ |
| 208 | { |
| 209 | "status_details": "This is definitely an error." |
| 210 | } |
| 211 | """); |
| 212 | OpenAIFile fileInfo = await InvokeUploadFileSyncOrAsync(clientOptions, fileSourceKind); |
| 213 | |
| 214 | Assert.That(fileInfo.StatusDetails, Is.EqualTo("This is definitely an error.")); |
| 215 | } |
| 216 | #pragma warning restore CS0618 |
| 217 | |
| 218 | [Test] |
| 219 | public async Task UploadFileDeserializesBigSizes() |
| 220 | { |
| 221 | long bigSize = (long)int.MaxValue + (long)int.MaxValue / 2; |
| 222 | |
| 223 | OpenAIClientOptions clientOptions = GetClientOptionsWithMockResponse(200, $$""" |
| 224 | { |
| 225 | "bytes": {{bigSize}} |
| 226 | } |
| 227 | """); |
| 228 | OpenAIFile fileInfo = await InvokeUploadFileSyncOrAsync(clientOptions, FileSourceKind.UsingFilePath); |
| 229 | |
| 230 | Assert.That(fileInfo.SizeInBytesLong, Is.EqualTo(bigSize)); |
| 231 | Assert.Throws<OverflowException>(() => _ = fileInfo.SizeInBytes); |
| 232 | } |
| 233 | |
| 234 | [Test] |
| 235 | public void UploadFileRespectsTheCancellationToken() |
| 236 | { |
| 237 | OpenAIFileClient client = CreateProxyFromClient(new OpenAIFileClient(s_fakeCredential)); |
| 238 | using var stream = new MemoryStream(Array.Empty<byte>()); |
| 239 | using CancellationTokenSource cancellationSource = new(); |
| 240 | cancellationSource.Cancel(); |
| 241 | |
| 242 | Assert.That(async () => await client.UploadFileAsync(stream, "filename.txt", FileUploadPurpose.Assistants, cancellationSource.Token), |
| 243 | Throws.InstanceOf<OperationCanceledException>()); |
| 244 | } |
| 245 | |
| 246 | [Test] |
| 247 | public async Task GetFilesDeserializesId() |
| 248 | { |
| 249 | OpenAIClientOptions clientOptions = GetClientOptionsWithMockResponse(200, """ |
| 250 | { |
| 251 | "object": "list", |
| 252 | "data": [ |
| 253 | { |
| 254 | "id": "returned_file_id" |
| 255 | } |
| 256 | ] |
| 257 | } |
| 258 | """); |
| 259 | OpenAIFileClient client = CreateProxyFromClient(new OpenAIFileClient(s_fakeCredential, clientOptions)); |
| 260 | |
| 261 | OpenAIFileCollection fileInfoCollection = await client.GetFilesAsync(FilePurpose.Assistants); |
| 262 | OpenAIFile fileInfo = fileInfoCollection.Single(); |
| 263 | |
| 264 | Assert.That(fileInfo.Id, Is.EqualTo("returned_file_id")); |
| 265 | } |
| 266 | |
| 267 | [Test] |
| 268 | public async Task GetFilesDeserializesCreatedAt() |
| 269 | { |
| 270 | OpenAIClientOptions clientOptions = GetClientOptionsWithMockResponse(200, """ |
| 271 | { |
| 272 | "object": "list", |
| 273 | "data": [ |
| 274 | { |
| 275 | "created_at": 1704096000 |
| 276 | } |
| 277 | ] |
| 278 | } |
| 279 | """); |
| 280 | OpenAIFileClient client = CreateProxyFromClient(new OpenAIFileClient(s_fakeCredential, clientOptions)); |
| 281 | |
| 282 | OpenAIFileCollection fileInfoCollection = await client.GetFilesAsync(FilePurpose.Assistants); |
| 283 | OpenAIFile fileInfo = fileInfoCollection.Single(); |
| 284 | |
| 285 | Assert.That(fileInfo.CreatedAt.ToUnixTimeSeconds(), Is.EqualTo(1704096000)); |
| 286 | } |
| 287 | |
| 288 | [Test] |
| 289 | [TestCaseSource(nameof(s_purposeSource))] |
| 290 | public async Task GetFilesDeserializesPurpose((string stringValue, FilePurpose expectedValue) purpose) |
| 291 | { |
| 292 | OpenAIClientOptions clientOptions = GetClientOptionsWithMockResponse(200, $$""" |
| 293 | { |
| 294 | "object": "list", |
| 295 | "data": [ |
| 296 | { |
| 297 | "purpose": "{{purpose.stringValue}}" |
| 298 | } |
| 299 | ] |
| 300 | } |
| 301 | """); |
| 302 | OpenAIFileClient client = CreateProxyFromClient(new OpenAIFileClient(s_fakeCredential, clientOptions)); |
| 303 | |
| 304 | OpenAIFileCollection fileInfoCollection = await client.GetFilesAsync(FilePurpose.Assistants); |
| 305 | OpenAIFile fileInfo = fileInfoCollection.Single(); |
| 306 | |
| 307 | Assert.That(fileInfo.Purpose, Is.EqualTo(purpose.expectedValue)); |
| 308 | } |
| 309 | |
| 310 | #pragma warning disable CS0618 |
| 311 | [Test] |
| 312 | [TestCaseSource(nameof(s_statusSource))] |
| 313 | public async Task GetFilesDeserializesStatus((string stringValue, FileStatus expectedValue) status) |
| 314 | { |
| 315 | OpenAIClientOptions clientOptions = GetClientOptionsWithMockResponse(200, $$""" |
| 316 | { |
| 317 | "object": "list", |
| 318 | "data": [ |
| 319 | { |
| 320 | "status": "{{status.stringValue}}" |
| 321 | } |
| 322 | ] |
| 323 | } |
| 324 | """); |
| 325 | OpenAIFileClient client = CreateProxyFromClient(new OpenAIFileClient(s_fakeCredential, clientOptions)); |
| 326 | |
| 327 | OpenAIFileCollection fileInfoCollection = await client.GetFilesAsync(FilePurpose.Assistants); |
| 328 | OpenAIFile fileInfo = fileInfoCollection.Single(); |
| 329 | |
| 330 | Assert.That(fileInfo.Status, Is.EqualTo(status.expectedValue)); |
| 331 | } |
| 332 | #pragma warning restore CS0618 |
| 333 | |
| 334 | #pragma warning disable CS0618 |
| 335 | [Test] |
| 336 | public async Task GetFilesDeserializesStatusDetails() |
| 337 | { |
| 338 | OpenAIClientOptions clientOptions = GetClientOptionsWithMockResponse(200, """ |
| 339 | { |
| 340 | "object": "list", |
| 341 | "data": [ |
| 342 | { |
| 343 | "status_details": "This is definitely an error." |
| 344 | } |
| 345 | ] |
| 346 | } |
| 347 | """); |
| 348 | OpenAIFileClient client = CreateProxyFromClient(new OpenAIFileClient(s_fakeCredential, clientOptions)); |
| 349 | |
| 350 | OpenAIFileCollection fileInfoCollection = await client.GetFilesAsync(FilePurpose.Assistants); |
| 351 | OpenAIFile fileInfo = fileInfoCollection.Single(); |
| 352 | |
| 353 | Assert.That(fileInfo.StatusDetails, Is.EqualTo("This is definitely an error.")); |
| 354 | } |
| 355 | #pragma warning restore CS0618 |
| 356 | |
| 357 | [Test] |
| 358 | public void GetFilesRespectsTheCancellationToken() |
| 359 | { |
| 360 | OpenAIFileClient client = CreateProxyFromClient(new OpenAIFileClient(s_fakeCredential)); |
| 361 | using CancellationTokenSource cancellationSource = new(); |
| 362 | cancellationSource.Cancel(); |
| 363 | |
| 364 | Assert.That(async () => await client.GetFilesAsync(FilePurpose.Assistants, cancellationSource.Token), |
| 365 | Throws.InstanceOf<OperationCanceledException>()); |
| 366 | } |
| 367 | |
| 368 | [Test] |
| 369 | public void DownloadFileRespectsTheCancellationToken() |
| 370 | { |
| 371 | OpenAIFileClient client = CreateProxyFromClient(new OpenAIFileClient(s_fakeCredential)); |
| 372 | using CancellationTokenSource cancellationSource = new(); |
| 373 | cancellationSource.Cancel(); |
| 374 | |
| 375 | Assert.That(async () => await client.DownloadFileAsync("fileId", cancellationSource.Token), |
| 376 | Throws.InstanceOf<OperationCanceledException>()); |
| 377 | } |
| 378 | |
| 379 | [Test] |
| 380 | public void DeleteFileRespectsTheCancellationToken() |
| 381 | { |
| 382 | OpenAIFileClient client = CreateProxyFromClient(new OpenAIFileClient(s_fakeCredential)); |
| 383 | using CancellationTokenSource cancellationSource = new(); |
| 384 | cancellationSource.Cancel(); |
| 385 | |
| 386 | Assert.That(async () => await client.DeleteFileAsync("fileId", cancellationSource.Token), |
| 387 | Throws.InstanceOf<OperationCanceledException>()); |
| 388 | } |
| 389 | |
| 390 | private OpenAIClientOptions GetClientOptionsWithMockResponse(int status, string content) |
| 391 | { |
| 392 | MockPipelineResponse response = new MockPipelineResponse(status).WithContent(content); |
| 393 | |
| 394 | return new OpenAIClientOptions() |
| 395 | { |
| 396 | Transport = new MockPipelineTransport(_ => response) |
| 397 | { |
| 398 | ExpectSyncPipeline = !IsAsync |
| 399 | } |
| 400 | }; |
| 401 | } |
| 402 | |
| 403 | private async ValueTask<OpenAIFile> InvokeUploadFileSyncOrAsync(OpenAIClientOptions clientOptions, FileSourceKind fileSourceKind) |
| 404 | { |
| 405 | OpenAIFileClient client = CreateProxyFromClient(new OpenAIFileClient(s_fakeCredential, clientOptions)); |
| 406 | string filename = "images_dog_and_cat.png"; |
| 407 | string path = Path.Combine("Assets", filename); |
| 408 | |
| 409 | if (fileSourceKind == FileSourceKind.UsingStream) |
| 410 | { |
| 411 | using FileStream file = File.OpenRead(path); |
| 412 | |
| 413 | return await client.UploadFileAsync(file, filename, purpose: FileUploadPurpose.Assistants); |
| 414 | } |
| 415 | else if (fileSourceKind == FileSourceKind.UsingFilePath) |
| 416 | { |
| 417 | return await client.UploadFileAsync(path, purpose: FileUploadPurpose.Assistants); |
| 418 | } |
| 419 | else if (fileSourceKind == FileSourceKind.UsingBinaryData) |
| 420 | { |
| 421 | using FileStream file = File.OpenRead(path); |
| 422 | BinaryData content = BinaryData.FromStream(file); |
| 423 | |
| 424 | return await client.UploadFileAsync(content, filename, purpose: FileUploadPurpose.Assistants); |
| 425 | } |
| 426 | |
| 427 | Assert.Fail("Invalid source kind."); |
| 428 | return null; |
| 429 | } |
| 430 | } |
| 431 | |