Skip to content

Commit b2551aa

Browse files
authored
Merge pull request #332 from The-Standard-Organization/users/glhays/minorfoundation-repository-fkTest
MINOR FOUNDATION: Repository ForeignKey Tests
2 parents 6867b87 + 574c593 commit b2551aa

4 files changed

Lines changed: 141 additions & 0 deletions

File tree

GitFyle.Core.Api.Tests.Unit/Services/Foundations/Repositories/RepositoryServiceTests.Exceptions.Add.cs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,62 @@ await Assert.ThrowsAsync<RepositoryDependencyException>(
177177
this.storageBrokerMock.VerifyNoOtherCalls();
178178
}
179179

180+
[Fact]
181+
public async Task ShouldThrowDependencyValidationExceptionOnAddIfForeignKeyExceptionOccursAndLogItAsync()
182+
{
183+
// given
184+
Repository someRepository = CreateRandomRepository();
185+
string someMessage = GetRandomString();
186+
187+
var foreignKeyConstraintConflictException =
188+
new ForeignKeyConstraintConflictException(someMessage);
189+
190+
var invalidRepositoryReferenceException =
191+
new InvalidRepositoryReferenceException(
192+
message: "Invalid repository reference error occurred.",
193+
innerException: foreignKeyConstraintConflictException,
194+
data: foreignKeyConstraintConflictException.Data);
195+
196+
var expectedRepositoryDependencyValidationException =
197+
new RepositoryDependencyValidationException(
198+
message: "Repository validation error occurred, fix errors and try again.",
199+
innerException: invalidRepositoryReferenceException,
200+
data: invalidRepositoryReferenceException.Data);
201+
202+
this.dateTimeBrokerMock.Setup(broker =>
203+
broker.GetCurrentDateTimeOffsetAsync())
204+
.ThrowsAsync(foreignKeyConstraintConflictException);
205+
206+
// when
207+
ValueTask<Repository> addRepositoryTask =
208+
this.repositoryService.AddRepositoryAsync(someRepository);
209+
210+
RepositoryDependencyValidationException actualRepositoryDependencyValidationException =
211+
await Assert.ThrowsAsync<RepositoryDependencyValidationException>(
212+
addRepositoryTask.AsTask);
213+
214+
// then
215+
actualRepositoryDependencyValidationException.Should()
216+
.BeEquivalentTo(expectedRepositoryDependencyValidationException);
217+
218+
this.dateTimeBrokerMock.Verify(broker =>
219+
broker.GetCurrentDateTimeOffsetAsync(),
220+
Times.Once);
221+
222+
this.loggingBrokerMock.Verify(broker =>
223+
broker.LogErrorAsync(It.Is(SameExceptionAs(
224+
expectedRepositoryDependencyValidationException))),
225+
Times.Once);
226+
227+
this.storageBrokerMock.Verify(broker =>
228+
broker.InsertRepositoryAsync(It.IsAny<Repository>()),
229+
Times.Never);
230+
231+
this.dateTimeBrokerMock.VerifyNoOtherCalls();
232+
this.loggingBrokerMock.VerifyNoOtherCalls();
233+
this.storageBrokerMock.VerifyNoOtherCalls();
234+
}
235+
180236
[Fact]
181237
public async Task ShouldThrowServiceExceptionOnAddIfServiceErrorOccurredAndLogItAsync()
182238
{

GitFyle.Core.Api.Tests.Unit/Services/Foundations/Repositories/RepositoryServiceTests.Exceptions.Modify.cs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
using System;
66
using System.Threading.Tasks;
7+
using EFxceptions.Models.Exceptions;
78
using FluentAssertions;
89
using GitFyle.Core.Api.Models.Foundations.Repositories;
910
using GitFyle.Core.Api.Models.Foundations.Repositories.Exceptions;
@@ -189,6 +190,63 @@ await Assert.ThrowsAsync<RepositoryDependencyValidationException>(
189190
this.storageBrokerMock.VerifyNoOtherCalls();
190191
}
191192

193+
[Fact]
194+
public async Task ShouldThrowDependencyValidationExceptionOnModifyIfForeignKeyErrorOccursAndLogItAsync()
195+
{
196+
// given
197+
Repository someRepository = CreateRandomRepository();
198+
string someMessage = GetRandomString();
199+
string exceptionMessage = someMessage;
200+
201+
var foreignKeyConstraintConflictException =
202+
new ForeignKeyConstraintConflictException(exceptionMessage);
203+
204+
var invalidRepositoryReferenceException =
205+
new InvalidRepositoryReferenceException(
206+
message: "Invalid repository reference error occurred.",
207+
innerException: foreignKeyConstraintConflictException,
208+
data: foreignKeyConstraintConflictException.Data);
209+
210+
var expectedRepositoryDependencyValidationException =
211+
new RepositoryDependencyValidationException(
212+
message: "Repository validation error occurred, fix errors and try again.",
213+
innerException: invalidRepositoryReferenceException,
214+
data: invalidRepositoryReferenceException.Data);
215+
216+
this.dateTimeBrokerMock.Setup(broker =>
217+
broker.GetCurrentDateTimeOffsetAsync())
218+
.ThrowsAsync(foreignKeyConstraintConflictException);
219+
220+
// when
221+
ValueTask<Repository> modifyRepositoryTask =
222+
this.repositoryService.ModifyRepositoryAsync(someRepository);
223+
224+
RepositoryDependencyValidationException actualRepositoryDependencyValidationException =
225+
await Assert.ThrowsAsync<RepositoryDependencyValidationException>(
226+
modifyRepositoryTask.AsTask);
227+
228+
// then
229+
actualRepositoryDependencyValidationException.Should()
230+
.BeEquivalentTo(expectedRepositoryDependencyValidationException);
231+
232+
this.dateTimeBrokerMock.Verify(broker =>
233+
broker.GetCurrentDateTimeOffsetAsync(),
234+
Times.Once);
235+
236+
this.loggingBrokerMock.Verify(broker =>
237+
broker.LogErrorAsync(It.Is(SameExceptionAs(
238+
expectedRepositoryDependencyValidationException))),
239+
Times.Once);
240+
241+
this.storageBrokerMock.Verify(broker =>
242+
broker.InsertRepositoryAsync(It.IsAny<Repository>()),
243+
Times.Never);
244+
245+
this.dateTimeBrokerMock.VerifyNoOtherCalls();
246+
this.loggingBrokerMock.VerifyNoOtherCalls();
247+
this.storageBrokerMock.VerifyNoOtherCalls();
248+
}
249+
192250
[Fact]
193251
public async Task ShouldThrowServiceExceptionOnModifyIfServiceErrorOccursAndLogItAsync()
194252
{
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// ----------------------------------------------------------------------------------
2+
// Copyright (c) The Standard Organization: A coalition of the Good-Hearted Engineers
3+
// ----------------------------------------------------------------------------------
4+
5+
using System;
6+
using System.Collections;
7+
using Xeptions;
8+
9+
namespace GitFyle.Core.Api.Models.Foundations.Repositories.Exceptions
10+
{
11+
public class InvalidRepositoryReferenceException : Xeption
12+
{
13+
public InvalidRepositoryReferenceException(string message, Exception innerException, IDictionary data)
14+
: base(message, innerException, data)
15+
{ }
16+
}
17+
}

GitFyle.Core.Api/Services/Foundations/Repositories/RepositoryService.Exceptions.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@ private async ValueTask<Repository> TryCatch(ReturningRepositoryFunction returni
5555

5656
throw await CreateAndLogDependencyValidationExceptionAsync(alreadyExistsRepositoryException);
5757
}
58+
catch (ForeignKeyConstraintConflictException foreignKeyConstraintConflictException)
59+
{
60+
var invalidRepositoryReferenceException =
61+
new InvalidRepositoryReferenceException(
62+
message: "Invalid repository reference error occurred.",
63+
innerException: foreignKeyConstraintConflictException,
64+
data: foreignKeyConstraintConflictException.Data);
65+
66+
throw await CreateAndLogDependencyValidationExceptionAsync(invalidRepositoryReferenceException);
67+
}
5868
catch (DbUpdateConcurrencyException dbUpdateConcurrencyException)
5969
{
6070
var concurrencyGemException =

0 commit comments

Comments
 (0)