Exemplo n.º 1
0
        public async Task Update_Genre_Should_Persiste()
        {
            using (var factory = new MusicStoreContextFactory())
            {
                int genreId = 0;
                using (var context = factory.CreateMusicStoreContext())
                {
                    var genre = new GenreEntity
                    {
                        Name = "Name"
                    };
                    context.Genres.Add(genre);
                    context.SaveChanges();

                    genreId = genre.Id;
                }
                using (var context = factory.CreateMusicStoreContext())
                {
                    var genreForUpdateDto = new GenreForUpdatingDto
                    {
                        Id   = genreId,
                        Name = "Updated"
                    };
                    var unitOfWork = new UnitOfWork(context, _mapper);
                    await unitOfWork.Genres.UpdateAsync(genreForUpdateDto);

                    await unitOfWork.SaveAsync();
                }
                using (var context = factory.CreateMusicStoreContext())
                {
                    var genre = context.Genres.First();
                    Assert.Equal("Updated", genre.Name);
                }
            }
        }
Exemplo n.º 2
0
        public async Task Add_Existing_Song_ToExisting_Album_Should_Persisite()
        {
            using (var factory = new MusicStoreContextFactory())
            {
                int songId  = 0;
                int albumId = 0;
                using (var context = factory.CreateMusicStoreContext())
                {
                    var song  = new SongEntity();
                    var album = new AlbumEntity();

                    context.Songs.Add(song);
                    context.Albums.Add(album);
                    context.SaveChanges();

                    songId  = song.Id;
                    albumId = album.Id;
                }
                using (var context = factory.CreateMusicStoreContext())
                {
                    var unitOfWork = new UnitOfWork(context, _mapper);
                    await unitOfWork.Songs.AddSongToAlbum(songId, albumId);

                    await unitOfWork.SaveAsync();
                }
                using (var context = factory.CreateMusicStoreContext())
                {
                    var song = context.Songs.Include(s => s.Album).First();
                    Assert.NotNull(song.Album);
                }
            }
        }
Exemplo n.º 3
0
        public async Task Delete_Song_Should_Persiste_Without_Deleting_The_Associated_Album()
        {
            using (var factory = new MusicStoreContextFactory())
            {
                int songId = 0;
                using (var context = factory.CreateMusicStoreContext())
                {
                    context.Database.EnsureCreated();

                    var song = new SongEntity
                    {
                        Name  = "First Song",
                        Album = new AlbumEntity()
                    };
                    context.Songs.Add(song);
                    context.SaveChanges();

                    songId = song.Id;
                }
                using (var context = factory.CreateMusicStoreContext())
                {
                    var unitOfWork = new UnitOfWork(context, _mapper);
                    await unitOfWork.Songs.DeleteAsync(songId);

                    await unitOfWork.SaveAsync();
                }
                using (var context = factory.CreateMusicStoreContext())
                {
                    var song  = context.Songs.FirstOrDefault();
                    var album = context.Albums.First();
                    Assert.Null(song);
                    Assert.NotNull(album);
                }
            }
        }
Exemplo n.º 4
0
        public async Task Delete_Album_Should_Persiste()
        {
            using (var factory = new MusicStoreContextFactory())
            {
                var albumId = 0;
                using (var context = factory.CreateMusicStoreContext())
                {
                    var album = new AlbumEntity
                    {
                        Name        = "Name",
                        Description = "Description"
                    };
                    context.Albums.Add(album);
                    context.SaveChanges();

                    albumId = album.Id;
                }
                using (var context = factory.CreateMusicStoreContext())
                {
                    var unitOfWork = new UnitOfWork(context, _mapper);
                    await unitOfWork.Albums.DeleteAsync(albumId);

                    await unitOfWork.SaveAsync();
                }
                using (var context = factory.CreateMusicStoreContext())
                {
                    var album = context.Albums.FirstOrDefault();
                    Assert.Null(album);
                }
            }
        }
Exemplo n.º 5
0
        public async Task Get_Album_Should_Not_Be_Null()
        {
            using (var factory = new MusicStoreContextFactory())
            {
                var albumId = 0;
                using (var context = factory.CreateMusicStoreContext())
                {
                    var album = new AlbumEntity
                    {
                        Name        = "Name",
                        Description = "Description"
                    };
                    context.Albums.Add(album);
                    context.SaveChanges();

                    albumId = album.Id;
                }
                using (var context = factory.CreateMusicStoreContext())
                {
                    var unitOfWork = new UnitOfWork(context, _mapper);
                    var album      = await unitOfWork.Albums.GetAsync(albumId);

                    Assert.NotNull(album);
                }
            }
        }
