Skip to content

Commit

Permalink
ACCEPTANCES: Upload File
Browse files Browse the repository at this point in the history
  • Loading branch information
sulton-max committed Jul 3, 2023
1 parent afd7d03 commit 0a3411d
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// ----------------------------------------------------------------------------------
// Copyright (c) The Standard Organization, a coalition of the Good-Hearted Engineers
// ----------------------------------------------------------------------------------

using System.Threading.Tasks;
using FluentAssertions;
using Force.DeepCloner;
using Standard.AI.OpenAI.Models.Services.Foundations.AIFiles;
using Standard.AI.OpenAI.Models.Services.Foundations.ExternalAIFiles;
using WireMock.RequestBuilders;
using WireMock.ResponseBuilders;
using Xunit;

namespace Standard.AI.OpenAI.Tests.Acceptance.Clients.AIFiles
{
public partial class AIFilesClientTests
{
[Fact]
public async Task ShouldUploadFileAsync()
{
AIFileRequest randomAIFileRequest =
CreateRandomAIFileRequest();

var uploadAIFileRequest = new AIFile
{
Request = randomAIFileRequest
};

ExternalAIFileRequest externalAIFileRequest =
ConvertToExternalAIFileRequest(uploadAIFileRequest.Request);

ExternalAIFileResponse externalAIFileResponse =
CreateExternalAiFileResponseOnUpload(externalAIFileRequest);

AIFileResponse aiFileResponse =
ConvertToAIFileResponse(externalAIFileResponse);

AIFile expectedAIFile = uploadAIFileRequest.DeepClone();
expectedAIFile.Response = aiFileResponse;

this.wireMockServer.Given(
Request.Create()
.UsingPost()
.WithPath("/v1/files")
.WithHeader("Authorization", $"Bearer {this.apiKey}")
.WithHeader("OpenAI-Organization", this.organizationId))
.RespondWith(
Response.Create()
.WithBodyAsJson(externalAIFileResponse));

// when
AIFile actualAIFile =
await this.openAIClient.AIFiles.UploadFileAsync(uploadAIFileRequest);

// then
actualAIFile.Should().BeEquivalentTo(expectedAIFile);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Moq;
using Standard.AI.OpenAI.Clients.OpenAIs;
using Standard.AI.OpenAI.Models.Configurations;
using Standard.AI.OpenAI.Models.Services.Foundations.AIFiles;
Expand Down Expand Up @@ -58,7 +60,19 @@ private static AIFileResponse ConvertToAIFileResponse(ExternalAIFileResponse ext
StatusDetails = externalAIFileResponse.StatusDetails
};
}

private static ExternalAIFileRequest ConvertToExternalAIFileRequest(AIFileRequest aiFileRequest)
{
var externalAIFileRequest = new ExternalAIFileRequest
{
Purpose = aiFileRequest.Purpose,
FileName = aiFileRequest.Name,
File = aiFileRequest.Content
};

return externalAIFileRequest;
}

private static AIFile ConvertToAIFileOnDelete(ExternalAIFileResponse externalAiFileResponse)
{
AIFileResponse response = ConvertToAIFileResponse(externalAiFileResponse);
Expand All @@ -80,7 +94,31 @@ private static AIFileStatus ConvertToAIFileStatus(string externalStatus)
_ => AIFileStatus.Unknown
};
}

private static ExternalAIFileResponse CreateExternalAiFileResponseOnUpload(
ExternalAIFileRequest externalAiFileRequest
)
{
Filler<ExternalAIFileResponse> externalAiFileResponseFiller = CreateExternalAIFileResponseFiller();
externalAiFileResponseFiller.Setup()
.OnProperty(response => response.FileName)
.Use(externalAiFileRequest.FileName)
.OnProperty(response => response.Purpose)
.Use(externalAiFileRequest.Purpose);

return externalAiFileResponseFiller.Create();
}

private static AIFileRequest CreateRandomAIFileRequest()
{
var aiFileRequestFiller = CreateRandomAIFileRequestFiller();
aiFileRequestFiller.Setup()
.OnProperty(request => request.Content)
.Use(CreateRandomStream());

return aiFileRequestFiller.Create();
}

private static ExternalAIFileResponse CreateRandomDeletedAIFileResponse()
{
var filler = CreateExternalAIFileResponseFiller();
Expand All @@ -104,10 +142,28 @@ private static ExternalAIFileResponse CreateRandomDeletedAIFileResponse()

return filler.Create();
}

private static Stream CreateRandomStream()
{
var mockStream = new Mock<MemoryStream>();

mockStream.SetupGet(stream =>
stream.ReadTimeout)
.Returns(0);

mockStream.SetupGet(stream =>
stream.WriteTimeout)
.Returns(0);

return mockStream.Object;
}

private static ExternalAIFilesResult CreateRandomExternalAIFilesResult() =>
CreateExternalAIFilesResultFiller().Create();

private static Filler<AIFileRequest> CreateRandomAIFileRequestFiller() =>
new Filler<AIFileRequest>();

private static Filler<ExternalAIFilesResult> CreateExternalAIFilesResultFiller() =>
new Filler<ExternalAIFilesResult>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="Moq" Version="4.18.4" />
<PackageReference Include="Tynamix.ObjectFiller" Version="1.5.8" />
<PackageReference Include="WireMock.Net" Version="1.5.20" />
<PackageReference Include="xunit" Version="2.4.2" />
Expand Down

0 comments on commit 0a3411d

Please sign in to comment.