完成变量服务的单元测试

This commit is contained in:
2025-07-24 21:41:00 +08:00
parent 4657a461e3
commit 35e5033094
9 changed files with 152 additions and 20 deletions

View File

@@ -10,7 +10,11 @@ public class VariableDto
{ {
public int Id { get; set; } public int Id { get; set; }
public string Name { get; set; } public string Name { get; set; }
public string Address { get; set; } public string? S7Address { get; set; }
public string? DataValue { get; set; }
public string? DisplayValue { get; set; }
public VariableTableDto? VariableTable { get; set; }
public List<VariableMqttAliasDto>? MqttAliases { get; set; }
public SignalType DataType { get; set; } public SignalType DataType { get; set; }
public PollLevelType PollLevel { get; set; } public PollLevelType PollLevel { get; set; }
public bool IsActive { get; set; } public bool IsActive { get; set; }
@@ -29,4 +33,5 @@ public class VariableDto
public DateTime UpdatedAt { get; set; } public DateTime UpdatedAt { get; set; }
public string UpdatedBy { get; set; } public string UpdatedBy { get; set; }
public bool IsModified { get; set; } public bool IsModified { get; set; }
public string Description { get; set; }
} }

View File

@@ -25,10 +25,10 @@ public interface IVariableAppService
/// <summary> /// <summary>
/// 异步更新一个已存在的变量。 /// 异步更新一个已存在的变量。
/// </summary> /// </summary>
Task UpdateVariableAsync(VariableDto variableDto); Task<int> UpdateVariableAsync(VariableDto variableDto);
/// <summary> /// <summary>
/// 异步删除一个变量。 /// 异步删除一个变量。
/// </summary> /// </summary>
Task DeleteVariableAsync(int id); Task<bool> DeleteVariableAsync(int id);
} }

View File

@@ -45,7 +45,20 @@ public class MappingProfile : Profile
CreateMap<Variable, VariableDto>() CreateMap<Variable, VariableDto>()
.ForMember(dest => dest.DataType, opt => opt.MapFrom(src => src.DataType.ToString())) .ForMember(dest => dest.DataType, opt => opt.MapFrom(src => src.DataType.ToString()))
.ForMember(dest => dest.CSharpDataType, opt => opt.MapFrom(src => src.CSharpDataType)) .ForMember(dest => dest.CSharpDataType, opt => opt.MapFrom(src => src.CSharpDataType))
.ForMember(dest => dest.Address, opt => opt.Ignore()); .ForMember(dest => dest.S7Address, opt => opt.MapFrom(src => src.S7Address))
.ForMember(dest => dest.DataValue, opt => opt.MapFrom(src => src.DataValue))
.ForMember(dest => dest.DisplayValue, opt => opt.MapFrom(src => src.DisplayValue))
.ForMember(dest => dest.VariableTable, opt => opt.MapFrom(src => src.VariableTable))
.ForMember(dest => dest.MqttAliases, opt => opt.MapFrom(src => src.MqttAliases))
.ForMember(dest => dest.Description, opt => opt.MapFrom(src => src.Description));
CreateMap<VariableDto, Variable>()
.ForMember(dest => dest.S7Address, opt => opt.MapFrom(src => src.S7Address))
.ForMember(dest => dest.VariableTable, opt => opt.Ignore())
.ForMember(dest => dest.MqttAliases, opt => opt.Ignore())
.ForMember(dest => dest.DataValue, opt => opt.Ignore())
.ForMember(dest => dest.DisplayValue, opt => opt.Ignore())
.ForMember(dest => dest.Description, opt => opt.MapFrom(src => src.Description));
// MqttServer 映射 // MqttServer 映射
CreateMap<MqttServer, MqttServerDto>().ReverseMap(); CreateMap<MqttServer, MqttServerDto>().ReverseMap();

View File

