添加单元 测试

This commit is contained in:
2025-07-21 23:04:28 +08:00
parent 8f3543afb5
commit b881c89d96
9 changed files with 127 additions and 14 deletions

View File

@@ -5,5 +5,7 @@ namespace DMS.Core.Interfaces
void InitializeTables();
void InitializeTableIndex();
void InitializeMenus();
bool IsAnyTable(string tableName);
}
}

View File

@@ -12,6 +12,7 @@
<ItemGroup>
<PackageReference Include="Bogus" Version="35.6.3" />
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="JetBrains.Annotations" Version="2025.1.0-eap1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="xunit" Version="2.5.3" />

View File

@@ -0,0 +1,31 @@
using DMS.Core.Interfaces;
using DMS.Infrastructure.Configurations;
using DMS.Infrastructure.Data;
using DMS.Infrastructure.Services;
namespace DMS.Infrastructure.UnitTests.Services;
public class DatabaseServiceTest
{
private IDatabaseService _databaseService;
public DatabaseServiceTest()
{
AppSettings appSettings = new AppSettings();
appSettings.Database.Database = "dms_test";
SqlSugarDbContext dbContext=new SqlSugarDbContext(appSettings);
_databaseService = new DatabaseService(dbContext);
}
[Fact]
public void InitializeTables_Test()
{
_databaseService.InitializeTables();
Assert.True(_databaseService.IsAnyTable("dbdevice"));
}
[Fact]
public void InitializeTableIndex_Test()
{
_databaseService.InitializeTableIndex();
}
}

View File

@@ -0,0 +1,51 @@
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;
using JetBrains.Annotations;
namespace DMS.Infrastructure.UnitTests.Services;
[TestSubject(typeof(DeviceService))]
public class DeviceServiceTest
{
private readonly DeviceRepository _deviceRepository;
private readonly DeviceService _deviceService;
private readonly IMapper _mapper;
public DeviceServiceTest()
{
// 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);
_deviceRepository= new DeviceRepository(_mapper,dbContext);
_deviceService = new DeviceService(_deviceRepository);
}
[Fact]
public async Task AddAsync_Test()
{
var dbDevice = FakerHelper.FakeDbDevice();
var addDevice= await _deviceService.AddAsync(_mapper.Map<Device>(dbDevice));
Assert.NotEqual(0, addDevice.Id);
}
}

View File

@@ -1,10 +1,7 @@
using DMS.Core.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
namespace DMS.Config
namespace DMS.Infrastructure.Configurations
{
public class DatabaseSettings
{

View File

@@ -1,4 +1,4 @@
using DMS.Config;
using DMS.Infrastructure.Configurations;
using SqlSugar;
namespace DMS.Infrastructure.Data;

View File

@@ -56,6 +56,7 @@ public class DbDevice
/// <summary>
///
/// </summary>
[SugarColumn(IsNullable = true)]
public string CpuType { get; set; }
/// <summary>
/// 设备槽号 (针对PLC等设备)。
@@ -73,4 +74,11 @@ public class DbDevice
/// 设备是否激活/启用。
/// </summary>
public bool IsActive { get; set; }
/// <summary>
/// 此设备包含的变量表集合。
/// </summary>
[SugarColumn(IsIgnore = true)]
public List<DbVariableTable> VariableTables { get; set; } = new();
}

View File

@@ -17,15 +17,30 @@ public class MappingProfile : Profile
// --- 设备映射 (包含List的父对象) ---
// AutoMapper 会自动使用上面的规则来处理 VariableTables 属性
CreateMap<DbDevice, Device>().ReverseMap();
CreateMap<DbDevice, Device>()
.ReverseMap();
// --- 变量表映射 (List中的元素) ---
CreateMap<DbVariableTable, VariableTable>().ReverseMap();
CreateMap<DbVariableTable, VariableTable>()
.ForMember(dest => dest.Variables, opt => opt.Ignore())
.ForMember(dest => dest.Device, opt => opt.Ignore())
.ReverseMap();
CreateMap<DbVariable, Variable>().ReverseMap();
CreateMap<DbVariable, Variable>()
.ForMember(dest => dest.Description, opt => opt.Ignore())
.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())
.ReverseMap();
// --- MQTT 和 变量数据 映射 ---
CreateMap<DbMqttServer, MqttServer>().ReverseMap();
CreateMap<DbVariableMqttAlias, VariableMqttAlias>().ReverseMap();
CreateMap<DbMqttServer, MqttServer>()
.ForMember(dest => dest.VariableAliases, opt => opt.Ignore())
.ReverseMap();
CreateMap<DbVariableMqttAlias, VariableMqttAlias>()
.ForMember(dest => dest.Variable, opt => opt.Ignore())
.ForMember(dest => dest.MqttServer, opt => opt.Ignore())
.ReverseMap();
CreateMap<DbMenu, MenuBean>().ReverseMap();

View File

@@ -1,6 +1,6 @@
using DMS.Config;
using DMS.Core.Interfaces;
using DMS.Core.Models;
using DMS.Infrastructure.Configurations;
using DMS.Infrastructure.Data;
using DMS.Infrastructure.Entities;
using SqlSugar;
@@ -22,7 +22,6 @@ public class DatabaseService : IDatabaseService
public void InitializeTables()
{
_db.DbMaintenance.CreateDatabase();
_db.CodeFirst.InitTables<DbNlog>();
_db.CodeFirst.InitTables<DbDevice>();
@@ -53,6 +52,15 @@ public class DatabaseService : IDatabaseService
},true);
}
public bool IsAnyTable(string tableName)
{
return _db.DbMaintenance.IsAnyTable(tableName, false);
}
public bool IsAnyIndex(string indexName)
{
return _db.DbMaintenance.IsAnyIndex(indexName);
}
public void InitializeMenus()
{