添加单元 测试
This commit is contained in:
@@ -5,5 +5,7 @@ namespace DMS.Core.Interfaces
|
|||||||
void InitializeTables();
|
void InitializeTables();
|
||||||
void InitializeTableIndex();
|
void InitializeTableIndex();
|
||||||
void InitializeMenus();
|
void InitializeMenus();
|
||||||
|
|
||||||
|
bool IsAnyTable(string tableName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -12,6 +12,7 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Bogus" Version="35.6.3" />
|
<PackageReference Include="Bogus" Version="35.6.3" />
|
||||||
<PackageReference Include="coverlet.collector" Version="6.0.0" />
|
<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="Microsoft.NET.Test.Sdk" Version="17.8.0" />
|
||||||
<PackageReference Include="Moq" Version="4.20.72" />
|
<PackageReference Include="Moq" Version="4.20.72" />
|
||||||
<PackageReference Include="xunit" Version="2.5.3" />
|
<PackageReference Include="xunit" Version="2.5.3" />
|
||||||
|
|||||||
31
DMS.Infrastructure.UnitTests/Services/DatabaseServiceTest.cs
Normal file
31
DMS.Infrastructure.UnitTests/Services/DatabaseServiceTest.cs
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
51
DMS.Infrastructure.UnitTests/Services/DeviceServiceTest.cs
Normal file
51
DMS.Infrastructure.UnitTests/Services/DeviceServiceTest.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,10 +1,7 @@
|
|||||||
using DMS.Core.Models;
|
using DMS.Core.Models;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.IO;
|
|
||||||
|
|
||||||
namespace DMS.Config
|
namespace DMS.Infrastructure.Configurations
|
||||||
{
|
{
|
||||||
public class DatabaseSettings
|
public class DatabaseSettings
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
using DMS.Config;
|
using DMS.Infrastructure.Configurations;
|
||||||
using SqlSugar;
|
using SqlSugar;
|
||||||
|
|
||||||
namespace DMS.Infrastructure.Data;
|
namespace DMS.Infrastructure.Data;
|
||||||
|
|||||||
@@ -56,6 +56,7 @@ public class DbDevice
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
///
|
///
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
[SugarColumn(IsNullable = true)]
|
||||||
public string CpuType { get; set; }
|
public string CpuType { get; set; }
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 设备槽号 (针对PLC等设备)。
|
/// 设备槽号 (针对PLC等设备)。
|
||||||
@@ -73,4 +74,11 @@ public class DbDevice
|
|||||||
/// 设备是否激活/启用。
|
/// 设备是否激活/启用。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool IsActive { get; set; }
|
public bool IsActive { get; set; }
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 此设备包含的变量表集合。
|
||||||
|
/// </summary>
|
||||||
|
[SugarColumn(IsIgnore = true)]
|
||||||
|
public List<DbVariableTable> VariableTables { get; set; } = new();
|
||||||
}
|
}
|
||||||
@@ -17,15 +17,30 @@ public class MappingProfile : Profile
|
|||||||
|
|
||||||
// --- 设备映射 (包含List的父对象) ---
|
// --- 设备映射 (包含List的父对象) ---
|
||||||
// AutoMapper 会自动使用上面的规则来处理 VariableTables 属性
|
// AutoMapper 会自动使用上面的规则来处理 VariableTables 属性
|
||||||
CreateMap<DbDevice, Device>().ReverseMap();
|
CreateMap<DbDevice, Device>()
|
||||||
|
.ReverseMap();
|
||||||
|
|
||||||
// --- 变量表映射 (List中的元素) ---
|
// --- 变量表映射 (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 和 变量数据 映射 ---
|
// --- MQTT 和 变量数据 映射 ---
|
||||||
CreateMap<DbMqttServer, MqttServer>().ReverseMap();
|
CreateMap<DbMqttServer, MqttServer>()
|
||||||
CreateMap<DbVariableMqttAlias, VariableMqttAlias>().ReverseMap();
|
.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();
|
CreateMap<DbMenu, MenuBean>().ReverseMap();
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
using DMS.Config;
|
|
||||||
using DMS.Core.Interfaces;
|
using DMS.Core.Interfaces;
|
||||||
using DMS.Core.Models;
|
using DMS.Core.Models;
|
||||||
|
using DMS.Infrastructure.Configurations;
|
||||||
using DMS.Infrastructure.Data;
|
using DMS.Infrastructure.Data;
|
||||||
using DMS.Infrastructure.Entities;
|
using DMS.Infrastructure.Entities;
|
||||||
using SqlSugar;
|
using SqlSugar;
|
||||||
@@ -22,7 +22,6 @@ public class DatabaseService : IDatabaseService
|
|||||||
|
|
||||||
public void InitializeTables()
|
public void InitializeTables()
|
||||||
{
|
{
|
||||||
|
|
||||||
_db.DbMaintenance.CreateDatabase();
|
_db.DbMaintenance.CreateDatabase();
|
||||||
_db.CodeFirst.InitTables<DbNlog>();
|
_db.CodeFirst.InitTables<DbNlog>();
|
||||||
_db.CodeFirst.InitTables<DbDevice>();
|
_db.CodeFirst.InitTables<DbDevice>();
|
||||||
@@ -53,6 +52,15 @@ public class DatabaseService : IDatabaseService
|
|||||||
},true);
|
},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()
|
public void InitializeMenus()
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user