Skip to content

Commit

Permalink
Merge pull request #22 from The-Standard-Organization/users/ZafarUrak…
Browse files Browse the repository at this point in the history
…ov/code-rub-wrap-into-trycatch

CODE RUB: Wrap into TryCatch
  • Loading branch information
cjdutoit authored Apr 29, 2024
2 parents 14c91b6 + c3cb62a commit 5ec033a
Show file tree
Hide file tree
Showing 5 changed files with 332 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
// Copyright(c) The Standard Organization: A coalition of the Good-Hearted Engineers
// ----------------------------------------------------------------------------------

using FluentAssertions;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Moq;
using STX.EFxceptions.Abstractions.Models.Exceptions;
using STX.EFxceptions.SQLite.Base.Models.Exceptions;
using Xunit;

Expand All @@ -17,124 +20,302 @@ public void ShouldThrowDbUpdateExceptionIfErrorCodeIsNotRecognized()
// given
int sqlForeignKeyConstraintConflictErrorCode = 0000;
string randomErrorMessage = CreateRandomErrorMessage();
SqliteException foreignKeyConstraintConflictException = CreateSQLiteException();

SqliteException foreignKeyConstraintConflictExceptionThrown =
CreateSQLiteException(
message: randomErrorMessage,
errorCode: sqlForeignKeyConstraintConflictErrorCode);

var dbUpdateException = new DbUpdateException(
message: randomErrorMessage,
innerException: foreignKeyConstraintConflictException);
innerException: foreignKeyConstraintConflictExceptionThrown);

DbUpdateException expectedDbUpdateException = dbUpdateException;

this.sqliteErrorBrokerMock.Setup(broker =>
broker.GetErrorCode(foreignKeyConstraintConflictException))
broker.GetErrorCode(foreignKeyConstraintConflictExceptionThrown))
.Returns(sqlForeignKeyConstraintConflictErrorCode);

// when . then
Assert.Throws<DbUpdateException>(() =>
this.sqliteEFxceptionService.ThrowMeaningfulException(dbUpdateException));
// when
DbUpdateException actualDbUpdateException =
Assert.Throws<DbUpdateException>(() => this.sqliteEFxceptionService
.ThrowMeaningfulException(dbUpdateException));

// then
actualDbUpdateException.Should().BeEquivalentTo(expectedDbUpdateException);

this.sqliteErrorBrokerMock.Verify(broker => broker
.GetErrorCode(foreignKeyConstraintConflictExceptionThrown), Times.Once());

sqliteErrorBrokerMock.VerifyNoOtherCalls();
}

[Fact]
public void ShouldThrowInvalidColumnNameException()
{
// given
int sqlInvalidColumnNameErrorCode = 207;
string randomErrorMessage = CreateRandomErrorMessage();
SqliteException invalidColumnNameException = CreateSQLiteException();
string randomSqliteExceptionMessage = CreateRandomErrorMessage();

var dbUpdateException = new DbUpdateException(
message: randomErrorMessage,
innerException: invalidColumnNameException);
SqliteException invalidColumnNameExceptionThrown =
CreateSQLiteException(
message: randomSqliteExceptionMessage,
errorCode: sqlInvalidColumnNameErrorCode);

string randomDbUpdateExceptionMessage = CreateRandomErrorMessage();

DbUpdateException dbUpdateExceptionThrown = new DbUpdateException(
message: randomDbUpdateExceptionMessage,
innerException: invalidColumnNameExceptionThrown);

var ivalidColumnNameSqliteException =
new InvalidColumnNameSQLiteException(
message: invalidColumnNameExceptionThrown.Message);

var expectedInvalidColumnNameException =
new InvalidColumnNameException(
message: ivalidColumnNameSqliteException.Message,
innerException: ivalidColumnNameSqliteException);

this.sqliteErrorBrokerMock.Setup(broker =>
broker.GetErrorCode(invalidColumnNameException))
broker.GetErrorCode(invalidColumnNameExceptionThrown))
.Returns(sqlInvalidColumnNameErrorCode);

