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

            httpMock.Setup(x => x.GetAsync <YouTube.OnlyIdResponse>(It.Is <string>(s => checkApiCall(s, "channels", null))))
            .ReturnsAsync(new YouTube.OnlyIdResponse {
                Items = new List <YouTube.Item> {
                    _mockOnlyIdResponse.Items[0]
                }
            });
            httpMock.Setup(x => x.GetAsync <YouTube.OnlyIdResponse>(It.Is <string>(s => checkApiCall(s, "playlistItems", null))))
            .ReturnsAsync(_mockOnlyIdResponse);
            httpMock.Setup(x => x.GetAsync <YouTube.VideoResponse>(It.Is <string>(s => checkApiCall(s, "videos", null))))
            .ReturnsAsync(new YouTube.VideoResponse {
                Items = new List <YouTube.Video> {
                    _mockVideoResponse.Items[0], _mockVideoResponse.Items[1], _mockVideoResponse.Items[2]
                }
            });

            // Run
            var videos = await youTubeService.GetChannelUploadsAsync("1");

            // Assert
            Assert.AreEqual(_mockOnlyIdResponse.Items.Count, videos.Count);

            // Verify
            httpMock.VerifyAll();
        }
        public void GetChannelUploadsAsyncWithCancellationTokenTest()
        {
            // 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.OnlyIdResponse>(It.Is <string>(s => checkApiCall(s, "channels", null))))
            .ReturnsAsync(new YouTube.OnlyIdResponse {
                Items = new List <YouTube.Item> {
                    _mockOnlyIdResponse.Items[0]
                }
            });
            httpMock.Setup(x => x.GetAsync <YouTube.OnlyIdResponse>(It.Is <string>(s => checkApiCall(s, "playlistItems", null))))
            .Callback(() => ctSource.Cancel())
            .ReturnsAsync(_mockOnlyIdResponse);

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

            // Verify
            httpMock.VerifyAll();
        }
        public async Task GetChannelUploadsAsyncWithProgressTest()
        {
            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.OnlyIdResponse>(It.Is <string>(s => checkApiCall(s, "channels", null))))
            .ReturnsAsync(new YouTube.OnlyIdResponse {
                Items = new List <YouTube.Item> {
                    _mockOnlyIdResponse.Items[0]
                }
            });
            httpMock.Setup(x => x.GetAsync <YouTube.OnlyIdResponse>(It.Is <string>(s => checkApiCall(s, "playlistItems", null))))
            .ReturnsAsync(_mockOnlyIdResponse);
            httpMock.Setup(x => x.GetAsync <YouTube.VideoResponse>(It.Is <string>(s => checkApiCall(s, "videos", null))))
            .ReturnsAsync(new YouTube.VideoResponse {
                Items = new List <YouTube.Video> {
                    _mockVideoResponse.Items[0], _mockVideoResponse.Items[1], _mockVideoResponse.Items[2]
                }
            });

            // Run
            var videos = await youTubeService.GetChannelUploadsAsync("1", progressIndicator);

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

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

            // Verify
            httpMock.VerifyAll();
        }
Esempio n. 4
0
        public async Task TestChannelUploads()
        {
            var service = new YouTubeService(new SafeHttpClient(), new Configuration.YouTubeConfiguration {
                ApiKey = _apiKey
            });

            var channelId = await service.GetChannelIdAsync(channelName);

            Assert.IsNotNull(channelId);

            var progressCounter   = 0;
            var progressIndicator = new Progress <int>(i => Interlocked.Increment(ref progressCounter));
            var channelItems      = await service.GetChannelUploadsAsync(channelId, progressIndicator);

            Assert.IsNotNull(channelItems);
            Assert.IsNotEmpty(channelItems);
            Assert.Greater(progressCounter, 0);

            TestContext.WriteLine($"Channel items: {channelItems.Count}");
            foreach (var item in channelItems)
            {
                Assert.IsNotNull(item?.Id);
            }
        }