Example #1
0
        public async Task ReturnsCommentIntactIfNotAnalyzed()
        {
            var config = Options.Create(new WebConfiguration
            {
                TextAnalyticsSubscriptionKey = _invalidSubscriptionKey
            });

            var textAnalyticsClientFactoryMock = new Mock <ITextAnalyticsClientFactory>
            {
                DefaultValue = DefaultValue.Mock
            };
            ITextAnalyzer textAnalyzer = new TextAnalyzer(config, textAnalyticsClientFactoryMock.Object);

            var comment = new Comment("test-post-id", new string('*', 555), "John Doe");
            var result  = await textAnalyzer.AnalyzeAsync(comment).ConfigureAwait(false);

            Assert.Equal(comment.Score, result.Comment.Score);
        }
Example #2
0
        public async Task AnalyzesSentimentScore(string message, double?score)
        {
            var config = Options.Create(new WebConfiguration
            {
                TextAnalyticsSubscriptionKey = _validSubscriptionKey
            });

            // SentimentBatchAsync extension method uses SentimentWithHttpMessagesAsync internally and mocking
            // extension methods with Moq isn't currently possible. Extension methods are "hard dependencies" that
            // one could also break by introducing a wrapper class and an interface.
            // Let's mock SentimentWithHttpMessagesAsync method here instead.
            var httpOperationResponse = new HttpOperationResponse <SentimentBatchResult>
            {
                Body = new SentimentBatchResult(new SentimentBatchResultItem[]
                {
                    new SentimentBatchResultItem(score: score)
                })
            };
            var textAnalyticsClientMock = new Mock <ITextAnalyticsClient>();

            textAnalyticsClientMock.Setup(x => x.SentimentWithHttpMessagesAsync(
                                              It.IsAny <bool?>(),
                                              It.IsAny <MultiLanguageBatchInput>(),
                                              It.IsAny <Dictionary <string, List <string> > >(),
                                              It.IsAny <CancellationToken>()))
            .ReturnsAsync(httpOperationResponse);

            var textAnalyticsClientFactoryMock = new Mock <ITextAnalyticsClientFactory>();

            textAnalyticsClientFactoryMock.Setup(x => x.CreateClient(It.IsAny <string>(), It.IsAny <string>()))
            .Returns(textAnalyticsClientMock.Object);

            ITextAnalyzer textAnalyzer = new TextAnalyzer(config, textAnalyticsClientFactoryMock.Object);

            var comment         = new Comment("test-post-id", message, "John Doe");
            var analyzedComment = await textAnalyzer.AnalyzeAsync(comment).ConfigureAwait(false);

            httpOperationResponse.Dispose();

            Assert.Equal($"{score:0.00}", analyzedComment.Comment.Score);
        }