public async Task GetCommentsAsyncWithProgressTest()
        {
            var progressCalled    = 0;
            var progressIndicator = new Progress <int>(i => Interlocked.Increment(ref progressCalled));

            // Mock
            var httpMock       = new Mock <ISafeHttpClient>(MockBehavior.Strict);
            var youTubeService = new YouTubeService(httpMock.Object, config);

            httpMock.Setup(x => x.GetAsync <YouTube.CommentThreadResponse>(It.Is <string>(s => s.Contains("commentThreads") && checkApiCall(s, "commentThreads", null))))
            .ReturnsAsync(_mockCommentThreadResponse);
            httpMock.Setup(x => x.GetAsync <YouTube.CommentResponse>(It.Is <string>(s => checkApiCall(s, "comments", null))))
            .ReturnsAsync(_mockCommentResponse);

            // Run
            var comments = await youTubeService.GetCommentsAsync("1", progressIndicator);

            // Wait for progress
            Thread.Sleep(10);

            // Assert
            Assert.AreEqual(3, comments.Count);
            Assert.Greater(progressCalled, 0);

            // Verify
            httpMock.VerifyAll();
        }
Esempio n. 2
0
        public async Task TestComments()
        {
            var progressCounter = 0;
            var service         = new YouTubeService(new SafeHttpClient(), new Configuration.YouTubeConfiguration {
                ApiKey = _apiKey
            });
            var videoId = videoUrl;

            videoId = service.ParseVideoId(new Uri(videoId)) ?? videoId;
            var video             = service.GetVideoAsync(videoId).Result;
            var totalCommentCount = video.CommentCount;

            TestContext.WriteLine($"Title: {video.Title}");
            TestContext.WriteLine($"Comments: {totalCommentCount}");

            TestContext.Write("Parsing comments... ");

            var progressIndicator = new Progress <int>(i => Interlocked.Increment(ref progressCounter));
            var results           = await service.GetCommentsAsync(videoId, progressIndicator);

            TestContext.WriteLine("Done.");

            var commentsCt = results.Count;

            TestContext.WriteLine($"\nComments retrieved: { commentsCt }");

            Assert.Greater(commentsCt, 100);
            Assert.Greater(progressCounter, 0);
            foreach (var comment in results.Take(100))
            {
                Assert.IsNotNull(comment.Content);
                Assert.IsNotNull(comment.Author);
                Assert.AreEqual("YouTube", comment.Source);
            }
        }
        public void GetCommentsAsyncWithCancellationTokenTest()
        {
            // Mock
            var ctSource       = new CancellationTokenSource();
            var httpMock       = new Mock <ISafeHttpClient>(MockBehavior.Strict);
            var youTubeService = new YouTubeService(httpMock.Object, config);

            httpMock.Setup(x => x.GetAsync <YouTube.CommentThreadResponse>(It.Is <string>(s => s.Contains("commentThreads") && checkApiCall(s, "commentThreads", null))))
            .Callback(() => ctSource.Cancel())
            .ReturnsAsync(_mockCommentThreadResponse);

            // Assert
            Assert.ThrowsAsync <TaskCanceledException>(async() => await youTubeService.GetCommentsAsync("1", ctSource.Token));

            // Verify
            httpMock.VerifyAll();
        }
        public async Task GetCommentsAsyncNotFoundTest()
        {
            // Mock
            var httpMock       = new Mock <ISafeHttpClient>(MockBehavior.Strict);
            var youTubeService = new YouTubeService(httpMock.Object, config);

            httpMock.Setup(x => x.GetAsync <YouTube.CommentThreadResponse>(It.Is <string>(s => checkApiCall(s, "commentThreads", null))))
            .ReturnsAsync(null as YouTube.CommentThreadResponse);

            // Run
            var comments = await youTubeService.GetCommentsAsync("1");

            // Assert
            Assert.IsEmpty(comments);

            // Verify
            httpMock.VerifyAll();
        }