Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TA] Test suit cleanup #14684

Merged
merged 1 commit into from
Aug 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

240 changes: 240 additions & 0 deletions sdk/textanalytics/Azure.AI.TextAnalytics/tests/DetectLanguageTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using NUnit.Framework;

namespace Azure.AI.TextAnalytics.Tests
{
public class DetectLanguageTests : TextAnalyticsClientLiveTestBase
{
public DetectLanguageTests(bool isAsync) : base(isAsync) { }

private const string singleEnglish = "This is written in English.";
private const string singleSpanish = "Este documento está en español";

private static List<string> batchConvenienceDocuments = new List<string>
{
"Hello world",
"Bonjour tout le monde",
"Hola mundo"
};

private static List<DetectLanguageInput> batchDocuments = new List<DetectLanguageInput>
{
new DetectLanguageInput("1", "Hello world")
{
CountryHint = "us",
},
new DetectLanguageInput("2", "Bonjour tout le monde")
{
CountryHint = "fr",
},
new DetectLanguageInput("3", "Hola mundo")
{
CountryHint = "es",
},
new DetectLanguageInput("4", ":) :( :D")
{
CountryHint = "us",
}
};

[Test]
public async Task DetectLanguageTest()
{
TextAnalyticsClient client = GetClient();
string document = singleEnglish;

DetectedLanguage language = await client.DetectLanguageAsync(document);

Assert.IsNotNull(language.Name);
Assert.IsNotNull(language.Iso6391Name);
Assert.Greater(language.ConfidenceScore, 0.0);
}

[Test]
public async Task DetectLanguageWithCountryHintTest()
{
TextAnalyticsClient client = GetClient();
string document = singleSpanish;

DetectedLanguage language = await client.DetectLanguageAsync(document, "CO");

Assert.IsNotNull(language.Name);
Assert.IsNotNull(language.Iso6391Name);
Assert.Greater(language.ConfidenceScore, 0.0);
}

[Test]
public void DetectLanguageWithErrorCountryHintTest()
{
TextAnalyticsClient client = GetClient();
string document = singleSpanish;

RequestFailedException ex = Assert.ThrowsAsync<RequestFailedException>(() => client.DetectLanguageAsync(document, "COLOMBIA"));
Assert.AreEqual(TextAnalyticsErrorCode.InvalidCountryHint, ex.ErrorCode);
}

[Test]
public async Task DetectLanguageWithNoneCountryHintTest()
{
TextAnalyticsClient client = GetClient();
string document = singleSpanish;

DetectedLanguage language = await client.DetectLanguageAsync(document, DetectLanguageInput.None);
Assert.IsNotNull(language.Name);
Assert.IsNotNull(language.Iso6391Name);
Assert.Greater(language.ConfidenceScore, 0.0);
}

[Test]
public async Task DetectLanguageWithNoneDefaultCountryHintTest()
{
var options = new TextAnalyticsClientOptions()
{
DefaultCountryHint = DetectLanguageInput.None
};

TextAnalyticsClient client = GetClient(options: options);
string document = singleSpanish;

DetectedLanguage language = await client.DetectLanguageAsync(document, DetectLanguageInput.None);
Assert.IsNotNull(language.Name);
Assert.IsNotNull(language.Iso6391Name);
Assert.Greater(language.ConfidenceScore, 0.0);
}

[Test]
public async Task DetectLanguageBatchConvenienceTest()
{
TextAnalyticsClient client = GetClient();
List<string> documents = batchConvenienceDocuments;

DetectLanguageResultCollection results = await client.DetectLanguageBatchAsync(documents, options: new TextAnalyticsRequestOptions() { ModelVersion = "2019-10-01" });

Assert.AreEqual("English", results[0].PrimaryLanguage.Name);
Assert.AreEqual("French", results[1].PrimaryLanguage.Name);
Assert.AreEqual("Spanish", results[2].PrimaryLanguage.Name);

Assert.AreEqual(0, results[0].Statistics.CharacterCount);
Assert.AreEqual(0, results[0].Statistics.TransactionCount);
Assert.IsNull(results.Statistics);
}

[Test]
public async Task DetectLanguageBatchConvenienceWithStatisticsTest()
{
TextAnalyticsClient client = GetClient();
List<string> documents = batchConvenienceDocuments;

var options = new TextAnalyticsRequestOptions()
{
IncludeStatistics = true,
ModelVersion = "2019-10-01"
};

DetectLanguageResultCollection results = await client.DetectLanguageBatchAsync(documents, "us", options);

Assert.AreEqual("English", results[0].PrimaryLanguage.Name);
Assert.AreEqual("French", results[1].PrimaryLanguage.Name);
Assert.AreEqual("Spanish", results[2].PrimaryLanguage.Name);

Assert.IsNotNull(results.Statistics);
Assert.Greater(results.Statistics.DocumentCount, 0);
Assert.Greater(results.Statistics.TransactionCount, 0);
Assert.GreaterOrEqual(results.Statistics.InvalidDocumentCount, 0);
Assert.GreaterOrEqual(results.Statistics.ValidDocumentCount, 0);

Assert.IsNotNull(results[0].Statistics);
Assert.Greater(results[0].Statistics.CharacterCount, 0);
Assert.Greater(results[0].Statistics.TransactionCount, 0);
}

[Test]
public async Task DetectLanguageBatchTest()
{
TextAnalyticsClient client = GetClient();
List<DetectLanguageInput> documents = batchDocuments;

DetectLanguageResultCollection results = await client.DetectLanguageBatchAsync(documents, options: new TextAnalyticsRequestOptions() { ModelVersion = "2019-10-01" });

Assert.AreEqual("English", results[0].PrimaryLanguage.Name);
Assert.AreEqual("French", results[1].PrimaryLanguage.Name);
Assert.AreEqual("Spanish", results[2].PrimaryLanguage.Name);
Assert.AreEqual("(Unknown)", results[3].PrimaryLanguage.Name);
}

[Test]
public async Task DetectLanguageBatchWithStatisticsTest()
{
TextAnalyticsClient client = GetClient();
List<DetectLanguageInput> documents = batchDocuments;

var options = new TextAnalyticsRequestOptions()
{
IncludeStatistics = true,
ModelVersion = "2019-10-01"
};

DetectLanguageResultCollection results = await client.DetectLanguageBatchAsync(documents, options: options);

Assert.AreEqual("English", results[0].PrimaryLanguage.Name);
Assert.AreEqual("French", results[1].PrimaryLanguage.Name);
Assert.AreEqual("Spanish", results[2].PrimaryLanguage.Name);
Assert.AreEqual("(Unknown)", results[3].PrimaryLanguage.Name);
Assert.IsNotNull(results[0].Statistics);
Assert.IsNotNull(results[0].Statistics.CharacterCount);
Assert.IsNotNull(results[0].Statistics.TransactionCount);
}

[Test]
public async Task DetectLanguageBatchWithErrorTest()
{
TextAnalyticsClient client = GetClient();
var documents = new List<string>
{
"Hello world",
"",
"Hola mundo"
};

DetectLanguageResultCollection results = await client.DetectLanguageBatchAsync(documents);

Assert.IsTrue(!results[0].HasError);
Assert.IsTrue(!results[2].HasError);

var exceptionMessage = "Cannot access result for document 1, due to error InvalidDocument: Document text is empty.";
Assert.IsTrue(results[1].HasError);
InvalidOperationException ex = Assert.Throws<InvalidOperationException>(() => results[1].PrimaryLanguage.GetType());
Assert.AreEqual(exceptionMessage, ex.Message);
}

[Test]
public void DetectLanguageBatchWithNullIdTest()
{
TextAnalyticsClient client = GetClient();
var documents = new List<DetectLanguageInput> { new DetectLanguageInput(null, "Hello world") };

RequestFailedException ex = Assert.ThrowsAsync<RequestFailedException>(
async () => await client.DetectLanguageBatchAsync(documents, options: new TextAnalyticsRequestOptions() { ModelVersion = "2019-10-01" }));
Assert.AreEqual(TextAnalyticsErrorCode.InvalidDocument, ex.ErrorCode);
}

[Test]
public async Task DetectLanguageBatchWithNullTextTest()
{
TextAnalyticsClient client = GetClient();
var documents = new List<DetectLanguageInput> { new DetectLanguageInput("1", null) };

DetectLanguageResultCollection results = await client.DetectLanguageBatchAsync(documents, options: new TextAnalyticsRequestOptions() { ModelVersion = "2019-10-01" });

var exceptionMessage = "Cannot access result for document 1, due to error InvalidDocument: Document text is empty.";
Assert.IsTrue(results[0].HasError);
InvalidOperationException ex = Assert.Throws<InvalidOperationException>(() => results[0].PrimaryLanguage.GetType());
Assert.AreEqual(exceptionMessage, ex.Message);
}
}
}
Loading