openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
tests/Files/Files.UploadsTests.cs
248lines · modecode
| 1 | using Microsoft.ClientModel.TestFramework; |
| 2 | using NUnit.Framework; |
| 3 | using OpenAI.Files; |
| 4 | using OpenAI.Tests.Utility; |
| 5 | using System; |
| 6 | using System.ClientModel; |
| 7 | using System.Text.Json; |
| 8 | using System.Threading.Tasks; |
| 9 | using static OpenAI.Tests.TestHelpers; |
| 10 | |
| 11 | namespace OpenAI.Tests.Files; |
| 12 | |
| 13 | [Category("Uploads")] |
| 14 | public class UploadsTests : OpenAIRecordedTestBase |
| 15 | { |
| 16 | public UploadsTests(bool isAsync) : base(isAsync) |
| 17 | { |
| 18 | } |
| 19 | |
| 20 | [RecordedTest] |
| 21 | [TestCase(true)] |
| 22 | [TestCase(false)] |
| 23 | public async Task CreateUploadWorks(bool useTopLevelClient) |
| 24 | { |
| 25 | // Test with the top-level client as well to validate that both FileClient constructors |
| 26 | // are setting the internal Uploads client correctly. |
| 27 | |
| 28 | OpenAIFileClient fileClient = useTopLevelClient |
| 29 | ? GetProxiedTestTopLevelClient().GetOpenAIFileClient() |
| 30 | : GetTestClient(); |
| 31 | BinaryContent content = BinaryContent.Create(BinaryData.FromObjectAsJson(new |
| 32 | { |
| 33 | purpose = "fine-tune", |
| 34 | filename = "uploads_test_file.jsonl", |
| 35 | bytes = 8, |
| 36 | mime_type = "text/jsonl" |
| 37 | })); |
| 38 | |
| 39 | ClientResult result = await fileClient.CreateUploadAsync(content); |
| 40 | |
| 41 | BinaryData response = result.GetRawResponse().Content; |
| 42 | using JsonDocument jsonDocument = JsonDocument.Parse(response); |
| 43 | UploadDetails uploadDetails = GetUploadDetails(jsonDocument); |
| 44 | |
| 45 | long unixTime2024 = (new DateTimeOffset(2024, 01, 01, 0, 0, 0, TimeSpan.Zero)).ToUnixTimeSeconds(); |
| 46 | |
| 47 | Assert.That(uploadDetails.Id, Does.StartWith("upload_")); |
| 48 | Assert.That(uploadDetails.Object, Is.EqualTo("upload")); |
| 49 | Assert.That(uploadDetails.Bytes, Is.EqualTo(8)); |
| 50 | Assert.That(uploadDetails.CreatedAt, Is.GreaterThan(unixTime2024)); |
| 51 | Assert.That(uploadDetails.Filename, Is.EqualTo("uploads_test_file.jsonl")); |
| 52 | Assert.That(uploadDetails.Purpose, Is.EqualTo("fine-tune")); |
| 53 | Assert.That(uploadDetails.Status, Is.EqualTo("pending")); |
| 54 | Assert.That(uploadDetails.ExpiresAt, Is.GreaterThan(uploadDetails.CreatedAt)); |
| 55 | } |
| 56 | |
| 57 | [RecordedTest] |
| 58 | public async Task AddUploadPartWorks() |
| 59 | { |
| 60 | using (Recording.DisableRequestBodyRecording()) // Temp pending https://github.com/Azure/azure-sdk-tools/issues/11901 |
| 61 | { |
| 62 | OpenAIFileClient fileClient = GetTestClient(); |
| 63 | UploadDetails uploadDetails = await CreateTestUploadAsync(fileClient); |
| 64 | using MultiPartFormDataBinaryContent content = new(); |
| 65 | |
| 66 | content.Add([1, 2, 3, 4], "data", "data", "application/octet-stream"); |
| 67 | |
| 68 | ClientResult result = await fileClient.AddUploadPartAsync(uploadDetails.Id, content, content.ContentType); |
| 69 | BinaryData response = result.GetRawResponse().Content; |
| 70 | using JsonDocument jsonDocument = JsonDocument.Parse(response); |
| 71 | UploadPartDetails uploadPartDetails = GetUploadPartDetails(jsonDocument); |
| 72 | |
| 73 | Assert.That(uploadPartDetails.Id, Does.StartWith("part_")); |
| 74 | Assert.That(uploadPartDetails.Object, Is.EqualTo("upload.part")); |
| 75 | Assert.That(uploadPartDetails.CreatedAt, Is.GreaterThanOrEqualTo(uploadDetails.CreatedAt)); |
| 76 | Assert.That(uploadPartDetails.UploadId, Is.EqualTo(uploadDetails.Id)); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | [RecordedTest] |
| 81 | public async Task CompleteUploadWorks() |
| 82 | { |
| 83 | using (Recording.DisableRequestBodyRecording()) // Temp pending https://github.com/Azure/azure-sdk-tools/issues/11901 |
| 84 | { |
| 85 | OpenAIFileClient fileClient = GetTestClient(); |
| 86 | UploadDetails createdUploadDetails = await CreateTestUploadAsync(fileClient); |
| 87 | using MultiPartFormDataBinaryContent firstPartContent = new(); |
| 88 | using MultiPartFormDataBinaryContent secondPartContent = new(); |
| 89 | |
| 90 | firstPartContent.Add([1, 2, 3, 4], "data", "data", "application/octet-stream"); |
| 91 | secondPartContent.Add([5, 6, 7, 8], "data", "data", "application/octet-stream"); |
| 92 | |
| 93 | UploadPartDetails firstPartDetails = await AddTestUploadPartAsync(fileClient, createdUploadDetails.Id, firstPartContent); |
| 94 | UploadPartDetails secondPartDetails = await AddTestUploadPartAsync(fileClient, createdUploadDetails.Id, secondPartContent); |
| 95 | |
| 96 | BinaryContent content = BinaryContent.Create(BinaryData.FromObjectAsJson(new |
| 97 | { |
| 98 | part_ids = new[] { |
| 99 | firstPartDetails.Id, |
| 100 | secondPartDetails.Id |
| 101 | } |
| 102 | })); |
| 103 | |
| 104 | ClientResult result = await fileClient.CompleteUploadAsync(createdUploadDetails.Id, content); |
| 105 | |
| 106 | BinaryData response = result.GetRawResponse().Content; |
| 107 | using JsonDocument jsonDocument = JsonDocument.Parse(response); |
| 108 | JsonElement fileElement = jsonDocument.RootElement.GetProperty("file"); |
| 109 | string fileId = fileElement.GetProperty("id").GetString(); |
| 110 | |
| 111 | await fileClient.DeleteFileAsync(fileId); |
| 112 | |
| 113 | UploadDetails completedUploadDetails = GetUploadDetails(jsonDocument); |
| 114 | |
| 115 | Assert.That(completedUploadDetails.Id, Is.EqualTo(createdUploadDetails.Id)); |
| 116 | Assert.That(completedUploadDetails.Object, Is.EqualTo(createdUploadDetails.Object)); |
| 117 | Assert.That(completedUploadDetails.Bytes, Is.EqualTo(createdUploadDetails.Bytes)); |
| 118 | Assert.That(completedUploadDetails.CreatedAt, Is.EqualTo(createdUploadDetails.CreatedAt)); |
| 119 | Assert.That(completedUploadDetails.Filename, Is.EqualTo(createdUploadDetails.Filename)); |
| 120 | Assert.That(completedUploadDetails.Purpose, Is.EqualTo(createdUploadDetails.Purpose)); |
| 121 | Assert.That(completedUploadDetails.Status, Is.EqualTo("completed")); |
| 122 | Assert.That(completedUploadDetails.ExpiresAt, Is.EqualTo(createdUploadDetails.ExpiresAt)); |
| 123 | |
| 124 | string fileObject = fileElement.GetProperty("object").GetString(); |
| 125 | int fileBytes = fileElement.GetProperty("bytes").GetInt32(); |
| 126 | long fileCreatedAt = fileElement.GetProperty("created_at").GetInt64(); |
| 127 | string filename = fileElement.GetProperty("filename").GetString(); |
| 128 | string filePurpose = fileElement.GetProperty("purpose").GetString(); |
| 129 | |
| 130 | Assert.That(fileId, Does.StartWith("file-")); |
| 131 | Assert.That(fileObject, Is.EqualTo("file")); |
| 132 | Assert.That(fileBytes, Is.EqualTo(createdUploadDetails.Bytes)); |
| 133 | Assert.That(fileCreatedAt, Is.GreaterThanOrEqualTo(createdUploadDetails.CreatedAt)); |
| 134 | Assert.That(filename, Is.EqualTo(createdUploadDetails.Filename)); |
| 135 | Assert.That(filePurpose, Is.EqualTo(createdUploadDetails.Purpose)); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | [RecordedTest] |
| 140 | public async Task CancelUploadWorks() |
| 141 | { |
| 142 | OpenAIFileClient fileClient = GetTestClient(); |
| 143 | UploadDetails createdUploadDetails = await CreateTestUploadAsync(fileClient); |
| 144 | |
| 145 | ClientResult result = await fileClient.CancelUploadAsync(createdUploadDetails.Id); |
| 146 | |
| 147 | BinaryData response = result.GetRawResponse().Content; |
| 148 | using JsonDocument jsonDocument = JsonDocument.Parse(response); |
| 149 | UploadDetails canceledUploadDetails = GetUploadDetails(jsonDocument); |
| 150 | |
| 151 | Assert.That(canceledUploadDetails.Id, Is.EqualTo(createdUploadDetails.Id)); |
| 152 | Assert.That(canceledUploadDetails.Object, Is.EqualTo(createdUploadDetails.Object)); |
| 153 | Assert.That(canceledUploadDetails.Bytes, Is.EqualTo(createdUploadDetails.Bytes)); |
| 154 | Assert.That(canceledUploadDetails.CreatedAt, Is.EqualTo(createdUploadDetails.CreatedAt)); |
| 155 | Assert.That(canceledUploadDetails.Filename, Is.EqualTo(createdUploadDetails.Filename)); |
| 156 | Assert.That(canceledUploadDetails.Purpose, Is.EqualTo(createdUploadDetails.Purpose)); |
| 157 | Assert.That(canceledUploadDetails.Status, Is.EqualTo("cancelled")); |
| 158 | Assert.That(canceledUploadDetails.ExpiresAt, Is.EqualTo(createdUploadDetails.ExpiresAt)); |
| 159 | } |
| 160 | |
| 161 | private async Task<UploadDetails> CreateTestUploadAsync(OpenAIFileClient fileClient) |
| 162 | { |
| 163 | BinaryContent content = BinaryContent.Create(BinaryData.FromObjectAsJson(new |
| 164 | { |
| 165 | purpose = "fine-tune", |
| 166 | filename = "uploads_test_file" + Recording.Random.NewGuid() + ".jsonl", |
| 167 | bytes = 8, |
| 168 | mime_type = "text/jsonl" |
| 169 | })); |
| 170 | |
| 171 | ClientResult result = await fileClient.CreateUploadAsync(content); |
| 172 | BinaryData response = result.GetRawResponse().Content; |
| 173 | using JsonDocument jsonDocument = JsonDocument.Parse(response); |
| 174 | |
| 175 | return GetUploadDetails(jsonDocument); |
| 176 | } |
| 177 | |
| 178 | private async Task<UploadPartDetails> AddTestUploadPartAsync(OpenAIFileClient fileClient, string uploadId, MultiPartFormDataBinaryContent content) |
| 179 | { |
| 180 | ClientResult result = await fileClient.AddUploadPartAsync(uploadId, content, content.ContentType); |
| 181 | BinaryData response = result.GetRawResponse().Content; |
| 182 | using JsonDocument jsonDocument = JsonDocument.Parse(response); |
| 183 | |
| 184 | return GetUploadPartDetails(jsonDocument); |
| 185 | } |
| 186 | |
| 187 | private UploadDetails GetUploadDetails(JsonDocument jsonDocument) |
| 188 | { |
| 189 | string id = jsonDocument.RootElement.GetProperty("id").GetString(); |
| 190 | string @object = jsonDocument.RootElement.GetProperty("object").GetString(); |
| 191 | int bytes = jsonDocument.RootElement.GetProperty("bytes").GetInt32(); |
| 192 | long createdAt = jsonDocument.RootElement.GetProperty("created_at").GetInt64(); |
| 193 | string filename = jsonDocument.RootElement.GetProperty("filename").GetString(); |
| 194 | string purpose = jsonDocument.RootElement.GetProperty("purpose").GetString(); |
| 195 | string status = jsonDocument.RootElement.GetProperty("status").GetString(); |
| 196 | long expiresAt = jsonDocument.RootElement.GetProperty("expires_at").GetInt64(); |
| 197 | |
| 198 | return new UploadDetails() |
| 199 | { |
| 200 | Id = id, |
| 201 | Object = @object, |
| 202 | Bytes = bytes, |
| 203 | CreatedAt = createdAt, |
| 204 | Filename = filename, |
| 205 | Purpose = purpose, |
| 206 | Status = status, |
| 207 | ExpiresAt = expiresAt |
| 208 | }; |
| 209 | } |
| 210 | |
| 211 | private UploadPartDetails GetUploadPartDetails(JsonDocument jsonDocument) |
| 212 | { |
| 213 | string id = jsonDocument.RootElement.GetProperty("id").GetString(); |
| 214 | string @object = jsonDocument.RootElement.GetProperty("object").GetString(); |
| 215 | long createdAt = jsonDocument.RootElement.GetProperty("created_at").GetInt64(); |
| 216 | string uploadId = jsonDocument.RootElement.GetProperty("upload_id").GetString(); |
| 217 | |
| 218 | return new UploadPartDetails() |
| 219 | { |
| 220 | Id = id, |
| 221 | Object = @object, |
| 222 | CreatedAt = createdAt, |
| 223 | UploadId = uploadId |
| 224 | }; |
| 225 | } |
| 226 | |
| 227 | private OpenAIFileClient GetTestClient() => GetProxiedOpenAIClient<OpenAIFileClient>(TestScenario.Files); |
| 228 | |
| 229 | private readonly struct UploadDetails |
| 230 | { |
| 231 | public string Id { get; init; } |
| 232 | public string Object { get; init; } |
| 233 | public int Bytes { get; init; } |
| 234 | public long CreatedAt { get; init; } |
| 235 | public string Filename { get; init; } |
| 236 | public string Purpose { get; init; } |
| 237 | public string Status { get; init; } |
| 238 | public long ExpiresAt { get; init; } |
| 239 | } |
| 240 | |
| 241 | private readonly struct UploadPartDetails |
| 242 | { |
| 243 | public string Id { get; init; } |
| 244 | public string Object { get; init; } |
| 245 | public long CreatedAt { get; init; } |
| 246 | public string UploadId { get; init; } |
| 247 | } |
| 248 | } |