public async Task GetPlaylists(Action <PlaylistData, InsertStatus> callback) { await foreach (var playlist in _youTubeApi.GetPlaylists()) { var result = await _youTubeCleanupToolDbContextFactory.Create().UpsertPlaylist(playlist); callback(playlist, result); } await _youTubeCleanupToolDbContextFactory.Create().SaveChangesAsync(); }
public async Task GetPlaylists(Func <IData, InsertStatus, CancellationToken, Task> callback, CancellationToken cancellationToken) { var context = _youTubeCleanupToolDbContextFactory.Create(); await foreach (var playlist in _youTubeApi.GetPlaylists().WithCancellation(cancellationToken)) { var result = await context.UpsertPlaylist(playlist); await callback(playlist, result, cancellationToken); cancellationToken.ThrowIfCancellationRequested(); } await context.SaveChangesAsync(cancellationToken); }
public async Task When_getting_playlists_from_YouTube_api_Then_they_are_saved_to_the_database( [Frozen] IYouTubeCleanupToolDbContext youTubeCleanupToolDbContext, [Frozen] IYouTubeCleanupToolDbContextFactory youTubeCleanupToolDbContextFactory, [Frozen] IYouTubeApi youTubeApi, List <PlaylistData> playlistData, GetAndCacheYouTubeData getAndCacheYouTubeData) { youTubeCleanupToolDbContextFactory.Create().Returns(youTubeCleanupToolDbContext); youTubeApi.GetPlaylists().Returns(TestExtensions.ToAsyncEnumerable(playlistData)); // Act await getAndCacheYouTubeData.GetPlaylists(Callback, CancellationToken.None); // Assert await foreach (var _ in youTubeApi.Received(1).GetPlaylists()) { } await youTubeCleanupToolDbContext.Received(3).UpsertPlaylist(Arg.Any <PlaylistData>()); await youTubeCleanupToolDbContext.Received(1).SaveChangesAsync(); }
public async Task When_getting_playlists_from_YouTube_api_Then_they_are_saved_to_the_database( [Frozen] IYouTubeCleanupToolDbContext youTubeCleanupToolDbContext, [Frozen] IYouTubeCleanupToolDbContextFactory youTubeCleanupToolDbContextFactory, [Frozen] IYouTubeApi youTubeApi, List <PlaylistData> playlistData, GetAndCacheYouTubeData getAndCacheYouTubeData) { youTubeCleanupToolDbContextFactory.Create().Returns(youTubeCleanupToolDbContext); youTubeApi.GetPlaylists().Returns(TestExtensions.ToAsyncEnumerable(playlistData)); var callback = new Action <PlaylistData, InsertStatus>((data, insertStatus) => _testOutputHelper.WriteLine($"{data.Title} - {insertStatus}")); // Act await getAndCacheYouTubeData.GetPlaylists(callback); // Assert await foreach (var _ in youTubeApi.Received(1).GetPlaylists()) { } await youTubeCleanupToolDbContext.Received(3).UpsertPlaylist(Arg.Any <PlaylistData>()); await youTubeCleanupToolDbContext.Received(1).SaveChangesAsync(); }