Beispiel #1
0
        public static async Task TestsWithSpeech(string slots, string entityType, string entityValue)
        {
            var fileName   = "sample.txt";
            var intent     = Guid.NewGuid().ToString();
            var transcript = Guid.NewGuid().ToString();

            var content    = default(string);
            var mockClient = new Mock <ILexTestClient>();

            mockClient.Setup(lex => lex.PostContentAsync(
                                 It.Is <PostContentRequest>(request => ((FileStream)request.InputStream).Name.EndsWith(fileName, StringComparison.InvariantCulture)),
                                 It.IsAny <CancellationToken>()))
            .Returns(() => Task.FromResult(new PostContentResponse
            {
                IntentName      = intent,
                InputTranscript = transcript,
                Slots           = slots,
            }))
            .Callback <PostContentRequest, CancellationToken>(
                (request, cancellationToken) => content = GetContent(request.InputStream));

            using (var lex = new LexNLUTestClient(string.Empty, string.Empty, new LexSettings(), mockClient.Object))
            {
                // 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")
                content.Should().Be("hello world");

                // assert result type
                result.Should().BeOfType <JsonLabeledUtterance>();

                // assert intent and text
                result.Intent.Should().Be(intent);
                result.Text.Should().Be(transcript);

                // assert entities
                if (slots == null)
                {
                    result.Entities.Should().BeNull();
                }
                else
                {
                    result.Entities.Count.Should().Be(1);
                    result.Entities[0].EntityType.Should().Be(entityType);
                    result.Entities[0].EntityValue.Value <string>().Should().BeEquivalentTo(entityValue);
                }
            }
        }
        public static void ThrowsArgumentNull()
        {
            var nullBotName   = new Action(() => new LexNLUTestClient(null, string.Empty, default(ILexTestClient)));
            var nullBotAlias  = new Action(() => new LexNLUTestClient(string.Empty, null, default(ILexTestClient)));
            var nullLexClient = new Action(() => new LexNLUTestClient(string.Empty, string.Empty, default(ILexTestClient)));

            nullBotName.Should().Throw <ArgumentNullException>().And.ParamName.Should().Be("botName");
            nullBotAlias.Should().Throw <ArgumentNullException>().And.ParamName.Should().Be("botAlias");
            nullLexClient.Should().Throw <ArgumentNullException>().And.ParamName.Should().Be("lexClient");

            using (var lex = new LexNLUTestClient(string.Empty, string.Empty, new Mock <ILexTestClient>().Object))
            {
                var nullSpeechFile    = new Func <Task>(() => lex.TestSpeechAsync(null));
                var nullTestUtterance = new Func <Task>(() => lex.TestAsync(default(JToken)));
                nullSpeechFile.Should().Throw <ArgumentNullException>().And.ParamName.Should().Be("speechFile");
                nullTestUtterance.Should().Throw <ArgumentNullException>().And.ParamName.Should().Be("query");
            }
        }