public void UpdatePersonByIdExceptionThrow() { // Arrange var mockDynamoDb = new Mock <IDynamoDBContext>(); _classUnderTest = new DynamoDbGateway(mockDynamoDb.Object, _mockUpdater.Object, _logger.Object); var id = Guid.NewGuid(); var query = ConstructQuery(id); var constructRequest = ConstructRequest(); var exception = new ApplicationException("Test exception"); mockDynamoDb.Setup(x => x.LoadAsync <PersonDbEntity>(id, default)) .ThrowsAsync(exception); // Act Func <Task <UpdateEntityResult <PersonDbEntity> > > func = async() => await _classUnderTest.UpdatePersonByIdAsync(constructRequest, RequestBody, query, 0) .ConfigureAwait(false); // Assert func.Should().Throw <ApplicationException>().WithMessage(exception.Message); mockDynamoDb.Verify(x => x.LoadAsync <PersonDbEntity>(id, default), Times.Once); _logger.VerifyExact(LogLevel.Debug, $"Calling IDynamoDBContext.SaveAsync to update id {query.Id}", Times.Never()); }
public async Task UpdatePersonSuccessfulUpdates() { // Arrange var person = ConstructPerson(); var personDb = person.ToDatabase(); var query = ConstructQuery(person.Id); await _dbFixture.SaveEntityAsync(personDb).ConfigureAwait(false); var constructRequest = ConstructRequest(); var updatedPerson = person.DeepClone(); updatedPerson.FirstName = constructRequest.FirstName; updatedPerson.PlaceOfBirth = constructRequest.PlaceOfBirth; updatedPerson.DateOfDeath = constructRequest.DateOfDeath; updatedPerson.VersionNumber = 0; _mockUpdater.Setup(x => x.UpdateEntity(It.IsAny <PersonDbEntity>(), RequestBody, constructRequest)) .Returns(new UpdateEntityResult <PersonDbEntity>() { UpdatedEntity = updatedPerson.ToDatabase(), OldValues = new Dictionary <string, object> { { "firstName", person.FirstName }, { "placeOfBirth", person.PlaceOfBirth }, { "dateOfDeath", person.DateOfDeath } }, NewValues = new Dictionary <string, object> { { "firstName", updatedPerson.FirstName }, { "placeOfBirth", updatedPerson.PlaceOfBirth }, { "dateOfDeath", updatedPerson.DateOfDeath } } }); //Act var response = await _classUnderTest.UpdatePersonByIdAsync(constructRequest, RequestBody, query, 0).ConfigureAwait(false); //Assert var load = await _dbFixture.DynamoDbContext.LoadAsync <PersonDbEntity>(person.Id).ConfigureAwait(false); // Changed load.FirstName.Should().Be(updatedPerson.FirstName); load.PlaceOfBirth.Should().Be(updatedPerson.PlaceOfBirth); load.DateOfDeath.Should().Be(updatedPerson.DateOfDeath); // Not changed load.Surname.Should().Be(person.Surname); load.DateOfBirth.Should().Be(person.DateOfBirth); load.Id.Should().Be(person.Id); load.MiddleName.Should().Be(person.MiddleName); load.PersonTypes.Should().BeEquivalentTo(person.PersonTypes); load.PreferredFirstName.Should().Be(person.PreferredFirstName); load.PreferredMiddleName.Should().Be(person.PreferredMiddleName); load.PreferredSurname.Should().Be(person.PreferredSurname); load.PreferredTitle.Should().Be(person.PreferredTitle); load.Tenures.Should().BeEquivalentTo(person.Tenures); load.Title.Should().Be(person.Title); var expectedVersionNumber = 1; load.VersionNumber.Should().Be(expectedVersionNumber); load.LastModified.Should().BeCloseTo(DateTime.UtcNow, 500); }