@@ -59,14 +59,14 @@ public class VariableAppService : IVariableAppService
{ {
await _repoManager.BeginTranAsync(); await _repoManager.BeginTranAsync();
var variable = _mapper.Map<Variable>(variableDto); var variable = _mapper.Map<Variable>(variableDto);
await _repoManager.Variables.AddAsync(variable); var addedVariable = await _repoManager.Variables.AddAsync(variable);
await _repoManager.CommitAsync(); await _repoManager.CommitAsync();
return variable.Id; return addedVariable.Id;
} }
catch (Exception ex) catch (Exception ex)
{ {
await _repoManager.RollbackAsync(); await _repoManager.RollbackAsync();
throw new ApplicationException("创建变量时发生错误,操作已回滚", ex); throw new ApplicationException($"创建变量时发生错误,操作已回滚,错误信息:{ex.Message}", ex);
} }
} }
@@ -74,9 +74,9 @@ public class VariableAppService : IVariableAppService
/// 异步更新一个已存在的变量(事务性操作)。 /// 异步更新一个已存在的变量(事务性操作)。
/// </summary> /// </summary>
/// <param name="variableDto">要更新的变量数据传输对象。</param> /// <param name="variableDto">要更新的变量数据传输对象。</param>
/// <returns>表示异步操作的任务。</returns> /// <returns>受影响的行数。</returns>
/// <exception cref="ApplicationException">如果找不到变量或更新变量时发生错误。</exception> /// <exception cref="ApplicationException">如果找不到变量或更新变量时发生错误。</exception>
public async Task UpdateVariableAsync(VariableDto variableDto) public async Task<int> UpdateVariableAsync(VariableDto variableDto)
{ {
try try
{ {
@@ -87,13 +87,14 @@ public class VariableAppService : IVariableAppService
throw new ApplicationException($"Variable with ID {variableDto.Id} not found."); throw new ApplicationException($"Variable with ID {variableDto.Id} not found.");
} }
_mapper.Map(variableDto, variable); _mapper.Map(variableDto, variable);
await _repoManager.Variables.UpdateAsync(variable); int res = await _repoManager.Variables.UpdateAsync(variable);
await _repoManager.CommitAsync(); await _repoManager.CommitAsync();
return res;
} }
catch (Exception ex) catch (Exception ex)
{ {
await _repoManager.RollbackAsync(); await _repoManager.RollbackAsync();
throw new ApplicationException("更新变量时发生错误,操作已回滚", ex); throw new ApplicationException($"更新变量时发生错误,操作已回滚,错误信息:{ex.Message}", ex);
} }
} }
@@ -101,20 +102,26 @@ public class VariableAppService : IVariableAppService
/// 异步删除一个变量(事务性操作)。 /// 异步删除一个变量(事务性操作)。
/// </summary> /// </summary>
/// <param name="id">要删除变量的ID。</param> /// <param name="id">要删除变量的ID。</param>
/// <returns>表示异步操作的任务。</returns> /// <returns>如果删除成功则为 true否则为 false。</returns>
/// <exception cref="ApplicationException">如果删除变量时发生错误。</exception> /// <exception cref="InvalidOperationException">如果删除变量失败。</exception>
public async Task DeleteVariableAsync(int id) /// <exception cref="ApplicationException">如果删除变量时发生其他错误。</exception>
public async Task<bool> DeleteVariableAsync(int id)
{ {
try try
{ {
await _repoManager.BeginTranAsync(); await _repoManager.BeginTranAsync();
await _repoManager.Variables.DeleteByIdAsync(id); var delRes = await _repoManager.Variables.DeleteByIdAsync(id);
if (delRes == 0)
{
throw new InvalidOperationException($"删除变量失败变量ID:{id}请检查变量Id是否存在");
}
await _repoManager.CommitAsync(); await _repoManager.CommitAsync();
return true;
} }
catch (Exception ex) catch (Exception ex)
{ {
await _repoManager.RollbackAsync(); await _repoManager.RollbackAsync();
throw new ApplicationException("删除变量时发生错误,操作已回滚", ex); throw new ApplicationException($"删除变量时发生错误,操作已回滚,错误信息:{ex.Message}", ex);
} }
} }
} }

