From b0d5db3626696ce1177722c713c42934791ead77 Mon Sep 17 00:00:00 2001 From: "David P.G" Date: Tue, 22 Jul 2025 22:01:44 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=B4=E6=97=B6=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DMS.Infrastructure.UnitTests/FakerHelper.cs | 20 +++--- .../Services/DeviceServiceTest.cs | 15 ++++ .../Services/VariableTableServieTest.cs | 71 +++++++++++++++++++ .../Entities/DbVariableTable.cs | 41 ++++++++++- 4 files changed, 135 insertions(+), 12 deletions(-) create mode 100644 DMS.Infrastructure.UnitTests/Services/VariableTableServieTest.cs diff --git a/DMS.Infrastructure.UnitTests/FakerHelper.cs b/DMS.Infrastructure.UnitTests/FakerHelper.cs index 366053c..86ea66d 100644 --- a/DMS.Infrastructure.UnitTests/FakerHelper.cs +++ b/DMS.Infrastructure.UnitTests/FakerHelper.cs @@ -28,16 +28,16 @@ namespace DMS.Infrastructure.UnitTests return dbDevice; } - // public static DbVariableTable FakeDbVariableTable() - // { - // var dbVarTable = new Faker() - // .RuleFor(d => d.Name, f => f.Commerce.ProductName()) - // .RuleFor(d => d.Description, f => f.Commerce.ProductDescription()) - // .Generate(); - // dbVarTable.ProtocolType = Core.Enums.ProtocolType.S7; - // dbVarTable.IsActive=true; - // return dbVarTable; - // } + public static DbVariableTable FakeDbVariableTable() + { + var dbVarTable = new Faker() + .RuleFor(d => d.Name, f => f.Commerce.ProductName()) + .RuleFor(d => d.Description, f => f.Commerce.ProductDescription()) + .Generate(); + dbVarTable.Protocol = ProtocolType.S7; + dbVarTable.IsActive=true; + return dbVarTable; + } // public static DbVariable FakeDbVariable() // { diff --git a/DMS.Infrastructure.UnitTests/Services/DeviceServiceTest.cs b/DMS.Infrastructure.UnitTests/Services/DeviceServiceTest.cs index f42b719..3f8cceb 100644 --- a/DMS.Infrastructure.UnitTests/Services/DeviceServiceTest.cs +++ b/DMS.Infrastructure.UnitTests/Services/DeviceServiceTest.cs @@ -54,5 +54,20 @@ public class DeviceServiceTest { var device= await _deviceService.TakeAsync(2); Assert.Equal(2,device.Count); + } + [Fact] + public async Task UpdateAsync_Test() + { + var devices= await _deviceService.TakeAsync(1); + devices[0].IpAddress = "127.0.0.1"; + var res= await _deviceService.UpdateAsync(devices[0]); + Assert.Equal(1,res); + } + [Fact] + public async Task DeleteAsync_Test() + { + var devices= await _deviceService.TakeAsync(1); + var res= await _deviceService.DeleteAsync(devices[0]); + Assert.Equal(1,res); } } \ No newline at end of file diff --git a/DMS.Infrastructure.UnitTests/Services/VariableTableServieTest.cs b/DMS.Infrastructure.UnitTests/Services/VariableTableServieTest.cs new file mode 100644 index 0000000..a499a9f --- /dev/null +++ b/DMS.Infrastructure.UnitTests/Services/VariableTableServieTest.cs @@ -0,0 +1,71 @@ +using AutoMapper; +using DMS.Core.Models; +using DMS.Infrastructure.Configurations; +using DMS.Infrastructure.Data; +using DMS.Infrastructure.Profiles; +using DMS.Infrastructure.Repositories; +using DMS.Infrastructure.Services; + +namespace DMS.Infrastructure.UnitTests.Services; + +public class VariableTableServieTest +{ + private readonly VariableTableRepository _variableTableRepository; + private readonly VariableTableService _variableTableService; + private readonly IMapper _mapper; + + public VariableTableServieTest() + { + // 1. 创建 MapperConfiguration + var mappingConfig = new MapperConfiguration(mc => + + { + // 添加你的所有 Profile + mc.AddProfile(new MappingProfile()); + // 如果有其他 Profile,也可以在这里添加 + // mc.AddProfile(new AnotherProfile()); + }); + + // 2. 验证映射配置是否有效 (可选,但在开发环境中推荐) + mappingConfig.AssertConfigurationIsValid(); + // 3. 创建 IMapper 实例 + _mapper = mappingConfig.CreateMapper(); + + + AppSettings appSettings = new AppSettings(); + appSettings.Database.Database = "dms_test"; + SqlSugarDbContext dbContext = new SqlSugarDbContext(appSettings); + _variableTableRepository= new VariableTableRepository(_mapper,dbContext); + _variableTableService = new VariableTableService(_variableTableRepository); + } + + [Fact] + public async Task AddAsync_Test() + { + // var dbDevice = FakerHelper + // var addDevice= await _variableTableService.AddAsync(_mapper.Map(dbDevice)); + // Assert.NotEqual(0, addDevice.Id); + } + + [Fact] + public async Task TakeAsync_Test() + { + var device= await _variableTableService.TakeAsync(2); + Assert.Equal(2,device.Count); + } + [Fact] + public async Task UpdateAsync_Test() + { + var devices= await _variableTableService.TakeAsync(1); + // devices[0].IpAddress = "127.0.0.1"; + var res= await _variableTableService.UpdateAsync(devices[0]); + Assert.Equal(1,res); + } + [Fact] + public async Task DeleteAsync_Test() + { + var devices= await _variableTableService.TakeAsync(1); + var res= await _variableTableService.DeleteAsync(devices[0]); + Assert.Equal(1,res); + } +} \ No newline at end of file diff --git a/DMS.Infrastructure/Entities/DbVariableTable.cs b/DMS.Infrastructure/Entities/DbVariableTable.cs index 1c67962..9745786 100644 --- a/DMS.Infrastructure/Entities/DbVariableTable.cs +++ b/DMS.Infrastructure/Entities/DbVariableTable.cs @@ -1,14 +1,51 @@ -using SqlSugar; +using DMS.Core.Enums; +using SqlSugar; +using SqlSugar.DbConvert; namespace DMS.Infrastructure.Entities; +/// +/// 变量表 +/// public class DbVariableTable { + /// + /// 主键ID + /// [SugarColumn(IsPrimaryKey = true, IsIdentity = true)] public int Id { get; set; } + /// + /// 变量表名称 + /// public string Name { get; set; } + /// + /// 描述 + /// + [SugarColumn(IsNullable = true)] public string Description { get; set; } + /// + /// 是否激活 + /// public bool IsActive { get; set; } + /// + /// 设备ID + /// public int DeviceId { get; set; } - public int Protocol { get; set; } // 对应 ProtocolType 枚举 + /// + /// 关联的设备 + /// + [SugarColumn(IsIgnore = true)] + public DbDevice Device { get; set; } + + /// + /// 协议类型 + /// + [SugarColumn(ColumnDataType="varchar(20)",SqlParameterDbType=typeof(EnumToStringConvert))] + public ProtocolType Protocol { get; set; } // 对应 ProtocolType 枚举 + + /// + /// 此设备包含的变量表集合。 + /// + [SugarColumn(IsIgnore = true)] + public List Variables { get; set; } = new(); } \ No newline at end of file