// when . then
Assert.Throws<InvalidColumnNameSQLiteException>(() =>
this.sqliteEFxceptionService.ThrowMeaningfulException(dbUpdateException));
// when
InvalidColumnNameException actualInvalidColumnNameException =
Assert.Throws<InvalidColumnNameException>(() =>
this.sqliteEFxceptionService.ThrowMeaningfulException(dbUpdateExceptionThrown));
// then
actualInvalidColumnNameException.Should()
.BeEquivalentTo(
expectation: expectedInvalidColumnNameException,
config: options => options
.Excluding(ex => ex.TargetSite)
.Excluding(ex => ex.StackTrace)
.Excluding(ex => ex.Source)
.Excluding(ex => ex.InnerException.TargetSite)
.Excluding(ex => ex.InnerException.StackTrace)
.Excluding(ex => ex.InnerException.Source));

this.sqliteErrorBrokerMock.Verify(broker => broker
.GetErrorCode(invalidColumnNameExceptionThrown), Times.Once());

sqliteErrorBrokerMock.VerifyNoOtherCalls();
}

[Fact]
public void ShouldThrowInvalidObjectNameSqliteException()
public void ShouldThrowInvalidObjectNameException()
{
// given
int sqlInvalidObjectNameErrorCode = 208;
string randomErrorMessage = CreateRandomErrorMessage();
SqliteException invalidObjectNameException = CreateSQLiteException();
string randomSqliteExceptionMessage = CreateRandomErrorMessage();

SqliteException invalidObjectNameExceptionThrown =
CreateSQLiteException(
message: randomSqliteExceptionMessage,
errorCode: sqlInvalidObjectNameErrorCode);

string randomDbUpdateExceptionMessage = CreateRandomErrorMessage();

var dbUpdateException = new DbUpdateException(
message: randomErrorMessage,
innerException: invalidObjectNameException);
message: randomDbUpdateExceptionMessage,
innerException: invalidObjectNameExceptionThrown);

var invalidObjectNameSqliteException =
new InvalidObjectNameSQLiteException(
message: invalidObjectNameExceptionThrown.Message);

var expectedInvalidObjectNameException =
new InvalidObjectNameException(
message: invalidObjectNameSqliteException.Message,
innerException: invalidObjectNameSqliteException);

this.sqliteErrorBrokerMock.Setup(broker =>
broker.GetErrorCode(invalidObjectNameException))
broker.GetErrorCode(invalidObjectNameExceptionThrown))
.Returns(sqlInvalidObjectNameErrorCode);

// when . then
Assert.Throws<InvalidObjectNameSQLiteException>(() =>
this.sqliteEFxceptionService.ThrowMeaningfulException(dbUpdateException));
// when
InvalidObjectNameException actualInvalidObjectNameException =
Assert.Throws<InvalidObjectNameException>(() =>
this.sqliteEFxceptionService.ThrowMeaningfulException(dbUpdateException));

// then
actualInvalidObjectNameException.Should()
.BeEquivalentTo(
expectation: expectedInvalidObjectNameException,
config: options => options
.Excluding(ex => ex.TargetSite)
.Excluding(ex => ex.StackTrace)
.Excluding(ex => ex.Source)
.Excluding(ex => ex.InnerException.TargetSite)
.Excluding(ex => ex.InnerException.StackTrace)
.Excluding(ex => ex.InnerException.Source));

this.sqliteErrorBrokerMock.Verify(broker => broker
.GetErrorCode(invalidObjectNameExceptionThrown), Times.Once());

sqliteErrorBrokerMock.VerifyNoOtherCalls();
}

