Skip to content

Commit

Permalink
[FormRecognizer] Samples test fix (#11728)
Browse files Browse the repository at this point in the history
* Created FRLiveTestBase

* Make previously ignored tests work (samples)
  • Loading branch information
kinelski authored May 4, 2020
1 parent 96ad305 commit e554e75
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 48 deletions.
2 changes: 2 additions & 0 deletions sdk/formrecognizer/Azure.AI.FormRecognizer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ foreach (FormPage page in formPages.Value)
Recognize and extract form fields and other content from your custom forms, using models you train with your own form types.

```C# Snippet:FormRecognizerSample3RecognizeCustomFormsFromUri
string modelId = "<modelId>";

Response<IReadOnlyList<RecognizedForm>> forms = await client.StartRecognizeCustomFormsFromUri(modelId, new Uri(formUri)).WaitForCompletionAsync();
foreach (RecognizedForm form in forms.Value)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ var client = new FormRecognizerClient(new Uri(endpoint), credential);
To recognize form fields and other content from your custom forms from a given file at a URI, use the `StartRecognizeCustomFormsFromUri` method. The returned value is a collection of `RecognizedForm` objects -- one for each page in the submitted document.

```C# Snippet:FormRecognizerSample3RecognizeCustomFormsFromUri
string modelId = "<modelId>";

Response<IReadOnlyList<RecognizedForm>> forms = await client.StartRecognizeCustomFormsFromUri(modelId, new Uri(formUri)).WaitForCompletionAsync();
foreach (RecognizedForm form in forms.Value)
{
Expand All @@ -48,6 +50,8 @@ To recognize form fields and other content from your custom forms from a file st
```C# Snippet:FormRecognizerRecognizeCustomFormsFromFile
using (FileStream stream = new FileStream(formFilePath, FileMode.Open))
{
string modelId = "<modelId>";

Response<IReadOnlyList<RecognizedForm>> forms = await client.StartRecognizeCustomForms(modelId, stream).WaitForCompletionAsync();
/*
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using System.Threading.Tasks;
using Azure.AI.FormRecognizer.Models;
using Azure.AI.FormRecognizer.Training;
using Azure.Core.TestFramework;
using NUnit.Framework;
using NUnit.Framework.Internal;

Expand All @@ -21,16 +20,14 @@ 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 FormRecognizerClientLiveTests : RecordedTestBase<FormRecognizerTestEnvironment>
public class FormRecognizerClientLiveTests : FormRecognizerLiveTestBase
{
/// <summary>
/// Initializes a new instance of the <see cref="FormRecognizerClientLiveTests"/> 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 FormRecognizerClientLiveTests(bool isAsync) : base(isAsync)
{
Sanitizer = new FormRecognizerRecordedTestSanitizer();
Matcher = new FormRecognizerRecordMatcher();
}

/// <summary>
Expand All @@ -49,22 +46,6 @@ private FormRecognizerClient CreateInstrumentedFormRecognizerClient()
return InstrumentClient(client);
}

/// <summary>
/// Creates a <see cref="FormTrainingClient" /> with the endpoint and API key provided via environment
/// variables and instruments it to make use of the Azure Core Test Framework functionalities.
/// </summary>
/// <returns>The instrumented <see cref="FormTrainingClient" />.</returns>
private FormTrainingClient CreateInstrumentedFormTrainingClient()
{
var endpoint = new Uri(TestEnvironment.Endpoint);
var credential = new AzureKeyCredential(TestEnvironment.ApiKey);

var options = Recording.InstrumentClientOptions(new FormRecognizerClientOptions());
var client = new FormTrainingClient(endpoint, credential, options);

return InstrumentClient(client);
}

/// <summary>
/// Verifies that the <see cref="FormRecognizerClient" /> is able to connect to the Form
/// Recognizer cognitive service and perform operations.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,40 +17,22 @@ 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 FormTrainingClientLiveTests : RecordedTestBase<FormRecognizerTestEnvironment>
public class FormTrainingClientLiveTests : FormRecognizerLiveTestBase
{
/// <summary>
/// Initializes a new instance of the <see cref="FormTrainingClientLiveTests"/> 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 FormTrainingClientLiveTests(bool isAsync) : base(isAsync)
{
Sanitizer = new FormRecognizerRecordedTestSanitizer();
Matcher = new FormRecognizerRecordMatcher();
}

/// <summary>
/// Creates a <see cref="FormTrainingClient" /> with the endpoint and API key provided via environment
/// variables and instruments it to make use of the Azure Core Test Framework functionalities.
/// </summary>
/// <returns>The instrumented <see cref="FormTrainingClient" />.</returns>
private FormTrainingClient CreateInstrumentedClient()
{
var endpoint = new Uri(TestEnvironment.Endpoint);
var credential = new AzureKeyCredential(TestEnvironment.ApiKey);

var options = Recording.InstrumentClientOptions(new FormRecognizerClientOptions());
var client = new FormTrainingClient(endpoint, credential, options);

return InstrumentClient(client);
}

[Test]
[TestCase(true)]
[TestCase(false)]
public async Task StartTraining(bool labeled)
{
var client = CreateInstrumentedClient();
var client = CreateInstrumentedFormTrainingClient();
var trainingFiles = new Uri(TestEnvironment.BlobContainerSasUrl);
TrainingOperation operation;

Expand Down Expand Up @@ -100,7 +82,7 @@ public async Task StartTraining(bool labeled)
[Test]
public async Task StartTrainingError()
{
var client = CreateInstrumentedClient();
var client = CreateInstrumentedFormTrainingClient();

var containerUrl = new Uri("https://someUrl");

Expand Down Expand Up @@ -128,7 +110,7 @@ public async Task StartTrainingError()
[TestCase(false)]
public async Task TrainingOps(bool labeled)
{
var client = CreateInstrumentedClient();
var client = CreateInstrumentedFormTrainingClient();
var trainingFiles = new Uri(TestEnvironment.BlobContainerSasUrl);
TrainingOperation operation;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using Azure.AI.FormRecognizer.Training;
using Azure.Core.TestFramework;

namespace Azure.AI.FormRecognizer.Tests
{
public class FormRecognizerLiveTestBase : RecordedTestBase<FormRecognizerTestEnvironment>
{
public FormRecognizerLiveTestBase(bool isAsync) : base(isAsync)
{
Sanitizer = new FormRecognizerRecordedTestSanitizer();
Matcher = new FormRecognizerRecordMatcher();
}

/// <summary>
/// Creates a <see cref="FormTrainingClient" /> with the endpoint and API key provided via environment
/// variables and instruments it to make use of the Azure Core Test Framework functionalities.
/// </summary>
/// <returns>The instrumented <see cref="FormTrainingClient" />.</returns>
protected FormTrainingClient CreateInstrumentedFormTrainingClient()
{
var endpoint = new Uri(TestEnvironment.Endpoint);
var credential = new AzureKeyCredential(TestEnvironment.ApiKey);

var options = Recording.InstrumentClientOptions(new FormRecognizerClientOptions());
var client = new FormTrainingClient(endpoint, credential, options);

return InstrumentClient(client);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Threading.Tasks;
using Azure.AI.FormRecognizer.Models;
using Azure.AI.FormRecognizer.Tests;
using Azure.AI.FormRecognizer.Training;
using Azure.Core.TestFramework;
using NUnit.Framework;

Expand All @@ -15,16 +16,26 @@ namespace Azure.AI.FormRecognizer.Samples
public partial class FormRecognizerSamples : SamplesBase<FormRecognizerTestEnvironment>
{
[Test]
[Ignore("Need to revisit how to pass the modelId. Issue https://github.com/Azure/azure-sdk-for-net/issues/11493")]
public async Task RecognizeCustomFormsFromFile()
{
string endpoint = TestEnvironment.Endpoint;
string apiKey = TestEnvironment.ApiKey;
string trainingFileUrl = TestEnvironment.BlobContainerSasUrl;

// Firstly, create a trained model we can use to recognize the custom form. Please note that
// models can also be trained using a graphical user interface such as the Form Recognizer
// Labeling Tool found here:
// https://docs.microsoft.com/azure/cognitive-services/form-recognizer/quickstarts/label-tool

FormTrainingClient trainingClient = new FormTrainingClient(new Uri(endpoint), new AzureKeyCredential(apiKey));
CustomFormModel model = await trainingClient.StartTraining(new Uri(trainingFileUrl)).WaitForCompletionAsync();

// Proceed with the custom form recognition.

FormRecognizerClient client = new FormRecognizerClient(new Uri(endpoint), new AzureKeyCredential(apiKey));

string formFilePath = FormRecognizerTestEnvironment.CreatePath("Form_1.jpg");
string modelId = "<your model id>";
string modelId = model.ModelId;

using (FileStream stream = new FileStream(formFilePath, FileMode.Open))
{
Expand All @@ -46,6 +57,9 @@ public async Task RecognizeCustomFormsFromFile()
}
}
}

// Delete the model on completion to clean environment.
trainingClient.DeleteModel(model.ModelId);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Threading.Tasks;
using Azure.AI.FormRecognizer.Models;
using Azure.AI.FormRecognizer.Tests;
using Azure.AI.FormRecognizer.Training;
using Azure.Core.TestFramework;
using NUnit.Framework;

Expand All @@ -14,18 +15,29 @@ namespace Azure.AI.FormRecognizer.Samples
public partial class FormRecognizerSamples : SamplesBase<FormRecognizerTestEnvironment>
{
[Test]
[Ignore("Need to revisit how to pass the modelId. Issue https://github.com/Azure/azure-sdk-for-net/issues/11493")]
public async Task RecognizeCustomFormsFromUri()
{
string endpoint = TestEnvironment.Endpoint;
string apiKey = TestEnvironment.ApiKey;
string trainingFileUrl = TestEnvironment.BlobContainerSasUrl;

// Firstly, create a trained model we can use to recognize the custom form. Please note that
// models can also be trained using a graphical user interface such as the Form Recognizer
// Labeling Tool found here:
// https://docs.microsoft.com/azure/cognitive-services/form-recognizer/quickstarts/label-tool

FormTrainingClient trainingClient = new FormTrainingClient(new Uri(endpoint), new AzureKeyCredential(apiKey));
CustomFormModel model = await trainingClient.StartTraining(new Uri(trainingFileUrl)).WaitForCompletionAsync();

// Proceed with the custom form recognition.

FormRecognizerClient client = new FormRecognizerClient(new Uri(endpoint), new AzureKeyCredential(apiKey));

string formUri = FormRecognizerTestEnvironment.CreateUri("Form_1.jpg");
string modelId = "<your model id>";
string modelId = model.ModelId;

#region Snippet:FormRecognizerSample3RecognizeCustomFormsFromUri
//@@ string modelId = "<modelId>";

Response<IReadOnlyList<RecognizedForm>> forms = await client.StartRecognizeCustomFormsFromUri(modelId, new Uri(formUri)).WaitForCompletionAsync();
foreach (RecognizedForm form in forms.Value)
Expand All @@ -45,6 +57,9 @@ public async Task RecognizeCustomFormsFromUri()
}
}
#endregion

// Delete the model on completion to clean environment.
trainingClient.DeleteModel(model.ModelId);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,27 +112,39 @@ public async Task RecognizeReceiptFromFile()
}

[Test]
[Ignore("Need to revisit how to pass the modelId. Issue https://github.com/Azure/azure-sdk-for-net/issues/11493")]
public async Task RecognizeCustomFormsFromFile()
{
string endpoint = TestEnvironment.Endpoint;
string apiKey = TestEnvironment.ApiKey;
string trainingFileUrl = TestEnvironment.BlobContainerSasUrl;

// Firstly, create a trained model we can use to recognize the custom form.

FormTrainingClient trainingClient = new FormTrainingClient(new Uri(endpoint), new AzureKeyCredential(apiKey));
CustomFormModel model = await trainingClient.StartTraining(new Uri(trainingFileUrl)).WaitForCompletionAsync();

// Proceed with the custom form recognition.

var credential = new AzureKeyCredential(apiKey);
var client = new FormRecognizerClient(new Uri(endpoint), credential);

string formFilePath = FormRecognizerTestEnvironment.CreatePath("Form_1.jpg");
string modelId = "<your model id>";
string modelId = model.ModelId;

#region Snippet:FormRecognizerRecognizeCustomFormsFromFile
using (FileStream stream = new FileStream(formFilePath, FileMode.Open))
{
//@@ string modelId = "<modelId>";

Response<IReadOnlyList<RecognizedForm>> forms = await client.StartRecognizeCustomForms(modelId, stream).WaitForCompletionAsync();
/*
*
*/
}
#endregion

// Delete the model on completion to clean environment.
trainingClient.DeleteModel(model.ModelId);
}
}
}

0 comments on commit e554e75

Please sign in to comment.