Exemplo n.º 6
0
        public async Task Adding_Song_With_Album_Should_Persiste()
        {
            using (var factory = new MusicStoreContextFactory())
            {
                using (var context = factory.CreateMusicStoreContext())
                {
                    var unitOfWork = new UnitOfWork(context, _mapper);

                    var album = new AlbumEntity();
                    context.Albums.Add(album);
                    context.SaveChanges();

                    var song = new SongForCreatingDto
                    {
                        Name    = "First Song",
                        AlbumId = album.Id
                    };

                    await unitOfWork.Songs.AddAsync(song);

                    await unitOfWork.SaveAsync();
                }
                using (var context = factory.CreateMusicStoreContext())
                {
                    var song = context.Songs.Include(a => a.Album).First();
                    Assert.NotNull(song.Album);
                }
            }
        }
Exemplo n.º 7
0
        public async Task Delete_Album_Should_Persiste()
        {
            using (var factory = new MusicStoreContextFactory())
            {
                int genreId = 0;
                using (var context = factory.CreateMusicStoreContext())
                {
                    context.Database.EnsureCreated();

                    var genre = new GenreEntity
                    {
                        Name = "Name"
                    };
                    context.Genres.Add(genre);
                    context.SaveChanges();

                    genreId = genre.Id;
                }
                using (var context = factory.CreateMusicStoreContext())
                {
                    var unitOfWork = new UnitOfWork(context, _mapper);
                    await unitOfWork.Genres.DeleteAsync(genreId);

                    await unitOfWork.SaveAsync();
                }
                using (var context = factory.CreateMusicStoreContext())
                {
                    var genre = context.Genres.FirstOrDefault();
                    Assert.Null(genre);
                }
            }
        }
Exemplo n.º 8
0
        public async Task Update_Song_With_Genres_Should_Persiste()
        {
            using (var factory = new MusicStoreContextFactory())
            {
                var genresIds = new List <int>();
                var songId    = 0;
                using (var context = factory.CreateMusicStoreContext())
                {
                    var genres = new List <GenreEntity>
                    {
                        new GenreEntity(),
                        new GenreEntity(),
                        new GenreEntity()
                    };
                    context.Genres.AddRange(genres);
                    context.SaveChanges();
                    genresIds = genres.Select(s => s.Id).ToList();

                    var song = new SongEntity
                    {
                        Name      = "First Song",
                        GenreSong = new List <GenreSongEntity>
                        {
                            new GenreSongEntity
                            {
                                GenreId = genresIds[0]
                            },
                            new GenreSongEntity
                            {
                                GenreId = genresIds[1]
                            }
                        }
                    };
                    context.Songs.Add(song);
                    context.SaveChanges();

                    songId = song.Id;
                }
                using (var context = factory.CreateMusicStoreContext())
                {
                    var unitOfWork = new UnitOfWork(context, _mapper);
                    var songDto    = new SongForUpdatingDto
                    {
                        Id        = songId,
                        GenresIds = new List <int>
                        {
                            genresIds[2]
                        }
                    };
                    await unitOfWork.Songs.UpdateAsync(songDto);

                    await unitOfWork.SaveAsync();
                }
                using (var context = factory.CreateMusicStoreContext())
                {
                    var song = context.Songs.Include(c => c.GenreSong).First();
                    Assert.Single(song.GenreSong);
                }
            }
        }
Exemplo n.º 9
0
        public async Task Adding_Song_With_Invalid_AlbumId_Should_NOT_Persiste()
        {
            using (var factory = new MusicStoreContextFactory())
            {
                using (var context = factory.CreateMusicStoreContext())
                {
                    var unitOfWork = new UnitOfWork(context, _mapper);

                    var song = new SongForCreatingDto
                    {
                        Name    = "First Song",
                        AlbumId = 1
                    };

                    try
                    {
                        await unitOfWork.Songs.AddAsync(song);

                        await unitOfWork.SaveAsync();
                    }
                    catch { }
                }
                using (var context = factory.CreateMusicStoreContext())
                {
                    Assert.Null(context.Songs.FirstOrDefault());
                }
            }
        }
Exemplo n.º 10
0
        public async Task Get_Song_Should_Include_Genres_And_Album()
        {
            using (var factory = new MusicStoreContextFactory())
            {
                int songId = 0;
                using (var context = factory.CreateMusicStoreContext())
                {
                    context.Database.EnsureCreated();

                    var song = new SongEntity
                    {
                        Name  = "First Song",
                        Album = new AlbumEntity()
                    };
                    var genreSongs = new List <GenreSongEntity>
                    {
                        new GenreSongEntity
                        {
                            Genre = new GenreEntity
                            {
                                Name = "Romantic"
                            },
                            Song = song
                        },
                        new GenreSongEntity
                        {
                            Genre = new GenreEntity
                            {
                                Name = "Classic"
                            },
                            Song = song
                        }
                    };
                    context.AddRange(genreSongs);
                    context.SaveChanges();

                    songId = song.Id;
                }
                using (var context = factory.CreateMusicStoreContext())
                {
                    var unitOfWork = new UnitOfWork(context, _mapper);

                    var song = await unitOfWork.Songs.GetAsync(songId);

                    Assert.NotNull(song.Album);
                    Assert.Equal(2, song.Genres.Count);
                }
            }
        }
