Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
204ac3e
ShouldModifyPetAsync -> FAIL
DilmurodDeveloper Jul 23, 2025
e037148
ShouldModifyPetAsync -> PASS
DilmurodDeveloper Jul 23, 2025
d2e46f7
ShouldThrowValidationExceptionOnModifyIfPetIsNullAndLogItAsync -> FAIL
DilmurodDeveloper Jul 23, 2025
989afbb
ShouldThrowValidationExceptionOnModifyIfPetIsNullAndLogItAsync -> PASS
DilmurodDeveloper Jul 23, 2025
6515243
ShouldThrowValidationExceptionOnModifyIfPetIsInvalidAndLogItAsync -> …
DilmurodDeveloper Jul 23, 2025
a52e1a3
ShouldThrowValidationExceptionOnModifyIfPetIsInvalidAndLogItAsync -> …
DilmurodDeveloper Jul 23, 2025
cd611c0
ShouldThrowValidationExceptionOnModifyIfPetDoesNotExistAndLogItAsync …
DilmurodDeveloper Jul 23, 2025
c38d5bf
ShouldThrowValidationExceptionOnModifyIfPetDoesNotExistAndLogItAsync …
DilmurodDeveloper Jul 23, 2025
b479eee
ShouldThrowCriticalDependencyExceptionOnModifyIfSqlErrorOccursAndLogI…
DilmurodDeveloper Jul 23, 2025
3c42b7a
ShouldThrowCriticalDependencyExceptionOnModifyIfSqlErrorOccursAndLogI…
DilmurodDeveloper Jul 23, 2025
abd406c
ShouldThrowDependencyExceptionOnModifyIfDatabaseUpdateExceptionOccurs…
DilmurodDeveloper Jul 23, 2025
cdb02e0
ShouldThrowDependencyExceptionOnModifyIfDatabaseUpdateExceptionOccurs…
DilmurodDeveloper Jul 23, 2025
ad8ab82
ShouldThrowDependencyValidationExceptionOnModifyIfDatabaseUpdateConcu…
DilmurodDeveloper Jul 23, 2025
3239f11
ShouldThrowDependencyValidationExceptionOnModifyIfDatabaseUpdateConcu…
DilmurodDeveloper Jul 23, 2025
423886d
ShouldThrowServiceExceptionOnModifyIfDatabaseUpdateErrorOccursAndLogI…
DilmurodDeveloper Jul 23, 2025
125b690
ShouldThrowServiceExceptionOnModifyIfDatabaseUpdateErrorOccursAndLogI…
DilmurodDeveloper Jul 23, 2025
adfd803
CODE RUB: Implement TryCatch
DilmurodDeveloper Jul 23, 2025
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
@@ -0,0 +1,209 @@
//- - - - - - - - - - - - - - - - - - - - - - - - - -
// Copyright (c) Coalition of Good-Hearted Engineers
// Free to Use for Precise File Conversion
//- - - - - - - - - - - - - - - - - - - - - - - - - -

using FluentAssertions;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using Moq;
using xChanger.Api.Models.Foundations.Pets;
using xChanger.Api.Models.Foundations.Pets.Exceptions;

