SA-29 added Unit test for OpenAiService

This commit is contained in:
shchoholiev-opflo 2023-10-14 02:13:50 +00:00
parent bdc3e658cc
commit 236153d486
6 changed files with 129 additions and 1 deletions

View File

@ -21,7 +21,8 @@
"ms-dotnettools.csharp",
"patcx.vscode-nuget-gallery",
"mhutchie.git-graph",
"fernandoescolar.vscode-solution-explorer"
"fernandoescolar.vscode-solution-explorer",
"formulahendry.dotnet-test-explorer"
]
}
}

3
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,3 @@
{
"editor.formatOnType": true
}

View File

@ -0,0 +1 @@
global using Xunit;

View File

@ -0,0 +1,86 @@
using System.Net;
using Moq;
using Moq.Protected;
using ShoppingAssistantApi.Application.IServices;
using ShoppingAssistantApi.Application.Models.OpenAi;
using ShoppingAssistantApi.Domain.Enums;
using ShoppingAssistantApi.Infrastructure.Services;
namespace ShoppingAssistantApi.UnitTests;
public class OpenAiServiceTests
{
private readonly IOpenAiService _openAiService;
private readonly Mock<HttpMessageHandler> _mockHttpMessageHandler;
private readonly HttpClient _httpClient;
public OpenAiServiceTests()
{
// Mock any dependencies
_mockHttpMessageHandler = new Mock<HttpMessageHandler>();
_httpClient = new HttpClient(_mockHttpMessageHandler.Object);
_openAiService = new OpenAiService(_httpClient);
}
[Fact]
public async Task GetChatCompletion_ValidChat_ReturnsNewMessage()
{
// Arrange
_mockHttpMessageHandler
.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.IsAny<HttpRequestMessage>(),
ItExpr.IsAny<CancellationToken>()
)
.ReturnsAsync(new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK,
Content = new StringContent(@"
{
""id"": ""chatcmpl-89OMdgTZXOLAXv7bPUJ4SwrPpS5Md"",
""object"": ""chat.completion"",
""created"": 1697249299,
""model"": ""gpt-3.5-turbo-0613"",
""choices"": [
{
""index"": 0,
""message"": {
""role"": ""assistant"",
""content"": ""Hello World!""
},
""finish_reason"": ""stop""
}
],
""usage"": {
""prompt_tokens"": 10,
""completion_tokens"": 3,
""total_tokens"": 13
}
}"),
});
var chat = new ChatCompletionRequest
{
Messages = new List<OpenAiMessage>
{
new OpenAiMessage
{
Role = OpenAiRole.User,
Content = "Return Hello World!"
}
}
};
// Act
var newMessage = await _openAiService.GetChatCompletion(chat, CancellationToken.None);
// Assert
Assert.NotNull(newMessage);
Assert.Equal("Hello, World!", newMessage.Content);
}
// TODO: Add more tests
}

View File

@ -0,0 +1,31 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.1" />
<PackageReference Include="Moq" Version="4.20.69" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.2.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ShoppingAssistantApi.Infrastructure\ShoppingAssistantApi.Infrastructure.csproj" />
<ProjectReference Include="..\ShoppingAssistantApi.Application\ShoppingAssistantApi.Application.csproj" />
</ItemGroup>
</Project>

View File

@ -15,6 +15,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ShoppingAssistantApi.Api",
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ShoppingAssistantApi.Tests", "ShoppingAssistantApi.Tests\ShoppingAssistantApi.Tests.csproj", "{297B5378-79D7-406C-80A5-151C6B3EA147}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ShoppingAssistantApi.UnitTests", "ShoppingAssistantApi.UnitTests\ShoppingAssistantApi.UnitTests.csproj", "{B4EFE8F1-89F5-44E4-BD0A-4F63D09C8E6F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -45,6 +47,10 @@ Global
{297B5378-79D7-406C-80A5-151C6B3EA147}.Debug|Any CPU.Build.0 = Debug|Any CPU
{297B5378-79D7-406C-80A5-151C6B3EA147}.Release|Any CPU.ActiveCfg = Release|Any CPU
{297B5378-79D7-406C-80A5-151C6B3EA147}.Release|Any CPU.Build.0 = Release|Any CPU
{B4EFE8F1-89F5-44E4-BD0A-4F63D09C8E6F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B4EFE8F1-89F5-44E4-BD0A-4F63D09C8E6F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B4EFE8F1-89F5-44E4-BD0A-4F63D09C8E6F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B4EFE8F1-89F5-44E4-BD0A-4F63D09C8E6F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE