Skip to content

Commit

Permalink
add AAD and improve polling for playback (Azure#17296)
Browse files Browse the repository at this point in the history
  • Loading branch information
maririos authored and annelo-msft committed Feb 17, 2021
1 parent e350bcb commit 99ab9a2
Show file tree
Hide file tree
Showing 23 changed files with 1,394 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,33 @@ public AnalyzeOperationTests(bool isAsync) : base(isAsync) { }
}
};

[Test]
public async Task AnalyzeOperationWithAADTest()
{
TextAnalyticsClient client = GetClient(useTokenCredential: true);

AnalyzeOperationOptions operationOptions = new AnalyzeOperationOptions()
{
KeyPhrasesTaskParameters = new KeyPhrasesTaskParameters(),
};

AnalyzeOperation operation = await client.StartAnalyzeOperationBatchAsync(batchConvenienceDocuments, operationOptions);

await operation.WaitForCompletionAsync(PollingInterval);

AnalyzeOperationResult resultCollection = operation.Value;

IReadOnlyList<EntityRecognitionTasksItem> entityRecognitionTasksItemCollection = resultCollection.Tasks.EntityRecognitionTasks;
IReadOnlyList<EntityRecognitionPiiTasksItem> entityRecognitionPiiTasksItemCollection = resultCollection.Tasks.EntityRecognitionPiiTasks;
ExtractKeyPhrasesResultCollection keyPhrasesResult = resultCollection.Tasks.KeyPhraseExtractionTasks[0].Results;

Assert.IsNotNull(keyPhrasesResult);
Assert.IsNotNull(entityRecognitionTasksItemCollection);
Assert.IsNotNull(entityRecognitionPiiTasksItemCollection);

Assert.AreEqual(2, keyPhrasesResult.Count);
}

[Test]
public async Task AnalyzeOperationTest()
{
Expand All @@ -43,7 +70,7 @@ public async Task AnalyzeOperationTest()

AnalyzeOperation operation = await client.StartAnalyzeOperationBatchAsync(batchConvenienceDocuments, operationOptions, "en");

await operation.WaitForCompletionAsync();
await operation.WaitForCompletionAsync(PollingInterval);

AnalyzeOperationResult resultCollection = operation.Value;

Expand Down Expand Up @@ -97,7 +124,7 @@ public async Task AnalyzeOperationWithLanguageTest()

AnalyzeOperation operation = await client.StartAnalyzeOperationBatchAsync(batchDocuments, operationOptions);

await operation.WaitForCompletionAsync();
await operation.WaitForCompletionAsync(PollingInterval);

AnalyzeOperationResult resultCollection = operation.Value;

Expand Down Expand Up @@ -150,7 +177,7 @@ public async Task AnalyzeOperationWithMultipleTasks()

AnalyzeOperation operation = await client.StartAnalyzeOperationBatchAsync(batchDocuments, operationOptions);

await operation.WaitForCompletionAsync();
await operation.WaitForCompletionAsync(PollingInterval);

AnalyzeOperationResult resultCollection = operation.Value;

Expand Down Expand Up @@ -220,7 +247,7 @@ public async Task AnalyzeOperationWithSkipParameter()

AnalyzeOperation operation = await client.StartAnalyzeOperationBatchAsync(batchConvenienceDocuments, operationOptions, "en");

await operation.WaitForCompletionAsync();
await operation.WaitForCompletionAsync(PollingInterval);

AnalyzeOperationResult resultCollection = operation.Value;

Expand Down Expand Up @@ -253,7 +280,7 @@ public async Task AnalyzeOperationWithTopParameter()

AnalyzeOperation operation = await client.StartAnalyzeOperationBatchAsync(batchConvenienceDocuments, operationOptions, "en");

await operation.WaitForCompletionAsync();
await operation.WaitForCompletionAsync(PollingInterval);

AnalyzeOperationResult resultCollection = operation.Value;

Expand Down Expand Up @@ -323,7 +350,7 @@ public async Task AnalyzeOperationBatchWithPHIDomain()

AnalyzeOperation operation = await client.StartAnalyzeOperationBatchAsync(documents, operationOptions, "en");

await operation.WaitForCompletionAsync();
await operation.WaitForCompletionAsync(PollingInterval);

AnalyzeOperationResult resultCollection = operation.Value;

Expand Down Expand Up @@ -368,7 +395,7 @@ public async Task AnalyzeOperationBatchWithStatisticsTest()

AnalyzeOperation operation = await client.StartAnalyzeOperationBatchAsync(batchDocuments, operationOptions);

await operation.WaitForCompletionAsync();
await operation.WaitForCompletionAsync(PollingInterval);

AnalyzeOperationResult resultCollection = operation.Value;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,19 @@ public AnalyzeSentimentTests(bool isAsync) : base(isAsync) { }
}
};

