public static void GetArtistAsync_ArtistHasAccessibleTrack_ReturnsExpectedArtistDetails()
            {
                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 album     = random.AddAlbum(artist);
                    var directory = random.AddDirectory(library);
                    var file      = random.AddFile(directory);
                    var track     = random.AddTrack(file, artist, album);
                    dbContext.SaveChanges();

                    var result = RestApiQueries.GetArtistAsync(dbContext, user.UserId, artist.ArtistId, CancellationToken.None).GetAwaiter().GetResult();

                    Assert.Equal("r" + artist.ArtistId, result.id);
                    Assert.Equal(artist.Name, result.name);
                    Assert.Null(result.coverArt);
                    Assert.Equal(1, result.albumCount);
                    Assert.False(result.starredSpecified);
                    Assert.Equal(default, result.starred);
            public static void GetGenresAsync_GenreHasAcessibleNonAccessControlledTrack_GenreIsReturned()
            {
                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(accessControlled: false);
                    var genre      = random.AddGenre();
                    var artist     = random.AddArtist();
                    var album      = random.AddAlbum(artist);
                    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 result = RestApiQueries.GetGenresAsync(dbContext, user.UserId, CancellationToken.None).GetAwaiter().GetResult();

                    var resultGenre = Assert.Single(result.genre);
                    Assert.Equal(genre.Name, resultGenre.Text.Single());
                }
            }
Example #3
0
            public static void GetAlbumList2StarredAsync_AlbumHasStarFromOtherUser_AlbumIsNotReturned()
            {
                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 otherUser = random.AddUser();
                    var library   = random.AddLibrary(accessControlled: false);
                    var artist    = random.AddArtist();
                    var album     = random.AddAlbum(artist);
                    var albumStar = random.AddAlbumStar(album, otherUser);
                    var directory = random.AddDirectory(library);
                    var file      = random.AddFile(directory);
                    var track     = random.AddTrack(file, artist, album);
                    dbContext.SaveChanges();

                    var result = RestApiQueries.GetAlbumList2StarredAsync(dbContext, user.UserId, null, 0, 10, CancellationToken.None).GetAwaiter().GetResult();

                    Assert.Empty(result.album);
                }
            }
Example #4
0
            public static void CanAddTracksAsync_InaccessibleTrack_ReturnsFalse()
            {
                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(accessControlled: true);
                    var artist    = random.AddArtist();
                    var album     = random.AddAlbum(artist);
                    var directory = random.AddDirectory(library);
                    var file      = random.AddFile(directory);
                    var track     = random.AddTrack(file, artist, album);
                    dbContext.SaveChanges();

                    int[] trackIds = new[] { track.TrackId };
                    bool  result   = RestApiQueries.CanAddTracksAsync(dbContext, user.UserId, trackIds, CancellationToken.None).GetAwaiter().GetResult();

                    Assert.False(result);
                }
            }
            public static void GetCoverArtStreamInfoAsync_Always_ReturnsExpectedPictureDetails()
            {
                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 directory = random.AddDirectory(library);
                    var file      = random.AddFile(directory);
                    var picture   = random.AddPicture(file);
                    dbContext.SaveChanges();

                    var result = RestApiQueries.GetCoverArtStreamInfoAsync(dbContext, user.UserId, picture.StreamHash, CancellationToken.None).GetAwaiter().GetResult();

                    Assert.Equal(picture.File.Directory.Path, result.DirectoryPath);
                    Assert.Equal(picture.File.Name, result.FileName);
                    Assert.Equal(picture.StreamIndex, result.StreamIndex);
                }
            }
            public static void TestStarTrackAsync()
            {
                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 directory = random.AddDirectory(library);
                    var trackFile = random.AddFile(directory);
                    var artist    = random.AddArtist();
                    var album     = random.AddAlbum(artist);
                    var track     = random.AddTrack(trackFile, artist, album);
                    dbContext.SaveChanges();

                    for (int i = 0; i < 2; ++i)
                    {
                        RestApiQueries.StarTrackAsync(dbContext, user.UserId, track.TrackId, CancellationToken.None).GetAwaiter().GetResult();
                        dbContext.SaveChanges();

                        Assert.True(dbContext.TrackStars.Any(s => s.TrackId == track.TrackId && s.UserId == user.UserId));
                    }
                }
            }
