完成变量服务的单元测试

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 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 PollLevelType PollLevel { get; set; }
public bool IsActive { get; set; }
@@ -29,4 +33,5 @@ public class VariableDto
public DateTime UpdatedAt { get; set; }
public string UpdatedBy { get; set; }
public bool IsModified { get; set; }
public string Description { get; set; }
}

View File

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

View File

@@ -45,7 +45,20 @@ public class MappingProfile : Profile
CreateMap<Variable, VariableDto>()
.ForMember(dest => dest.DataType, opt => opt.MapFrom(src => src.DataType.ToString()))
.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 映射
CreateMap<MqttServer, MqttServerDto>().ReverseMap();

View File

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