|
| 1 | +using ContentUnderstanding.Common; |
| 2 | +using ContentUnderstanding.Common.Extensions; |
| 3 | +using ConversationalFieldExtraction.Interfaces; |
| 4 | +using ConversationalFieldExtraction.Services; |
| 5 | +using Microsoft.Extensions.Configuration; |
| 6 | +using Microsoft.Extensions.DependencyInjection; |
| 7 | +using Microsoft.Extensions.Hosting; |
| 8 | +using System.Text.Json; |
| 9 | + |
| 10 | +namespace AzureAiContentUnderstanding.Tests |
| 11 | +{ |
| 12 | + public class ConversationalConversationalFieldExtractionIntegrationTest |
| 13 | + { |
| 14 | + private readonly IConversationalFieldExtractionService service; |
| 15 | + |
| 16 | + public ConversationalConversationalFieldExtractionIntegrationTest() |
| 17 | + { |
| 18 | + var host = Host.CreateDefaultBuilder() |
| 19 | + .ConfigureServices((context, services) => |
| 20 | + { |
| 21 | + if (string.IsNullOrWhiteSpace(context.Configuration.GetValue<string>("AZURE_CU_CONFIG:Endpoint"))) |
| 22 | + { |
| 23 | + throw new ArgumentException("Endpoint must be provided in appsettings.json."); |
| 24 | + } |
| 25 | + if (string.IsNullOrWhiteSpace(context.Configuration.GetValue<string>("AZURE_CU_CONFIG:ApiVersion"))) |
| 26 | + { |
| 27 | + throw new ArgumentException("API version must be provided in appsettings.json."); |
| 28 | + } |
| 29 | + services.AddConfigurations(opts => |
| 30 | + { |
| 31 | + context.Configuration.GetSection("AZURE_CU_CONFIG").Bind(opts); |
| 32 | + // This header is used for sample usage telemetry, please comment out this line if you want to opt out. |
| 33 | + opts.UserAgent = "azure-ai-content-understanding-dotnet/conversational_field_extraction"; |
| 34 | + }); |
| 35 | + services.AddTokenProvider(); |
| 36 | + services.AddHttpClient<AzureContentUnderstandingClient>(); |
| 37 | + services.AddSingleton<IConversationalFieldExtractionService, ConversationalFieldExtractionService>(); |
| 38 | + }) |
| 39 | + .Build(); |
| 40 | + |
| 41 | + service = host.Services.GetService<IConversationalFieldExtractionService>()!; |
| 42 | + } |
| 43 | + |
| 44 | + [Fact(DisplayName = "Conversational Field Extraction Integration Test")] |
| 45 | + [Trait("Category", "Integration")] |
| 46 | + public async Task RunAsync() |
| 47 | + { |
| 48 | + Exception? serviceException = null; |
| 49 | + try |
| 50 | + { |
| 51 | + var ExtractionTemplates = new Dictionary<string, (string, string)> |
| 52 | + { |
| 53 | + { "call_recording_pretranscribe_batch", ("./analyzer_templates/call_recording_analytics_text.json", "./data/batch_pretranscribed.json") }, |
| 54 | + { "call_recording_pretranscribe_fast", ("./analyzer_templates/call_recording_analytics_text.json", "./data/fast_pretranscribed.json") }, |
| 55 | + { "call_recording_pretranscribe_cu", ("./analyzer_templates/call_recording_analytics_text.json", "./data/cu_pretranscribed.json") } |
| 56 | + }; |
| 57 | + var analyzerId = $"conversational-field-extraction-sample-{Guid.NewGuid()}"; |
| 58 | + |
| 59 | + foreach (var item in ExtractionTemplates) |
| 60 | + { |
| 61 | + // Extract the template path and sample file path from the dictionary |
| 62 | + var (analyzerTemplatePath, analyzerSampleFilePath) = ExtractionTemplates[item.Key]; |
| 63 | + |
| 64 | + // Create the analyzer from the template |
| 65 | + await CreateAnalyzerFromTemplateAsync(analyzerId, analyzerTemplatePath); |
| 66 | + |
| 67 | + // Extract fields using the created analyzer |
| 68 | + await ExtractFieldsWithAnalyzerAsync(analyzerId, analyzerSampleFilePath); |
| 69 | + |
| 70 | + // Clean up the analyzer after use |
| 71 | + await service.DeleteAnalyzerAsync(analyzerId); |
| 72 | + } |
| 73 | + } |
| 74 | + catch (Exception ex) |
| 75 | + { |
| 76 | + serviceException = ex; |
| 77 | + } |
| 78 | + |
| 79 | + // Assert that no exceptions were thrown during the test. |
| 80 | + Assert.Null(serviceException); |
| 81 | + } |
| 82 | + |
| 83 | + private async Task CreateAnalyzerFromTemplateAsync(string analyzerId, string analyzerTemplatePath) |
| 84 | + { |
| 85 | + // Implementation for creating an analyzer from a template |
| 86 | + var resultJson = await service.CreateAnalyzerFromTemplateAsync(analyzerId, analyzerTemplatePath); |
| 87 | + Assert.NotNull(resultJson); |
| 88 | + Assert.True(resultJson.RootElement.TryGetProperty("result", out JsonElement result)); |
| 89 | + Assert.True(result.TryGetProperty("warnings", out var warnings)); |
| 90 | + Assert.False(warnings.EnumerateArray().Any(), "The warnings array should be empty"); |
| 91 | + Assert.True(result.TryGetProperty("status", out JsonElement status)); |
| 92 | + Assert.Equal("ready", status.ToString()); |
| 93 | + Assert.True(result.TryGetProperty("mode", out JsonElement mode)); |
| 94 | + Assert.Equal("standard", mode.ToString()); |
| 95 | + Assert.True(result.TryGetProperty("fieldSchema", out JsonElement fieldSchema)); |
| 96 | + Assert.True(fieldSchema.TryGetProperty("fields", out JsonElement fields)); |
| 97 | + Assert.True(!string.IsNullOrWhiteSpace(fields.GetRawText())); |
| 98 | + } |
| 99 | + |
| 100 | + private async Task ExtractFieldsWithAnalyzerAsync(string analyzerId, string analyzerSampleFilePath) |
| 101 | + { |
| 102 | + // Implementation for extracting fields using the created analyzer |
| 103 | + var resultJson = await service.ExtractFieldsWithAnalyzerAsync(analyzerId, analyzerSampleFilePath); |
| 104 | + Assert.NotNull(resultJson); |
| 105 | + Assert.True(resultJson.RootElement.TryGetProperty("result", out JsonElement result)); |
| 106 | + Assert.True(result.TryGetProperty("warnings", out var warnings)); |
| 107 | + Assert.False(warnings.EnumerateArray().Any(), "The warnings array should be empty"); |
| 108 | + Assert.True(result.TryGetProperty("contents", out JsonElement contents)); |
| 109 | + Assert.True(contents[0].TryGetProperty("markdown", out JsonElement markdown)); |
| 110 | + Assert.True(!string.IsNullOrWhiteSpace(markdown.GetRawText())); |
| 111 | + Assert.True(contents[0].TryGetProperty("fields", out JsonElement fields)); |
| 112 | + Assert.True(!string.IsNullOrWhiteSpace(fields.GetRawText())); |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments