Skip to content

Commit a95f905

Browse files
committed
remaining tests
1 parent 82379a2 commit a95f905

File tree

149 files changed

+39063
-823
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

149 files changed

+39063
-823
lines changed

nuget.config

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<configuration>
33
<packageSources>
44
<!--To inherit the global NuGet package sources remove the <clear/> line below -->
55
<clear />
66
<add key="nuget" value="https://api.nuget.org/v3/index.json" />
77
<add key="azure-sdk-dev-feed" value="https://pkgs.dev.azure.com/azure-sdk/public/_packaging/azure-sdk-for-net/nuget/v3/index.json" />
8-
<add key="Package source" value="C:\Users\mredding\source\repos\azure-sdk-for-net\artifacts\packages\Release" />
98
</packageSources>
109
</configuration>

tests/Embeddings/EmbeddingsMockTests.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using OpenAI.Tests.Utility;
66
using System;
77
using System.ClientModel;
8+
using System.ClientModel.Primitives;
89
using System.Linq;
910
using System.Threading;
1011
using System.Threading.Tasks;
@@ -200,6 +201,42 @@ public void GenerateEmbeddingsWithIntegersRespectsTheCancellationToken()
200201
Throws.InstanceOf<OperationCanceledException>());
201202
}
202203

204+
[Test]
205+
public void SerializeEmbeddingCollection()
206+
{
207+
// TODO: Add this test.
208+
}
209+
210+
[Test]
211+
public void JsonArraySupport()
212+
{
213+
string json = """
214+
{
215+
"object":"list",
216+
"data":[
217+
{
218+
"object":"embedding",
219+
"embedding":[-0.011229509,0.107915245,-0.15163477]
220+
}
221+
]
222+
}
223+
""";
224+
225+
BinaryData binaryData = BinaryData.FromString(json);
226+
227+
OpenAIEmbeddingCollection embeddings = ModelReaderWriter.Read<OpenAIEmbeddingCollection>(binaryData);
228+
229+
Assert.That(embeddings, Is.Not.Null);
230+
Assert.That(embeddings.Count, Is.EqualTo(1));
231+
var embedding = embeddings[0];
232+
Assert.That(embedding, Is.Not.Null);
233+
ReadOnlySpan<float> vector = embedding.ToFloats().Span;
234+
Assert.That(vector.Length, Is.EqualTo(3));
235+
Assert.That(vector[0], Is.EqualTo(-0.011229509f));
236+
Assert.That(vector[1], Is.EqualTo(0.107915245f));
237+
Assert.That(vector[2], Is.EqualTo(-0.15163477f));
238+
}
239+
203240
private OpenAIClientOptions GetClientOptionsWithMockResponse(int status, string content)
204241
{
205242
MockPipelineResponse response = new MockPipelineResponse(status).WithContent(content);

tests/Embeddings/EmbeddingsTests.cs

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
namespace OpenAI.Tests.Embeddings;
1313

14-
[Parallelizable(ParallelScope.All)]
1514
[Category("Embeddings")]
1615
public class EmbeddingsTests : OpenAIRecordedTestBase
1716
{
@@ -184,40 +183,4 @@ public async Task EmbeddingFromStringAndEmbeddingFromTokenIdsAreEqual()
184183
Assert.That(vector1.Span[i], Is.EqualTo(vector2.Span[i]).Within(0.0005));
185184
}
186185
}
187-
188-
[Test]
189-
public void SerializeEmbeddingCollection()
190-
{
191-
// TODO: Add this test.
192-
}
193-
194-
[Test]
195-
public void JsonArraySupport()
196-
{
197-
string json = """
198-
{
199-
"object":"list",
200-
"data":[
201-
{
202-
"object":"embedding",
203-
"embedding":[-0.011229509,0.107915245,-0.15163477]
204-
}
205-
]
206-
}
207-
""";
208-
209-
BinaryData binaryData = BinaryData.FromString(json);
210-
211-
OpenAIEmbeddingCollection embeddings = ModelReaderWriter.Read<OpenAIEmbeddingCollection>(binaryData);
212-
213-
Assert.That(embeddings, Is.Not.Null);
214-
Assert.That(embeddings.Count, Is.EqualTo(1));
215-
var embedding = embeddings[0];
216-
Assert.That(embedding, Is.Not.Null);
217-
ReadOnlySpan<float> vector = embedding.ToFloats().Span;
218-
Assert.That(vector.Length, Is.EqualTo(3));
219-
Assert.That(vector[0], Is.EqualTo(-0.011229509f));
220-
Assert.That(vector[1], Is.EqualTo(0.107915245f));
221-
Assert.That(vector[2], Is.EqualTo(-0.15163477f));
222-
}
223186
}

