Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rodion-m committed Jul 13, 2023
1 parent 6cc79d8 commit 11658b7
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 60 deletions.
6 changes: 6 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,10 @@
<Version>2.7.1</Version>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>

<ItemGroup>
<InternalsVisibleTo Include="OpenAI.ChatGpt.UnitTests"/>
<InternalsVisibleTo Include="OpenAI.ChatGpt.IntegrationTests"/>
<InternalsVisibleTo Include="DynamicProxyGenAssembly2"/>
</ItemGroup>
</Project>
40 changes: 20 additions & 20 deletions OpenAI.ChatGpt.Modules.Translator/ChatGPTTranslatorService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,19 +88,19 @@ public async Task<string> TranslateText(
rawResponseGetter,
cancellationToken);
return response;

string CreateTextTranslationPrompt(string sourceLanguage, string targetLanguage)
{
ArgumentNullException.ThrowIfNull(sourceLanguage);
ArgumentNullException.ThrowIfNull(targetLanguage);
return $"I want you to act as a translator from {sourceLanguage} to {targetLanguage}. " +
"The user provides with a sentence and you translate it. " +
"In the response write ONLY translated text." +
(_extraPrompt is not null ? "\n" + _extraPrompt : "");
}
}

public async Task<TObject> TranslateObject<TObject>(
internal virtual string CreateTextTranslationPrompt(string sourceLanguage, string targetLanguage)
{
ArgumentNullException.ThrowIfNull(sourceLanguage);
ArgumentNullException.ThrowIfNull(targetLanguage);
return $"I want you to act as a translator from {sourceLanguage} to {targetLanguage}. " +
"The user provides with a sentence and you translate it. " +
"In the response write ONLY translated text." +
(_extraPrompt is not null ? "\n" + _extraPrompt : "");
}

public virtual async Task<TObject> TranslateObject<TObject>(
TObject objectToTranslate,
string? sourceLanguage = null,
string? targetLanguage = null,
Expand Down Expand Up @@ -144,14 +144,14 @@ public async Task<TObject> TranslateObject<TObject>(
cancellationToken
);
return response;
string CreateObjectTranslationPrompt(string sourceLanguage, string targetLanguage)
{
ArgumentNullException.ThrowIfNull(sourceLanguage);
ArgumentNullException.ThrowIfNull(targetLanguage);
return $"I want you to act as a translator from {sourceLanguage} to {targetLanguage}. " +
"The user provides you with an object in json. You translate only the text fields that need to be translated. " +
(_extraPrompt is not null ? "\n" + _extraPrompt : "");
}
}

internal string CreateObjectTranslationPrompt(string sourceLanguage, string targetLanguage)
{
ArgumentNullException.ThrowIfNull(sourceLanguage);
ArgumentNullException.ThrowIfNull(targetLanguage);
return $"I want you to act as a translator from {sourceLanguage} to {targetLanguage}. " +
"The user provides you with an object in json. You translate only the text fields that need to be translated. " +
(_extraPrompt is not null ? "\n" + _extraPrompt : "");
}
}
14 changes: 0 additions & 14 deletions OpenAI_DotNet.sln
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ChatGpt.TelegramBotExample"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenAI.ChatGpt.Modules.Translator", "OpenAI.ChatGpt.Modules.Translator\OpenAI.ChatGpt.Modules.Translator.csproj", "{E155D31C-0061-40A3-AD54-93B5DD08836B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenAI.ChatGpt.Modules.Translator.UnitTests", "tests\OpenAI.ChatGpt.Modules.Translator.UnitTests\OpenAI.ChatGpt.Modules.Translator.UnitTests.csproj", "{49F18714-F5F9-4FFC-A674-39CE166466A6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenAI.ChatGpt.Modules.Translator.IntegrationTests", "tests\OpenAI.ChatGpt.Modules.Translator.IntegrationTests\OpenAI.ChatGpt.Modules.Translator.IntegrationTests.csproj", "{600195A2-6E93-46AF-87A7-EA2E48E5AE24}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenAI.Tests.Shared", "tests\OpenAI.Tests.Shared\OpenAI.Tests.Shared.csproj", "{E303F270-6091-47DE-9260-DAD6122005A7}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenAI.ChatGpt.Modules.StructuredResponse", "OpenAI.ChatGpt.Modules.StructuredResponse\OpenAI.ChatGpt.Modules.StructuredResponse.csproj", "{F2968A66-5672-439E-823E-D35100CA067D}"
Expand Down Expand Up @@ -81,14 +77,6 @@ Global
{E155D31C-0061-40A3-AD54-93B5DD08836B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E155D31C-0061-40A3-AD54-93B5DD08836B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E155D31C-0061-40A3-AD54-93B5DD08836B}.Release|Any CPU.Build.0 = Release|Any CPU
{49F18714-F5F9-4FFC-A674-39CE166466A6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{49F18714-F5F9-4FFC-A674-39CE166466A6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{49F18714-F5F9-4FFC-A674-39CE166466A6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{49F18714-F5F9-4FFC-A674-39CE166466A6}.Release|Any CPU.Build.0 = Release|Any CPU
{600195A2-6E93-46AF-87A7-EA2E48E5AE24}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{600195A2-6E93-46AF-87A7-EA2E48E5AE24}.Debug|Any CPU.Build.0 = Debug|Any CPU
{600195A2-6E93-46AF-87A7-EA2E48E5AE24}.Release|Any CPU.ActiveCfg = Release|Any CPU
{600195A2-6E93-46AF-87A7-EA2E48E5AE24}.Release|Any CPU.Build.0 = Release|Any CPU
{E303F270-6091-47DE-9260-DAD6122005A7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E303F270-6091-47DE-9260-DAD6122005A7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E303F270-6091-47DE-9260-DAD6122005A7}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand All @@ -108,8 +96,6 @@ Global
{983A565C-5AE2-4F76-8B7B-7C5C051072C7} = {926D63B6-9F6A-45A1-B5B7-5F36352C23AB}
{88213D5C-AADC-4F03-ACA3-7ADDCCE87DF4} = {80DB142C-5A1B-431C-BD85-19C83C6FC0A3}
{8C86E60A-77C2-4204-AF36-F4B845474016} = {80DB142C-5A1B-431C-BD85-19C83C6FC0A3}
{49F18714-F5F9-4FFC-A674-39CE166466A6} = {926D63B6-9F6A-45A1-B5B7-5F36352C23AB}
{600195A2-6E93-46AF-87A7-EA2E48E5AE24} = {926D63B6-9F6A-45A1-B5B7-5F36352C23AB}
{E303F270-6091-47DE-9260-DAD6122005A7} = {926D63B6-9F6A-45A1-B5B7-5F36352C23AB}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using FluentAssertions;
using OpenAI.Tests.Shared;
using OpenAI.ChatGpt.Modules.Translator;

namespace OpenAI.ChatGpt.Modules.Translator.IntegrationTests;
namespace OpenAI.ChatGpt.IntegrationTests;

public class ChatGptTranslatorServiceTests
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using FluentAssertions;
using Moq;
using OpenAI.ChatGpt.Models.ChatCompletion;
using OpenAI.ChatGpt.Models.ChatCompletion.Messaging;
using OpenAI.ChatGpt.Modules.Translator;

namespace OpenAI.ChatGpt.Modules.Translator.UnitTests;
namespace OpenAI.ChatGpt.UnitTests;

public class ChatGptTranslatorServiceTests
{
Expand Down Expand Up @@ -46,31 +44,29 @@ public async Task Translate_without_source_and_target_languages_uses_default_lan
It.IsAny<Action<ChatCompletionRequest>>(),
It.IsAny<Action<ChatCompletionResponse>>(),
It.IsAny<CancellationToken>()))
.ReturnsAsync("Привет, мир!")
.ReturnsAsync("Привет, мир!");

var translatorServiceMock = new Mock<ChatGPTTranslatorService>(
(IOpenAiClient) clientMock.Object,
expectedSourceLanguage,
expectedTargetLanguage,
null);

translatorServiceMock.Setup(service => service.CreateTextTranslationPrompt(
It.IsAny<string>(), It.IsAny<string>()))
.Returns($"{expectedSourceLanguage} => {expectedTargetLanguage}")
.Verifiable();
var translatorService = new ChatGPTTranslatorService(
clientMock.Object,
defaultSourceLanguage: expectedSourceLanguage,
defaultTargetLanguage: expectedTargetLanguage);

var translatorService = translatorServiceMock.Object;

// Act
var translatedText = await translatorService.TranslateText(textToTranslate);
_ = await translatorService.TranslateText(textToTranslate);

// Assert
clientMock.Verify(client => client.GetChatCompletions(
It.Is<UserOrSystemMessage>(dialog =>
dialog.GetMessages().Any(
message => message.Role == "system" &&
message.Content.Contains($"I want you to act as a translator from {expectedSourceLanguage} to {expectedTargetLanguage}"))),
It.IsAny<int>(),
It.IsAny<string>(),
It.IsAny<float>(),
It.IsAny<string>(),
It.IsAny<Action<ChatCompletionRequest>>(),
It.IsAny<Action<ChatCompletionResponse>>(),
It.IsAny<CancellationToken>()),
translatorServiceMock.Verify(service =>
service.CreateTextTranslationPrompt(
expectedSourceLanguage, expectedTargetLanguage),
Times.Once);
translatedText.Should().Be("Привет, мир!");
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.5" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="Moq" Version="4.18.4" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down

0 comments on commit 11658b7

Please sign in to comment.