[Fact]
public void ShouldThrowForeignKeyConstraintConflictSqliteException()
public void ShouldThrowForeignKeyConstraintConflictException()
{
// given
int sqlForeignKeyConstraintConflictErrorCode = 547;
string randomErrorMessage = CreateRandomErrorMessage();
SqliteException foreignKeyConstraintConflictException = CreateSQLiteException();
string randomSqliteExceptionMessage = CreateRandomErrorMessage();

SqliteException foreignKeyConstraintConflictSqliteExceptionThrown =
CreateSQLiteException(
message: randomSqliteExceptionMessage,
errorCode: sqlForeignKeyConstraintConflictErrorCode);

string randomDbUpdateExceptionMessage = CreateRandomErrorMessage();

var dbUpdateException = new DbUpdateException(
message: randomErrorMessage,
innerException: foreignKeyConstraintConflictException);
message: randomDbUpdateExceptionMessage,
innerException: foreignKeyConstraintConflictSqliteExceptionThrown);

var foreignKeyConstraintConflictSqliteException =
new ForeignKeyConstraintConflictSQLiteException(
message: foreignKeyConstraintConflictSqliteExceptionThrown.Message);

var expectedForeignKeyConstraintConflictException =
new ForeignKeyConstraintConflictException(
message: foreignKeyConstraintConflictSqliteException.Message,
innerException: foreignKeyConstraintConflictSqliteException);

this.sqliteErrorBrokerMock.Setup(broker =>
broker.GetErrorCode(foreignKeyConstraintConflictException))
broker.GetErrorCode(foreignKeyConstraintConflictSqliteExceptionThrown))
.Returns(sqlForeignKeyConstraintConflictErrorCode);

// when . then
Assert.Throws<ForeignKeyConstraintConflictSQLiteException>(() =>
this.sqliteEFxceptionService.ThrowMeaningfulException(dbUpdateException));
// when
var actualForeignKeyConstraintConflictException =
Assert.Throws<ForeignKeyConstraintConflictException>(() =>
this.sqliteEFxceptionService.ThrowMeaningfulException(dbUpdateException));

// then
actualForeignKeyConstraintConflictException.Should()
.BeEquivalentTo(
expectation: expectedForeignKeyConstraintConflictException,
config: options => options
.Excluding(ex => ex.TargetSite)
.Excluding(ex => ex.StackTrace)
.Excluding(ex => ex.Source)
.Excluding(ex => ex.InnerException.TargetSite)
.Excluding(ex => ex.InnerException.StackTrace)
.Excluding(ex => ex.InnerException.Source));

this.sqliteErrorBrokerMock.Verify(broker => broker
.GetErrorCode(foreignKeyConstraintConflictSqliteExceptionThrown), Times.Once());

sqliteErrorBrokerMock.VerifyNoOtherCalls();
}

[Fact]
public void ShouldThrowDuplicateKeyWithUniqueIndexSqliteException()
public void ShouldThrowDuplicateKeyWithUniqueIndexException()
{
// given
int sqlDuplicateKeyWithUniqueIndexErrorCode = 2601;
string randomErrorMessage = CreateRandomErrorMessage();
SqliteException sqlDuplicateKeyWithUniqueIndexException = CreateSQLiteException();
int sqlDuplicateKeyErrorCode = 2601;
string randomSqliteExceptionMessage = CreateRandomErrorMessage();

SqliteException duplicateKeyWithUniqueIndexSqliteExceptionThrown =
CreateSQLiteException(
message: randomSqliteExceptionMessage,
errorCode: sqlDuplicateKeyErrorCode);

string randomDbUpdateExceptionMessage = CreateRandomErrorMessage();

var dbUpdateException = new DbUpdateException(
message: randomErrorMessage,
innerException: sqlDuplicateKeyWithUniqueIndexException);
message: randomDbUpdateExceptionMessage,
innerException: duplicateKeyWithUniqueIndexSqliteExceptionThrown);

var duplicateKeyWithUniqueIndexSqliteException =
new DuplicateKeyWithUniqueIndexSQLiteException(
message: duplicateKeyWithUniqueIndexSqliteExceptionThrown.Message);

var expectedDuplicateKeyWithUniqueIndexException =
new DuplicateKeyWithUniqueIndexException(
message: duplicateKeyWithUniqueIndexSqliteException.Message,
innerException: duplicateKeyWithUniqueIndexSqliteException);

