Skip to content

Commit 253e98d

Browse files
authored
[FormRecognizer] Implemented BoundingBox, DocumentField, and Operation live tests (#24555)
1 parent f31b41b commit 253e98d

File tree

30 files changed

+4068
-1457
lines changed

30 files changed

+4068
-1457
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using NUnit.Framework;
7+
8+
namespace Azure.AI.FormRecognizer.DocumentAnalysis.Tests
9+
{
10+
/// <summary>
11+
/// The suite of tests for the <see cref="BoundingBox"/> struct.
12+
/// </summary>
13+
public class BoundingBoxTests
14+
{
15+
[Test]
16+
public void IndexerThrowsWhenBoundingBoxIsDefault()
17+
{
18+
BoundingBox boundingBox = default;
19+
Assert.Throws<IndexOutOfRangeException>(() => { var _ = boundingBox[0]; });
20+
}
21+
22+
[Test]
23+
public void IndexerThrowsWhenBoundingBoxIsEmpty()
24+
{
25+
BoundingBox boundingBox = new BoundingBox(new List<float>());
26+
Assert.Throws<IndexOutOfRangeException>(() => { var _ = boundingBox[0]; });
27+
}
28+
29+
[Test]
30+
public void ToStringDoesNotThrowWhenBoundingBoxIsDefault()
31+
{
32+
BoundingBox boundingBox = default;
33+
Assert.DoesNotThrow(() => boundingBox.ToString());
34+
}
35+
36+
[Test]
37+
public void ToStringDoesNotThrowWhenBoundingBoxIsEmpty()
38+
{
39+
BoundingBox boundingBox = new BoundingBox(new List<float>());
40+
Assert.DoesNotThrow(() => boundingBox.ToString());
41+
}
42+
}
43+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
using NUnit.Framework;
6+
7+
namespace Azure.AI.FormRecognizer.DocumentAnalysis.Tests
8+
{
9+
/// <summary>
10+
/// The suite of tests for the <see cref="DocumentField"/> class.
11+
/// </summary>
12+
public class DocumentFieldTests
13+
{
14+
[Test]
15+
public void InstantiateDocumentFieldWithInt64AndNoValue()
16+
{
17+
var field = new DocumentField(DocumentFieldType.Int64);
18+
19+
Assert.AreEqual(DocumentFieldType.Int64, field.ValueType);
20+
Assert.Throws<InvalidOperationException>(() => field.AsInt64());
21+
}
22+
23+
[Test]
24+
public void InstantiateDocumentFieldWithDoubleAndNoValue()
25+
{
26+
var field = new DocumentField(DocumentFieldType.Double);
27+
28+
Assert.AreEqual(DocumentFieldType.Double, field.ValueType);
29+
Assert.Throws<InvalidOperationException>(() => field.AsDouble());
30+
}
31+
32+
[Test]
33+
public void InstantiateDocumentFieldWithDateAndNoValue()
34+
{
35+
var field = new DocumentField(DocumentFieldType.Date);
36+
37+
Assert.AreEqual(DocumentFieldType.Date, field.ValueType);
38+
Assert.Throws<InvalidOperationException>(() => field.AsDate());
39+
}
40+
41+
[Test]
42+
public void InstantiateDocumentFieldWithTimeAndNoValue()
43+
{
44+
var field = new DocumentField(DocumentFieldType.Time);
45+
46+
Assert.AreEqual(DocumentFieldType.Time, field.ValueType);
47+
Assert.Throws<InvalidOperationException>(() => field.AsTime());
48+
}
49+
50+
[Test]
51+
public void InstantiateDocumentFieldWithSelectionMarkAndNoValue()
52+
{
53+
var field = new DocumentField(DocumentFieldType.SelectionMark);
54+
55+
Assert.AreEqual(DocumentFieldType.SelectionMark, field.ValueType);
56+
Assert.Throws<InvalidOperationException>(() => field.AsSelectionMarkState());
57+
}
58+
59+
[Test]
60+
public void InstantiateDocumentFieldWithSignatureTypeAndNoValue()
61+
{
62+
var field = new DocumentField(DocumentFieldType.Signature);
63+
64+
Assert.AreEqual(DocumentFieldType.Signature, field.ValueType);
65+
Assert.Throws<InvalidOperationException>(() => field.AsSignatureType());
66+
}
67+
}
68+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
using System.Threading.Tasks;
6+
using Azure.Core.TestFramework;
7+
using NUnit.Framework;
8+
9+
using TestFile = Azure.AI.FormRecognizer.Tests.TestFile;
10+
11+
namespace Azure.AI.FormRecognizer.DocumentAnalysis.Tests
12+
{
13+
/// <summary>
14+
/// The suite of tests for the <see cref="Operation{T}"/> subclasses.
15+
/// </summary>
16+
/// <remarks>
17+
/// These tests have a dependency on live Azure services and may incur costs for the associated
18+
/// Azure subscription.
19+
/// </remarks>
20+
public class OperationsLiveTests : DocumentAnalysisLiveTestBase
21+
{
22+
/// <summary>
23+
/// Initializes a new instance of the <see cref="OperationsLiveTests"/> class.
24+
/// </summary>
25+
/// <param name="isAsync">A flag used by the Azure Core Test Framework to differentiate between tests for asynchronous and synchronous methods.</param>
26+
public OperationsLiveTests(bool isAsync, DocumentAnalysisClientOptions.ServiceVersion serviceVersion)
27+
: base(isAsync, serviceVersion)
28+
{
29+
}
30+
31+
[RecordedTest]
32+
public async Task AnalyzeDocumentOperationCanPollFromNewObject()
33+
{
34+
var client = CreateDocumentAnalysisClient(out var nonInstrumentedClient);
35+
var modelId = Recording.GenerateId();
36+
37+
await using var _ = await CreateDisposableBuildModelAsync(modelId);
38+
39+
var uri = DocumentAnalysisTestEnvironment.CreateUri(TestFile.Blank);
40+
var operation = await client.StartAnalyzeDocumentFromUriAsync(modelId, uri);
41+
42+
var sameOperation = InstrumentOperation(new AnalyzeDocumentOperation(operation.Id, nonInstrumentedClient));
43+
await sameOperation.WaitForCompletionAsync();
44+
45+
Assert.IsTrue(sameOperation.HasValue);
46+
Assert.AreEqual(1, sameOperation.Value.Pages.Count);
47+
}
48+
49+
[RecordedTest]
50+
public async Task BuildModelOperationCanPollFromNewObject()
51+
{
52+
var client = CreateDocumentModelAdministrationClient(out var nonInstrumentedClient);
53+
var trainingFilesUri = new Uri(TestEnvironment.BlobContainerSasUrl);
54+
var modelId = Recording.GenerateId();
55+
56+
var operation = await client.StartBuildModelAsync(trainingFilesUri, modelId);
57+
58+
var sameOperation = InstrumentOperation(new BuildModelOperation(operation.Id, nonInstrumentedClient));
59+
await sameOperation.WaitForCompletionAsync();
60+
61+
Assert.IsTrue(sameOperation.HasValue);
62+
Assert.AreEqual(modelId, sameOperation.Value.ModelId);
63+
}
64+
65+
[RecordedTest]
66+
public async Task CopyModelOperationCanPollFromNewObject()
67+
{
68+
var client = CreateDocumentModelAdministrationClient(out var nonInstrumentedClient);
69+
var modelId = Recording.GenerateId();
70+
71+
await using var trainedModel = await CreateDisposableBuildModelAsync(modelId);
72+
73+
var targetModelId = Recording.GenerateId();
74+
CopyAuthorization targetAuth = await client.GetCopyAuthorizationAsync(targetModelId);
75+
76+
var operation = await client.StartCopyModelAsync(trainedModel.ModelId, targetAuth);
77+
78+
var sameOperation = InstrumentOperation(new CopyModelOperation(operation.Id, nonInstrumentedClient));
79+
await sameOperation.WaitForCompletionAsync();
80+
81+
Assert.IsTrue(sameOperation.HasValue);
82+
Assert.AreEqual(targetModelId, sameOperation.Value.ModelId);
83+
}
84+
}
85+
}

sdk/formrecognizer/Azure.AI.FormRecognizer/tests/FormRecognizerModels/OperationsLiveTests.cs renamed to sdk/formrecognizer/Azure.AI.FormRecognizer/tests/FormRecognizerModels/FormRecognizerOperationsLiveTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ namespace Azure.AI.FormRecognizer.Tests
1818
/// These tests have a dependency on live Azure services and may incur costs for the associated
1919
/// Azure subscription.
2020
/// </remarks>
21-
public class OperationsLiveTests : FormRecognizerLiveTestBase
21+
public class FormRecognizerOperationsLiveTests : FormRecognizerLiveTestBase
2222
{
2323
/// <summary>
24-
/// Initializes a new instance of the <see cref="OperationsLiveTests"/> class.
24+
/// Initializes a new instance of the <see cref="FormRecognizerOperationsLiveTests"/> class.
2525
/// </summary>
2626
/// <param name="isAsync">A flag used by the Azure Core Test Framework to differentiate between tests for asynchronous and synchronous methods.</param>
27-
public OperationsLiveTests(bool isAsync, FormRecognizerClientOptions.ServiceVersion serviceVersion)
27+
public FormRecognizerOperationsLiveTests(bool isAsync, FormRecognizerClientOptions.ServiceVersion serviceVersion)
2828
: base(isAsync, serviceVersion)
2929
{
3030
}

sdk/formrecognizer/Azure.AI.FormRecognizer/tests/FormTrainingClient/FormTrainingClientLiveTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ public async Task StartCreateComposedModelFailsWithInvalidId()
317317
[TestCase(false, true)]
318318
[TestCase(true, false)]
319319
[TestCase(false, false)]
320+
[Ignore("https://github.com/Azure/azure-sdk-for-net/issues/24552")]
320321
public async Task TrainingOps(bool labeled, bool useTokenCredential)
321322
{
322323
var client = CreateFormTrainingClient(useTokenCredential);

sdk/formrecognizer/Azure.AI.FormRecognizer/tests/Infrastructure/DocumentAnalysisLiveTestBase.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,21 @@ public DocumentAnalysisLiveTestBase(bool isAsync, DocumentAnalysisClientOptions.
3232
/// <param name="useTokenCredential">Whether or not to use a <see cref="TokenCredential"/> to authenticate. An <see cref="AzureKeyCredential"/> is used by default.</param>
3333
/// <param name="apiKey">The API key to use for authentication. Defaults to <see cref="DocumentAnalysisTestEnvironment.ApiKey"/>.</param>
3434
/// <returns>The instrumented <see cref="DocumentAnalysisClient" />.</returns>
35-
protected DocumentAnalysisClient CreateDocumentAnalysisClient(bool useTokenCredential = false, string apiKey = default)
35+
protected DocumentAnalysisClient CreateDocumentAnalysisClient(bool useTokenCredential = false, string apiKey = default) => CreateDocumentAnalysisClient(out _, useTokenCredential, apiKey);
36+
37+
/// <summary>
38+
/// Creates a <see cref="DocumentAnalysisClient" /> with the endpoint and API key provided via environment
39+
/// variables and instruments it to make use of the Azure Core Test Framework functionalities.
40+
/// </summary>
41+
/// <param name="nonInstrumentedClient">The non-instrumented version of the client to be used to resume LROs.</param>
42+
/// <param name="useTokenCredential">Whether or not to use a <see cref="TokenCredential"/> to authenticate. An <see cref="AzureKeyCredential"/> is used by default.</param>
43+
/// <param name="apiKey">The API key to use for authentication. Defaults to <see cref="DocumentAnalysisTestEnvironment.ApiKey"/>.</param>
44+
/// <returns>The instrumented <see cref="DocumentAnalysisClient" />.</returns>
45+
protected DocumentAnalysisClient CreateDocumentAnalysisClient(out DocumentAnalysisClient nonInstrumentedClient, bool useTokenCredential = false, string apiKey = default)
3646
{
3747
var endpoint = new Uri(TestEnvironment.Endpoint);
3848
var options = InstrumentClientOptions(new DocumentAnalysisClientOptions(_serviceVersion));
3949

40-
DocumentAnalysisClient nonInstrumentedClient;
41-
4250
if (useTokenCredential)
4351
{
4452
nonInstrumentedClient = new DocumentAnalysisClient(endpoint, TestEnvironment.Credential, options);

0 commit comments

Comments
 (0)