public async Task UpdateAsync_given_existing_character_Updates_properties() { var context = new Mock <IFuturamaContext>(); var entity = new Character { Id = 42 }; context.Setup(c => c.Characters.FindAsync(42)).ReturnsAsync(entity); using (var repository = new CharacterRepository(context.Object)) { var character = new CharacterCreateUpdateDTO { Id = 42, ActorId = 12, Name = "Name", Species = "Species", Planet = "Planet" }; await repository.UpdateAsync(character); } Assert.Equal(12, entity.ActorId); Assert.Equal("Name", entity.Name); Assert.Equal("Species", entity.Species); Assert.Equal("Planet", entity.Planet); }
public async Task UpdateAsync_given_non_existing_dto_returns_false() { using (var connection = await CreateConnectionAsync()) using (var context = await CreateContextAsync(connection)) { context.Actors.Add(new Actor { Name = "John DiMaggio" }); await context.SaveChangesAsync(); var repository = new CharacterRepository(context); var dto = new CharacterCreateUpdateDTO { Id = 1, ActorId = 1, Name = "Bender", Species = "Robot", Planet = "Earth" }; var updated = await repository.UpdateAsync(dto); Assert.False(updated); } }
public async Task CreateAsync_given_dto_returns_created_Character() { using (var connection = await CreateConnectionAsync()) using (var context = await CreateContextAsync(connection)) { context.Actors.Add(new Actor { Name = "John DiMaggio" }); await context.SaveChangesAsync(); var repository = new CharacterRepository(context); var dto = new CharacterCreateUpdateDTO { ActorId = 1, Name = "Bender", Species = "Robot", Planet = "Earth" }; var character = await repository.CreateAsync(dto); Assert.Equal(1, character.Id); Assert.Equal(1, character.ActorId); Assert.Equal("Bender", character.Name); Assert.Equal("Robot", character.Species); Assert.Equal("Earth", character.Planet); } }
public void Create_given_a_character_it_maps_to_character() { var character = default(Character); var dto = new CharacterCreateUpdateDTO { ActorId = 42, Name = "Turange Leela", Species = "Mutant, Human", Planet = "Earth" }; var mock = new Mock <IFuturamaContext>(); mock.Setup(s => s.Characters.Add(It.IsAny <Character>())) .Callback <Character>(c => character = c); using (var repository = new CharacterRepository(mock.Object)) { repository.Create(dto); } Assert.Equal(42, character.ActorId); Assert.Equal("Turange Leela", character.Name); Assert.Equal("Mutant, Human", character.Species); Assert.Equal("Earth", character.Planet); }
public EditCharacterPage(CharacterCreateUpdateDTO character) { InitializeComponent(); BindingContext = _viewModel = DependencyService.Resolve <EditCharacterViewModel>(); _viewModel.Navigation = Navigation; _viewModel.Init(character); }
public async Task <ActionResult <CharacterDTO> > Post([FromBody] CharacterCreateUpdateDTO character) { //if (!ModelState.IsValid) //{ // return BadRequest(ModelState); //} var dto = await _repository.CreateAsync(character); return(CreatedAtAction(nameof(Get), new { id = dto.Id }, dto)); }
public async Task <IActionResult> Post([FromBody] CharacterCreateUpdateDTO character) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var id = await _repository.CreateAsync(character); return(CreatedAtAction(nameof(Get), new { id }, null)); }
public async Task <IActionResult> Put(int id, [FromBody] CharacterCreateUpdateDTO character) { var updated = await _repository.UpdateAsync(character); if (updated) { return(NoContent()); } return(NotFound()); }
public async Task Create_given_invalid_character_returns_BadRequest() { var character = new CharacterCreateUpdateDTO(); var repository = new Mock <ICharacterRepository>(); var controller = new CharactersController(repository.Object); controller.ModelState.AddModelError(string.Empty, "Error"); var response = await controller.Post(character); Assert.IsType <BadRequestObjectResult>(response); }
public async Task <CharacterDTO> CreateAsync(CharacterCreateUpdateDTO character) { var entity = new Character { ActorId = character.ActorId, Name = character.Name, Species = character.Species, Planet = character.Planet, }; _context.Characters.Add(entity); await _context.SaveChangesAsync(); return(await FindAsync(entity.Id)); }
public async Task Create_given_character_returns_CreateAtAction() { var character = new CharacterCreateUpdateDTO { Name = "New guy", Species = "Human" }; var repository = new Mock <ICharacterRepository>(); repository.Setup(r => r.CreateAsync(character)).ReturnsAsync(42); var controller = new CharactersController(repository.Object); var response = await controller.Post(character) as CreatedAtActionResult; Assert.Equal("Get", response.ActionName); Assert.Equal(42, response.RouteValues["id"]); }
public int Create(CharacterCreateUpdateDTO character) { var entity = new Character { ActorId = character.ActorId, Name = character.Name, Species = character.Species, Planet = character.Planet, }; _context.Characters.Add(entity); _context.SaveChanges(); return(entity.Id); }
public async Task Create_given_character_calls_SaveChangesAsync() { var context = new Mock <IFuturamaContext>(); context.Setup(c => c.Characters.Add(It.IsAny <Character>())); using (var repository = new CharacterRepository(context.Object)) { var character = new CharacterCreateUpdateDTO { Name = "Name", Species = "Species", }; await repository.CreateAsync(character); } context.Verify(c => c.SaveChangesAsync(default(CancellationToken))); }
public async Task <bool> UpdateAsync(CharacterCreateUpdateDTO character) { var entity = await _context.Characters.FindAsync(character.Id); if (entity == null) { return(false); } entity.ActorId = character.ActorId; entity.Name = character.Name; entity.Species = character.Species; entity.Planet = character.Planet; await _context.SaveChangesAsync(); return(true); }
public async Task UpdateAsync_given_non_existing_dto_returns_false() { using (var connection = await CreateConnectionAsync()) using (var context = await CreateContextAsync(connection)) { var repository = new CharacterRepository(context); var dto = new CharacterCreateUpdateDTO { Id = 0, Name = "Bender", Species = "Robot", Planet = "Earth" }; var updated = await repository.UpdateAsync(dto); Assert.False(updated); } }
public async Task Update_given_non_existing_character_returns_false() { var context = new Mock <IFuturamaContext>(); context.Setup(c => c.Characters.FindAsync(42)).ReturnsAsync(default(Character)); using (var repository = new CharacterRepository(context.Object)) { var character = new CharacterCreateUpdateDTO { Id = 42, Name = "Name", Species = "Species", }; var result = await repository.UpdateAsync(character); Assert.False(result); } }
public async Task Update_given_non_existing_character_does_not_SaveChangesAsync() { var context = new Mock <IFuturamaContext>(); context.Setup(c => c.Characters.FindAsync(42)).ReturnsAsync(default(Character)); using (var repository = new CharacterRepository(context.Object)) { var character = new CharacterCreateUpdateDTO { Id = 42, Name = "Name", Species = "Species", }; await repository.UpdateAsync(character); } context.Verify(c => c.SaveChangesAsync(default(CancellationToken)), Times.Never); }
public async Task UpdateAsync_given_existing_dto_updates_entity() { using (var connection = await CreateConnectionAsync()) using (var context = await CreateContextAsync(connection)) { context.Actors.Add(new Actor { Name = "John DiMaggio" }); await context.SaveChangesAsync(); context.Characters.Add(new Character { Name = "Fry", Species = "Human" }); await context.SaveChangesAsync(); var repository = new CharacterRepository(context); var dto = new CharacterCreateUpdateDTO { Id = 1, ActorId = 1, Name = "Bender", Species = "Robot", Planet = "Earth" }; var updated = await repository.UpdateAsync(dto); Assert.True(updated); var entity = await context.Characters.FindAsync(1); Assert.Equal(1, entity.ActorId); Assert.Equal("Bender", entity.Name); Assert.Equal("Robot", entity.Species); Assert.Equal("Earth", entity.Planet); } }
public async Task Create_given_character_returns_new_Id() { var entity = default(Character); var context = new Mock <IFuturamaContext>(); context.Setup(c => c.Characters.Add(It.IsAny <Character>())) .Callback <Character>(t => entity = t); context.Setup(c => c.SaveChangesAsync(default(CancellationToken))) .Returns(Task.FromResult(0)) .Callback(() => entity.Id = 42); using (var repository = new CharacterRepository(context.Object)) { var character = new CharacterCreateUpdateDTO { Name = "Name", Species = "Species", }; var id = await repository.CreateAsync(character); Assert.Equal(42, id); } }
public async Task CreateAsync_given_character_adds_it() { var entity = default(Character); var context = new Mock <IFuturamaContext>(); context.Setup(c => c.Characters.Add(It.IsAny <Character>())).Callback <Character>(t => entity = t); using (var repository = new CharacterRepository(context.Object)) { var character = new CharacterCreateUpdateDTO { ActorId = 1, Name = "Name", Species = "Species", Planet = "Planet" }; await repository.CreateAsync(character); } Assert.Equal(1, entity.ActorId); Assert.Equal("Name", entity.Name); Assert.Equal("Species", entity.Species); Assert.Equal("Planet", entity.Planet); }
public void Update(CharacterCreateUpdateDTO character) { throw new NotImplementedException(); }
public async Task <CharacterDTO> CreateAsync(CharacterCreateUpdateDTO character) { var response = await _client.PostAsJsonAsync("api/characters", character); return(await response.Content.ReadAsAsync <CharacterDTO>()); }
public async Task <bool> UpdateAsync(CharacterCreateUpdateDTO character) { var response = await _client.PutAsJsonAsync($"api/characters/{character.Id}", character); return(response.IsSuccessStatusCode); }
public Task UpdateAsync(CharacterCreateUpdateDTO character) { throw new NotImplementedException(); }
public async Task <ActionResult <CharacterDTO> > Post([FromBody] CharacterCreateUpdateDTO character) { var dto = await _repository.CreateAsync(character); return(CreatedAtAction(nameof(Get), new { id = dto.Id }, dto)); }