public async Task When_getting_playlistItems_from_YouTube_api_Then_they_are_saved_to_the_database(
            [Frozen] IYouTubeCleanupToolDbContext youTubeCleanupToolDbContext,
            [Frozen] IYouTubeCleanupToolDbContextFactory youTubeCleanupToolDbContextFactory,
            [Frozen] IYouTubeApi youTubeApi,
            PlaylistData playlist,
            List <PlaylistItemData> playlistItemData,
            GetAndCacheYouTubeData getAndCacheYouTubeData)
        {
            youTubeCleanupToolDbContextFactory.Create().Returns(youTubeCleanupToolDbContext);
            youTubeCleanupToolDbContext.GetPlaylists().Returns(new List <PlaylistData> {
                playlist
            });
            youTubeApi.GetPlaylistItems(Arg.Any <string>(), Arg.Any <Func <string, Task> >()).Returns(TestExtensions.ToAsyncEnumerable(playlistItemData));

            youTubeCleanupToolDbContext.GetPlaylistItems(Arg.Any <string>()).Returns(new List <PlaylistItemData>());

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

            // Act
            await getAndCacheYouTubeData.GetPlaylistItems(callback);

            // Assert
            await foreach (var _ in youTubeApi.Received(1).GetPlaylistItems(Arg.Any <string>(), Arg.Any <Func <string, Task> >()))
            {
            }
            await youTubeCleanupToolDbContext.Received(3).UpsertPlaylistItem(Arg.Any <PlaylistItemData>());

            await youTubeCleanupToolDbContext.Received(1).SaveChangesAsync();
        }
        public async Task When_getting_playlistItems_from_YouTube_api_Then_they_are_saved_to_the_database(
            [Frozen] IYouTubeCleanupToolDbContext youTubeCleanupToolDbContext,
            [Frozen] IYouTubeCleanupToolDbContextFactory youTubeCleanupToolDbContextFactory,
            [Frozen] IYouTubeApi youTubeApi,
            PlaylistData playlist,
            List <PlaylistItemData> playlistItemData,
            GetAndCacheYouTubeData getAndCacheYouTubeData)
        {
            youTubeCleanupToolDbContextFactory.Create().Returns(youTubeCleanupToolDbContext);
            youTubeCleanupToolDbContext.GetPlaylists().Returns(new List <PlaylistData> {
                playlist
            });
            youTubeApi.GetPlaylistItems(Arg.Any <string>(), Arg.Any <Func <string, Task> >()).Returns(TestExtensions.ToAsyncEnumerable(playlistItemData));

            youTubeCleanupToolDbContext.GetPlaylistItems(Arg.Any <string>()).Returns(new List <PlaylistItemData>());

            // Act
            await getAndCacheYouTubeData.GetPlaylistItems(Callback, CancellationToken.None);

            // Assert
            await foreach (var _ in youTubeApi.Received(1).GetPlaylistItems(Arg.Any <string>(), Arg.Any <Func <string, Task> >()))
            {
            }
            await youTubeCleanupToolDbContext.Received(3).UpsertPlaylistItem(Arg.Any <PlaylistItemData>());

            await youTubeCleanupToolDbContext.Received(1).SaveChangesAsync();
        }
Exemplo n.º 3
0
        private async Task GetPlaylistItems(Action <PlaylistItemData, InsertStatus> callback, PlaylistData playlist)
        {
            var playlistItems = new List <PlaylistItemData>();

            await foreach (var playlistItem in _youTubeApi.GetPlaylistItems(playlist.Id, RemovePlaylist))
            {
                playlistItems.Add(playlistItem);
                var result = await _youTubeCleanupToolDbContextFactory.Create().UpsertPlaylistItem(playlistItem);

                callback(playlistItem, result);
            }

            // Now we have to remove playlist items we didn't get back from the API - Otherwise if we delete + add items then we end up with duplicates
            var originalPlaylistItems = await _youTubeCleanupToolDbContextFactory.Create().GetPlaylistItems(playlist.Id);

            var playlistItemsHashSet = new HashSet <string>(playlistItems.Select(x => x.Id).ToList());

            foreach (var playlistItem in originalPlaylistItems)
            {
                if (!playlistItemsHashSet.Contains(playlistItem.Id))
                {
                    _youTubeCleanupToolDbContextFactory.Create().RemovePlaylistItem(playlistItem);
                    callback(playlistItem, InsertStatus.Deleted);
                }
            }
        }
