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] Document model administration tests cleanup (1/2) #35747

Merged
merged 1 commit into from
Apr 25, 2023
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
Expand Up @@ -2,7 +2,6 @@
// Licensed under the MIT License.

using System.Collections.Generic;
using Azure.Core;

namespace Azure.AI.FormRecognizer.DocumentAnalysis
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,43 +46,6 @@ public async Task GetResourceDetails()
Assert.IsNotNull(resourceDetails.CustomDocumentModelLimit);
}

[RecordedTest]
[TestCase(true)]
[TestCase(false)]
public async Task AdminOps(bool useTokenCredential)
kinelski marked this conversation as resolved.
Show resolved Hide resolved
{
var client = CreateDocumentModelAdministrationClient(useTokenCredential);
var trainingFilesUri = new Uri(TestEnvironment.BlobContainerSasUrl);
var modelId = Recording.GenerateId();
var description = "This is a test model.";

var options = new BuildDocumentModelOptions()
{
Description = description
};

foreach (var tag in _testingTags)
{
options.Tags.Add(tag);
}

BuildDocumentModelOperation operation = await client.BuildDocumentModelAsync(WaitUntil.Completed, trainingFilesUri, DocumentBuildMode.Template, modelId, options: options);
DocumentModelDetails resultModel = await client.GetDocumentModelAsync(modelId);

ValidateDocumentModelDetails(resultModel, description, _testingTags);

DocumentModelSummary modelSummary = client.GetDocumentModelsAsync().ToEnumerableAsync().Result
.FirstOrDefault(m => m.ModelId == modelId);

Assert.NotNull(modelSummary);

ValidateDocumentModelSummary(modelSummary, description, _testingTags);

await client.DeleteDocumentModelAsync(modelId);

Assert.ThrowsAsync<RequestFailedException>(() => client.GetDocumentModelAsync(modelId));
}

[RecordedTest]
[TestCase(true)]
[TestCase(false)]
Expand Down Expand Up @@ -218,33 +181,5 @@ private void ValidateDocumentModelDetails(DocumentModelDetails model, string des

// TODO add validation for Doctypes https://github.com/Azure/azure-sdk-for-net-pr/issues/1432
}

private void ValidateDocumentModelSummary(DocumentModelSummary model, string description = null, IReadOnlyDictionary<string, string> tags = null)
kinelski marked this conversation as resolved.
Show resolved Hide resolved
{
if (description != null)
{
Assert.AreEqual(description, model.Description);
}

if (tags != null)
{
CollectionAssert.AreEquivalent(tags, model.Tags);
}

Assert.IsNotNull(model.ModelId);
Assert.AreNotEqual(default(DateTimeOffset), model.CreatedOn);

if (_serviceVersion >= DocumentAnalysisClientOptions.ServiceVersion.V2023_02_28_Preview)
{
if (model.ExpiresOn.HasValue)
{
Assert.Greater(model.ExpiresOn, model.CreatedOn);
}
}
else
{
Assert.Null(model.ExpiresOn);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public async Task BuildDocumentClassifierCanAuthenticateWithTokenCredential()
await using var disposableClassifier = await BuildDisposableDocumentClassifierAsync(classifierId);

// Sanity check to make sure we got an actual response back from the service.
Assert.AreEqual(classifierId, disposableClassifier.Value.ClassifierId);
Assert.AreEqual(classifierId, disposableClassifier.ClassifierId);
}

[RecordedTest]
Expand Down Expand Up @@ -84,7 +84,7 @@ public async Task BuildDocumentClassifierWithAzureBlobContentSource()
Assert.Greater(classifier.CreatedOn, startTime);
Assert.Greater(classifier.ExpiresOn, classifier.CreatedOn);

AssertDocumentTypesAreEqual(documentTypes, classifier.DocumentTypes);
AssertDocumentTypeDictionariesAreEquivalent(documentTypes, classifier.DocumentTypes);
kinelski marked this conversation as resolved.
Show resolved Hide resolved
}

