Skip to content

Commit 6ca6350

Browse files
committed
test: Add write API tests
1 parent 1530850 commit 6ca6350

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

DeepLTests/BaseDeepLTest.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ static BaseDeepLTest() {
4343
};
4444
}
4545

46+
protected static DeepLClient CreateTestClient(bool randomAuthKey = false) {
47+
var authKey = randomAuthKey ? Guid.NewGuid().ToString() : AuthKey;
48+
return ServerUrl == null
49+
? new DeepLClient(authKey)
50+
: new DeepLClient(authKey, new DeepLClientOptions { ServerUrl = ServerUrl });
51+
}
52+
4653
protected static Translator CreateTestTranslator(bool randomAuthKey = false) {
4754
var authKey = randomAuthKey ? Guid.NewGuid().ToString() : AuthKey;
4855
return ServerUrl == null

DeepLTests/RephraseTextTest.cs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright 2025 DeepL SE (https://www.deepl.com)
2+
// Use of this source code is governed by an MIT
3+
// license that can be found in the LICENSE file.
4+
5+
using System.Threading.Tasks;
6+
using DeepL;
7+
using DeepL.Model;
8+
using Xunit;
9+
10+
namespace DeepLTests {
11+
public sealed class RephraseTextTest : BaseDeepLTest {
12+
[Fact]
13+
public async Task TestSingleText() {
14+
var client = CreateTestClient();
15+
var inputText = ExampleText("en");
16+
var result = await client.RephraseTextAsync(ExampleText("en"), LanguageCode.EnglishAmerican);
17+
checkSanityOfImprovements(inputText, result);
18+
}
19+
20+
[Fact]
21+
public async Task TestTextArray() {
22+
var client = CreateTestClient();
23+
var texts = new[] { ExampleText("en"), ExampleText("en") };
24+
var inputText = ExampleText("en");
25+
var results = await client.RephraseTextAsync(texts, LanguageCode.EnglishAmerican);
26+
foreach (var result in results) {
27+
checkSanityOfImprovements(inputText, result);
28+
}
29+
}
30+
31+
[RealServerOnlyFact]
32+
public async Task TestBusinessStyle() {
33+
var client = CreateTestClient();
34+
var inputText = "As Gregor Samsa awoke one morning from uneasy dreams he found himself transformed in his bed into a gigantic insect.";
35+
var result = await client.RephraseTextAsync(
36+
inputText, LanguageCode.EnglishAmerican, new TextRephraseOptions { WritingStyle = "business" }
37+
);
38+
checkSanityOfImprovements(inputText, result);
39+
}
40+
41+
private void checkSanityOfImprovements(
42+
string inputText,
43+
WriteResult result,
44+
string expectedSourceLangUppercase="EN",
45+
string expectedTargetLangUppercase="EN-US",
46+
float epsilon=0.2f) {
47+
Assert.Equal(expectedSourceLangUppercase, result.DetectedSourceLanguageCode.ToUpper());
48+
Assert.Equal(expectedTargetLangUppercase, result.TargetLanguageCode.ToUpper());
49+
var ratio = ((float) result.Text.Length) / inputText.Length;
50+
Assert.True(1 / (1.0 + epsilon) <= ratio, $"Rephrased text is too short compared to input text.\n{inputText}\n{result.Text}");
51+
Assert.True(ratio <= (1.0 + epsilon), $"Rephrased text is too long compared to input text.\n{inputText}\n{result.Text}");
52+
}
53+
}
54+
}

0 commit comments

Comments
 (0)