public async Task EditAsync_ShouldSuccessfullyEdit() { MapperInitializer.InitializeMapper(); var context = ApplicationDbContextInMemoryFactory.InitializeContext(); await this.SeedDataAsync(context); var truckService = new TruckService(context); var editTruckServiceModel = new EditTruckServiceModel(); var truckId = 1; var truckRegistrationNumber = "FN 3"; editTruckServiceModel.Id = truckId; editTruckServiceModel.RegistrationNumber = truckRegistrationNumber; await truckService.EditAsync(editTruckServiceModel); var expectedResult = truckRegistrationNumber; var actualResult = truckService .All() .First() .RegistrationNumber; Assert.True(expectedResult == actualResult); }
public async Task EditAsync(EditTruckServiceModel editTruckServiceModel) { var truck = await this.context .Trucks .FindAsync(editTruckServiceModel.Id); if (truck == null) { throw new ArgumentNullException(string.Format(InvalidTruckIdErrorMessage, editTruckServiceModel.Id)); } if (string.IsNullOrWhiteSpace(editTruckServiceModel.RegistrationNumber)) { throw new ArgumentNullException(EmptyTruckErrorMessage); } if (await this.context.Trucks.AnyAsync(t => t.RegistrationNumber == editTruckServiceModel.RegistrationNumber)) { throw new InvalidOperationException(TruckExistErrorMessage); } if (editTruckServiceModel.RegistrationNumber.Length > AttributesConstraints.TruckRegistrationNumberMaxLength) { throw new InvalidOperationException(string.Format(TruckRegistrationNumberMaxLengthErrorMessage, AttributesConstraints.TruckRegistrationNumberMaxLength)); } truck.RegistrationNumber = editTruckServiceModel.RegistrationNumber; await this.context.SaveChangesAsync(); }
public async Task EditAsync_WithNonExistingIdShouldThrowArgumentNullException() { MapperInitializer.InitializeMapper(); var context = ApplicationDbContextInMemoryFactory.InitializeContext(); await this.SeedDataAsync(context); var truckService = new TruckService(context); var editTruckServiceModel = new EditTruckServiceModel(); editTruckServiceModel.Id = 3; await Assert.ThrowsAsync <ArgumentNullException>(async() => { await truckService.EditAsync(editTruckServiceModel); }); }
public async Task EditAsync_WithOverMaxNameLengthShouldThrowInvalidOperationException() { MapperInitializer.InitializeMapper(); var context = ApplicationDbContextInMemoryFactory.InitializeContext(); await this.SeedDataAsync(context); var truckService = new TruckService(context); var editTruckServiceModel = new EditTruckServiceModel(); var truckRegistrationNumber = "qwertyuiop qwertyuiop qwertyuiop qwertyuiop qwertyuiop"; editTruckServiceModel.RegistrationNumber = truckRegistrationNumber; editTruckServiceModel.Id = 1; var message = "Truck's Registration number cannot be more than 8 characters."; var exception = await Assert.ThrowsAsync <InvalidOperationException>(async() => { await truckService.EditAsync(editTruckServiceModel); }); Assert.Equal(message, exception.Message); }
public async Task EditAsync_WithExistingNameShouldThrowInvalidOperationException() { MapperInitializer.InitializeMapper(); var context = ApplicationDbContextInMemoryFactory.InitializeContext(); await this.SeedDataAsync(context); var truckService = new TruckService(context); var editTruckServiceModel = new EditTruckServiceModel(); var truckregistrationNumber = "TRN 2"; editTruckServiceModel.RegistrationNumber = truckregistrationNumber; editTruckServiceModel.Id = 1; var message = "Truck's Registration number already exists."; var exception = await Assert.ThrowsAsync <InvalidOperationException>(async() => { await truckService.EditAsync(editTruckServiceModel); }); Assert.Equal(message, exception.Message); }