Skip to content

Commit

Permalink
fix(RollBack): Fix roll back (#33)
Browse files Browse the repository at this point in the history
* Add failed EdgesAdditionService unit tests.

* Complete EdgesAdditionService class & pass its unit tests.

* add & pass SingleNodeAdditionService unit tests.

* fix single node addition

* Complete SingleEdgeAdditionService class & pass its tests.

* some changes.

* fix nodesAddition RollBack and its tests!

* fix roll back for edges

---------

Co-authored-by: sadq <mamsa0205@gmail.com>
  • Loading branch information
msmahdinejad and SwimmingRieux authored Aug 24, 2024
1 parent f55c851 commit f143191
Show file tree
Hide file tree
Showing 10 changed files with 186 additions and 133 deletions.
109 changes: 54 additions & 55 deletions RelationshipAnalysis.Test/Services/EdgesAdditionServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -311,61 +311,60 @@ private IFormFile CreateFileMock(string csvContent)
fileMock.Length.Returns(stream.Length);
return fileMock;
}

// // TODO
// [Fact]
// public async Task AddEdges_ShouldReturnBadRequestAndRollBack_WhenDbFailsToAddData()
// {
// // Arrange
// var expected = new ActionResponse<MessageDto>()
// {
// Data = new MessageDto(Resources.SuccessfulEdgeAdditionMessage),
// StatusCode = StatusCodeType.Success
// };
// var csvContent = @"""SourceAcount"",""DestiantionAccount"",""Amount"",""Date"",""TransactionID"",""Type""
// ""6534454617"",""6039548046"",""500,000,000"",""1399/04/23"",""153348811341"",""پایا""
// ""6534454617"",""6039548046"",""500,000,000"",""1399/04/23"",""153348811341"",""پایا""
// ""6039548046"",""5287517379"",""100,000,000"",""1399/04/23"",""192524206627"",""پایا""";
// var fileToBeSend = CreateFileMock(csvContent);
//
// var validatorMock = NSubstitute.Substitute.For<ICsvValidatorService>();
// validatorMock.Validate(fileToBeSend, "TransactionID", "SourceAcount", "DestiantionAccount").Returns(expected);
// var processorMock = NSubstitute.Substitute.For<ICsvProcessorService>();
// processorMock.ProcessCsvAsync(fileToBeSend).Returns(new List<dynamic>());
// var additionServiceMock = new Mock<ISingleEdgeAdditionService>();
//
// // Setup the mock to throw an exception for any inputs
// additionServiceMock
// .Setup(service => service.AddSingleEdge(
// It.IsAny<IDictionary<string, object>>(),
// It.IsAny<string>(),
// It.IsAny<string>(),
// It.IsAny<string>(),
// It.IsAny<int>(),
// It.IsAny<int>(),
// It.IsAny<int>()
// ))
// .Throws(new Exception("Custom exception message"));
// _sut = new EdgesAdditionService(_serviceProvider, validatorMock, processorMock, additionServiceMock.Object);
//
// // Act
// var result = await _sut.AddEdges(new UploadEdgeDto()
// {
// File = fileToBeSend,
// EdgeCategoryName = "Transaction",
// UniqueKeyHeaderName = "TransactionID",
// SourceNodeCategoryName = "Account",
// TargetNodeCategoryName = "Account",
// SourceNodeHeaderName = "SourceAcount",
// TargetNodeHeaderName = "DestiantionAccount"
// });
// // Assert
// Assert.Equivalent(expected, result);
// using var scope = _serviceProvider.CreateScope();
// var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
// Assert.Equal(0, context.Nodes.Count());
// Assert.Equal("Custom exception message", result.Data.Message);
// }