Example #7
0
            public static void CreatePlaylistAsync_Always_CreatesPlaylist()
            {
                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();
                    dbContext.SaveChanges();

                    const string playlistName = "playlistName";

                    int playlistId = RestApiQueries.CreatePlaylistAsync(dbContext, user.UserId, playlistName, CancellationToken.None).GetAwaiter().GetResult();
                    var playlists  = dbContext.Playlists.Local.Where(p => p.PlaylistId == playlistId).ToArray();
                    dbContext.SaveChanges();

                    var playlist = Assert.Single(playlists);
                    Assert.Equal(user.UserId, playlist.UserId);
                    Assert.Equal(playlistName, playlist.Name);
                    Assert.Null(playlist.Description);
                    Assert.False(playlist.IsPublic);
                    Assert.InRange(playlist.Created, DateTime.UtcNow - TimeSpan.FromSeconds(30), DateTime.UtcNow);
                    Assert.InRange(playlist.Modified, playlist.Created, playlist.Created + TimeSpan.FromSeconds(1));
                    Assert.Empty(dbContext.PlaylistTracks.Where(pt => pt.PlaylistId == playlist.PlaylistId));
                }
            }
            public static void GetCoverArtStreamInfoAsync_PictureHasAccessibleAndInaccessibleFiles_ReturnsPictureDetails()
            {
                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 inaccessibleLibrary   = random.AddLibrary(accessControlled: true);
                    var inaccessibleDirectory = random.AddDirectory(inaccessibleLibrary);
                    var inaccessibleFile      = random.AddFile(inaccessibleDirectory);
                    var inaccessiblePicture   = random.AddPicture(inaccessibleFile);
                    var accessibleLibrary     = random.AddLibrary(accessControlled: false);
                    var accessibleDirectory   = random.AddDirectory(accessibleLibrary);
                    var accessibleFile        = random.AddFile(accessibleDirectory);
                    var accessiblePicture     = random.AddPicture(accessibleFile);
                    dbContext.SaveChanges();

                    var result = RestApiQueries.GetCoverArtStreamInfoAsync(dbContext, user.UserId, accessiblePicture.StreamHash, CancellationToken.None).GetAwaiter().GetResult();

                    Assert.Equal(accessiblePicture.File.Directory.Path, result.DirectoryPath);
                    Assert.Equal(accessiblePicture.File.Name, result.FileName);
                    Assert.Equal(accessiblePicture.StreamIndex, result.StreamIndex);
                }
            }
Example #9
0
            public static void AddPlaylistSongsAsync_AccessibleAccessControledTrack_AddsTracksToPlaylist()
            {
                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(accessControlled: true);
                    var libraryUser = random.AddLibraryUser(library, user);
                    var artist      = random.AddArtist();
                    var album       = random.AddAlbum(artist);
                    var directory   = random.AddDirectory(library);
                    var file        = random.AddFile(directory);
                    var track       = random.AddTrack(file, artist, album);
                    var playlist    = random.AddPlaylist(user);
                    dbContext.SaveChanges();

                    int[] trackIds = new[] { track.TrackId };
                    RestApiQueries.AddPlaylistSongsAsync(dbContext, user.UserId, playlist.PlaylistId, trackIds, CancellationToken.None).GetAwaiter().GetResult();
                    dbContext.SaveChanges();

                    var playlistTracks = dbContext.PlaylistTracks
                                         .Where(pt => pt.PlaylistId == playlist.PlaylistId)
                                         .OrderBy(pt => pt.Index);
                    Assert.Equal(new[] { track.TrackId }, playlistTracks.Select(pt => pt.TrackId).ToArray());
                    Assert.Equal(Enumerable.Range(0, 1).ToArray(), playlistTracks.Select(pt => pt.Index).ToArray());
                }
            }
            public static void TestUpdatePlaylistAsync()
            {
                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 directory     = random.AddDirectory(library);
                    var trackFile     = random.AddFile(directory);
                    var artist        = random.AddArtist();
                    var album         = random.AddAlbum(artist);
                    var track         = random.AddTrack(trackFile, artist, album);
                    var playlist      = random.AddPlaylist(user);
                    var playlistTrack = random.AddPlaylistTrack(playlist, track, 0);
                    dbContext.SaveChanges();

                    RestApiQueries.UpdatePlaylistAsync(dbContext, user.UserId, playlist.PlaylistId, "playlistName", null, null, CancellationToken.None).GetAwaiter().GetResult();
                    dbContext.SaveChanges();

                    Assert.True(dbContext.Playlists.Any(p => p.PlaylistId == playlist.PlaylistId && p.Name == "playlistName"));
                }
            }
