Fix removing primitives

This commit is contained in:
Evgeny Redikultsev
2025-12-07 18:36:50 +05:00
parent 70bfd065c4
commit 681ab17781
32 changed files with 697 additions and 224 deletions

View File

@@ -0,0 +1,101 @@
using DataAccess.DTOs;
using Moq;
using NUnit.Framework;
using StructureHelperCommon.Infrastructures.Interfaces;
using StructureHelperLogics.NdmCalculations.Primitives;
using System;
namespace StructureHelperTests.UnitTests.ConvertStrategiesTest
{
[TestFixture]
public class HasForceAndPrimitivesProcessLogicTests
{
private Mock<IProcessLogic<IHasForceActions>> mockForcesLogic;
private Mock<IProcessLogic<IHasPrimitives>> mockPrimitivesLogic;
private Mock<IHasForcesAndPrimitives> mockSource;
private Mock<IHasForcesAndPrimitives> mockTarget;
private Mock<Dictionary<(Guid id, Type type), ISaveable>> mockDictionary;
[SetUp]
public void SetUp()
{
mockForcesLogic = new Mock<IProcessLogic<IHasForceActions>>(MockBehavior.Strict);
mockPrimitivesLogic = new Mock<IProcessLogic<IHasPrimitives>>(MockBehavior.Strict);
mockDictionary = new Mock<Dictionary<(Guid id, Type type), ISaveable>>();
mockSource = new Mock<IHasForcesAndPrimitives>();
mockTarget = new Mock<IHasForcesAndPrimitives>();
}
[Test]
public void Process_WhenUsingDIInjects_ShouldCallBothInternalLogics()
{
// Arrange
var sut = new HasForcesAndPrimitivesProcessLogic(
ConvertDirection.FromDTO,
mockForcesLogic.Object,
mockPrimitivesLogic.Object
)
{
Source = mockSource.Object,
Target = mockTarget.Object
};
// Setup mocked behavior
mockForcesLogic.SetupSet(x => x.Source = mockSource.Object);
mockForcesLogic.SetupSet(x => x.Target = mockTarget.Object);
mockForcesLogic.SetupSet(x => x.ReferenceDictionary = mockDictionary.Object);
mockForcesLogic.Setup(x => x.Process());
mockPrimitivesLogic.SetupSet(x => x.Source = mockSource.Object);
mockPrimitivesLogic.SetupSet(x => x.Target = mockTarget.Object);
mockPrimitivesLogic.SetupSet(x => x.ReferenceDictionary = mockDictionary.Object);
mockPrimitivesLogic.Setup(x => x.Process());
sut.ReferenceDictionary = new Dictionary<(Guid id, Type type), ISaveable>();
// Act
sut.Process();
// Assert
mockForcesLogic.Verify(x => x.Process(), Times.Once);
mockPrimitivesLogic.Verify(x => x.Process(), Times.Once);
}
[Test]
public void Process_PassesSourceAndTargetCorrectly_ToBothSubLogics()
{
// Arrange
var sut = new HasForcesAndPrimitivesProcessLogic(
ConvertDirection.ToDTO,
mockForcesLogic.Object,
mockPrimitivesLogic.Object)
{
Source = mockSource.Object,
Target = mockTarget.Object,
};
mockForcesLogic.SetupSet(x => x.Source = mockSource.Object);
mockForcesLogic.SetupSet(x => x.Target = mockTarget.Object);
mockForcesLogic.Setup(x => x.Process());
mockPrimitivesLogic.SetupSet(x => x.Source = mockSource.Object);
mockPrimitivesLogic.SetupSet(x => x.Target = mockTarget.Object);
mockPrimitivesLogic.Setup(x => x.Process());
sut.ReferenceDictionary = new Dictionary<(Guid id, Type type), ISaveable>();
// Act
sut.Process();
// Assert
mockForcesLogic.VerifySet(x => x.Source = mockSource.Object);
mockForcesLogic.VerifySet(x => x.Target = mockTarget.Object);
mockPrimitivesLogic.VerifySet(x => x.Source = mockSource.Object);
mockPrimitivesLogic.VerifySet(x => x.Target = mockTarget.Object);
}
}
}

View File

