public void Update_given_existing_entity_uses_existing_city() { var builder = new DbContextOptionsBuilder <SuperheroContext>().UseInMemoryDatabase(nameof(Update_given_no_entity_returns_NotFound)); var context = new SuperheroContext(builder.Options); var entity = new Superhero { Name = "Bruce Wayne", AlterEgo = "Batman" }; context.Superheroes.Add(entity); var entityCity = new City { Name = "Metropolis" }; context.Cities.Add(entityCity); context.SaveChanges(); var repository = new SuperheroRepository(context); var superhero = new SuperheroUpdateDTO { Id = entity.Id, Name = "Clark Kent", AlterEgo = "Superman", CityName = "Metropolis" }; var response = repository.Update(superhero); var updated = context.Superheroes.Find(entity.Id); Assert.Equal("Clark Kent", updated.Name); Assert.Equal("Superman", updated.AlterEgo); Assert.Equal(entityCity.Id, updated.CityId); }
public async Task Update_turns_Batman_into_Catwoman() { var superhero = new SuperheroUpdateDTO { Id = 2, Name = "Selina Kyle", AlterEgo = "Catwoman", CityName = "Gotham City", Occupation = "Thief", Gender = Gender.Female, FirstAppearance = 1940, Powers = new HashSet <string> { "exceptional martial artist", "gymnastic ability", "combat skill" } }; var result = await _repository.Update(superhero); Assert.Equal(Updated, result); var entity = await _context.Superheroes .Include(h => h.City) .Include(h => h.Powers) .ThenInclude(h => h.Power) .FirstOrDefaultAsync(h => h.Id == 2); Assert.Equal("Selina Kyle", entity.Name); Assert.Equal("Catwoman", entity.AlterEgo); Assert.Equal("Thief", entity.Occupation); Assert.Equal(Female, entity.Gender); Assert.Equal(1940, entity.FirstAppearance); Assert.Equal(2, entity.CityId); Assert.Equal("Gotham City", entity.City.Name); Assert.Equal(new[] { "exceptional martial artist", "gymnastic ability", "combat skill" }, entity.Powers.Select(p => p.Power.Name).ToHashSet() ); }
public void Update_given_no_entity_returns_NotFound() { var builder = new DbContextOptionsBuilder <SuperheroContext>().UseInMemoryDatabase(nameof(Update_given_no_entity_returns_NotFound)); var context = new SuperheroContext(builder.Options); var repository = new SuperheroRepository(context); var superhero = new SuperheroUpdateDTO { Id = 42 }; var response = repository.Update(superhero); Assert.Equal(NotFound, response); }