Exemplo n.º 1
0
        public async Task GetVideos(Action <VideoData, InsertStatus> callback, bool getAllVideos, CancellationToken cancellationToken)
        {
            var playlistItems = await _youTubeCleanupToolDbContextFactory.Create().GetPlaylistItems();

            var videosToGet  = playlistItems.Select(x => x.VideoId).ToList();
            var videosToSkip = getAllVideos ? new List <string>() : (await _youTubeCleanupToolDbContextFactory.Create().GetVideos()).Select(x => x.Id);

            videosToGet = videosToGet.Except(videosToSkip).ToList();
            await foreach (var video in _youTubeApi.GetVideos(videosToGet))
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    callback(new VideoData {
                        Title = "CANCELLED", Id = "CANCELLED"
                    }, InsertStatus.Inserted);
                    return;
                }

                if (video.IsDeletedFromYouTube)
                {
                    // We only want to insert this if we haven't already - because we want to preserve any existing data we have
                    if (!await _youTubeCleanupToolDbContextFactory.Create().VideoExists(video.Id))
                    {
                        await UpsertVideo(callback, video);
                    }
                }
                else
                {
                    video.ThumbnailBytes = await GetThumbnail(cancellationToken, video);
                    await UpsertVideo(callback, video);
                }
            }
        }
Exemplo n.º 2
0
        public async Task GetVideos(Func <IData, InsertStatus, CancellationToken, Task> callback, bool getAllVideos, CancellationToken cancellationToken)
        {
            var context       = _youTubeCleanupToolDbContextFactory.Create();
            var playlistItems = await context.GetPlaylistItems();

            var videosToGet  = playlistItems.Select(x => x.VideoId).ToList();
            var videosToSkip = getAllVideos ? new List <string>() : (await context.GetVideos()).Select(x => x.Id);

            videosToGet = videosToGet.Except(videosToSkip).ToList();
            await foreach (var video in _youTubeApi.GetVideos(videosToGet).WithCancellation(cancellationToken))
            {
                cancellationToken.ThrowIfCancellationRequested();

                if (video.IsDeletedFromYouTube)
                {
                    // We only want to insert this if we haven't already - because we want to preserve any existing data we have
                    if (!await context.VideoExists(video.Id))
                    {
                        await UpsertVideo(callback, video, cancellationToken);
                    }
                }
                else
                {
                    video.ThumbnailBytes = await GetThumbnail(cancellationToken, video);
                    await UpsertVideo(callback, video, cancellationToken);
                }
            }
        }
        public async Task When_only_getting_videos_that_dont_already_exist_Then_YouTube_api_is_not_called_to_get_the_data(
            [Frozen] IYouTubeCleanupToolDbContext youTubeCleanupToolDbContext,
            [Frozen] IYouTubeCleanupToolDbContextFactory youTubeCleanupToolDbContextFactory,
            [Frozen] IYouTubeApi youTubeApi,
            [Frozen] IFixture fixture,
            GetAndCacheYouTubeData getAndCacheYouTubeData
            )
        {
            // TODO: add customization so we don't need to do this everywhere we have a DbContext and ContextFactory
            youTubeCleanupToolDbContextFactory.Create().Returns(youTubeCleanupToolDbContext);
            var playlistItemData = fixture.CreateMany <PlaylistItemData>(3).ToList();

            youTubeCleanupToolDbContext.GetPlaylistItems().Returns(playlistItemData);
            var videoData = playlistItemData.Take(1).Select(x => new VideoData {
                Id = x.VideoId
            }).ToList();

            youTubeCleanupToolDbContext.GetVideos().Returns(videoData);

            // Make sure the videos aren't "deleted from YouTube"
            var videos = fixture.CreateMany <VideoData>(2).ToList();

            videos.ForEach(x => x.IsDeletedFromYouTube = false);

            youTubeApi.GetVideos(Arg.Any <List <string> >()).Returns(TestExtensions.ToAsyncEnumerable(videos));

            var callback = new Action <VideoData, InsertStatus>((data, insertStatus) => _testOutputHelper.WriteLine($"{data.Title} - {insertStatus}"));

            // Act
            await getAndCacheYouTubeData.GetVideos(callback, false, CancellationToken.None);

            // Assert
            // TODO: Assert we're passing in expectedGetTheseVideos to GetVideos
            await foreach (var _ in youTubeApi.Received(1).GetVideos(Arg.Any <List <string> >()))
            {
            }
            await youTubeCleanupToolDbContext.Received(2).UpsertVideo(Arg.Any <VideoData>());

            await youTubeCleanupToolDbContext.Received(2).SaveChangesAsync();
        }
        public async Task When_getting_all_videos_Then_YouTube_api_is_called_to_get_the_data_for_everything(
            [Frozen] IYouTubeCleanupToolDbContext youTubeCleanupToolDbContext,
            [Frozen] IYouTubeCleanupToolDbContextFactory youTubeCleanupToolDbContextFactory,
            [Frozen] IYouTubeApi youTubeApi,
            [Frozen] IFixture fixture,
            GetAndCacheYouTubeData getAndCacheYouTubeData
            )
        {
            youTubeCleanupToolDbContextFactory.Create().Returns(youTubeCleanupToolDbContext);
            var playlistItemData = fixture.CreateMany <PlaylistItemData>(3).ToList();

            youTubeCleanupToolDbContext.GetPlaylistItems().Returns(playlistItemData);
            var videoData = playlistItemData.Take(1).Select(x => new VideoData {
                Id = x.VideoId
            }).ToList();

            youTubeCleanupToolDbContext.GetVideos().Returns(videoData);

            // Make sure the videos aren't "deleted from YouTube"
            var videos = fixture.CreateMany <VideoData>(3).ToList();

            videos.ForEach(x => x.IsDeletedFromYouTube = false);

            youTubeApi.GetVideos(Arg.Any <List <string> >()).Returns(TestExtensions.ToAsyncEnumerable(videos));

            var callback = new Action <VideoData, InsertStatus>((data, insertStatus) => _testOutputHelper.WriteLine($"{data.Title} - {insertStatus}"));

            // Act
            await getAndCacheYouTubeData.GetVideos(callback, true, CancellationToken.None);

            // Assert
            await foreach (var _ in youTubeApi.Received(1).GetVideos(Arg.Any <List <string> >()))
            {
            }
            await youTubeCleanupToolDbContext.Received(3).UpsertVideo(Arg.Any <VideoData>());

            await youTubeCleanupToolDbContext.Received(3).SaveChangesAsync();
        }