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

[FormRecognizer] Implemented BoundingBox, DocumentField, and Operation live tests #24555

Merged
merged 3 commits into from
Oct 8, 2021
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

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

namespace Azure.AI.FormRecognizer.DocumentAnalysis.Tests
{
/// <summary>
/// The suite of tests for the <see cref="BoundingBox"/> struct.
/// </summary>
public class BoundingBoxTests
kinelski marked this conversation as resolved.
Show resolved Hide resolved
{
[Test]
public void IndexerThrowsWhenBoundingBoxIsDefault()
{
BoundingBox boundingBox = default;
Assert.Throws<IndexOutOfRangeException>(() => { var _ = boundingBox[0]; });
}

[Test]
public void IndexerThrowsWhenBoundingBoxIsEmpty()
{
BoundingBox boundingBox = new BoundingBox(new List<float>());
Assert.Throws<IndexOutOfRangeException>(() => { var _ = boundingBox[0]; });
}

[Test]
public void ToStringDoesNotThrowWhenBoundingBoxIsDefault()
{
BoundingBox boundingBox = default;
Assert.DoesNotThrow(() => boundingBox.ToString());
}

[Test]
public void ToStringDoesNotThrowWhenBoundingBoxIsEmpty()
{
BoundingBox boundingBox = new BoundingBox(new List<float>());
Assert.DoesNotThrow(() => boundingBox.ToString());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using NUnit.Framework;

namespace Azure.AI.FormRecognizer.DocumentAnalysis.Tests
{
/// <summary>
/// The suite of tests for the <see cref="DocumentField"/> class.
/// </summary>
public class DocumentFieldTests
kinelski marked this conversation as resolved.
Show resolved Hide resolved
{
[Test]
public void InstantiateDocumentFieldWithInt64AndNoValue()
{
var field = new DocumentField(DocumentFieldType.Int64);

Assert.AreEqual(DocumentFieldType.Int64, field.ValueType);
Assert.Throws<InvalidOperationException>(() => field.AsInt64());
}

[Test]
public void InstantiateDocumentFieldWithDoubleAndNoValue()
{
var field = new DocumentField(DocumentFieldType.Double);

Assert.AreEqual(DocumentFieldType.Double, field.ValueType);
Assert.Throws<InvalidOperationException>(() => field.AsDouble());
}

[Test]
public void InstantiateDocumentFieldWithDateAndNoValue()
{
var field = new DocumentField(DocumentFieldType.Date);

Assert.AreEqual(DocumentFieldType.Date, field.ValueType);
Assert.Throws<InvalidOperationException>(() => field.AsDate());
}

[Test]
public void InstantiateDocumentFieldWithTimeAndNoValue()
{
var field = new DocumentField(DocumentFieldType.Time);

Assert.AreEqual(DocumentFieldType.Time, field.ValueType);
Assert.Throws<InvalidOperationException>(() => field.AsTime());
}

[Test]
public void InstantiateDocumentFieldWithSelectionMarkAndNoValue()
{
var field = new DocumentField(DocumentFieldType.SelectionMark);

Assert.AreEqual(DocumentFieldType.SelectionMark, field.ValueType);
Assert.Throws<InvalidOperationException>(() => field.AsSelectionMarkState());
}

[Test]
public void InstantiateDocumentFieldWithSignatureTypeAndNoValue()
{
var field = new DocumentField(DocumentFieldType.Signature);

Assert.AreEqual(DocumentFieldType.Signature, field.ValueType);
Assert.Throws<InvalidOperationException>(() => field.AsSignatureType());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Threading.Tasks;
using Azure.Core.TestFramework;
using NUnit.Framework;

using TestFile = Azure.AI.FormRecognizer.Tests.TestFile;

namespace Azure.AI.FormRecognizer.DocumentAnalysis.Tests
{
/// <summary>
/// The suite of tests for the <see cref="Operation{T}"/> subclasses.
/// </summary>
/// <remarks>
/// These tests have a dependency on live Azure services and may incur costs for the associated
/// Azure subscription.
/// </remarks>
public class OperationsLiveTests : DocumentAnalysisLiveTestBase
{
/// <summary>
/// Initializes a new instance of the <see cref="OperationsLiveTests"/> class.
/// </summary>
/// <param name="isAsync">A flag used by the Azure Core Test Framework to differentiate between tests for asynchronous and synchronous methods.</param>
public OperationsLiveTests(bool isAsync, DocumentAnalysisClientOptions.ServiceVersion serviceVersion)
: base(isAsync, serviceVersion)
{
}

[RecordedTest]
public async Task AnalyzeDocumentOperationCanPollFromNewObject()
{
var client = CreateDocumentAnalysisClient(out var nonInstrumentedClient);
var modelId = Recording.GenerateId();

await using var _ = await CreateDisposableBuildModelAsync(modelId);

var uri = DocumentAnalysisTestEnvironment.CreateUri(TestFile.Blank);
var operation = await client.StartAnalyzeDocumentFromUriAsync(modelId, uri);

var sameOperation = InstrumentOperation(new AnalyzeDocumentOperation(operation.Id, nonInstrumentedClient));
await sameOperation.WaitForCompletionAsync();

Assert.IsTrue(sameOperation.HasValue);
Assert.AreEqual(1, sameOperation.Value.Pages.Count);
}

[RecordedTest]
public async Task BuildModelOperationCanPollFromNewObject()
kinelski marked this conversation as resolved.
Show resolved Hide resolved
{
var client = CreateDocumentModelAdministrationClient(out var nonInstrumentedClient);
var trainingFilesUri = new Uri(TestEnvironment.BlobContainerSasUrl);
var modelId = Recording.GenerateId();

var operation = await client.StartBuildModelAsync(trainingFilesUri, modelId);

var sameOperation = InstrumentOperation(new BuildModelOperation(operation.Id, nonInstrumentedClient));
await sameOperation.WaitForCompletionAsync();

Assert.IsTrue(sameOperation.HasValue);
Assert.AreEqual(modelId, sameOperation.Value.ModelId);
}

[RecordedTest]
public async Task CopyModelOperationCanPollFromNewObject()
{
var client = CreateDocumentModelAdministrationClient(out var nonInstrumentedClient);
var modelId = Recording.GenerateId();

await using var trainedModel = await CreateDisposableBuildModelAsync(modelId);

var targetModelId = Recording.GenerateId();
CopyAuthorization targetAuth = await client.GetCopyAuthorizationAsync(targetModelId);

var operation = await client.StartCopyModelAsync(trainedModel.ModelId, targetAuth);

var sameOperation = InstrumentOperation(new CopyModelOperation(operation.Id, nonInstrumentedClient));
await sameOperation.WaitForCompletionAsync();

Assert.IsTrue(sameOperation.HasValue);
Assert.AreEqual(targetModelId, sameOperation.Value.ModelId);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ namespace Azure.AI.FormRecognizer.Tests
/// These tests have a dependency on live Azure services and may incur costs for the associated
/// Azure subscription.
/// </remarks>
public class OperationsLiveTests : FormRecognizerLiveTestBase
public class FormRecognizerOperationsLiveTests : FormRecognizerLiveTestBase
kinelski marked this conversation as resolved.
Show resolved Hide resolved
{
/// <summary>
/// Initializes a new instance of the <see cref="OperationsLiveTests"/> class.
/// Initializes a new instance of the <see cref="FormRecognizerOperationsLiveTests"/> class.
/// </summary>
/// <param name="isAsync">A flag used by the Azure Core Test Framework to differentiate between tests for asynchronous and synchronous methods.</param>
public OperationsLiveTests(bool isAsync, FormRecognizerClientOptions.ServiceVersion serviceVersion)
public FormRecognizerOperationsLiveTests(bool isAsync, FormRecognizerClientOptions.ServiceVersion serviceVersion)
: base(isAsync, serviceVersion)
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ public async Task StartCreateComposedModelFailsWithInvalidId()
[TestCase(false, true)]
[TestCase(true, false)]
[TestCase(false, false)]
[Ignore("https://github.com/Azure/azure-sdk-for-net/issues/24552")]
public async Task TrainingOps(bool labeled, bool useTokenCredential)
kinelski marked this conversation as resolved.
Show resolved Hide resolved
{
var client = CreateFormTrainingClient(useTokenCredential);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,21 @@ public DocumentAnalysisLiveTestBase(bool isAsync, DocumentAnalysisClientOptions.
/// <param name="useTokenCredential">Whether or not to use a <see cref="TokenCredential"/> to authenticate. An <see cref="AzureKeyCredential"/> is used by default.</param>
/// <param name="apiKey">The API key to use for authentication. Defaults to <see cref="DocumentAnalysisTestEnvironment.ApiKey"/>.</param>
/// <returns>The instrumented <see cref="DocumentAnalysisClient" />.</returns>
protected DocumentAnalysisClient CreateDocumentAnalysisClient(bool useTokenCredential = false, string apiKey = default)
protected DocumentAnalysisClient CreateDocumentAnalysisClient(bool useTokenCredential = false, string apiKey = default) => CreateDocumentAnalysisClient(out _, useTokenCredential, apiKey);

/// <summary>
/// Creates a <see cref="DocumentAnalysisClient" /> with the endpoint and API key provided via environment
/// variables and instruments it to make use of the Azure Core Test Framework functionalities.
/// </summary>
/// <param name="nonInstrumentedClient">The non-instrumented version of the client to be used to resume LROs.</param>
/// <param name="useTokenCredential">Whether or not to use a <see cref="TokenCredential"/> to authenticate. An <see cref="AzureKeyCredential"/> is used by default.</param>
/// <param name="apiKey">The API key to use for authentication. Defaults to <see cref="DocumentAnalysisTestEnvironment.ApiKey"/>.</param>
/// <returns>The instrumented <see cref="DocumentAnalysisClient" />.</returns>
protected DocumentAnalysisClient CreateDocumentAnalysisClient(out DocumentAnalysisClient nonInstrumentedClient, bool useTokenCredential = false, string apiKey = default)
{
var endpoint = new Uri(TestEnvironment.Endpoint);
var options = InstrumentClientOptions(new DocumentAnalysisClientOptions(_serviceVersion));

DocumentAnalysisClient nonInstrumentedClient;

if (useTokenCredential)
{
nonInstrumentedClient = new DocumentAnalysisClient(endpoint, TestEnvironment.Credential, options);
Expand Down
Loading