public static void GetAlbumList2ByGenreAsync_LibraryDoesNotExist_ThrowsDataNotFoundError()
            {
                var dbConnection = OpenSqliteDatabase();

                var dbContextOptionsBuilder = new DbContextOptionsBuilder <MediaInfoContext>()
                                              .DisableClientSideEvaluation()
                                              .UseSqlite(dbConnection);

                using (var dbContext = new MediaInfoContext(dbContextOptionsBuilder.Options))
                {
                    var random     = new RandomPopulator(dbContext);
                    var user       = random.AddUser();
                    var library    = random.AddLibrary();
                    var artist     = random.AddArtist();
                    var genre      = random.AddGenre();
                    var album      = random.AddAlbum(artist, genre: genre);
                    var directory  = random.AddDirectory(library);
                    var file       = random.AddFile(directory);
                    var track      = random.AddTrack(file, artist, album);
                    var trackGenre = random.AddTrackGenre(track, genre);
                    dbContext.SaveChanges();

                    var ex = Assert.Throws <RestApiErrorException>(() => RestApiQueries.GetAlbumList2ByGenreAsync(dbContext, user.UserId, library.LibraryId + 1, 0, 10, genre.Name, CancellationToken.None).GetAwaiter().GetResult());

                    var expectedException = RestApiErrorException.DataNotFoundError();
                    Assert.Equal(expectedException.Message, ex.Message);
                    Assert.Equal(expectedException.Code, ex.Code);
                }
            }
            public static void GetAlbumList2ByGenreAsync_LibraryIsSpecified_ReturnsExpectedAlbums()
            {
                var dbConnection = OpenSqliteDatabase();

                var dbContextOptionsBuilder = new DbContextOptionsBuilder <MediaInfoContext>()
                                              .DisableClientSideEvaluation()
                                              .UseSqlite(dbConnection);

                using (var dbContext = new MediaInfoContext(dbContextOptionsBuilder.Options))
                {
                    var random          = new RandomPopulator(dbContext);
                    var user            = random.AddUser();
                    var library         = random.AddLibrary();
                    var otherLibrary    = random.AddLibrary();
                    var artist          = random.AddArtist();
                    var genre           = random.AddGenre();
                    var album           = random.AddAlbum(artist, genre: genre);
                    var directory       = random.AddDirectory(library);
                    var file            = random.AddFile(directory);
                    var track           = random.AddTrack(file, artist, album);
                    var trackGenre      = random.AddTrackGenre(track, genre);
                    var otherDirectory  = random.AddDirectory(otherLibrary);
                    var otherFile       = random.AddFile(otherDirectory);
                    var otherTrack      = random.AddTrack(otherFile, artist, album);
                    var otherTrackGenre = random.AddTrackGenre(otherTrack, genre);
                    dbContext.SaveChanges();

                    var result = RestApiQueries.GetAlbumList2ByGenreAsync(dbContext, user.UserId, library.LibraryId, 0, 10, genre.Name, CancellationToken.None).GetAwaiter().GetResult();

                    var resultAlbum = Assert.Single(result.album);
                    Assert.Equal("a" + album.AlbumId, resultAlbum.id);
                    Assert.Equal(1, resultAlbum.songCount);
                    Assert.Equal(Math.Round(track.Duration ?? 0), resultAlbum.duration);
                }
            }
            public static void GetAlbumList2ByGenreAsync_VariousOffsetAndCount_ReturnsExpectedAlbumDetails(int albumCount, int count)
            {
                var dbConnection = OpenSqliteDatabase();

                var dbContextOptionsBuilder = new DbContextOptionsBuilder <MediaInfoContext>()
                                              .DisableClientSideEvaluation()
                                              .UseSqlite(dbConnection);

                using (var dbContext = new MediaInfoContext(dbContextOptionsBuilder.Options))
                {
                    var random  = new RandomPopulator(dbContext);
                    var user    = random.AddUser();
                    var library = random.AddLibrary();
                    var artist  = random.AddArtist();
                    var genre   = random.AddGenre();
                    var albums  = new List <Album>();
                    var tracks  = new List <Track>();
                    for (int i = 0; i < albumCount; ++i)
                    {
                        var album = random.AddAlbum(artist, genre: genre);
                        albums.Add(album);
                        var directory = random.AddDirectory(library);
                        var file      = random.AddFile(directory);
                        var track     = random.AddTrack(file, artist, album);
                        tracks.Add(track);
                        var trackGenre = random.AddTrackGenre(track, genre);
                    }
                    dbContext.SaveChanges();

                    albums = albums
                             .OrderBy(a => a.SortTitle ?? a.Title, _stringComparer)
                             .ThenBy(a => a.AlbumId)
                             .ToList();

                    for (int i = 0; i <= albumCount; ++i)
                    {
                        var result = RestApiQueries.GetAlbumList2ByGenreAsync(dbContext, user.UserId, null, i, count, genre.Name, CancellationToken.None).GetAwaiter().GetResult();

                        Assert.Equal(albums.Skip(i).Take(count).Select(a => "a" + a.AlbumId).ToArray(),
                                     result.album.Select(a => a.id).ToArray());
                        foreach (var resultAlbum in result.album)
                        {
                            var album       = albums.Single(a => "a" + a.AlbumId == resultAlbum.id);
                            var albumTracks = tracks.Where(t => t.Album == album);

                            Assert.Equal(album.Title, resultAlbum.name);
                            Assert.Equal(album.Artist.Name, resultAlbum.artist);
                            Assert.Equal("r" + album.ArtistId, resultAlbum.artistId);
                            Assert.Equal(album.CoverPictureId?.ToString("X8"), resultAlbum.coverArt);
                            Assert.Equal(albumTracks.Count(), resultAlbum.songCount);
                            Assert.Equal(Math.Round(albumTracks.Sum(t => t.Duration) ?? 0), resultAlbum.duration);
                            Assert.False(resultAlbum.playCountSpecified);
                            Assert.Equal(default, resultAlbum.playCount);