Example #11
0
            public static void AddPlaylistSongsAsync_InvalidTrackId_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 album     = random.AddAlbum(artist);
                    var directory = random.AddDirectory(library);
                    var file      = random.AddFile(directory);
                    var track     = random.AddTrack(file, artist, album);
                    var playlist  = random.AddPlaylist(user);
                    dbContext.SaveChanges();

                    int[] trackIds = new[] { track.TrackId, track.TrackId + 1 };
                    var   ex       = Assert.Throws <RestApiErrorException>(() => RestApiQueries.AddPlaylistSongsAsync(dbContext, user.UserId, playlist.PlaylistId, trackIds, CancellationToken.None).GetAwaiter().GetResult());

                    var expectedException = RestApiErrorException.DataNotFoundError();
                    Assert.Equal(expectedException.Message, ex.Message);
                    Assert.Equal(expectedException.Code, ex.Code);
                }
            }
            public static void TestGetMusicFoldersAsync()
            {
                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 directory = random.AddDirectory(library);
                    dbContext.SaveChanges();

                    var result = RestApiQueries.GetMusicFoldersAsync(dbContext, user.UserId, CancellationToken.None).GetAwaiter().GetResult();

                    Assert.NotNull(result);
                    Assert.NotNull(result.musicFolder);
                    Assert.Single(result.musicFolder);
                    Assert.Equal(library.LibraryId, result.musicFolder[0].id);
                    Assert.Equal(library.Name, result.musicFolder[0].name);
                }
            }
            public static void GetArtistsAsync_ArtistIsPlaceholder_ReturnsPlaceholderName()
            {
                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();
                    artist.Name     = null;
                    artist.SortName = null;
                    var album     = random.AddAlbum(artist);
                    var directory = random.AddDirectory(library);
                    var file      = random.AddFile(directory);
                    var track     = random.AddTrack(file, artist, album);
                    dbContext.SaveChanges();

                    var result = RestApiQueries.GetArtistsAsync(dbContext, user.UserId, null, CancellationToken.None).GetAwaiter().GetResult();

                    var resultArtist = Assert.Single(result.index.SelectMany(i => i.artist));
                    Assert.Equal("[no artist]", resultArtist.name);
                }
            }
            public static void GetArtistsAsync_LibraryIsSpecified_ReturnsExpectedArtists()
            {
                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 album          = random.AddAlbum(artist);
                    var directory      = random.AddDirectory(library);
                    var file           = random.AddFile(directory);
                    var track          = random.AddTrack(file, artist, album);
                    var otherDirectory = random.AddDirectory(otherLibrary);
                    var otherFile      = random.AddFile(otherDirectory);
                    var otherTrack     = random.AddTrack(otherFile, artist, album);
                    dbContext.SaveChanges();

                    var result = RestApiQueries.GetArtistsAsync(dbContext, user.UserId, library.LibraryId, CancellationToken.None).GetAwaiter().GetResult();

                    var resultArtist = Assert.Single(result.index.SelectMany(x => x.artist));
                    Assert.Equal("r" + artist.ArtistId, resultArtist.id);
                    Assert.Equal(artist.Name, resultArtist.name);
                    Assert.Equal(1, resultArtist.albumCount);
                }
            }
            public static void GetAlbumList2OrderedByAlbumTitleAsync_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 album          = random.AddAlbum(artist);
                    var directory      = random.AddDirectory(library);
                    var file           = random.AddFile(directory);
                    var track          = random.AddTrack(file, artist, album);
                    var otherDirectory = random.AddDirectory(otherLibrary);
                    var otherFile      = random.AddFile(otherDirectory);
                    var otherTrack     = random.AddTrack(otherFile, artist, album);
                    dbContext.SaveChanges();

                    var result = RestApiQueries.GetAlbumList2OrderedByAlbumTitleAsync(dbContext, user.UserId, library.LibraryId, 0, 10, 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 TestRemovePlaylistSongsAsync()
            {
                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 directory      = random.AddDirectory(library);
                    var trackFile      = random.AddFile(directory);
                    var artist         = random.AddArtist();
                    var album          = random.AddAlbum(artist);
                    var track1         = random.AddTrack(trackFile, artist, album);
                    var track2         = random.AddTrack(trackFile, artist, album);
                    var playlist       = random.AddPlaylist(user);
                    var playlistTrack1 = random.AddPlaylistTrack(playlist, track1, 0);
                    var playlistTrack2 = random.AddPlaylistTrack(playlist, track2, 1);
                    dbContext.SaveChanges();

                    RestApiQueries.RemovePlaylistSongsAsync(dbContext, user.UserId, playlist.PlaylistId, new[] { 0 }, CancellationToken.None).GetAwaiter().GetResult();
                    dbContext.SaveChanges();

                    Assert.False(dbContext.PlaylistTracks.Any(pt => pt.PlaylistId == playlist.PlaylistId && pt.TrackId == track1.TrackId));
                    Assert.True(dbContext.PlaylistTracks.Any(pt => pt.PlaylistId == playlist.PlaylistId && pt.TrackId == track2.TrackId));
                }
            }
            public static void GetAlbumList2OrderedByAlbumTitleAsync_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 album     = random.AddAlbum(artist);
                    var directory = random.AddDirectory(library);
                    var file      = random.AddFile(directory);
                    var track     = random.AddTrack(file, artist, album);
                    dbContext.SaveChanges();

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

                    var expectedException = RestApiErrorException.DataNotFoundError();
                    Assert.Equal(expectedException.Message, ex.Message);
                    Assert.Equal(expectedException.Code, ex.Code);
                }
            }
            public static void GetAlbumAsync_NoAccessibleTrack_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(accessControlled: true);
                    var artist    = random.AddArtist();
                    var album     = random.AddAlbum(artist);
                    var directory = random.AddDirectory(library);
                    var file      = random.AddFile(directory);
                    var track     = random.AddTrack(file, artist, album);
                    dbContext.SaveChanges();

                    string transcodedSuffix = "mp3";
                    var    ex = Assert.Throws <RestApiErrorException>(() => RestApiQueries.GetAlbumAsync(dbContext, user.UserId, album.AlbumId, transcodedSuffix, CancellationToken.None).GetAwaiter().GetResult());

                    var expectedException = RestApiErrorException.DataNotFoundError();
                    Assert.Equal(expectedException.Message, ex.Message);
                    Assert.Equal(expectedException.Code, ex.Code);
                }
            }
            public static void GetAlbumAsync_AccessibleTrack_ReturnsExpectedAlbumDetails()
            {
                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 album     = random.AddAlbum(artist);
                    var directory = random.AddDirectory(library);
                    var file      = random.AddFile(directory);
                    var track     = random.AddTrack(file, artist, album);
                    dbContext.SaveChanges();

                    string transcodedSuffix = "mp3";
                    var    result           = RestApiQueries.GetAlbumAsync(dbContext, user.UserId, album.AlbumId, transcodedSuffix, CancellationToken.None).GetAwaiter().GetResult();

                    var tracks = new[] { track };

                    Assert.Equal("a" + album.AlbumId, result.id);
                    Assert.Equal(album.Title, result.name);
                    Assert.Equal(album.Artist.Name, result.artist);
                    Assert.Equal("r" + artist.ArtistId, result.artistId);
                    Assert.Equal(album.CoverPictureId?.ToString("X8"), result.coverArt);
                    Assert.Equal(tracks.Count(), result.songCount);
                    Assert.Equal(Math.Round(tracks.Sum(t => t.Duration) ?? 0), result.duration);
                    Assert.False(result.playCountSpecified);
                    Assert.Equal(default, result.playCount);
Example #20
0
            public static void TestUnstarAlbumAsync()
            {
                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 artist    = random.AddArtist();
                    var album     = random.AddAlbum(artist);
                    var albumStar = random.AddAlbumStar(album, user);
                    dbContext.SaveChanges();

                    for (int i = 0; i < 2; ++i)
                    {
                        RestApiQueries.UnstarAlbumAsync(dbContext, user.UserId, album.AlbumId, CancellationToken.None).GetAwaiter().GetResult();
                        dbContext.SaveChanges();

                        Assert.False(dbContext.AlbumStars.Any(s => s.AlbumId == album.AlbumId && s.UserId == user.UserId));
                    }
                }
            }
            public static void DeletePlaylistAsync_PlaylistOwnedByUser_DeletesPlaylist(bool playlistIsPublic)
            {
                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 directory     = random.AddDirectory(library);
                    var file          = random.AddFile(directory);
                    var artist        = random.AddArtist();
                    var album         = random.AddAlbum(artist);
                    var track         = random.AddTrack(file, artist, album);
                    var playlist      = random.AddPlaylist(user, playlistIsPublic);
                    var playlistTrack = random.AddPlaylistTrack(playlist, track, 0);
                    dbContext.SaveChanges();

                    bool canDeleteAllPublicPlaylists = true;
                    RestApiQueries.DeletePlaylistAsync(dbContext, user.UserId, canDeleteAllPublicPlaylists, playlist.PlaylistId, CancellationToken.None).GetAwaiter().GetResult();
                    dbContext.SaveChanges();

                    Assert.False(dbContext.Playlists.Any(p => p.PlaylistId == playlist.PlaylistId));
                }
            }
            public static void TestGetStarred2Async()
            {
                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 directory  = random.AddDirectory(library);
                    var trackFile  = random.AddFile(directory);
                    var genre      = random.AddGenre();
                    var artist     = random.AddArtist();
                    var album      = random.AddAlbum(artist);
                    var track      = random.AddTrack(trackFile, artist, album, genre: genre);
                    var artistStar = random.AddArtistStar(artist, user);
                    var albumStar  = random.AddAlbumStar(album, user);
                    var trackStar  = random.AddTrackStar(track, user);
                    dbContext.SaveChanges();

                    var result = RestApiQueries.GetStarred2Async(dbContext, user.UserId, null, "opus", CancellationToken.None).GetAwaiter().GetResult();

                    Assert.NotNull(result);
                    // TODO
                }
            }