// TODO
[Fact]
public async Task AddEdges_ShouldReturnBadRequestAndRollBack_WhenDbFailsToAddData()
{
// Arrange
var expected = new ActionResponse<MessageDto>()
{
Data = new MessageDto(Resources.SuccessfulEdgeAdditionMessage),
StatusCode = StatusCodeType.Success
};
var csvContent = @"""SourceAcount"",""DestiantionAccount"",""Amount"",""Date"",""TransactionID"",""Type""
""6534454617"",""6039548046"",""500,000,000"",""1399/04/23"",""153348811341"",""پایا""
""6534454617"",""6039548046"",""500,000,000"",""1399/04/23"",""153348811341"",""پایا""
""6039548046"",""5287517379"",""100,000,000"",""1399/04/23"",""192524206627"",""پایا""";
var fileToBeSend = CreateFileMock(csvContent);

var validatorMock = NSubstitute.Substitute.For<ICsvValidatorService>();
validatorMock.Validate(fileToBeSend, "TransactionID", "SourceAcount", "DestiantionAccount").Returns(expected);
var processorMock = NSubstitute.Substitute.For<ICsvProcessorService>();
processorMock.ProcessCsvAsync(fileToBeSend).Returns(new List<dynamic>() { new Dictionary<string, object>()});
var additionServiceMock = new Mock<ISingleEdgeAdditionService>();

// Setup the mock to throw an exception for any inputs
additionServiceMock
.Setup(service => service.AddSingleEdge(
It.IsAny<ApplicationDbContext>(),
It.IsAny<IDictionary<string, object>>(),
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<int>(),
It.IsAny<int>(),
It.IsAny<int>()
))
.Throws(new Exception("Custom exception message"));
_sut = new EdgesAdditionService(_serviceProvider, validatorMock, processorMock, additionServiceMock.Object);

// Act
var result = await _sut.AddEdges(new UploadEdgeDto()
{
File = fileToBeSend,
EdgeCategoryName = "Transaction",
UniqueKeyHeaderName = "TransactionID",
SourceNodeCategoryName = "Account",
TargetNodeCategoryName = "Account",
SourceNodeHeaderName = "SourceAcount",
TargetNodeHeaderName = "DestiantionAccount"
});
// Assert
using var scope = _serviceProvider.CreateScope();
var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
Assert.Equal(0, context.Nodes.Count());
Assert.Equal("Custom exception message", result.Data.Message);
}

}
84 changes: 47 additions & 37 deletions RelationshipAnalysis.Test/Services/NodesAdditionServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using NSubstitute;
using RelationshipAnalysis.Context;
using RelationshipAnalysis.Dto;
Expand Down Expand Up @@ -143,43 +144,52 @@ public async Task AddNodes_ShouldReturnSuccess_WhenNodeDtoIsValid()
Assert.Equivalent(expected, result);
}

