Ejemplo n.º 1
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());
                }
            }
Ejemplo n.º 2
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 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());
                }
            }
            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));
                }
            }
Ejemplo n.º 5
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));
                }
            }
Ejemplo n.º 6
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 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 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 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 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"));
                }
            }
            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 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);
                }
            }
Ejemplo n.º 14
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);
                }
            }
            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 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));
                    }
                }
            }
            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 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);
                }
            }
Ejemplo n.º 19
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));
                    }
                }
            }
Ejemplo n.º 20
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 CreateUserAsync_UsernameNotAlreadyExists_CreatesUser(bool isAdmin, bool isGuest, bool canJukebox)
            {
                var dbConnection = OpenSqliteDatabase();

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

                using (var dbContext = new MediaInfoContext(dbContextOptionsBuilder.Options))
                {
                    const string username = "******";
                    const string password = "******";

                    int userId = RestApiQueries.CreateUserAsync(dbContext, username, password, isAdmin, isGuest, canJukebox, CancellationToken.None).GetAwaiter().GetResult();
                    var users  = dbContext.Users.Local.Where(u => u.UserId == userId).ToArray();
                    dbContext.SaveChanges();

                    var user = Assert.Single(users);
                    Assert.Equal(username, user.Name);
                    Assert.Equal(password, user.Password);
                    Assert.Equal(128_000, user.MaxBitRate);
                    Assert.Equal(isAdmin, user.IsAdmin);
                    Assert.Equal(isGuest, user.IsGuest);
                    Assert.Equal(canJukebox, user.CanJukebox);
                    Assert.Empty(dbContext.LibraryUsers.Where(lu => lu.UserId == user.UserId));
                    Assert.Empty(dbContext.Playlists.Where(p => p.UserId == user.UserId));
                }
            }
            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
                }
            }
            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);
        public async Task <Models.View.Name> Get(string id)
        {
            using (var context = new MediaInfoContext())
            {
                var name = await context.Name.SingleOrDefaultAsync(x => x.ValueId == id);

                return(new Models.View.Name(name));
            }
        }
Ejemplo n.º 26
0
        internal RandomPopulator(MediaInfoContext dbContext)
        {
            if (dbContext == null)
            {
                throw new ArgumentNullException(nameof(dbContext));
            }

            _dbContext = dbContext;
        }
Ejemplo n.º 27
0
        public async Task <Models.View.Title> Get(string id)
        {
            using (var context = new MediaInfoContext())
            {
                var title = await context.Title.Include(x => x.TitleName).Include("TitleName.Role").Include("TitleName.Name").SingleOrDefaultAsync(x => x.ValueId == id);

                return(new Models.View.Title(title));
            }
        }
            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);
                    }
                }
            }
Ejemplo n.º 29
0
 public async Task <IEnumerable <Models.View.Title> > Get(string nameId, TitlesByNameFilter filter)
 {
     using (var context = new MediaInfoContext())
     {
         return((await context.Name.Include(x => x.TitleName).Include("TitleName.Title").Include("TitleName.Role")
                 .SingleOrDefaultAsync(x => x.ValueId == nameId))
                .TitleName
                .Select(x => new Models.View.Title(x.Title)));
     }
 }
Ejemplo n.º 30
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);