@@ -0,0 +1,128 @@
using DataAccess.DTOs;
using Moq;
using NUnit.Framework;
using StructureHelperCommon.Infrastructures.Exceptions;
using StructureHelperCommon.Infrastructures.Interfaces;
using StructureHelperCommon.Models.Calculators;
using StructureHelperLogics.NdmCalculations.Analyses.ValueDiagrams;
using StructureHelperLogics.NdmCalculations.Primitives;
using System;
using System.Collections.Generic;
namespace StructureHelperTests.UnitTests.ConvertStrategiesTest
{
[TestFixture]
public class ValueDiagramCalculatorInputDataFromDTOConvertStrategyTests
{
private Mock<IUpdateStrategy<IValueDiagramCalculatorInputData>> mockUpdate;
private Mock<IConvertStrategy<ValueDiagramEntity, ValueDiagramEntityDTO>> mockDiagramConvert;
private Mock<IProcessLogic<IHasForcesAndPrimitives>> mockForcePrimLogic;
private Mock<IConvertStrategy<Accuracy, AccuracyDTO>> mockAccuracyConvert;
private ValueDiagramCalculatorInputDataFromDTOConvertStrategy sut;
private ValueDiagramCalculatorInputDataDTO sampleDTO;
private ValueDiagramEntityDTO diagramDTO;
private ValueDiagramEntity sampleDiagramEntity;
[SetUp]
public void SetUp()
{
mockUpdate = new Mock<IUpdateStrategy<IValueDiagramCalculatorInputData>>(MockBehavior.Strict);
mockDiagramConvert = new Mock<IConvertStrategy<ValueDiagramEntity, ValueDiagramEntityDTO>>(MockBehavior.Strict);
mockForcePrimLogic = new Mock<IProcessLogic<IHasForcesAndPrimitives>>(MockBehavior.Strict);
mockAccuracyConvert = new Mock<IConvertStrategy<Accuracy, AccuracyDTO>>(MockBehavior.Strict);
sut = new ValueDiagramCalculatorInputDataFromDTOConvertStrategy(
mockUpdate.Object,
mockDiagramConvert.Object,
mockForcePrimLogic.Object,
mockAccuracyConvert.Object
);
diagramDTO = new ValueDiagramEntityDTO(Guid.Empty);
sampleDiagramEntity = new ValueDiagramEntity(Guid.NewGuid());
sampleDTO = new ValueDiagramCalculatorInputDataDTO(Guid.NewGuid())
{
};
sampleDTO.Diagrams.Add(diagramDTO);
}
[Test]
public void GetNewItem_ShouldReturnItemWithSameId()
{
// Arrange
mockUpdate.Setup(x => x.Update(It.IsAny<IValueDiagramCalculatorInputData>(), sampleDTO));
mockForcePrimLogic.SetupSet(x => x.Source = sampleDTO);
mockForcePrimLogic.SetupSet(x => x.Target = It.IsAny<IValueDiagramCalculatorInputData>());
mockForcePrimLogic.Setup(x => x.Process());
mockDiagramConvert.Setup(x => x.Convert(diagramDTO)).Returns(sampleDiagramEntity);
// Act
var result = sut.GetNewItem(sampleDTO);
// Assert
Assert.That(sampleDTO.Id, Is.EqualTo(result.Id));
}
[Test]
public void GetNewItem_ShouldCallUpdateStrategy()
{
// Arrange
mockUpdate.Setup(x => x.Update(It.IsAny<IValueDiagramCalculatorInputData>(), sampleDTO));
mockForcePrimLogic.SetupSet(x => x.Source = sampleDTO);
mockForcePrimLogic.SetupSet(x => x.Target = It.IsAny<IValueDiagramCalculatorInputData>());
mockForcePrimLogic.Setup(x => x.Process());
mockDiagramConvert.Setup(x => x.Convert(diagramDTO)).Returns(sampleDiagramEntity);
// Act
sut.GetNewItem(sampleDTO);
// Assert
mockUpdate.Verify(x => x.Update(It.IsAny<IValueDiagramCalculatorInputData>(), sampleDTO), Times.Once);
}
[Test]
public void GetNewItem_ShouldCallForcePrimitiveProcessing()
{
// Arrange
mockUpdate.Setup(x => x.Update(It.IsAny<IValueDiagramCalculatorInputData>(), sampleDTO));
mockForcePrimLogic.SetupSet(x => x.Source = sampleDTO);
mockForcePrimLogic.SetupSet(x => x.Target = It.IsAny<IValueDiagramCalculatorInputData>());
mockForcePrimLogic.Setup(x => x.Process());
mockDiagramConvert.Setup(x => x.Convert(diagramDTO)).Returns(sampleDiagramEntity);
// Act
sut.GetNewItem(sampleDTO);
// Assert
mockForcePrimLogic.Verify(x => x.Process(), Times.Once);
}
[Test]
public void GetNewItem_ShouldConvertAllDiagrams()
{
// Arrange
mockUpdate.Setup(x => x.Update(It.IsAny<IValueDiagramCalculatorInputData>(), sampleDTO));
mockForcePrimLogic.SetupSet(x => x.Source = sampleDTO);
mockForcePrimLogic.SetupSet(x => x.Target = It.IsAny<IValueDiagramCalculatorInputData>());
mockForcePrimLogic.Setup(x => x.Process());
mockDiagramConvert.Setup(x => x.Convert(diagramDTO)).Returns(sampleDiagramEntity);
// Act
var result = sut.GetNewItem(sampleDTO);
// Assert
Assert.That(1, Is.EqualTo(result.Diagrams.Count));
Assert.That(sampleDiagramEntity, Is.SameAs(result.Diagrams[0]));
}
}
}