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