tests/Files/Files.UploadsTests.cs

Lines changed: 68 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
namespace OpenAI.Tests.Files;
1212

13-
[Parallelizable(ParallelScope.Fixtures)]
1413
[Category("Uploads")]
1514
public class UploadsTests : OpenAIRecordedTestBase
1615
{
@@ -29,7 +28,7 @@ public async Task CreateUploadWorks(bool useTopLevelClient)
2928
// are setting the internal Uploads client correctly.
3029

3130
OpenAIFileClient fileClient = useTopLevelClient
32-
? CreateProxyFromClient(TestHelpers.GetTestTopLevelClient().GetOpenAIFileClient())
31+
? GetProxiedTestTopLevelClient().GetOpenAIFileClient()
3332
: GetTestClient();
3433
BinaryContent content = BinaryContent.Create(BinaryData.FromObjectAsJson(new
3534
{
@@ -60,77 +59,83 @@ public async Task CreateUploadWorks(bool useTopLevelClient)
6059
[Test]
6160
public async Task AddUploadPartWorks()
6261
{
63-
OpenAIFileClient fileClient = GetTestClient();
64-
UploadDetails uploadDetails = await CreateTestUploadAsync(fileClient);
65-
using MultiPartFormDataBinaryContent content = new();
66-
67-
content.Add([1, 2, 3, 4], "data", "data", "application/octet-stream");
68-
69-
ClientResult result = await fileClient.AddUploadPartAsync(uploadDetails.Id, content, content.ContentType);
70-
BinaryData response = result.GetRawResponse().Content;
71-
using JsonDocument jsonDocument = JsonDocument.Parse(response);
72-
UploadPartDetails uploadPartDetails = GetUploadPartDetails(jsonDocument);
73-
74-
Assert.That(uploadPartDetails.Id, Does.StartWith("part_"));
75-
Assert.That(uploadPartDetails.Object, Is.EqualTo("upload.part"));
76-
Assert.That(uploadPartDetails.CreatedAt, Is.GreaterThanOrEqualTo(uploadDetails.CreatedAt));
77-
Assert.That(uploadPartDetails.UploadId, Is.EqualTo(uploadDetails.Id));
62+
using (Recording.DisableRequestBodyRecording()) // Temp pending https://github.com/Azure/azure-sdk-tools/issues/11901
63+
{
64+
OpenAIFileClient fileClient = GetTestClient();
65+
UploadDetails uploadDetails = await CreateTestUploadAsync(fileClient);
66+
using MultiPartFormDataBinaryContent content = new();
67+
68+
content.Add([1, 2, 3, 4], "data", "data", "application/octet-stream");
69+
70+
ClientResult result = await fileClient.AddUploadPartAsync(uploadDetails.Id, content, content.ContentType);
71+
BinaryData response = result.GetRawResponse().Content;
72+
using JsonDocument jsonDocument = JsonDocument.Parse(response);
73+
UploadPartDetails uploadPartDetails = GetUploadPartDetails(jsonDocument);
74+
75+
Assert.That(uploadPartDetails.Id, Does.StartWith("part_"));
76+
Assert.That(uploadPartDetails.Object, Is.EqualTo("upload.part"));
77+
Assert.That(uploadPartDetails.CreatedAt, Is.GreaterThanOrEqualTo(uploadDetails.CreatedAt));
78+
Assert.That(uploadPartDetails.UploadId, Is.EqualTo(uploadDetails.Id));
79+
}
7880
}
7981

8082
[Test]
8183
public async Task CompleteUploadWorks()
8284
{
83-
OpenAIFileClient fileClient = GetTestClient();
84-
UploadDetails createdUploadDetails = await CreateTestUploadAsync(fileClient);
85-
using MultiPartFormDataBinaryContent firstPartContent = new();
86-
using MultiPartFormDataBinaryContent secondPartContent = new();
85+
using (Recording.DisableRequestBodyRecording()) // Temp pending https://github.com/Azure/azure-sdk-tools/issues/11901
86+
{
87+
OpenAIFileClient fileClient = GetTestClient();
88+
UploadDetails createdUploadDetails = await CreateTestUploadAsync(fileClient);
89+
using MultiPartFormDataBinaryContent firstPartContent = new();
90+
using MultiPartFormDataBinaryContent secondPartContent = new();
8791

88-
firstPartContent.Add([1, 2, 3, 4], "data", "data", "application/octet-stream");
89-
secondPartContent.Add([5, 6, 7, 8], "data", "data", "application/octet-stream");
92+
firstPartContent.Add([1, 2, 3, 4], "data", "data", "application/octet-stream");
93+
secondPartContent.Add([5, 6, 7, 8], "data", "data", "application/octet-stream");
9094

91-
UploadPartDetails firstPartDetails = await AddTestUploadPartAsync(fileClient, createdUploadDetails.Id, firstPartContent);
92-
UploadPartDetails secondPartDetails = await AddTestUploadPartAsync(fileClient, createdUploadDetails.Id, secondPartContent);
95+
UploadPartDetails firstPartDetails = await AddTestUploadPartAsync(fileClient, createdUploadDetails.Id, firstPartContent);
96+
UploadPartDetails secondPartDetails = await AddTestUploadPartAsync(fileClient, createdUploadDetails.Id, secondPartContent);
9397

94-
BinaryContent content = BinaryContent.Create(BinaryData.FromObjectAsJson(new
95-
{
96-
part_ids = new[] {
98+
BinaryContent content = BinaryContent.Create(BinaryData.FromObjectAsJson(new
99+
{
100+
part_ids = new[] {
97101
firstPartDetails.Id,
98102
secondPartDetails.Id
99103
}
100-
}));
101-
102-
ClientResult result = await fileClient.CompleteUploadAsync(createdUploadDetails.Id, content);
103-
104-
BinaryData response = result.GetRawResponse().Content;
105-
using JsonDocument jsonDocument = JsonDocument.Parse(response);
106-
JsonElement fileElement = jsonDocument.RootElement.GetProperty("file");
107-
string fileId = fileElement.GetProperty("id").GetString();
108-
109-
await fileClient.DeleteFileAsync(fileId);
110-
111-
UploadDetails completedUploadDetails = GetUploadDetails(jsonDocument);
112-
113-
Assert.That(completedUploadDetails.Id, Is.EqualTo(createdUploadDetails.Id));
114-
Assert.That(completedUploadDetails.Object, Is.EqualTo(createdUploadDetails.Object));
115-
Assert.That(completedUploadDetails.Bytes, Is.EqualTo(createdUploadDetails.Bytes));
116-
Assert.That(completedUploadDetails.CreatedAt, Is.EqualTo(createdUploadDetails.CreatedAt));
117-
Assert.That(completedUploadDetails.Filename, Is.EqualTo(createdUploadDetails.Filename));
118-
Assert.That(completedUploadDetails.Purpose, Is.EqualTo(createdUploadDetails.Purpose));
119-
Assert.That(completedUploadDetails.Status, Is.EqualTo("completed"));
120-
Assert.That(completedUploadDetails.ExpiresAt, Is.EqualTo(createdUploadDetails.ExpiresAt));
121-
122-
string fileObject = fileElement.GetProperty("object").GetString();
123-
int fileBytes = fileElement.GetProperty("bytes").GetInt32();
124-
long fileCreatedAt = fileElement.GetProperty("created_at").GetInt64();
125-
string filename = fileElement.GetProperty("filename").GetString();
126-
string filePurpose = fileElement.GetProperty("purpose").GetString();
127-
128-
Assert.That(fileId, Does.StartWith("file-"));
129-
Assert.That(fileObject, Is.EqualTo("file"));
130-
Assert.That(fileBytes, Is.EqualTo(createdUploadDetails.Bytes));
131-
Assert.That(fileCreatedAt, Is.GreaterThanOrEqualTo(createdUploadDetails.CreatedAt));
132-
Assert.That(filename, Is.EqualTo(createdUploadDetails.Filename));
133-
Assert.That(filePurpose, Is.EqualTo(createdUploadDetails.Purpose));
104+
}));
105+
106+
ClientResult result = await fileClient.CompleteUploadAsync(createdUploadDetails.Id, content);
107+
108+
BinaryData response = result.GetRawResponse().Content;
109+
using JsonDocument jsonDocument = JsonDocument.Parse(response);
110+
JsonElement fileElement = jsonDocument.RootElement.GetProperty("file");
111+
string fileId = fileElement.GetProperty("id").GetString();
112+
113+
await fileClient.DeleteFileAsync(fileId);
114+
115+
UploadDetails completedUploadDetails = GetUploadDetails(jsonDocument);
116+
117+
Assert.That(completedUploadDetails.Id, Is.EqualTo(createdUploadDetails.Id));
118+
Assert.That(completedUploadDetails.Object, Is.EqualTo(createdUploadDetails.Object));
119+
Assert.That(completedUploadDetails.Bytes, Is.EqualTo(createdUploadDetails.Bytes));
120+
Assert.That(completedUploadDetails.CreatedAt, Is.EqualTo(createdUploadDetails.CreatedAt));
121+
Assert.That(completedUploadDetails.Filename, Is.EqualTo(createdUploadDetails.Filename));
122+
Assert.That(completedUploadDetails.Purpose, Is.EqualTo(createdUploadDetails.Purpose));
123+
Assert.That(completedUploadDetails.Status, Is.EqualTo("completed"));
124+
Assert.That(completedUploadDetails.ExpiresAt, Is.EqualTo(createdUploadDetails.ExpiresAt));
125+
126+
string fileObject = fileElement.GetProperty("object").GetString();
127+
int fileBytes = fileElement.GetProperty("bytes").GetInt32();
128+
long fileCreatedAt = fileElement.GetProperty("created_at").GetInt64();
129+
string filename = fileElement.GetProperty("filename").GetString();
130+
string filePurpose = fileElement.GetProperty("purpose").GetString();
131+
132+
Assert.That(fileId, Does.StartWith("file-"));
133+
Assert.That(fileObject, Is.EqualTo("file"));
134+
Assert.That(fileBytes, Is.EqualTo(createdUploadDetails.Bytes));
135+
Assert.That(fileCreatedAt, Is.GreaterThanOrEqualTo(createdUploadDetails.CreatedAt));
136+
Assert.That(filename, Is.EqualTo(createdUploadDetails.Filename));
137+
Assert.That(filePurpose, Is.EqualTo(createdUploadDetails.Purpose));
138+
}
134139
}
135140

136141
[Test]
@@ -160,7 +165,7 @@ private async Task<UploadDetails> CreateTestUploadAsync(OpenAIFileClient fileCli
160165
BinaryContent content = BinaryContent.Create(BinaryData.FromObjectAsJson(new
161166
{
162167
purpose = "fine-tune",
163-
filename = "uploads_test_file" + Guid.NewGuid() + ".jsonl",
168+
filename = "uploads_test_file" + Recording.Random.NewGuid() + ".jsonl",
164169
bytes = 8,
165170
mime_type = "text/jsonl"
166171
}));

0 commit comments

Comments
 (0)