Exemplo n.º 11
0
        public async Task Get_Album_Should_Include_Songs_And_Their_Genres()
        {
            using (var factory = new MusicStoreContextFactory())
            {
                var albumId = 0;
                using (var context = factory.CreateMusicStoreContext())
                {
                    var album = new AlbumEntity
                    {
                        Name        = "Name",
                        Description = "Description"
                    };
                    context.Albums.Add(album);
                    context.SaveChanges();
                    albumId = album.Id;

                    var song = new SongEntity()
                    {
                        AlbumId   = albumId,
                        GenreSong = new List <GenreSongEntity>()
                        {
                            new GenreSongEntity
                            {
                                Genre = new GenreEntity(),
                            },
                            new GenreSongEntity
                            {
                                Genre = new GenreEntity(),
                            }
                        }
                    };
                    context.Songs.Add(song);
                    context.SaveChanges();
                }
                using (var context = factory.CreateMusicStoreContext())
                {
                    var unitOfWork = new UnitOfWork(context, _mapper);
                    var album      = await unitOfWork.Albums.GetAsync(albumId);

                    Assert.NotNull(album);
                    Assert.Equal(2, album.Songs[0].Genres.Count);
                    Assert.NotNull(album.Songs[0].Genres[0]);
                    Assert.NotNull(album.Songs[0].Genres[1]);
                }
            }
        }
Exemplo n.º 12
0
        public async Task Adding_Song_With_Genres_Should_Persiste()
        {
            using (var factory = new MusicStoreContextFactory())
            {
                var genresIds = new List <int>();
                using (var context = factory.CreateMusicStoreContext())
                {
                    var genres = new List <GenreEntity>
                    {
                        new GenreEntity(),
                        new GenreEntity()
                    };
                    context.Genres.AddRange(genres);
                    context.SaveChanges();

                    genresIds.Add(genres[0].Id);
                    genresIds.Add(genres[1].Id);
                }
                using (var context = factory.CreateMusicStoreContext())
                {
                    var unitOfWork = new UnitOfWork(context, _mapper);
                    var song       = new SongForCreatingDto
                    {
                        Name      = "First Song",
                        GenresIds = new List <int>
                        {
                            genresIds[0],
                            genresIds[1]
                        }
                    };
                    await unitOfWork.Songs.AddAsync(song);

                    await unitOfWork.SaveAsync();
                }
                using (var context = factory.CreateMusicStoreContext())
                {
                    var song = context.Songs.Include(c => c.GenreSong).First();
                    Assert.Equal(2, song.GenreSong.Count);
                }
            }
        }
Exemplo n.º 13
0
        public async Task Update_Album_Should_Persiste()
        {
            using (var factory = new MusicStoreContextFactory())
            {
                var albumId = 0;
                using (var context = factory.CreateMusicStoreContext())
                {
                    var album = new AlbumEntity
                    {
                        Name        = "Name",
                        Description = "Description"
                    };
                    context.Albums.Add(album);
                    context.SaveChanges();

                    albumId = album.Id;
                }
                using (var context = factory.CreateMusicStoreContext())
                {
                    var albumForUpdateDto = new AlbumForUpdatingDto
                    {
                        Id          = albumId,
                        Name        = "Updated",
                        Description = "Updated"
                    };
                    var unitOfWork = new UnitOfWork(context, _mapper);
                    await unitOfWork.Albums.UpdateAsync(albumForUpdateDto);

                    await unitOfWork.SaveAsync();
                }
                using (var context = factory.CreateMusicStoreContext())
                {
                    var album = context.Albums.First();
                    Assert.Equal("Updated", album.Name);
                    Assert.Equal("Updated", album.Description);
                }
            }
        }
Exemplo n.º 14
0
        public async Task Add_Genre_Should_Persiste()
        {
            using (var factory = new MusicStoreContextFactory())
            {
                using (var context = factory.CreateMusicStoreContext())
                {
                    var unitOfWork = new UnitOfWork(context, _mapper);

                    var genre = new GenreForCreatingDto
                    {
                        Name = "Genre"
                    };

                    await unitOfWork.Genres.AddAsync(genre);

                    await unitOfWork.SaveAsync();
                }
                using (var context = factory.CreateMusicStoreContext())
                {
                    Assert.NotNull(context.Genres.First());
                }
            }
        }
