完成新建变量表的功能测试
This commit is contained in:
12
DMS.Application/DTOs/CreateVariableTableWithMenuDto.cs
Normal file
12
DMS.Application/DTOs/CreateVariableTableWithMenuDto.cs
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
|
||||||
|
using DMS.Core.Models;
|
||||||
|
|
||||||
|
namespace DMS.Application.DTOs
|
||||||
|
{
|
||||||
|
public class CreateVariableTableWithMenuDto
|
||||||
|
{
|
||||||
|
public VariableTableDto VariableTable { get; set; }
|
||||||
|
public int DeviceId { get; set; }
|
||||||
|
public MenuBeanDto Menu { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
16
DMS.Application/Interfaces/IVariableTableAppService.cs
Normal file
16
DMS.Application/Interfaces/IVariableTableAppService.cs
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
|
||||||
|
using DMS.Application.DTOs;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace DMS.Application.Interfaces
|
||||||
|
{
|
||||||
|
public interface IVariableTableAppService
|
||||||
|
{
|
||||||
|
Task<VariableTableDto> GetVariableTableByIdAsync(int id);
|
||||||
|
Task<List<VariableTableDto>> GetAllVariableTablesAsync();
|
||||||
|
Task<VariableTableDto> CreateVariableTableAsync(CreateVariableTableWithMenuDto createDto);
|
||||||
|
Task UpdateVariableTableAsync(VariableTableDto variableTableDto);
|
||||||
|
Task DeleteVariableTableAsync(int id);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -10,7 +10,7 @@ namespace DMS.Application.Services;
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 实现设备管理的应用服务。
|
/// 实现设备管理的应用服务。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class DeviceService : IDeviceAppService
|
public class DeviceAppService : IDeviceAppService
|
||||||
{
|
{
|
||||||
private readonly IRepositoryManager _repoManager;
|
private readonly IRepositoryManager _repoManager;
|
||||||
private readonly IMapper _mapper;
|
private readonly IMapper _mapper;
|
||||||
@@ -18,7 +18,7 @@ public class DeviceService : IDeviceAppService
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 构造函数,通过依赖注入获取仓储管理器和AutoMapper实例。
|
/// 构造函数,通过依赖注入获取仓储管理器和AutoMapper实例。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public DeviceService(IRepositoryManager repoManager, IMapper mapper)
|
public DeviceAppService(IRepositoryManager repoManager, IMapper mapper)
|
||||||
{
|
{
|
||||||
_repoManager = repoManager;
|
_repoManager = repoManager;
|
||||||
_mapper = mapper;
|
_mapper = mapper;
|
||||||
90
DMS.Application/Services/VariableTableAppService.cs
Normal file
90
DMS.Application/Services/VariableTableAppService.cs
Normal file
@@ -0,0 +1,90 @@
|
|||||||
|
using AutoMapper;
|
||||||
|
using DMS.Application.DTOs;
|
||||||
|
using DMS.Application.Interfaces;
|
||||||
|
using DMS.Core.Interfaces;
|
||||||
|
using DMS.Core.Models;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using DMS.Core.Enums;
|
||||||
|
|
||||||
|
namespace DMS.Application.Services
|
||||||
|
{
|
||||||
|
public class VariableTableAppService : IVariableTableAppService
|
||||||
|
{
|
||||||
|
private readonly IRepositoryManager _repositoryManager;
|
||||||
|
private readonly IMapper _mapper;
|
||||||
|
|
||||||
|
public VariableTableAppService(IRepositoryManager repositoryManager, IMapper mapper)
|
||||||
|
{
|
||||||
|
_repositoryManager = repositoryManager;
|
||||||
|
_mapper = mapper;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<VariableTableDto> GetVariableTableByIdAsync(int id)
|
||||||
|
{
|
||||||
|
var variableTable = await _repositoryManager.VariableTables.GetByIdAsync(id);
|
||||||
|
return _mapper.Map<VariableTableDto>(variableTable);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<List<VariableTableDto>> GetAllVariableTablesAsync()
|
||||||
|
{
|
||||||
|
var variableTables = await _repositoryManager.VariableTables.GetAllAsync();
|
||||||
|
return _mapper.Map<List<VariableTableDto>>(variableTables);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<VariableTableDto> CreateVariableTableAsync(CreateVariableTableWithMenuDto createDto)
|
||||||
|
{
|
||||||
|
await _repositoryManager.BeginTranAsync();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var variableTable = _mapper.Map<VariableTable>(createDto.VariableTable);
|
||||||
|
variableTable.DeviceId = createDto.DeviceId;
|
||||||
|
|
||||||
|
var createdVariableTable = await _repositoryManager.VariableTables.AddAsync(variableTable);
|
||||||
|
if (createdVariableTable.Id == 0)
|
||||||
|
{
|
||||||
|
throw new ApplicationException($"添加变量表失败,设备ID:{createDto.DeviceId},请检查。");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (createDto.Menu!=null)
|
||||||
|
{
|
||||||
|
var deviceMenu
|
||||||
|
= await _repositoryManager.Menus.GetMenuByTargetIdAsync(
|
||||||
|
MenuType.DeviceMenu, createDto.DeviceId);
|
||||||
|
if (deviceMenu == null)
|
||||||
|
{
|
||||||
|
throw new ApplicationException($"添加变量表菜单时,找不到设备ID:{createDto.DeviceId},请检查。");
|
||||||
|
}
|
||||||
|
|
||||||
|
var menu = _mapper.Map<MenuBean>(createDto.Menu);
|
||||||
|
menu.ParentId = deviceMenu.Id;
|
||||||
|
menu.TargetId = createdVariableTable.Id;
|
||||||
|
menu.MenuType = MenuType.VariableTableMenu;
|
||||||
|
await _repositoryManager.Menus.AddAsync(menu);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
await _repositoryManager.CommitAsync();
|
||||||
|
|
||||||
|
return _mapper.Map<VariableTableDto>(createdVariableTable);
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
await _repositoryManager.RollbackAsync();
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task UpdateVariableTableAsync(VariableTableDto variableTableDto)
|
||||||
|
{
|
||||||
|
var variableTable = _mapper.Map<VariableTable>(variableTableDto);
|
||||||
|
await _repositoryManager.VariableTables.UpdateAsync(variableTable);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task DeleteVariableTableAsync(int id)
|
||||||
|
{
|
||||||
|
await _repositoryManager.VariableTables.DeleteByIdAsync(id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,5 +8,6 @@ namespace DMS.Core.Interfaces.Repositories
|
|||||||
{
|
{
|
||||||
Task<int> DeleteMenuTreeByIdAsync(int id);
|
Task<int> DeleteMenuTreeByIdAsync(int id);
|
||||||
Task<int> DeleteMenuTreeByTargetIdAsync(MenuType menuType, int targetId);
|
Task<int> DeleteMenuTreeByTargetIdAsync(MenuType menuType, int targetId);
|
||||||
|
Task<MenuBean> GetMenuByTargetIdAsync(MenuType menuType, int targetId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -49,7 +49,8 @@ public class BaseServiceTest
|
|||||||
services.AddTransient<IInitializeRepository, InitializeRepository>();
|
services.AddTransient<IInitializeRepository, InitializeRepository>();
|
||||||
|
|
||||||
// 注册应用服务
|
// 注册应用服务
|
||||||
services.AddTransient<IDeviceAppService, DeviceService>();
|
services.AddTransient<IDeviceAppService, DeviceAppService>();
|
||||||
|
services.AddTransient<IVariableTableAppService, VariableTableAppService>();
|
||||||
// services.AddTransient<IVariableAppService, VariableAppService>(); // 如果需要测试 VariableService,取消此行注释
|
// services.AddTransient<IVariableAppService, VariableAppService>(); // 如果需要测试 VariableService,取消此行注释
|
||||||
// ... 在这里注册所有其他的应用服务 ...
|
// ... 在这里注册所有其他的应用服务 ...
|
||||||
|
|
||||||
|
|||||||
@@ -6,12 +6,12 @@ using Microsoft.Extensions.DependencyInjection;
|
|||||||
|
|
||||||
namespace DMS.Infrastructure.UnitTests.Services;
|
namespace DMS.Infrastructure.UnitTests.Services;
|
||||||
|
|
||||||
[TestSubject(typeof(DeviceService))]
|
[TestSubject(typeof(DeviceAppService))]
|
||||||
public class DeviceServiceTest : BaseServiceTest // 继承基类
|
public class DeviceAppServiceTest : BaseServiceTest // 继承基类
|
||||||
{
|
{
|
||||||
private readonly IDeviceAppService _deviceService;
|
private readonly IDeviceAppService _deviceService;
|
||||||
|
|
||||||
public DeviceServiceTest() : base()
|
public DeviceAppServiceTest() : base()
|
||||||
{
|
{
|
||||||
// 从 IoC 容器中解析出需要测试的服务
|
// 从 IoC 容器中解析出需要测试的服务
|
||||||
// 使用 GetRequiredService 可以确保如果服务未注册,测试会立即失败,这通常是我们想要的。
|
// 使用 GetRequiredService 可以确保如果服务未注册,测试会立即失败,这通常是我们想要的。
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
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(VariableTableAppService))]
|
||||||
|
public class VariableTableAppServiceTest : BaseServiceTest
|
||||||
|
{
|
||||||
|
private readonly IVariableTableAppService _variableTableAppService;
|
||||||
|
|
||||||
|
public VariableTableAppServiceTest()
|
||||||
|
{
|
||||||
|
_variableTableAppService = ServiceProvider.GetRequiredService<IVariableTableAppService>();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task CreateVariableTableAsyncTest()
|
||||||
|
{
|
||||||
|
var dto = new CreateVariableTableWithMenuDto()
|
||||||
|
{
|
||||||
|
VariableTable = FakerHelper.FakeVariableTableDto(),
|
||||||
|
Menu = FakerHelper.FakeCreateMenuDto(),
|
||||||
|
DeviceId = 5
|
||||||
|
};
|
||||||
|
var addVarTable= await _variableTableAppService.CreateVariableTableAsync(dto);
|
||||||
|
Assert.NotEqual(addVarTable.Id, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -101,6 +101,12 @@ public class MenuRepository : BaseRepository<DbMenu>, IMenuRepository
|
|||||||
return delConut;
|
return delConut;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<MenuBean> GetMenuByTargetIdAsync(MenuType menuType, int targetId)
|
||||||
|
{
|
||||||
|
var dbMenu = await Db.Queryable<DbMenu>().FirstAsync(m => m.MenuType == menuType && m.TargetId == targetId);
|
||||||
|
return _mapper.Map<MenuBean>(dbMenu);
|
||||||
|
}
|
||||||
|
|
||||||
public new async Task<List<MenuBean>> TakeAsync(int number)
|
public new async Task<List<MenuBean>> TakeAsync(int number)
|
||||||
{
|
{
|
||||||
var dbList = await base.TakeAsync(number);
|
var dbList = await base.TakeAsync(number);
|
||||||
|
|||||||
Reference in New Issue
Block a user