完成变量服务的单元测试
This commit is contained in:
@@ -142,5 +142,31 @@ namespace DMS.Infrastructure.UnitTests
|
||||
return menuDto;
|
||||
}
|
||||
|
||||
public static VariableDto FakeVariableDto()
|
||||
{
|
||||
var variableDto = new Faker<VariableDto>()
|
||||
.RuleFor(v => v.Name, f => f.Commerce.ProductName())
|
||||
.RuleFor(v => v.S7Address, f => $"DB1.DBD{f.Random.Int(0, 1000)}")
|
||||
.RuleFor(v => v.DataType, f => f.PickRandom<SignalType>())
|
||||
.RuleFor(v => v.PollLevel, f => f.PickRandom<PollLevelType>())
|
||||
.RuleFor(v => v.IsActive, f => f.Random.Bool())
|
||||
.RuleFor(v => v.IsHistoryEnabled, f => f.Random.Bool())
|
||||
.RuleFor(v => v.HistoryDeadband, f => f.Random.Double(0.0, 1.0))
|
||||
.RuleFor(v => v.IsAlarmEnabled, f => f.Random.Bool())
|
||||
.RuleFor(v => v.AlarmMinValue, f => f.Random.Double(0.0, 50.0))
|
||||
.RuleFor(v => v.AlarmMaxValue, f => f.Random.Double(50.0, 100.0))
|
||||
.RuleFor(v => v.AlarmDeadband, f => f.Random.Double(0.0, 1.0))
|
||||
.RuleFor(v => v.Protocol, f => f.PickRandom<ProtocolType>())
|
||||
.RuleFor(v => v.CSharpDataType, f => f.PickRandom(Enum.GetValues<CSharpDataType>()))
|
||||
.RuleFor(v => v.OpcUaNodeId, f => $"ns=2;s=My.Variable{f.Random.Int(1, 100)}")
|
||||
.RuleFor(v => v.ConversionFormula, f => "x * 1.0")
|
||||
.RuleFor(v => v.UpdatedBy, f => f.Name.FullName())
|
||||
.RuleFor(v => v.DataValue, f => f.Random.Double(0, 100).ToString())
|
||||
.RuleFor(v => v.DisplayValue, f => f.Random.Word())
|
||||
.RuleFor(v => v.Description, f => f.Lorem.Sentence())
|
||||
.Generate();
|
||||
variableDto.VariableTableId = 1; // Default to 1 for testing purposes
|
||||
return variableDto;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,6 +51,7 @@ public class BaseServiceTest
|
||||
// 注册应用服务
|
||||
services.AddTransient<IDeviceAppService, DeviceAppService>();
|
||||
services.AddTransient<IVariableTableAppService, VariableTableAppService>();
|
||||
services.AddTransient<IVariableAppService, VariableAppService>();
|
||||
// services.AddTransient<IVariableAppService, VariableAppService>(); // 如果需要测试 VariableService,取消此行注释
|
||||
// ... 在这里注册所有其他的应用服务 ...
|
||||
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
using DMS.Application.DTOs;
|
||||
using DMS.Application.Interfaces;
|
||||
using DMS.Application.Services;
|
||||
using JetBrains.Annotations;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace DMS.Infrastructure.UnitTests.Services;
|
||||
|
||||
[TestSubject(typeof(VariableAppService))]
|
||||
public class VariableAppServiceTest : BaseServiceTest
|
||||
{
|
||||
private readonly IVariableAppService _variableAppService;
|
||||
|
||||
public VariableAppServiceTest()
|
||||
{
|
||||
_variableAppService = ServiceProvider.GetRequiredService<IVariableAppService>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CreateVariableAsyncTest()
|
||||
{
|
||||
// Arrange
|
||||
var dto = FakerHelper.FakeVariableDto();
|
||||
dto.VariableTableId = 1; // Assuming a variable table with ID 1 exists for testing
|
||||
|
||||
// Act
|
||||
var createdId = await _variableAppService.CreateVariableAsync(dto);
|
||||
|
||||
// Assert
|
||||
Assert.NotEqual(0, createdId);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateVariableAsyncTest()
|
||||
{
|
||||
// Arrange: Create a variable first
|
||||
var createDto = FakerHelper.FakeVariableDto();
|
||||
createDto.VariableTableId = 1; // Assuming a variable table with ID 1 exists for testing
|
||||
var createdId = await _variableAppService.CreateVariableAsync(createDto);
|
||||
Assert.NotEqual(0, createdId);
|
||||
|
||||
// Retrieve the created variable to update
|
||||
var variableToUpdate = await _variableAppService.GetVariableByIdAsync(createdId);
|
||||
Assert.NotNull(variableToUpdate);
|
||||
|
||||
// Modify some properties
|
||||
variableToUpdate.Name = "Updated Variable Name";
|
||||
variableToUpdate.Description = "Updated Description";
|
||||
|
||||
// Act
|
||||
var affectedRows = await _variableAppService.UpdateVariableAsync(variableToUpdate);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(1, affectedRows);
|
||||
var updatedVariable = await _variableAppService.GetVariableByIdAsync(createdId);
|
||||
Assert.NotNull(updatedVariable);
|
||||
Assert.Equal("Updated Variable Name", updatedVariable.Name);
|
||||
Assert.Equal("Updated Description", updatedVariable.Description);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DeleteVariableAsyncTest()
|
||||
{
|
||||
// Arrange: Create a variable first
|
||||
var createDto = FakerHelper.FakeVariableDto();
|
||||
createDto.VariableTableId = 1; // Assuming a variable table with ID 1 exists for testing
|
||||
var createdId = await _variableAppService.CreateVariableAsync(createDto);
|
||||
Assert.NotEqual(0, createdId);
|
||||
|
||||
// Act
|
||||
var isDeleted = await _variableAppService.DeleteVariableAsync(createdId);
|
||||
|
||||
// Assert
|
||||
Assert.True(isDeleted);
|
||||
var deletedVariable = await _variableAppService.GetVariableByIdAsync(createdId);
|
||||
Assert.Null(deletedVariable);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user