-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #494 from hassanhabib/users/rcruzfreelance/foundat…
…ions-event-add FOUNDATIONS: Add Event
- Loading branch information
Showing
14 changed files
with
1,188 additions
and
11 deletions.
There are no files selected for viewing
268 changes: 268 additions & 0 deletions
268
Taarafo.Core.Tests.Unit/Services/Foundations/Events/EventServiceTests.Exceptions.Add.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,268 @@ | ||
// --------------------------------------------------------------- | ||
// Copyright (c) Coalition of the Good-Hearted Engineers | ||
// FREE TO USE TO CONNECT THE WORLD | ||
// --------------------------------------------------------------- | ||
|
||
using System; | ||
using System.Threading.Tasks; | ||
using EFxceptions.Models.Exceptions; | ||
using FluentAssertions; | ||
using Microsoft.Data.SqlClient; | ||
using Microsoft.EntityFrameworkCore; | ||
using Moq; | ||
using Taarafo.Core.Models.Events; | ||
using Taarafo.Core.Models.Events.Exceptions; | ||
using Xunit; | ||
|
||
namespace Taarafo.Core.Tests.Unit.Services.Foundations.Events | ||
{ | ||
public partial class EventServiceTests | ||
{ | ||
[Fact] | ||
public async Task ShouldThrowCriticalDependencyExceptionOnCreateIfSqlErrorOccursAndLogItAsync() | ||
{ | ||
// given | ||
DateTimeOffset randomDateTime = GetRandomDateTimeOffset(); | ||
Event someEvent = CreateRandomEvent(randomDateTime); | ||
SqlException sqlException = GetSqlException(); | ||
|
||
var failedEventStorageException = | ||
new FailedEventStorageException(sqlException); | ||
|
||
var expectedEventDependencyException = | ||
new EventDependencyException(failedEventStorageException); | ||
|
||
this.dateTimeBrokerMock.Setup(broker => | ||
broker.GetCurrentDateTimeOffset()) | ||
.Throws(sqlException); | ||
|
||
// when | ||
ValueTask<Event> addEventTask = | ||
this.eventService.AddEventAsync(someEvent); | ||
|
||
EventDependencyException actualEventDependencyException = | ||
await Assert.ThrowsAsync<EventDependencyException>( | ||
addEventTask.AsTask); | ||
|
||
// then | ||
actualEventDependencyException.Should().BeEquivalentTo( | ||
expectedEventDependencyException); | ||
|
||
this.dateTimeBrokerMock.Verify(broker => | ||
broker.GetCurrentDateTimeOffset(), | ||
Times.Once); | ||
|
||
this.loggingBrokerMock.Verify(broker => | ||
broker.LogCritical(It.Is(SameExceptionAs( | ||
expectedEventDependencyException))), | ||
Times.Once); | ||
|
||
this.storageBrokerMock.Verify(broker => | ||
broker.InsertEventAsync(It.IsAny<Event>()), | ||
Times.Never); | ||
|
||
this.dateTimeBrokerMock.VerifyNoOtherCalls(); | ||
this.loggingBrokerMock.VerifyNoOtherCalls(); | ||
this.storageBrokerMock.VerifyNoOtherCalls(); | ||
} | ||
|
||
[Fact] | ||
public async Task ShouldThrowDependencyValidationExceptionOnAddIfEventAlreadyExsitsAndLogItAsync() | ||
{ | ||
// given | ||
DateTimeOffset randomDateTimeOffset = GetRandomDateTimeOffset(); | ||
Event randomEvent = CreateRandomEvent(randomDateTimeOffset); | ||
Event alreadyExistsEvent = randomEvent; | ||
string randomMessage = GetRandomMessage(); | ||
|
||
var duplicateKeyException = | ||
new DuplicateKeyException(randomMessage); | ||
|
||
var alreadyExistsEventException = | ||
new AlreadyExistsEventException(duplicateKeyException); | ||
|
||
var expectedEventDependencyValidationException = | ||
new EventDependencyValidationException(alreadyExistsEventException); | ||
|
||
this.dateTimeBrokerMock.Setup(broker => | ||
broker.GetCurrentDateTimeOffset()) | ||
.Throws(duplicateKeyException); | ||
|
||
// when | ||
ValueTask<Event> addEventTask = | ||
this.eventService.AddEventAsync(alreadyExistsEvent); | ||
|
||
EventDependencyValidationException actualEventDependencyValidationException = | ||
await Assert.ThrowsAsync<EventDependencyValidationException>( | ||
addEventTask.AsTask); | ||
|
||
// then | ||
actualEventDependencyValidationException.Should().BeEquivalentTo( | ||
expectedEventDependencyValidationException); | ||
|
||
this.dateTimeBrokerMock.Verify(broker => | ||
broker.GetCurrentDateTimeOffset(), | ||
Times.Once); | ||
|
||
this.storageBrokerMock.Verify(broker => | ||
broker.InsertEventAsync(It.IsAny<Event>()), | ||
Times.Never); | ||
|
||
this.loggingBrokerMock.Verify(broker => | ||
broker.LogError(It.Is(SameExceptionAs( | ||
expectedEventDependencyValidationException))), | ||
Times.Once); | ||
|
||
this.dateTimeBrokerMock.VerifyNoOtherCalls(); | ||
this.storageBrokerMock.VerifyNoOtherCalls(); | ||
this.loggingBrokerMock.VerifyNoOtherCalls(); | ||
} | ||
|
||
[Fact] | ||
public async void ShouldThrowDependencyValidationExceptionOnAddIfReferenceErrorOccursAndLogItAsync() | ||
{ | ||
// given | ||
Event someEvent = CreateRandomEvent(); | ||
string randomMessage = GetRandomMessage(); | ||
string exceptionMessage = randomMessage; | ||
|
||
var foreignKeyConstraintConflictException = | ||
new ForeignKeyConstraintConflictException(exceptionMessage); | ||
|
||
var invalidEventReferenceException = | ||
new InvalidEventReferenceException(foreignKeyConstraintConflictException); | ||
|
||
var expectedEventDependencyValidationException = | ||
new EventDependencyValidationException(invalidEventReferenceException); | ||
|
||
this.dateTimeBrokerMock.Setup(broker => | ||
broker.GetCurrentDateTimeOffset()) | ||
.Throws(foreignKeyConstraintConflictException); | ||
|
||
// when | ||
ValueTask<Event> addEventTask = | ||
this.eventService.AddEventAsync(someEvent); | ||
|
||
EventDependencyValidationException actualEventDependencyValidationException = | ||
await Assert.ThrowsAsync<EventDependencyValidationException>( | ||
addEventTask.AsTask); | ||
|
||
// then | ||
actualEventDependencyValidationException.Should().BeEquivalentTo( | ||
expectedEventDependencyValidationException); | ||
|
||
this.dateTimeBrokerMock.Verify(broker => | ||
broker.GetCurrentDateTimeOffset(), | ||
Times.Once()); | ||
|
||
this.loggingBrokerMock.Verify(broker => | ||
broker.LogError(It.Is(SameExceptionAs( | ||
expectedEventDependencyValidationException))), | ||
Times.Once); | ||
|
||
this.storageBrokerMock.Verify(broker => | ||
broker.InsertEventAsync(It.IsAny<Event>()), | ||
Times.Never); | ||
|
||
this.dateTimeBrokerMock.VerifyNoOtherCalls(); | ||
this.loggingBrokerMock.VerifyNoOtherCalls(); | ||
this.storageBrokerMock.VerifyNoOtherCalls(); | ||
} | ||
|
||
[Fact] | ||
public async Task ShouldThrowDependencyExceptionOnAddIfDatabaseUpdateErrorOccursAndLogItAsync() | ||
{ | ||
// given | ||
Event someEvent = CreateRandomEvent(); | ||
|
||
var databaseUpdateException = | ||
new DbUpdateException(); | ||
|
||
var failedEventStorageException = | ||
new FailedEventStorageException(databaseUpdateException); | ||
|
||
var expectedEventDependencyException = | ||
new EventDependencyException(failedEventStorageException); | ||
|
||
this.dateTimeBrokerMock.Setup(broker => | ||
broker.GetCurrentDateTimeOffset()) | ||
.Throws(databaseUpdateException); | ||
|
||
// when | ||
ValueTask<Event> addEventTask = | ||
this.eventService.AddEventAsync(someEvent); | ||
|
||
EventDependencyException actualEventDependencyException = | ||
await Assert.ThrowsAsync<EventDependencyException>( | ||
addEventTask.AsTask); | ||
|
||
// then | ||
actualEventDependencyException.Should().BeEquivalentTo( | ||
expectedEventDependencyException); | ||
|
||
this.dateTimeBrokerMock.Verify(broker => | ||
broker.GetCurrentDateTimeOffset(), | ||
Times.Once); | ||
|
||
this.storageBrokerMock.Verify(broker => | ||
broker.InsertEventAsync(It.IsAny<Event>()), | ||
Times.Never); | ||
|
||
this.loggingBrokerMock.Verify(broker => | ||
broker.LogError(It.Is(SameExceptionAs( | ||
expectedEventDependencyException))), | ||
Times.Once); | ||
|
||
this.dateTimeBrokerMock.VerifyNoOtherCalls(); | ||
this.storageBrokerMock.VerifyNoOtherCalls(); | ||
this.loggingBrokerMock.VerifyNoOtherCalls(); | ||
} | ||
|
||
[Fact] | ||
public async Task ShouldThrowServiceExceptionOnAddIfServiceErrorOccursAndLogItAsync() | ||
{ | ||
// given | ||
Event someEvent = CreateRandomEvent(); | ||
var serviceException = new Exception(); | ||
|
||
var failedEventServiceException = | ||
new FailedEventServiceException(serviceException); | ||
|
||
var expectedEventServiceException = | ||
new EventServiceException(failedEventServiceException); | ||
|
||
this.dateTimeBrokerMock.Setup(broker => | ||
broker.GetCurrentDateTimeOffset()) | ||
.Throws(serviceException); | ||
|
||
// when | ||
ValueTask<Event> addEventTask = | ||
this.eventService.AddEventAsync(someEvent); | ||
|
||
EventServiceException actualEventServiceException = | ||
await Assert.ThrowsAsync<EventServiceException>( | ||
addEventTask.AsTask); | ||
|
||
// then | ||
actualEventServiceException.Should().BeEquivalentTo( | ||
expectedEventServiceException); | ||
|
||
this.dateTimeBrokerMock.Verify(broker => | ||
broker.GetCurrentDateTimeOffset(), | ||
Times.Once); | ||
|
||
this.storageBrokerMock.Verify(broker => | ||
broker.InsertEventAsync(It.IsAny<Event>()), | ||
Times.Never); | ||
|
||
this.loggingBrokerMock.Verify(broker => | ||
broker.LogError(It.Is(SameExceptionAs( | ||
expectedEventServiceException))), | ||
Times.Once); | ||
|
||
this.dateTimeBrokerMock.VerifyNoOtherCalls(); | ||
this.storageBrokerMock.VerifyNoOtherCalls(); | ||
this.loggingBrokerMock.VerifyNoOtherCalls(); | ||
} | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
Taarafo.Core.Tests.Unit/Services/Foundations/Events/EventServiceTests.Logic.Add.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// --------------------------------------------------------------- | ||
// Copyright (c) Coalition of the Good-Hearted Engineers | ||
// FREE TO USE TO CONNECT THE WORLD | ||
// --------------------------------------------------------------- | ||
|
||
using System; | ||
using System.Threading.Tasks; | ||
using FluentAssertions; | ||
using Force.DeepCloner; | ||
using Moq; | ||
using Taarafo.Core.Models.Events; | ||
using Xunit; | ||
|
||
namespace Taarafo.Core.Tests.Unit.Services.Foundations.Events | ||
{ | ||
public partial class EventServiceTests | ||
{ | ||
[Fact] | ||
public async Task ShouldAddEventAsync() | ||
{ | ||
// given | ||
DateTimeOffset randomDateTime = GetRandomDateTimeOffset(); | ||
Event randomEvent = CreateRandomEvent(randomDateTime); | ||
Event inputEvent = randomEvent; | ||
Event insertedEvent = inputEvent; | ||
Event expectedEvent = insertedEvent.DeepClone(); | ||
|
||
this.dateTimeBrokerMock.Setup(broker => | ||
broker.GetCurrentDateTimeOffset()) | ||
.Returns(randomDateTime); | ||
|
||
this.storageBrokerMock.Setup(broker => | ||
broker.InsertEventAsync(inputEvent)) | ||
.ReturnsAsync(insertedEvent); | ||
|
||
// when | ||
Event actualEvent = | ||
await this.eventService.AddEventAsync(inputEvent); | ||
|
||
// then | ||
actualEvent.Should().BeEquivalentTo(expectedEvent); | ||
|
||
this.storageBrokerMock.Verify(broker => | ||
broker.InsertEventAsync(inputEvent), | ||
Times.Once()); | ||
|
||
this.storageBrokerMock.VerifyNoOtherCalls(); | ||
} | ||
} | ||
} |
Oops, something went wrong.