namespace xChanger.Api.Tests.Unit.Services.Foundations.Pets
{
public partial class PetServiceTests
{
[Fact]
public async Task ShouldThrowCriticalDependencyExceptionOnModifyIfSqlErrorOccursAndLogItAsync()
{
// given
Pet randomPet = CreateRandomPet();
Pet somePet = randomPet;
Guid petId = somePet.Id;
SqlException sqlException = GetSqlError();

var failedPetStorageException =
new FailedPetStorageException(sqlException);

var expectedPetDependencyException =
new PetDependencyException(failedPetStorageException);

this.storageBrokerMock.Setup(broker =>
broker.SelectPetByIdAsync(petId))
.Throws(sqlException);

// when
ValueTask<Pet> modifyPetTask =
this.petService.ModifyPetAsync(somePet);

PetDependencyException actualPetDependencyException =
await Assert.ThrowsAsync<PetDependencyException>(() =>
modifyPetTask.AsTask());

// then
actualPetDependencyException.Should()
.BeEquivalentTo(expectedPetDependencyException);

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

this.storageBrokerMock.Verify(broker =>
broker.SelectPetByIdAsync(petId),
Times.Once);

this.storageBrokerMock.Verify(broker =>
broker.UpdatePetAsync(somePet),
Times.Never);

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

[Fact]
public async Task ShouldThrowDependencyExceptionOnModifyIfDatabaseUpdateExceptionOccursAndLogItAsync()
{
// given
Pet randomPet = CreateRandomPet();
Pet somePet = randomPet;
Guid petId = somePet.Id;
var databaseUpdateException = new DbUpdateException();

var failedPetStorageException =
new FailedPetStorageException(databaseUpdateException);

var expectedPetDependencyException =
new PetDependencyException(failedPetStorageException);

this.storageBrokerMock.Setup(broker =>
broker.SelectPetByIdAsync(petId))
.Throws(databaseUpdateException);

// when
ValueTask<Pet> modifyPetTask =
this.petService.ModifyPetAsync(somePet);

PetDependencyException actualPetDependencyException =
await Assert.ThrowsAsync<PetDependencyException>(() =>
modifyPetTask.AsTask());

// then
actualPetDependencyException.Should()
.BeEquivalentTo(expectedPetDependencyException);

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

this.storageBrokerMock.Verify(broker =>
broker.SelectPetByIdAsync(petId),
Times.Once);

this.storageBrokerMock.Verify(broker =>
broker.UpdatePetAsync(somePet),
Times.Never);

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

[Fact]
public async Task ShouldThrowDependencyValidationExceptionOnModifyIfDatabaseUpdateConcurrencyErrorOccursAndLogItAsync()
{
// given
Pet randomPet = CreateRandomPet();
Pet somePet = randomPet;
Guid petId = somePet.Id;
var dbUpdateConcurrencyException = new DbUpdateConcurrencyException();

var lockedPetException =
new LockedPetException(dbUpdateConcurrencyException);

var expectedPetDependencyValidationException =
new PetDependencyValidationException(lockedPetException);

this.storageBrokerMock.Setup(broker =>
broker.SelectPetByIdAsync(petId))
.Throws(dbUpdateConcurrencyException);

// when
ValueTask<Pet> modifyPetTask =
this.petService.ModifyPetAsync(somePet);

PetDependencyValidationException actualPetDependencyValidationException =
await Assert.ThrowsAsync<PetDependencyValidationException>(() =>
modifyPetTask.AsTask());

// then
actualPetDependencyValidationException.Should()
.BeEquivalentTo(expectedPetDependencyValidationException);

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

this.storageBrokerMock.Verify(broker =>
broker.SelectPetByIdAsync(petId),
Times.Once);

this.storageBrokerMock.Verify(broker =>
broker.UpdatePetAsync(somePet),
Times.Never);

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

[Fact]
public async Task ShouldThrowServiceExceptionOnModifyIfDatabaseUpdateErrorOccursAndLogItAsync()
{
// given
Pet randomPet = CreateRandomPet();
Pet somePet = randomPet;
Guid petId = somePet.Id;
var serviceException = new Exception();

var failedPetServiceException =
new FailedPetServiceException(serviceException);

var expectedPetServiceException =
new PetServiceException(failedPetServiceException);

this.storageBrokerMock.Setup(broker =>
broker.SelectPetByIdAsync(petId))
.Throws(serviceException);

// when
ValueTask<Pet> modifyPetTask =
this.petService.ModifyPetAsync(somePet);

PetServiceException actualPetServiceException =
await Assert.ThrowsAsync<PetServiceException>(() =>
modifyPetTask.AsTask());

// then
actualPetServiceException.Should()
.BeEquivalentTo(expectedPetServiceException);

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

this.storageBrokerMock.Verify(broker =>
broker.SelectPetByIdAsync(petId),
Times.Once);

this.storageBrokerMock.Verify(broker =>
broker.UpdatePetAsync(somePet),
Times.Never);

this.loggingBrokerMock.VerifyNoOtherCalls();
this.storageBrokerMock.VerifyNoOtherCalls();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//- - - - - - - - - - - - - - - - - - - - - - - - - -
// Copyright (c) Coalition of Good-Hearted Engineers
// Free to Use for Precise File Conversion
//- - - - - - - - - - - - - - - - - - - - - - - - - -

using FluentAssertions;
using Force.DeepCloner;
using Moq;
using xChanger.Api.Models.Foundations.Pets;

namespace xChanger.Api.Tests.Unit.Services.Foundations.Pets
{
public partial class PetServiceTests
{
[Fact]
public async Task ShouldModifyPetAsync()
{
// given
Pet randomPet = CreateRandomPet();
Pet inputPet = randomPet;
Pet persistedPet = inputPet.DeepClone();
Pet updatedPet = inputPet;
Pet expectedPet = updatedPet.DeepClone();
Guid InputPetId = inputPet.Id;

this.storageBrokerMock.Setup(broker =>
broker.SelectPetByIdAsync(InputPetId))
.ReturnsAsync(persistedPet);

this.storageBrokerMock.Setup(broker =>
broker.UpdatePetAsync(inputPet))
.ReturnsAsync(updatedPet);

// when
Pet actualPet =
await this.petService.ModifyPetAsync(inputPet);

// then
actualPet.Should().BeEquivalentTo(expectedPet);

this.storageBrokerMock.Verify(broker =>
broker.SelectPetByIdAsync(InputPetId),
Times.Once);

this.storageBrokerMock.Verify(broker =>
broker.UpdatePetAsync(inputPet),
Times.Once);

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