[RecordedTest]
Expand Down Expand Up @@ -129,7 +129,7 @@ public async Task BuildDocumentClassifierWithAzureBlobFileListSource()
Assert.Greater(classifier.CreatedOn, startTime);
Assert.Greater(classifier.ExpiresOn, classifier.CreatedOn);

AssertDocumentTypesAreEqual(documentTypes, classifier.DocumentTypes);
AssertDocumentTypeDictionariesAreEquivalent(documentTypes, classifier.DocumentTypes);
}

#endregion Build
Expand All @@ -143,7 +143,7 @@ public async Task GetDocumentClassifier(bool useTokenCredential)
{
var client = CreateDocumentModelAdministrationClient(useTokenCredential);
var classifierId = Recording.GenerateId();
var description = "This classifier was generated by a .NET test.";
var description = $"This classifier was generated by a .NET test: {nameof(GetDocumentClassifier)}";
kinelski marked this conversation as resolved.
Show resolved Hide resolved
await using var disposableClassifier = await BuildDisposableDocumentClassifierAsync(classifierId, description);

DocumentClassifierDetails expected = disposableClassifier.Value;
Expand All @@ -155,7 +155,7 @@ public async Task GetDocumentClassifier(bool useTokenCredential)
Assert.AreEqual(expected.CreatedOn, classifier.CreatedOn);
Assert.AreEqual(expected.ExpiresOn, classifier.ExpiresOn);

AssertDocumentTypesAreEqual(expected.DocumentTypes, classifier.DocumentTypes);
AssertDocumentTypeDictionariesAreEquivalent(expected.DocumentTypes, classifier.DocumentTypes);
}

[RecordedTest]
Expand All @@ -179,16 +179,16 @@ public async Task GetDocumentClassifiers(bool useTokenCredential)
{
var client = CreateDocumentModelAdministrationClient(useTokenCredential);
var classifierIds = new string[] { Recording.GenerateId(), Recording.GenerateId() };
var description = "This classifier was generated by a .NET test.";
var description = $"This classifier was generated by a .NET test: {nameof(GetDocumentClassifiers)}";

await using var disposableClassifier0 = await BuildDisposableDocumentClassifierAsync(classifierIds[0], description);
await using var disposableClassifier1 = await BuildDisposableDocumentClassifierAsync(classifierIds[1], description);

var idMapping = new Dictionary<string, DocumentClassifierDetails>();
var expectedIdMapping = new Dictionary<string, DocumentClassifierDetails>()
{
{ disposableClassifier0.Value.ClassifierId, disposableClassifier0.Value },
{ disposableClassifier1.Value.ClassifierId, disposableClassifier1.Value }
{ classifierIds[0], disposableClassifier0.Value },
{ classifierIds[1], disposableClassifier1.Value }
kinelski marked this conversation as resolved.
Show resolved Hide resolved
};

await foreach (DocumentClassifierDetails classifier in client.GetDocumentClassifiersAsync())
Expand Down Expand Up @@ -217,7 +217,7 @@ public async Task GetDocumentClassifiers(bool useTokenCredential)
Assert.AreEqual(expected.CreatedOn, classifier.CreatedOn);
Assert.AreEqual(expected.ExpiresOn, classifier.ExpiresOn);

AssertDocumentTypesAreEqual(expected.DocumentTypes, classifier.DocumentTypes);
AssertDocumentTypeDictionariesAreEquivalent(expected.DocumentTypes, classifier.DocumentTypes);
}
}

Expand Down Expand Up @@ -252,7 +252,7 @@ public void DeleteDocumentClassifierThrowsWhenClassifierDoesNotExist()

#endregion Delete

private void AssertDocumentTypesAreEqual(IReadOnlyDictionary<string, ClassifierDocumentTypeDetails> docTypes1, IReadOnlyDictionary<string, ClassifierDocumentTypeDetails> docTypes2)
private void AssertDocumentTypeDictionariesAreEquivalent(IReadOnlyDictionary<string, ClassifierDocumentTypeDetails> docTypes1, IReadOnlyDictionary<string, ClassifierDocumentTypeDetails> docTypes2)
{
Assert.AreEqual(docTypes1.Count, docTypes2.Count);

Expand Down
Loading