|
| 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 System.Net; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using Azure.Core.TestFramework; |
| 9 | +using NUnit.Framework; |
| 10 | + |
| 11 | +namespace Azure.AI.DocumentIntelligence.Tests |
| 12 | +{ |
| 13 | + [PlaybackOnly("https://github.com/Azure/azure-sdk-for-net/issues/47557")] |
| 14 | + public class DocumentBatchLiveTests : DocumentIntelligenceLiveTestBase |
| 15 | + { |
| 16 | + public DocumentBatchLiveTests(bool isAsync) |
| 17 | + : base(isAsync) |
| 18 | + { |
| 19 | + } |
| 20 | + |
| 21 | + [Test] |
| 22 | + public async Task AnalyzeBatchDocuments() |
| 23 | + { |
| 24 | + var client = CreateDocumentIntelligenceClient(); |
| 25 | + var sourceContainerUri = new Uri(TestEnvironment.BatchSourceBlobSasUrl); |
| 26 | + var resultContainerUri = new Uri(TestEnvironment.BatchResultBlobSasUrl); |
| 27 | + var blobSource = new BlobContentSource(sourceContainerUri); |
| 28 | + var options = new AnalyzeBatchDocumentsOptions("prebuilt-read", blobSource, resultContainerUri) |
| 29 | + { |
| 30 | + OverwriteExisting = true |
| 31 | + }; |
| 32 | + |
| 33 | + var operation = await client.AnalyzeBatchDocumentsAsync(WaitUntil.Completed, options); |
| 34 | + |
| 35 | + Assert.That(operation.HasCompleted); |
| 36 | + Assert.That(operation.HasValue); |
| 37 | + |
| 38 | + ValidateAcordAnalyzeBatchResult(operation.Value); |
| 39 | + } |
| 40 | + |
| 41 | + [Test] |
| 42 | + public async Task GetAnalyzeBatchResult() |
| 43 | + { |
| 44 | + var client = CreateDocumentIntelligenceClient(); |
| 45 | + var sourceContainerUri = new Uri(TestEnvironment.BatchSourceBlobSasUrl); |
| 46 | + var resultContainerUri = new Uri(TestEnvironment.BatchResultBlobSasUrl); |
| 47 | + var blobSource = new BlobContentSource(sourceContainerUri); |
| 48 | + var options = new AnalyzeBatchDocumentsOptions("prebuilt-read", blobSource, resultContainerUri) |
| 49 | + { |
| 50 | + OverwriteExisting = true |
| 51 | + }; |
| 52 | + var startTime = Recording.UtcNow; |
| 53 | + |
| 54 | + var operation = await client.AnalyzeBatchDocumentsAsync(WaitUntil.Completed, options); |
| 55 | + var getResponse = await client.GetAnalyzeBatchResultAsync("prebuilt-read", operation.Id); |
| 56 | + var operationDetails = getResponse.Value; |
| 57 | + |
| 58 | + Assert.That(operationDetails.ResultId, Is.EqualTo(operation.Id)); |
| 59 | + Assert.That(operationDetails.Status, Is.EqualTo(DocumentIntelligenceOperationStatus.Succeeded)); |
| 60 | + Assert.That(operationDetails.CreatedOn, Is.GreaterThan(startTime - TimeSpan.FromHours(4))); |
| 61 | + Assert.That(operationDetails.LastUpdatedOn, Is.GreaterThanOrEqualTo(operationDetails.CreatedOn)); |
| 62 | + Assert.That(operationDetails.PercentCompleted, Is.EqualTo(100)); |
| 63 | + Assert.That(operationDetails.Error, Is.Null); |
| 64 | + |
| 65 | + DocumentAssert.AreEqual(operation.Value, operationDetails.Result); |
| 66 | + } |
| 67 | + |
| 68 | + [Test] |
| 69 | + public async Task GetAnalyzeBatchResults() |
| 70 | + { |
| 71 | + var client = CreateDocumentIntelligenceClient(); |
| 72 | + var sourceContainerUri = new Uri(TestEnvironment.BatchSourceBlobSasUrl); |
| 73 | + var resultContainerUri = new Uri(TestEnvironment.BatchResultBlobSasUrl); |
| 74 | + var blobSource = new BlobContentSource(sourceContainerUri); |
| 75 | + var options = new AnalyzeBatchDocumentsOptions("prebuilt-read", blobSource, resultContainerUri) |
| 76 | + { |
| 77 | + OverwriteExisting = true |
| 78 | + }; |
| 79 | + |
| 80 | + var operation0 = await client.AnalyzeBatchDocumentsAsync(WaitUntil.Completed, options); |
| 81 | + var operation1 = await client.AnalyzeBatchDocumentsAsync(WaitUntil.Completed, options); |
| 82 | + var getResponse0 = await client.GetAnalyzeBatchResultAsync("prebuilt-read", operation0.Id); |
| 83 | + var getResponse1 = await client.GetAnalyzeBatchResultAsync("prebuilt-read", operation1.Id); |
| 84 | + |
| 85 | + var expectedIdMapping = new Dictionary<string, AnalyzeBatchOperationDetails>() |
| 86 | + { |
| 87 | + { operation0.Id, getResponse0.Value }, |
| 88 | + { operation1.Id, getResponse1.Value } |
| 89 | + }; |
| 90 | + var idMapping = new Dictionary<string, AnalyzeBatchOperationDetails>(); |
| 91 | + |
| 92 | + await foreach (AnalyzeBatchOperationDetails operation in client.GetAnalyzeBatchResultsAsync("prebuilt-read")) |
| 93 | + { |
| 94 | + if (expectedIdMapping.ContainsKey(operation.ResultId)) |
| 95 | + { |
| 96 | + idMapping.Add(operation.ResultId, operation); |
| 97 | + } |
| 98 | + |
| 99 | + if (idMapping.Count == expectedIdMapping.Count) |
| 100 | + { |
| 101 | + break; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + Assert.That(idMapping.Count, Is.EqualTo(expectedIdMapping.Count)); |
| 106 | + |
| 107 | + foreach (string id in expectedIdMapping.Keys) |
| 108 | + { |
| 109 | + Assert.That(idMapping, Contains.Key(id)); |
| 110 | + |
| 111 | + AnalyzeBatchOperationDetails expected = expectedIdMapping[id]; |
| 112 | + AnalyzeBatchOperationDetails actual = idMapping[id]; |
| 113 | + |
| 114 | + Assert.That(actual.ResultId, Is.EqualTo(expected.ResultId)); |
| 115 | + Assert.That(actual.Status, Is.EqualTo(expected.Status)); |
| 116 | + Assert.That(actual.CreatedOn, Is.EqualTo(expected.CreatedOn)); |
| 117 | + Assert.That(actual.LastUpdatedOn, Is.EqualTo(expected.LastUpdatedOn)); |
| 118 | + Assert.That(actual.PercentCompleted, Is.Null); |
| 119 | + |
| 120 | + DocumentAssert.AreEqual(actual.Error, expected.Error); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + [Test] |
| 125 | + public async Task DeleteAnalyzeBatchResult() |
| 126 | + { |
| 127 | + var client = CreateDocumentIntelligenceClient(); |
| 128 | + var sourceContainerUri = new Uri(TestEnvironment.BatchSourceBlobSasUrl); |
| 129 | + var resultContainerUri = new Uri(TestEnvironment.BatchResultBlobSasUrl); |
| 130 | + var blobSource = new BlobContentSource(sourceContainerUri); |
| 131 | + var options = new AnalyzeBatchDocumentsOptions("prebuilt-read", blobSource, resultContainerUri) |
| 132 | + { |
| 133 | + OverwriteExisting = true |
| 134 | + }; |
| 135 | + |
| 136 | + var operation = await client.AnalyzeBatchDocumentsAsync(WaitUntil.Completed, options); |
| 137 | + var response = await client.DeleteAnalyzeBatchResultAsync("prebuilt-read", operation.Id); |
| 138 | + |
| 139 | + Assert.That(response.Status, Is.EqualTo((int)HttpStatusCode.NoContent)); |
| 140 | + } |
| 141 | + |
| 142 | + private void ValidateAcordAnalyzeBatchResult(AnalyzeBatchResult result) |
| 143 | + { |
| 144 | + Assert.That(result.SucceededCount, Is.EqualTo(6)); |
| 145 | + Assert.That(result.FailedCount, Is.EqualTo(0)); |
| 146 | + Assert.That(result.SkippedCount, Is.EqualTo(0)); |
| 147 | + |
| 148 | + string expectedSourcePrefix = string.Empty; |
| 149 | + string expectedResultPrefix = string.Empty; |
| 150 | + |
| 151 | + // We can't validate this in Playback mode since these environment variables |
| 152 | + // are not stored when recording. |
| 153 | + |
| 154 | + if (Mode == RecordedTestMode.Live) |
| 155 | + { |
| 156 | + int sourceQueryStart = TestEnvironment.BatchSourceBlobSasUrl.IndexOf('?'); |
| 157 | + int resultQueryStart = TestEnvironment.BatchResultBlobSasUrl.IndexOf('?'); |
| 158 | + |
| 159 | + expectedSourcePrefix = (sourceQueryStart == -1) |
| 160 | + ? TestEnvironment.BatchSourceBlobSasUrl |
| 161 | + : TestEnvironment.BatchSourceBlobSasUrl.Substring(0, sourceQueryStart); |
| 162 | + |
| 163 | + expectedResultPrefix = (resultQueryStart == -1) |
| 164 | + ? TestEnvironment.BatchResultBlobSasUrl |
| 165 | + : TestEnvironment.BatchResultBlobSasUrl.Substring(0, resultQueryStart); |
| 166 | + } |
| 167 | + |
| 168 | + foreach (var details in result.Details) |
| 169 | + { |
| 170 | + Assert.That(details.SourceUri.AbsoluteUri, Does.StartWith(expectedSourcePrefix)); |
| 171 | + Assert.That(details.ResultUri.AbsoluteUri, Does.StartWith(expectedResultPrefix)); |
| 172 | + Assert.That(details.Status, Is.EqualTo(DocumentIntelligenceOperationStatus.Succeeded)); |
| 173 | + Assert.That(details.Error, Is.Null); |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | +} |
0 commit comments