-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathChatGptTranslatorServiceTests.cs
83 lines (69 loc) · 2.77 KB
/
ChatGptTranslatorServiceTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using OpenAI.ChatGpt.Modules.Translator;
namespace OpenAI.ChatGpt.IntegrationTests;
public class ChatGptTranslatorServiceTests
{
private readonly IOpenAiClient _client;
public ChatGptTranslatorServiceTests()
{
_client = new OpenAiClient(Helpers.GetOpenAiKey());
}
[Fact]
public async Task Translate_from_English_to_Russian()
{
// Arrange
var sourceLanguage = "English";
var targetLanguage = "Russian";
var textToTranslate = "Hello, world!";
// Act
var translatedText = await _client.TranslateText(textToTranslate, sourceLanguage, targetLanguage);
// Assert
translatedText.Should().NotBeNullOrEmpty();
translatedText.Should().NotBe(textToTranslate);
var englishCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
translatedText.Should().NotContainAny(englishCharacters.Select(c => new string(new []{ c })));
// Check at least some characters are in the Cyrillic script
translatedText.Any(ch => ch >= 0x0400 && ch <= 0x04FF).Should().BeTrue();
}
[Fact]
public async Task Translate_object_from_English_to_Russian()
{
// Arrange
var sourceLanguage = "English";
var targetLanguage = "Russian";
var objectToTranslate = new Order(
Guid.NewGuid(),
new List<Order.Item>
{
new(1,"Book", 5),
new(2,"Pen", 10),
new(3,"Notebook", 3)
}
);
var englishRussianDictionary = new Dictionary<string, string>
{
{ "Book", "Книга" },
{ "Pen", "Ручка" },
{ "Notebook", "Тетрадь" }
};
// Act
var translatedObject = await _client.TranslateObject(objectToTranslate, sourceLanguage, targetLanguage);
// Assert
translatedObject.Should().NotBeNull();
translatedObject.Id.Should().Be(objectToTranslate.Id);
translatedObject.Items.Should().HaveCount(objectToTranslate.Items.Count);
foreach (var originalItem in objectToTranslate.Items)
{
var translatedItem = translatedObject.Items.FirstOrDefault(i => i.Id == originalItem.Id);
translatedItem.Should().NotBeNull();
translatedItem!.Name.Should().NotBe(originalItem.Name);
translatedItem.Quantity.Should().Be(originalItem.Quantity);
// Check the translation
englishRussianDictionary.TryGetValue(originalItem.Name, out var expectedRussianName);
translatedItem.Name.Should().Be(expectedRussianName);
}
}
private record Order(Guid Id, List<Order.Item> Items)
{
public record Item(int Id, string Name, int Quantity);
}
}