Example #23
0
            public static void TestGetUsersAsync()
            {
                var dbConnection = OpenSqliteDatabase();

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

                using (var dbContext = new MediaInfoContext(dbContextOptionsBuilder.Options))
                {
                    var random   = new RandomPopulator(dbContext);
                    var user1    = random.AddUser();
                    var user2    = random.AddUser();
                    var library1 = random.AddLibrary();
                    random.AddLibraryUser(library1, user1);
                    var library2 = random.AddLibrary();
                    random.AddLibraryUser(library2, user1);
                    random.AddLibraryUser(library2, user2);
                    dbContext.SaveChanges();

                    var result = RestApiQueries.GetUsersAsync(dbContext, CancellationToken.None).GetAwaiter().GetResult();

                    Assert.NotNull(result);
                    // TODO
                }
            }
            public static void GetGenresAsync_Always_ReturnsExpectedGenreDetails(string albumsTracksGenreNames)
            {
                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 genres      = new List <Genre>();
                    var trackGenres = new List <TrackGenre>();
                    foreach (string albumTracksGenreNames in albumsTracksGenreNames.Split(";"))
                    {
                        var directory = random.AddDirectory(library);
                        var album     = random.AddAlbum(artist);
                        foreach (string trackGenreNames in albumTracksGenreNames.Split(","))
                        {
                            var file  = random.AddFile(directory);
                            var track = random.AddTrack(file, artist, album);
                            foreach (string genreName in trackGenreNames.Split("/"))
                            {
                                var genre = genres.SingleOrDefault(g => g.Name == genreName);
                                if (genre == null)
                                {
                                    genre      = random.AddGenre();
                                    genre.Name = genreName;
                                    genres.Add(genre);
                                }
                                var trackGenre = random.AddTrackGenre(track, genre);
                                trackGenres.Add(trackGenre);
                            }
                        }
                    }
                    dbContext.SaveChanges();

                    var result = RestApiQueries.GetGenresAsync(dbContext, user.UserId, CancellationToken.None).GetAwaiter().GetResult();

                    Assert.Equal(genres.Count, result.genre.Length);
                    Assert.Equal(new SortedSet <string>(genres.Select(g => g.Name)),
                                 new SortedSet <string>(result.genre.Select(g => g.Text.Single())));
                    foreach (var resultGenre in result.genre)
                    {
                        string genreName   = resultGenre.Text.Single();
                        var    genreTracks = trackGenres.Where(rg => rg.Genre.Name == genreName).Select(tg => tg.Track);
                        var    genreAlbums = genreTracks.Select(t => t.Album).Distinct();

                        Assert.Equal(genreAlbums.Count(), resultGenre.albumCount);
                        Assert.Equal(genreTracks.Count(), resultGenre.songCount);
                    }
                }
            }
