Esempio n. 1
0
            public static void AddPlaylistSongsAsync_InaccessibleTrack_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);
                    var playlist  = random.AddPlaylist(user);
                    dbContext.SaveChanges();

                    int[] trackIds = new[] { track.TrackId };
                    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);
                }
            }
Esempio n. 2
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());
                }
            }
Esempio n. 3
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());
                }
            }