// // TODO
// [Fact]
// public async Task AddNodes_ShouldReturnBadRequestAndRollBack_WhenDbFailsToAddData()
// {
// // Arrange
// var expected = new ActionResponse<MessageDto>()
// {
// Data = new MessageDto(Resources.FailedAddRecordsMessage),
// StatusCode = StatusCodeType.BadRequest
// };
// var csvContent = @"""AccountID"",""CardID"",""IBAN""
// ""6534454617"",""6104335000000190"",""IR120778801496000000198""
// ""6534454617"",""6104335000000190"",""IR120778801496000000198""
// ""4000000028"",""6037699000000020"",""IR033880987114000000028""
// ";
// var fileToBeSend = CreateFileMock(csvContent);
//
// var validatorMock = NSubstitute.Substitute.For<ICsvValidatorService>();
// validatorMock.Validate(fileToBeSend, "AccountID").Returns(expected);
// var processorMock = NSubstitute.Substitute.For<ICsvProcessorService>();
// processorMock.ProcessCsvAsync(fileToBeSend).Returns(new List<dynamic>());
// var additionServiceMock = NSubstitute.Substitute.For<ISingleNodeAdditionService>();
// _sut = new NodesAdditionService(_serviceProvider, validatorMock, processorMock, additionServiceMock);
//
// // Act
// var result = await _sut.AddNodes(new UploadNodeDto()
// {
// File = fileToBeSend,
// NodeCategoryName = "Account",
// UniqueKeyHeaderName = "AccountID"
// });
// // Assert
// Assert.Equivalent(expected, result);
// using var scope = _serviceProvider.CreateScope();
// var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
// Assert.Equal(0, context.Nodes.Count());
// }
// TODO
[Fact]
public async Task AddNodes_ShouldReturnBadRequestAndRollBack_WhenDbFailsToAddData()
{
// Arrange
var expected = new ActionResponse<MessageDto>()
{
Data = new MessageDto(Resources.ValidFileMessage),
StatusCode = StatusCodeType.Success
};
var csvContent = @"""AccountID"",""CardID"",""IBAN""
""6534454617"",""6104335000000190"",""IR120778801496000000198""
""6534454617"",""6104335000000190"",""IR120778801496000000198""
""4000000028"",""6037699000000020"",""IR033880987114000000028""
";
var fileToBeSend = CreateFileMock(csvContent);

var validatorMock = NSubstitute.Substitute.For<ICsvValidatorService>();
validatorMock.Validate(fileToBeSend, "AccountID").Returns(expected);
var processorMock = NSubstitute.Substitute.For<ICsvProcessorService>();
processorMock.ProcessCsvAsync(fileToBeSend).Returns(new List<dynamic>(){ new Dictionary<string, object>() });
var additionServiceMock = new Mock<ISingleNodeAdditionService>();

additionServiceMock
.Setup(service => service.AddSingleNode(
It.IsAny<ApplicationDbContext>(),
It.IsAny<IDictionary<string, object>>(),
It.IsAny<string>(),
It.IsAny<int>()
))
.ThrowsAsync(new Exception("Custom exception message"));
_sut = new NodesAdditionService(_serviceProvider, validatorMock, processorMock, additionServiceMock.Object);

// Act
var result = await _sut.AddNodes(new UploadNodeDto()
{
File = fileToBeSend,
NodeCategoryName = "Account",
UniqueKeyHeaderName = "AccountID"
});
// Assert
using var scope = _serviceProvider.CreateScope();
var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
Assert.Equal(0, context.Nodes.Count());
Assert.Equal("Custom exception message", result.Data.Message);
}


private IFormFile CreateFileMock(string csvContent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,13 @@ public async Task AddSingleEdge_ShouldAddNewEdge_WhenValidRecordIsProvided()
{ "Attribute1", "Value1" }
};

using var scope = _serviceProvider.CreateScope();
var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();

// Act
await _sut.AddSingleEdge(record, "UniqueEdge", "SourceNode", "TargetNode", 1, 1, 1);
await _sut.AddSingleEdge(context, record, "UniqueEdge", "SourceNode", "TargetNode", 1, 1, 1);

// Assert
using var scope = _serviceProvider.CreateScope();
var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
var edge = await context.Edges.SingleOrDefaultAsync(e => e.EdgeUniqueString == "TestEdge");
Assert.NotNull(edge);
Assert.Equal(1, edge.EdgeSourceNodeId);
Expand All @@ -121,12 +122,14 @@ public async Task AddSingleEdge_ShouldAddNewAttributes_WhenEdgeExists()
{ "Attribute2", "Value2" }
};

using var scope = _serviceProvider.CreateScope();
var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();


// Act
await _sut.AddSingleEdge(record, "UniqueEdge", "SourceNode", "TargetNode", 1, 1, 1);
await _sut.AddSingleEdge(context, record, "UniqueEdge", "SourceNode", "TargetNode", 1, 1, 1);

// Assert
using var scope = _serviceProvider.CreateScope();
var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
var edge = await context.Edges.SingleOrDefaultAsync(e => e.EdgeUniqueString == "tran1");
Assert.NotNull(edge);
Assert.Equal(1, edge.EdgeSourceNodeId);
Expand Down Expand Up @@ -154,9 +157,13 @@ public async Task AddSingleEdge_ShouldThrowException_WhenUniqueEdgeNameIsEmpty()
{ "Attribute1", "Value1" }
};

using var scope = _serviceProvider.CreateScope();
var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();


// Act
var action = () =>
_sut.AddSingleEdge(record, "UniqueEdge", "SourceNode", "TargetNode", 1, 1, 1);
_sut.AddSingleEdge(context, record, "UniqueEdge", "SourceNode", "TargetNode", 1, 1, 1);

// Assert
await Assert.ThrowsAsync<Exception>(action);
Expand All @@ -174,10 +181,14 @@ public async Task AddSingleEdge_ShouldThrowException_WhenUniqueTargetNodeNameIsE
{ "TargetNode", "acc2" },
{ "Attribute1", "Value1" }
};

using var scope = _serviceProvider.CreateScope();
var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();


// Act
var action = () =>
_sut.AddSingleEdge(record, "UniqueEdge", "SourceNode", "TargetNode", 1, 1, 1);
_sut.AddSingleEdge(context, record, "UniqueEdge", "SourceNode", "TargetNode", 1, 1, 1);

// Assert
await Assert.ThrowsAsync<Exception>(action);
Expand All @@ -196,9 +207,12 @@ public async Task AddSingleEdge_ShouldThrowException_WhenUniqueSourceNodeNameIsE
{ "Attribute1", "Value1" }
};

using var scope = _serviceProvider.CreateScope();
var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();

// Act
var action = () =>
_sut.AddSingleEdge(record, "UniqueEdge", "SourceNode", "TargetNode", 1, 1, 1);
_sut.AddSingleEdge(context, record, "UniqueEdge", "SourceNode", "TargetNode", 1, 1, 1);

// Assert
await Assert.ThrowsAsync<Exception>(action);
Expand All @@ -215,9 +229,13 @@ public async Task AddSingleEdge_ShouldThrowException_WhenSourceNodeDoesNotExist(
{ "Attribute1", "Value1" }
};

using var scope = _serviceProvider.CreateScope();
var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();


// Act
var action = () =>
_sut.AddSingleEdge(record, "UniqueEdge", "SourceNode", "TargetNode", 1, 1, 1);
_sut.AddSingleEdge(context, record, "UniqueEdge", "SourceNode", "TargetNode", 1, 1, 1);

// Assert
await Assert.ThrowsAsync<Exception>(action);
Expand All @@ -234,10 +252,14 @@ public async Task AddSingleEdge_ShouldThrowException_WhenTargetNodeDoesNotExist(
{ "TargetNode", "NotExist" },
{ "Attribute1", "Value1" }
};

using var scope = _serviceProvider.CreateScope();
var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();


// Act
var action = () =>
_sut.AddSingleEdge(record, "UniqueEdge", "SourceNode", "TargetNode", 1, 1, 1);
_sut.AddSingleEdge(context, record, "UniqueEdge", "SourceNode", "TargetNode", 1, 1, 1);

// Assert
await Assert.ThrowsAsync<Exception>(action);
Expand All @@ -255,9 +277,12 @@ public async Task AddSingleEdge_ShouldThrowException_WhenEdgeValueAlreadyExists(
{ "att1", "dsjsdnfukj" }
};

using var scope = _serviceProvider.CreateScope();
var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();

// Act
var action = () =>
_sut.AddSingleEdge(record, "UniqueEdge", "SourceNode", "TargetNode", 1, 1, 1);
_sut.AddSingleEdge(context, record, "UniqueEdge", "SourceNode", "TargetNode", 1, 1, 1);


// Assert
Expand All @@ -275,10 +300,14 @@ public async Task AddSingleEdge_ShouldThrowException_WhenSourceOrDestinationIsDe
{ "TargetNode", "acc2" },
{ "att2", "dsjsdnfukj" }
};


using var scope = _serviceProvider.CreateScope();
var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();


// Act
var action = () =>
_sut.AddSingleEdge(record, "UniqueEdge", "SourceNode", "TargetNode", 1, 1, 1);
_sut.AddSingleEdge(context, record, "UniqueEdge", "SourceNode", "TargetNode", 1, 1, 1);


// Assert
Expand Down
Loading

0 comments on commit f143191

Please sign in to comment.