[Test]
public async Task AnalyzeSentimentWithAADTest()
{
TextAnalyticsClient client = GetClient(useTokenCredential: true);
string document = singleEnglish;

DocumentSentiment docSentiment = await client.AnalyzeSentimentAsync(document);

CheckAnalyzeSentimentProperties(docSentiment);
Assert.AreEqual("Positive", docSentiment.Sentiment.ToString());
Assert.AreEqual("Positive", docSentiment.Sentences.FirstOrDefault().Sentiment.ToString());
}

[Test]
public async Task AnalyzeSentimentTest()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,20 @@ public DetectLanguageTests(bool isAsync) : base(isAsync) { }
}
};

[Test]
[Test]
public async Task DetectLanguageWithAADTest()
{
TextAnalyticsClient client = GetClient(useTokenCredential: true);
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 DetectLanguageTest()
{
TextAnalyticsClient client = GetClient();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,19 @@ public ExtractKeyPhrasesTests(bool isAsync) : base(isAsync) { }
}
};

[Test]
public async Task ExtractKeyPhrasesWithAADTest()
{
TextAnalyticsClient client = GetClient(useTokenCredential: true);
string document = singleEnglish;

KeyPhraseCollection keyPhrases = await client.ExtractKeyPhrasesAsync(document);

Assert.AreEqual(2, keyPhrases.Count);
Assert.IsTrue(keyPhrases.Contains("cat"));
Assert.IsTrue(keyPhrases.Contains("veterinarian"));
}

