Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System;
using System.Threading.Tasks;
using EFxceptions.Models.Exceptions;
using FluentAssertions;
using GitFyle.Core.Api.Models.Foundations.Contributors;
using GitFyle.Core.Api.Models.Foundations.Contributors.Exceptions;
Expand Down Expand Up @@ -70,6 +71,67 @@ await Assert.ThrowsAsync<ContributorDependencyException>(
this.loggingBrokerMock.VerifyNoOtherCalls();
}

[Fact]
public async Task ShouldThrowValidationExceptionOnModifyIfReferenceErrorOccursAndLogItAsync()
{
// given
Contributor foreignKeyConflictedContributor = CreateRandomContributor();
string randomMessage = GetRandomString();
string exceptionMessage = randomMessage;

var foreignKeyConstraintConflictException =
new ForeignKeyConstraintConflictException(message: exceptionMessage);

var invalidContributorReferenceException =
new InvalidReferenceContributorException(
message: "Invalid contributor reference error occurred.",
innerException: foreignKeyConstraintConflictException,
data: foreignKeyConstraintConflictException.Data);

var expectedContributorDependencyValidationException =
new ContributorDependencyValidationException(
message: "Contributor dependency validation error occurred, fix errors and try again.",
innerException: invalidContributorReferenceException,
data: invalidContributorReferenceException.Data);

this.dateTimeBrokerMock.Setup(broker =>
broker.GetCurrentDateTimeOffsetAsync())
.Throws(foreignKeyConstraintConflictException);

// when
ValueTask<Contributor> modifyContributorTask =
this.contributorService.ModifyContributorAsync(foreignKeyConflictedContributor);

ContributorDependencyValidationException actualContributorDependencyValidationException =
await Assert.ThrowsAsync<ContributorDependencyValidationException>(
modifyContributorTask.AsTask);

// then
actualContributorDependencyValidationException.Should().BeEquivalentTo(
expectedContributorDependencyValidationException);

this.dateTimeBrokerMock.Verify(broker =>
broker.GetCurrentDateTimeOffsetAsync(),
Times.Once);

this.storageBrokerMock.Verify(broker =>
broker.SelectContributorByIdAsync(foreignKeyConflictedContributor.Id),
Times.Never);

this.loggingBrokerMock.Verify(broker =>
broker.LogErrorAsync(It.Is(SameExceptionAs(
expectedContributorDependencyValidationException))),
Times.Once);

this.storageBrokerMock.Verify(broker =>
broker.UpdateContributorAsync(foreignKeyConflictedContributor),
Times.Never);

this.dateTimeBrokerMock.VerifyNoOtherCalls();
this.storageBrokerMock.VerifyNoOtherCalls();
this.loggingBrokerMock.VerifyNoOtherCalls();
}

[Fact]
public async Task ShouldThrowDependencyExceptionOnModifyIfDatabaseUpdateExceptionOccursAndLogItAsync()
{
Expand Down