Exemplo n.º 4
0
        private async Task GetPlaylistItems(IYouTubeCleanupToolDbContext context, Func <IData, InsertStatus, CancellationToken, Task> callback, PlaylistData playlist, CancellationToken cancellationToken)
        {
            var playlistItems = new List <PlaylistItemData>();

            await foreach (var playlistItem in _youTubeApi.GetPlaylistItems(playlist.Id, RemovePlaylist).WithCancellation(cancellationToken))
            {
                playlistItems.Add(playlistItem);
                var result = await context.UpsertPlaylistItem(playlistItem);
                await callback(playlistItem, result, cancellationToken);

                cancellationToken.ThrowIfCancellationRequested();
            }

            // Now we have to remove playlist items we didn't get back from the API - Otherwise if we delete + add items then we end up with duplicates
            var originalPlaylistItems = await context.GetPlaylistItems(playlist.Id);

            var playlistItemsHashSet = new HashSet <string>(playlistItems.Select(x => x.Id).ToList());

            foreach (var playlistItem in originalPlaylistItems)
            {
                if (!playlistItemsHashSet.Contains(playlistItem.Id))
                {
                    context.RemovePlaylistItem(playlistItem);
                    await callback(playlistItem, InsertStatus.Deleted, cancellationToken);

                    cancellationToken.ThrowIfCancellationRequested();
                }
            }
        }
        public async Task When_getting_playlistItems_from_YouTube_api_Then_playlists_which_no_longer_exist_get_deleted_from_db(
            [Frozen] IYouTubeCleanupToolDbContext youTubeCleanupToolDbContext,
            [Frozen] IYouTubeCleanupToolDbContextFactory youTubeCleanupToolDbContextFactory,
            [Frozen] IYouTubeApi youTubeApi,
            PlaylistData playlist,

            GetAndCacheYouTubeData getAndCacheYouTubeData)
        {
            var playlistItems = new List <PlaylistItemData>
            {
                new()
                {
                    Id             = "1",
                    Title          = "a",
                    PlaylistDataId = playlist.Id
                },
                new()
                {
                    Id             = "2",
                    Title          = "b",
                    PlaylistDataId = playlist.Id
                }
            };

            youTubeCleanupToolDbContextFactory.Create().Returns(youTubeCleanupToolDbContext);
            youTubeCleanupToolDbContext.GetPlaylists().Returns(new List <PlaylistData> {
                playlist
            });
            youTubeApi.GetPlaylistItems(Arg.Any <string>(), Arg.Any <Func <string, Task> >()).Returns(TestExtensions.ToAsyncEnumerable(playlistItems));

            var originalPlaylistItems = new List <PlaylistItemData>
            {
                new()
                {
                    Id             = "1",
                    Title          = "a",
                    PlaylistDataId = playlist.Id
                },
                new()
                {
                    Id             = "2",
                    Title          = "b",
                    PlaylistDataId = playlist.Id
                },
                new()
                {
                    Id             = "3",
                    Title          = "c",
                    PlaylistDataId = playlist.Id
                }
            };

            youTubeCleanupToolDbContext.GetPlaylistItems(Arg.Any <string>()).Returns(originalPlaylistItems);

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

            await getAndCacheYouTubeData.GetPlaylistItems(callback);

            await foreach (var _ in youTubeApi.Received(1).GetPlaylistItems(Arg.Any <string>(), Arg.Any <Func <string, Task> >()))
            {
            }
            await youTubeCleanupToolDbContext.Received(2).UpsertPlaylistItem(Arg.Any <PlaylistItemData>());

            youTubeCleanupToolDbContext.Received(1).RemovePlaylistItem(Arg.Any <PlaylistItemData>());
            await youTubeCleanupToolDbContext.Received(1).SaveChangesAsync();
        }
    }
}