Exemplo n.º 15
0
        public async Task Adding_Song_Should_Persiste()
        {
            using (var factory = new MusicStoreContextFactory())
            {
                using (var context = factory.CreateMusicStoreContext())
                {
                    var unitOfWork = new UnitOfWork(context, _mapper);

                    var song = new SongForCreatingDto
                    {
                        Name = "First Song"
                    };

                    await unitOfWork.Songs.AddAsync(song);

                    await unitOfWork.SaveAsync();
                }
                using (var context = factory.CreateMusicStoreContext())
                {
                    Assert.NotNull(context.Songs.First());
                }
            }
        }
Exemplo n.º 16
0
        public async Task Add_Album_Should_Persiste()
        {
            using (var factory = new MusicStoreContextFactory())
            {
                using (var context = factory.CreateMusicStoreContext())
                {
                    var unitOfWork = new UnitOfWork(context, _mapper);

                    var album = new AlbumForCreatingDto
                    {
                        Name = "First Album"
                    };

                    await unitOfWork.Albums.AddAsync(album);

                    await unitOfWork.SaveAsync();
                }
                using (var context = factory.CreateMusicStoreContext())
                {
                    Assert.NotNull(context.Albums.First());
                }
            }
        }
Exemplo n.º 17
0
        public async Task Delete_Album_Should_Not_Delete_The_Songs_In_That_Album()
        {
            var albumId = 0;

            using (var factory = new MusicStoreContextFactory())
            {
                using (var context = factory.CreateMusicStoreContext())
                {
                    var album = new AlbumEntity
                    {
                        Name        = "Name",
                        Description = "Description",
                        Songs       = new List <SongEntity>
                        {
                            new SongEntity()
                        }
                    };
                    context.Albums.Add(album);
                    context.SaveChanges();

                    albumId = album.Id;
                }
                using (var context = factory.CreateMusicStoreContext())
                {
                    var unitOfWork = new UnitOfWork(context, _mapper);
                    await unitOfWork.Albums.DeleteAsync(albumId);

                    await unitOfWork.SaveAsync();
                }
                using (var context = factory.CreateMusicStoreContext())
                {
                    var song = context.Songs.Include(s => s.Album).First();
                    Assert.NotNull(song);
                    Assert.Null(song.Album);
                }
            }
        }
Exemplo n.º 18
0
        public async Task Update_Song_Should_Persiste()
        {
            using (var factory = new MusicStoreContextFactory())
            {
                int songId = 0;
                using (var context = factory.CreateMusicStoreContext())
                {
                    var song = new SongEntity
                    {
                        Name  = "First Song",
                        Album = new AlbumEntity()
                    };
                    context.Songs.Add(song);
                    context.SaveChanges();

                    songId = song.Id;
                }
                using (var context = factory.CreateMusicStoreContext())
                {
                    var songForUpdateDto = new SongForUpdatingDto
                    {
                        Id   = songId,
                        Name = "Updated"
                    };
                    var unitOfWork = new UnitOfWork(context, _mapper);
                    await unitOfWork.Songs.UpdateAsync(songForUpdateDto);

                    await unitOfWork.SaveAsync();
                }
                using (var context = factory.CreateMusicStoreContext())
                {
                    var song = context.Songs.First();
                    Assert.Equal("Updated", song.Name);
                    Assert.Null(song.AlbumId);
                }
            }
        }
Exemplo n.º 19
0
        public async Task Get_Song_Page_Should_Include_Album_And_Include_Genere()
        {
            using (var factory = new MusicStoreContextFactory())
            {
                using (var context = factory.CreateMusicStoreContext())
                {
                    var genres = new List <GenreEntity>
                    {
                        new GenreEntity {
                            Name = "1"
                        },
                        new GenreEntity {
                            Name = "2"
                        },
                    };
                    var songs = new List <SongEntity>
                    {
                        new SongEntity {
                            Album = new AlbumEntity()
                        },
                        new SongEntity {
                            Album = new AlbumEntity()
                        }
                    };

                    var genreSongs = new List <GenreSongEntity>
                    {
                        new GenreSongEntity
                        {
                            Genre = genres[0],
                            Song  = songs[0]
                        },
                        new GenreSongEntity
                        {
                            Genre = genres[1],
                            Song  = songs[0]
                        },
                        new GenreSongEntity
                        {
                            Genre = genres[0],
                            Song  = songs[1]
                        },
                        new GenreSongEntity
                        {
                            Genre = genres[1],
                            Song  = songs[1]
                        },
                    };
                    context.AddRange(genreSongs);
                    context.SaveChanges();
                }
                using (var context = factory.CreateMusicStoreContext())
                {
                    var unitOfWork = new UnitOfWork(context, _mapper);

                    var songPage = await unitOfWork.Songs.GetSongPage();

                    foreach (var song in songPage.TResult)
                    {
                        Assert.NotNull(song.Album);
                        Assert.Equal(2, song.Genres.Count);
                    }
                }
            }
        }