Example #25
0
            public static void GetAlbumList2StarredAsync_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 albums     = new List <Album>();
                    var albumStars = new List <AlbumStar>();
                    var tracks     = new List <Track>();
                    for (int i = 0; i < albumCount; ++i)
                    {
                        var album = random.AddAlbum(artist);
                        albums.Add(album);
                        var albumStar = random.AddAlbumStar(album, user);
                        albumStars.Add(albumStar);
                        var directory = random.AddDirectory(library);
                        var file      = random.AddFile(directory);
                        var track     = random.AddTrack(file, artist, album);
                        tracks.Add(track);
                    }
                    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.GetAlbumList2StarredAsync(dbContext, user.UserId, null, i, count, 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 albumStar   = albumStars.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);
            public static void GetArtistsAsync_Always_ReturnsExpectedArtistDetails(int[] albumCounts)
            {
                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 artists = new List <Artist>();
                    var albums  = new List <Album>();
                    var tracks  = new List <Track>();
                    for (int i = 0; i < albumCounts.Length; ++i)
                    {
                        var artist = random.AddArtist();
                        artists.Add(artist);
                        for (int j = 0; j < albumCounts[i]; ++j)
                        {
                            var directory = random.AddDirectory(library);
                            var album     = random.AddAlbum(artist);
                            albums.Add(album);
                            var file  = random.AddFile(directory);
                            var track = random.AddTrack(file, artist, album);
                            tracks.Add(track);
                        }
                    }
                    dbContext.SaveChanges();

                    var result = RestApiQueries.GetArtistsAsync(dbContext, user.UserId, null, CancellationToken.None).GetAwaiter().GetResult();

                    Assert.Equal(string.Empty, result.ignoredArticles);
                    Assert.Equal(artists.Count, result.index.Sum(i => i.artist.Length));
                    Assert.Equal(new SortedSet <string>(artists.Select(r => "r" + r.ArtistId)),
                                 new SortedSet <string>(result.index.SelectMany(i => i.artist).Select(a => a.id)));
                    foreach (var resultArtist in result.index.SelectMany(i => i.artist))
                    {
                        var artist       = artists.Single(r => "r" + r.ArtistId == resultArtist.id);
                        var artistAlbums = albums.Where(a => a.Artist == artist);

                        Assert.Equal(artist.Name, resultArtist.name);
                        Assert.Null(resultArtist.coverArt);
                        Assert.Equal(artistAlbums.Count(), resultArtist.albumCount);
                        Assert.False(resultArtist.starredSpecified);
                        Assert.Equal(default, resultArtist.starred);
            public static void GetGenresAsync_Always_GenresAreInExpectedOrder()
            {
                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 genres    = new List <Genre>();
                    var artist    = random.AddArtist();
                    var album     = random.AddAlbum();
                    var directory = random.AddDirectory(library);
                    foreach (string genreName in new[]
                    {
                        "A",
                        "a",
                        "C",
                        "𝓏",
                        "𓂀",
                        "B",
                        "b",
                    })
                    {
                        var genre = random.AddGenre();
                        genre.Name = genreName;
                        genres.Add(genre);
                        var file       = random.AddFile(directory);
                        var track      = random.AddTrack(file, artist, album);
                        var trackGenre = random.AddTrackGenre(track, genre);
                    }
                    dbContext.SaveChanges();

                    genres = genres
                             .OrderBy(g => g.Name, _stringComparer)
                             .ToList();

                    var result = RestApiQueries.GetGenresAsync(dbContext, user.UserId, CancellationToken.None).GetAwaiter().GetResult();

                    Assert.Equal(genres.Select(g => g.Name).ToArray(),
                                 result.genre.Select(g => g.Text.Single()).ToArray());
                }
            }
Example #28
0
            public static void AddPlaylistSongsAsync_VariousExistingAndAddedTrackCounts_AddsTracksToPlaylist(int tracksInPlaylistCount, int tracksToAddCount)
            {
                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 album     = random.AddAlbum(artist);
                    var directory = random.AddDirectory(library);
                    var file      = random.AddFile(directory);
                    var playlist  = random.AddPlaylist(user);
                    var tracks    = new List <Track>();
                    for (int i = 0; i < tracksInPlaylistCount; ++i)
                    {
                        var track = random.AddTrack(file, artist, album);
                        tracks.Add(track);
                        var playlistTrack = random.AddPlaylistTrack(playlist, track, i);
                    }
                    var tracksToAdd = new List <Track>();
                    for (int i = 0; i < tracksToAddCount; ++i)
                    {
                        var track = random.AddTrack(file, artist, album);
                        tracksToAdd.Add(track);
                    }
                    dbContext.SaveChanges();

                    int[] trackIdsToAdd = tracksToAdd.Select(t => t.TrackId).ToArray();
                    RestApiQueries.AddPlaylistSongsAsync(dbContext, user.UserId, playlist.PlaylistId, trackIdsToAdd, CancellationToken.None).GetAwaiter().GetResult();
                    dbContext.SaveChanges();

                    int[] expectedTrackIds = tracks.Concat(tracksToAdd).Select(t => t.TrackId).ToArray();

                    var playlistTracks = dbContext.PlaylistTracks
                                         .Where(pt => pt.PlaylistId == playlist.PlaylistId)
                                         .OrderBy(pt => pt.Index);
                    Assert.Equal(expectedTrackIds, playlistTracks.Select(pt => pt.TrackId).ToArray());
                    Assert.Equal(Enumerable.Range(0, expectedTrackIds.Length).ToArray(), playlistTracks.Select(pt => pt.Index).ToArray());
                }
            }
            public static void TestSetAllUserLibrariesAsync()
            {
                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();
                    dbContext.SaveChanges();

                    RestApiQueries.SetAllUserLibrariesAsync(dbContext, user.UserId, CancellationToken.None).GetAwaiter().GetResult();

                    // TODO
                }
            }
Example #30
0
            public static void CanAddTracksAsync_Empty_ReturnsTrue()
            {
                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();
                    dbContext.SaveChanges();

                    int[] trackIds = Array.Empty <int>();
                    bool  result   = RestApiQueries.CanAddTracksAsync(dbContext, user.UserId, trackIds, CancellationToken.None).GetAwaiter().GetResult();

                    Assert.True(result);
                }
            }