this.sqliteErrorBrokerMock.Setup(broker =>
broker.GetErrorCode(sqlDuplicateKeyWithUniqueIndexException))
.Returns(sqlDuplicateKeyWithUniqueIndexErrorCode);
broker.GetErrorCode(duplicateKeyWithUniqueIndexSqliteExceptionThrown))
.Returns(sqlDuplicateKeyErrorCode);

// when . then
Assert.Throws<DuplicateKeyWithUniqueIndexSQLiteException>(() =>
this.sqliteEFxceptionService.ThrowMeaningfulException(dbUpdateException));
// when
var actualDuplicateKeyWithUniqueIndexException =
Assert.Throws<DuplicateKeyWithUniqueIndexException>(() =>
this.sqliteEFxceptionService.ThrowMeaningfulException(dbUpdateException));

// then
actualDuplicateKeyWithUniqueIndexException.Should()
.BeEquivalentTo(
expectation: expectedDuplicateKeyWithUniqueIndexException,
config: options => options
.Excluding(ex => ex.TargetSite)
.Excluding(ex => ex.StackTrace)
.Excluding(ex => ex.Source)
.Excluding(ex => ex.InnerException.TargetSite)
.Excluding(ex => ex.InnerException.StackTrace)
.Excluding(ex => ex.InnerException.Source));

this.sqliteErrorBrokerMock.Verify(broker => broker
.GetErrorCode(duplicateKeyWithUniqueIndexSqliteExceptionThrown), Times.Once());

sqliteErrorBrokerMock.VerifyNoOtherCalls();
}

[Fact]
public void ShouldThrowDuplicateKeySqliteException()
public void ShouldThrowDuplicateKeyException()
{
// given
int sqlDuplicateKeyErrorCode = 2627;
string randomErrorMessage = CreateRandomErrorMessage();
SqliteException sqlDuplicateKeyException = CreateSQLiteException();
string randomSqliteExceptionMessage = CreateRandomErrorMessage();

SqliteException duplicateKeySqliteExceptionThrown = CreateSQLiteException(
message: randomSqliteExceptionMessage,
errorCode: sqlDuplicateKeyErrorCode);

string randomDbUpdateExceptionMessage = CreateRandomErrorMessage();

var dbUpdateException = new DbUpdateException(
message: randomErrorMessage,
innerException: sqlDuplicateKeyException);
message: randomDbUpdateExceptionMessage,
innerException: duplicateKeySqliteExceptionThrown);

var duplicateKeySqliteException =
new DuplicateKeySQLiteException(
message: duplicateKeySqliteExceptionThrown.Message);

var expectedDuplicateKeyException =
new DuplicateKeyException(
message: duplicateKeySqliteException.Message,
innerException: duplicateKeySqliteException);

this.sqliteErrorBrokerMock.Setup(broker =>
broker.GetErrorCode(sqlDuplicateKeyException))
broker.GetErrorCode(duplicateKeySqliteExceptionThrown))
.Returns(sqlDuplicateKeyErrorCode);

// when . then
Assert.Throws<DuplicateKeySQLiteException>(() =>
this.sqliteEFxceptionService.ThrowMeaningfulException(dbUpdateException));
// when
var actualDuplicateKeyException =
Assert.Throws<DuplicateKeyException>(() =>
this.sqliteEFxceptionService.ThrowMeaningfulException(dbUpdateException));

// then
actualDuplicateKeyException.Should()
.BeEquivalentTo(
expectation: expectedDuplicateKeyException,
config: options => options
.Excluding(ex => ex.TargetSite)
.Excluding(ex => ex.StackTrace)
.Excluding(ex => ex.Source)
.Excluding(ex => ex.InnerException.TargetSite)
.Excluding(ex => ex.InnerException.StackTrace)
.Excluding(ex => ex.InnerException.Source));

this.sqliteErrorBrokerMock.Verify(broker => broker
.GetErrorCode(duplicateKeySqliteExceptionThrown), Times.Once());

sqliteErrorBrokerMock.VerifyNoOtherCalls();
}
}
}
Loading

0 comments on commit 5ec033a

Please sign in to comment.