View File

@@ -142,5 +142,31 @@ namespace DMS.Infrastructure.UnitTests
return menuDto; 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;
}
} }
} }

View File

@@ -51,6 +51,7 @@ public class BaseServiceTest
// 注册应用服务 // 注册应用服务
services.AddTransient<IDeviceAppService, DeviceAppService>(); services.AddTransient<IDeviceAppService, DeviceAppService>();
services.AddTransient<IVariableTableAppService, VariableTableAppService>(); services.AddTransient<IVariableTableAppService, VariableTableAppService>();
services.AddTransient<IVariableAppService, VariableAppService>();
// services.AddTransient<IVariableAppService, VariableAppService>(); // 如果需要测试 VariableService取消此行注释 // services.AddTransient<IVariableAppService, VariableAppService>(); // 如果需要测试 VariableService取消此行注释
// ... 在这里注册所有其他的应用服务 ... // ... 在这里注册所有其他的应用服务 ...

View File

@@ -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);
}
}

View File

@@ -1,4 +1,5 @@
using SqlSugar; using SqlSugar;
using SqlSugar.DbConvert;
namespace DMS.Infrastructure.Entities; namespace DMS.Infrastructure.Entities;
@@ -7,7 +8,7 @@ public class DbVariable
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)] [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
public int Id { get; set; } public int Id { get; set; }
public string Name { get; set; } public string Name { get; set; }
public string Address { get; set; } public string Description { get; set; }
public int DataType { get; set; } // 对应 SignalType 枚举 public int DataType { get; set; } // 对应 SignalType 枚举
public int PollLevel { get; set; } // 对应 PollLevelType 枚举 public int PollLevel { get; set; } // 对应 PollLevelType 枚举
public bool IsActive { get; set; } public bool IsActive { get; set; }
@@ -20,8 +21,10 @@ public class DbVariable
public double AlarmMinValue { get; set; } public double AlarmMinValue { get; set; }
public double AlarmMaxValue { get; set; } public double AlarmMaxValue { get; set; }
public double AlarmDeadband { get; set; } public double AlarmDeadband { get; set; }
public int Protocol { get; set; } // 对应 ProtocolType 枚举 [SugarColumn(ColumnDataType="varchar(20)",SqlParameterDbType=typeof(EnumToStringConvert))]
public int CSharpDataType { get; set; } // 对应 CSharpDataType 枚举 public ProtocolType Protocol { get; set; } // 对应 ProtocolType 枚举
[SugarColumn(ColumnDataType="varchar(20)",SqlParameterDbType=typeof(EnumToStringConvert))]
public CSharpDataType CSharpDataType { get; set; } // 对应 CSharpDataType 枚举
public string ConversionFormula { get; set; } public string ConversionFormula { get; set; }
public DateTime CreatedAt { get; set; } public DateTime CreatedAt { get; set; }
public DateTime UpdatedAt { get; set; } public DateTime UpdatedAt { get; set; }

View File

@@ -27,7 +27,6 @@ public class MappingProfile : Profile
.ReverseMap(); .ReverseMap();
CreateMap<DbVariable, Variable>() CreateMap<DbVariable, Variable>()
.ForMember(dest => dest.Description, opt => opt.Ignore())
.ForMember(dest => dest.VariableTable, opt => opt.Ignore()) .ForMember(dest => dest.VariableTable, opt => opt.Ignore())
.ForMember(dest => dest.MqttAliases, opt => opt.Ignore()) .ForMember(dest => dest.MqttAliases, opt => opt.Ignore())
.ForMember(dest => dest.DataValue, opt => opt.Ignore()) .ForMember(dest => dest.DataValue, opt => opt.Ignore())