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();
        }
        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();
        }
        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();
        }
    }
}