public static async Task CreatesLabeledUtterances() { var text = Guid.NewGuid().ToString(); var intent = Guid.NewGuid().ToString(); var entityType = Guid.NewGuid().ToString(); var entityValue = Guid.NewGuid().ToString(); var slots = new Dictionary <string, string> { { entityType, entityValue }, }; var mockClient = new MockLexClient(); mockClient.Get <PostTextResponse>().IntentName = intent; using (var lex = new LexNLUService(string.Empty, string.Empty, new LexSettings(), mockClient)) { var response = await lex.TestAsync(text).ConfigureAwait(false); response.Text.Should().Be(text); response.Intent.Should().Be(intent); response.Entities.Should().BeEmpty(); mockClient.Get <PostTextResponse>().Slots = slots; response = await lex.TestAsync(text).ConfigureAwait(false); response.Entities[0].EntityType.Should().Be(entityType); response.Entities[0].EntityValue.Should().Be(entityValue); } }
public static void BuildFailureThrowsInvalidOperation() { var mockClient = new MockLexClient(); mockClient.Get <GetBotResponse>().Status = Status.BUILDING; // Wait for the second GetBot action to set status to failed var count = 0; void onRequest(object request) { if (request is GetBotRequest && ++count == 2) { mockClient.Get <GetBotResponse>().Status = Status.FAILED; } } mockClient.OnRequest = onRequest; using (var lex = new LexNLUService(string.Empty, string.Empty, new LexSettings(), mockClient)) { var utterance = new LabeledUtterance(string.Empty, string.Empty, null); var buildFailed = new Func <Task>(() => lex.TrainAsync(new[] { utterance })); buildFailed.Should().Throw <InvalidOperationException>(); mockClient.Get <GetBotResponse>().Status = new Status("UNKNOWN"); buildFailed.Should().Throw <InvalidOperationException>(); mockClient.Get <GetBotResponse>().Status = Status.NOT_BUILT; buildFailed.Should().NotThrow <InvalidOperationException>(); } }
public static void ImportFailureThrowsInvalidOperation() { var importId = Guid.NewGuid().ToString(); var failureReason = new List <string> { Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), }; var mockClient = new MockLexClient(); mockClient.Get <StartImportResponse>().ImportId = importId; mockClient.Get <StartImportResponse>().ImportStatus = ImportStatus.FAILED; mockClient.Get <GetImportResponse>().ImportId = importId; mockClient.Get <GetImportResponse>().ImportStatus = ImportStatus.FAILED; using (var lex = new LexNLUService(string.Empty, string.Empty, new LexSettings(), mockClient)) { var utterance = new LabeledUtterance(string.Empty, string.Empty, null); // Null failure reason should be okay var importFails = new Func <Task>(() => lex.TrainAsync(new[] { utterance })); importFails.Should().Throw <InvalidOperationException>().And.Message.Should().BeEmpty(); // Failure reason is concatenated in message mockClient.Get <GetImportResponse>().FailureReason = failureReason; var expectedMessage = string.Join(Environment.NewLine, failureReason); importFails.Should().Throw <InvalidOperationException>().And.Message.Should().Be(expectedMessage); } }
public static async Task WaitsForImportCompletion() { var importId = Guid.NewGuid().ToString(); var mockClient = new MockLexClient(); mockClient.Get <StartImportResponse>().ImportId = importId; mockClient.Get <StartImportResponse>().ImportStatus = ImportStatus.IN_PROGRESS; mockClient.Get <GetImportResponse>().ImportId = importId; mockClient.Get <GetImportResponse>().ImportStatus = ImportStatus.IN_PROGRESS; // Wait for the second GetImport action to set status to complete var count = 0; void onRequest(object request) { if (request is GetImportRequest && ++count == 2) { mockClient.Get <GetImportResponse>().ImportStatus = ImportStatus.COMPLETE; } } mockClient.OnRequest = onRequest; using (var lex = new LexNLUService(string.Empty, string.Empty, new LexSettings(), mockClient)) { var utterance = new LabeledUtterance(string.Empty, string.Empty, null); await lex.TrainAsync(new[] { utterance }).ConfigureAwait(false); // Assert two GetImport actions occur mockClient.Requests.OfType <GetImportRequest>().Count().Should().Be(2); // Assert that the time difference is at least two seconds var requests = mockClient.TimestampedRequests .Where(tuple => tuple.Item1 is GetImportRequest) .Select(tuple => new { Request = (GetImportRequest)tuple.Item1, Timestamp = tuple.Item2 }) .ToArray(); var difference = requests[1].Timestamp - requests[0].Timestamp; difference.Should().BeGreaterThan(TimeSpan.FromSeconds(2) - Epsilon); } }
public static async Task WaitsForBuildCompletion() { var mockClient = new MockLexClient(); mockClient.Get <GetBotResponse>().Status = Status.BUILDING; // Wait for the third GetBot action to set status to complete var count = 0; void onRequest(object request) { if (request is GetBotRequest && ++count == 3) { mockClient.Get <GetBotResponse>().Status = Status.READY; } } mockClient.OnRequest = onRequest; using (var lex = new LexNLUService(string.Empty, string.Empty, new LexSettings(), mockClient)) { var utterance = new LabeledUtterance(string.Empty, string.Empty, null); await lex.TrainAsync(new[] { utterance }).ConfigureAwait(false); // Assert three GetBot actions occur mockClient.Requests.OfType <GetBotRequest>().Count().Should().Be(3); // Assert that the time difference is at least two seconds var requests = mockClient.TimestampedRequests .Where(tuple => tuple.Item1 is GetBotRequest) .Select(tuple => new { Request = (GetBotRequest)tuple.Item1, Timestamp = tuple.Item2 }) .ToArray(); var difference = requests[2].Timestamp - requests[1].Timestamp; difference.Should().BeGreaterThan(TimeSpan.FromSeconds(2) - Epsilon); } }
public static async Task TestsWithSpeech() { var intent = Guid.NewGuid().ToString(); var transcript = Guid.NewGuid().ToString(); var entityType = Guid.NewGuid().ToString(); var entityValue = Guid.NewGuid().ToString(); var mockClient = new MockLexClient(); mockClient.Get <PostContentResponse>().IntentName = intent; mockClient.Get <PostContentResponse>().InputTranscript = transcript; using (var lex = new LexNLUService(string.Empty, string.Empty, new LexSettings(), mockClient)) { // slots response will be null in this first request // using a text file because we don't need to work with real audio var result = await lex.TestSpeechAsync(Path.Combine("assets", "sample.txt")).ConfigureAwait(false); // assert reads content from file (file contents are "hello world") var request = mockClient.Requests.OfType <PostContentRequest>().Single(); using (var reader = new StreamReader(request.InputStream)) { reader.ReadToEnd().Should().Be("hello world"); } // assert results result.Intent.Should().Be(intent); result.Text.Should().Be(transcript); result.Entities.Should().BeNull(); // test with valid slots response mockClient.Get <PostContentResponse>().Slots = $"{{\"{entityType}\":\"{entityValue}\"}}"; result = await lex.TestSpeechAsync(Path.Combine("assets", "sample.txt")).ConfigureAwait(false); result.Entities.Count.Should().Be(1); result.Entities[0].EntityType.Should().Be(entityType); result.Entities[0].EntityValue.Should().Be(entityValue); } }