[Test]
public async Task ExtractKeyPhrasesTest()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,30 @@ namespace Azure.AI.TextAnalytics.Tests
{
public class TextAnalyticsClientLiveTestBase : RecordedTestBase<TextAnalyticsTestEnvironment>
{
protected TimeSpan PollingInterval => TimeSpan.FromSeconds(Mode == RecordedTestMode.Playback ? 0 : 1);

public TextAnalyticsClientLiveTestBase(bool isAsync) : base(isAsync)
{
Sanitizer = new TextAnalyticsRecordedTestSanitizer();
}

public TextAnalyticsClient GetClient(AzureKeyCredential credential = default, TextAnalyticsClientOptions options = default)
public TextAnalyticsClient GetClient(
AzureKeyCredential credential = default,
TextAnalyticsClientOptions options = default,
bool useTokenCredential = default)
{
string apiKey = TestEnvironment.ApiKey;
credential ??= new AzureKeyCredential(apiKey);
var endpoint = new Uri(TestEnvironment.Endpoint);
options ??= new TextAnalyticsClientOptions();
return InstrumentClient(
new TextAnalyticsClient(
new Uri(TestEnvironment.Endpoint),
credential,
InstrumentClientOptions(options))
);

if (useTokenCredential)
{
return new TextAnalyticsClient(endpoint, TestEnvironment.Credential, InstrumentClientOptions(options));
}
else
{
credential ??= new AzureKeyCredential(TestEnvironment.ApiKey);
return new TextAnalyticsClient(endpoint, credential, InstrumentClientOptions(options));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,24 @@ public RecognizeEntitiesTests(bool isAsync) : base(isAsync) { }
}
};

[Test]
public async Task RecognizeEntitiesWithAADTest()
{
TextAnalyticsClient client = GetClient(useTokenCredential: true);
string document = singleEnglish;

CategorizedEntityCollection entities = await client.RecognizeEntitiesAsync(document);

Assert.AreEqual(3, entities.Count);

var entitiesList = new List<string> { "Bill Gates", "Microsoft", "Paul Allen" };
foreach (CategorizedEntity entity in entities)
{
Assert.IsTrue(entitiesList.Contains(entity.Text));
Assert.IsNotNull(entity.ConfidenceScore);
}
}

[Test]
public async Task RecognizeEntitiesTest()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public async Task RecognizeHealthcareEntitiesTest()

HealthcareOperation operation = await client.StartHealthcareAsync(document);

await operation.WaitForCompletionAsync();
await operation.WaitForCompletionAsync(PollingInterval);

RecognizeHealthcareEntitiesResultCollection resultCollection = operation.Value;

Expand Down Expand Up @@ -94,7 +94,7 @@ public async Task RecognizeHealthcareEntitiesWithLanguageTest()

HealthcareOperation operation = await client.StartHealthcareBatchAsync(new List<string>() { document }, "en");

await operation.WaitForCompletionAsync();
await operation.WaitForCompletionAsync(PollingInterval);

RecognizeHealthcareEntitiesResultCollection resultCollection = operation.Value;

Expand All @@ -117,7 +117,7 @@ public async Task RecognizeHealthcareEntitiesWithTopParameter()

HealthcareOperation operation = await client.StartHealthcareBatchAsync(batchDocuments, options);

await operation.WaitForCompletionAsync();
await operation.WaitForCompletionAsync(PollingInterval);

RecognizeHealthcareEntitiesResultCollection resultCollection = operation.Value;

Expand All @@ -141,7 +141,7 @@ public async Task RecognizeHealthcareEntitiesWithSkipParameter()

HealthcareOperation operation = await client.StartHealthcareBatchAsync(batchDocuments, options);

await operation.WaitForCompletionAsync();
await operation.WaitForCompletionAsync(PollingInterval);

RecognizeHealthcareEntitiesResultCollection resultCollection = operation.Value;

Expand All @@ -168,7 +168,7 @@ public async Task RecognizeHealthcareEntitiesBatchWithErrorTest()

HealthcareOperation operation = await client.StartHealthcareBatchAsync(documents);

await operation.WaitForCompletionAsync();
await operation.WaitForCompletionAsync(PollingInterval);

RecognizeHealthcareEntitiesResultCollection resultCollection = operation.Value;

Expand All @@ -186,7 +186,7 @@ public async Task RecognizeHealthcareEntitiesBatchConvenienceTest()

HealthcareOperation operation = await client.StartHealthcareBatchAsync(documents);

await operation.WaitForCompletionAsync();
await operation.WaitForCompletionAsync(PollingInterval);

RecognizeHealthcareEntitiesResultCollection resultCollection = operation.Value;

Expand All @@ -206,7 +206,7 @@ public async Task RecognizeHealthcareEntitiesBatchConvenienceWithStatisticsTest(

HealthcareOperation operation = await client.StartHealthcareBatchAsync(documents, "en", options);

await operation.WaitForCompletionAsync();
await operation.WaitForCompletionAsync(PollingInterval);

RecognizeHealthcareEntitiesResultCollection resultCollection = operation.Value;

Expand All @@ -230,7 +230,7 @@ public async Task RecognizeHealthcareEntitiesBatchTest()

HealthcareOperation operation = await client.StartHealthcareBatchAsync(documents);

await operation.WaitForCompletionAsync();
await operation.WaitForCompletionAsync(PollingInterval);

RecognizeHealthcareEntitiesResultCollection resultCollection = operation.Value;

Expand All @@ -250,7 +250,7 @@ public async Task RecognizeHealthcareEntitiesBatchWithStatisticsTest()

HealthcareOperation operation = await client.StartHealthcareBatchAsync(documents, options);

await operation.WaitForCompletionAsync();
await operation.WaitForCompletionAsync(PollingInterval);

RecognizeHealthcareEntitiesResultCollection resultCollection = operation.Value;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ public RecognizeLinkedEntitiesTests(bool isAsync) : base(isAsync) { }
}
};

[Test]
public async Task RecognizeLinkedEntitiesWithAADTest()
{
TextAnalyticsClient client = GetClient(useTokenCredential: true);
string document = singleEnglish;

LinkedEntityCollection linkedEntities = await client.RecognizeLinkedEntitiesAsync(document);
Assert.AreEqual(3, linkedEntities.Count);
}

[Test]
public async Task RecognizeLinkedEntitiesTest()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ public RecognizePiiEntitiesTests(bool isAsync) : base(isAsync) { }
}
};

[Test]
public async Task RecognizePiiEntitiesWithAADTest()
{
TextAnalyticsClient client = GetClient(useTokenCredential: true);
string document = singleEnglish;

PiiEntityCollection entities = await client.RecognizePiiEntitiesAsync(document);

Assert.AreEqual(2, entities.Count);
Assert.IsNotNull(entities.RedactedText);
}

[Test]
public async Task RecognizePiiEntitiesTest()
{
Expand Down
Loading

0 comments on commit 99ab9a2

Please sign in to comment.