新增CncService.Tests(180个测试全部通过)+ 修复Repository层SQL字段名bug
- 新增12个Service测试文件(180个测试用例,覆盖全部Service方法) - 修复BrandFieldMappingRepository.BatchCreate连接未打开的bug - 修复DailyProductionRepository中引用不存在的worker_id列的bug - 修复DashboardRepository.GetWorkerRank中引用不存在的worker_id列的bug - 修复SystemLogRepository.GetList参数未添加Limit/Offset的bugmain
parent
3a1912f4a1
commit
8845ffb3f6
@ -0,0 +1,149 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using CncModels.Constants;
|
||||
using CncModels.Dto;
|
||||
using CncModels.Dto.Alert;
|
||||
using CncService;
|
||||
using CncService.Impl;
|
||||
using Xunit;
|
||||
|
||||
namespace CncService.Tests
|
||||
{
|
||||
/// <summary>
|
||||
/// AlertService 告警管理测试
|
||||
/// 测试场景:查询、解决告警、批量解决、统计、参数校验
|
||||
/// </summary>
|
||||
[Collection("Database")]
|
||||
public class AlertServiceTests : IDisposable
|
||||
{
|
||||
private readonly AlertService _service;
|
||||
|
||||
public AlertServiceTests()
|
||||
{
|
||||
TestDb.TruncateAll();
|
||||
_service = ServiceFactory.CreateAlertService();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
TestDb.TruncateAll();
|
||||
}
|
||||
|
||||
/// <summary>辅助方法:插入测试告警(每次用唯一编码避免重复)</summary>
|
||||
private long InsertTestAlert(string alertType = "offline", int isResolved = 0)
|
||||
{
|
||||
// 只插入一次机床
|
||||
var machineCount = TestDb.QuerySingle<int>("SELECT COUNT(*) FROM cnc_machine");
|
||||
if (machineCount == 0)
|
||||
{
|
||||
TestDb.Execute(@"INSERT INTO cnc_collect_address (name, url, brand_id, collect_interval, is_enabled, created_at, updated_at)
|
||||
VALUES ('测试地址', 'http://test', 1, 30, 1, NOW(), NOW())");
|
||||
TestDb.Execute(@"INSERT INTO cnc_machine (device_code, name, workshop_id, collect_address_id, ip_address, brand_id, is_enabled, is_online, created_at, updated_at)
|
||||
VALUES ('M001', '机床1', 1, 1, '0.0.0.0', 1, 1, 0, NOW(), NOW())");
|
||||
}
|
||||
TestDb.Execute(@"INSERT INTO cnc_alert (alert_type, machine_id, title, is_resolved, created_at)
|
||||
VALUES (@alertType, 1, '测试告警', @isResolved, NOW())",
|
||||
new { alertType, isResolved });
|
||||
return TestDb.QuerySingle<long>("SELECT MAX(id) FROM cnc_alert");
|
||||
}
|
||||
|
||||
// ======== GetList ========
|
||||
|
||||
[Fact]
|
||||
public void GetList_无数据_返回空列表()
|
||||
{
|
||||
var result = _service.GetList(new AlertQuery { Page = 1, PageSize = 20 });
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(0, result.Total);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetList_查询参数为null_抛出BadRequest异常()
|
||||
{
|
||||
var ex = Assert.Throws<BusinessException>(() => _service.GetList(null));
|
||||
Assert.Equal(ErrorCode.BadRequest, ex.Code);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetList_有告警数据_返回分页结果()
|
||||
{
|
||||
InsertTestAlert();
|
||||
InsertTestAlert("overload");
|
||||
|
||||
var result = _service.GetList(new AlertQuery { Page = 1, PageSize = 20 });
|
||||
Assert.Equal(2, result.Total);
|
||||
}
|
||||
|
||||
// ======== Resolve ========
|
||||
|
||||
[Fact]
|
||||
public void Resolve_存在的ID_返回true()
|
||||
{
|
||||
var id = InsertTestAlert();
|
||||
var result = _service.Resolve(id);
|
||||
Assert.True(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Resolve_无效ID_抛出BadRequest异常()
|
||||
{
|
||||
var ex = Assert.Throws<BusinessException>(() => _service.Resolve(0));
|
||||
Assert.Equal(ErrorCode.BadRequest, ex.Code);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Resolve_负数ID_抛出BadRequest异常()
|
||||
{
|
||||
var ex = Assert.Throws<BusinessException>(() => _service.Resolve(-1));
|
||||
Assert.Equal(ErrorCode.BadRequest, ex.Code);
|
||||
}
|
||||
|
||||
// ======== BatchResolve ========
|
||||
|
||||
[Fact]
|
||||
public void BatchResolve_正常批量解决_返回成功数量()
|
||||
{
|
||||
var id1 = InsertTestAlert();
|
||||
var id2 = InsertTestAlert("overload");
|
||||
|
||||
var count = _service.BatchResolve(new List<long> { id1, id2 });
|
||||
Assert.Equal(2, count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BatchResolve_列表为null_抛出BadRequest异常()
|
||||
{
|
||||
var ex = Assert.Throws<BusinessException>(() => _service.BatchResolve(null));
|
||||
Assert.Equal(ErrorCode.BadRequest, ex.Code);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BatchResolve_空列表_抛出BadRequest异常()
|
||||
{
|
||||
var ex = Assert.Throws<BusinessException>(() => _service.BatchResolve(new List<long>()));
|
||||
Assert.Equal(ErrorCode.BadRequest, ex.Code);
|
||||
}
|
||||
|
||||
// ======== GetStatistics ========
|
||||
|
||||
[Fact]
|
||||
public void GetStatistics_无数据_返回全0()
|
||||
{
|
||||
var stats = _service.GetStatistics();
|
||||
Assert.NotNull(stats);
|
||||
Assert.Equal(0, stats.UnresolvedCount);
|
||||
Assert.NotNull(stats.UnresolvedByType);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetStatistics_有数据_返回正确统计()
|
||||
{
|
||||
InsertTestAlert("offline", 0);
|
||||
InsertTestAlert("overload", 0);
|
||||
InsertTestAlert("offline", 1); // 已解决
|
||||
|
||||
var stats = _service.GetStatistics();
|
||||
Assert.True(stats.UnresolvedCount >= 2);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,101 @@
|
||||
using System;
|
||||
using CncModels.Constants;
|
||||
using CncService;
|
||||
using CncService.Impl;
|
||||
using Xunit;
|
||||
|
||||
namespace CncService.Tests
|
||||
{
|
||||
/// <summary>
|
||||
/// CollectDataService 采集数据查询测试
|
||||
/// 测试场景:分页查询原始记录、获取最新记录、参数校验
|
||||
/// </summary>
|
||||
[Collection("Database")]
|
||||
public class CollectDataServiceTests : IDisposable
|
||||
{
|
||||
private readonly CollectDataService _service;
|
||||
|
||||
public CollectDataServiceTests()
|
||||
{
|
||||
TestDb.TruncateAll();
|
||||
_service = ServiceFactory.CreateCollectDataService();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
TestDb.TruncateAll();
|
||||
}
|
||||
|
||||
// ======== GetRawByAddress ========
|
||||
|
||||
[Fact]
|
||||
public void GetRawByAddress_无效地址ID_抛出BadRequest异常()
|
||||
{
|
||||
var ex = Assert.Throws<BusinessException>(() => _service.GetRawByAddress(0, 1, 20));
|
||||
Assert.Equal(ErrorCode.BadRequest, ex.Code);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetRawByAddress_负数地址ID_抛出BadRequest异常()
|
||||
{
|
||||
var ex = Assert.Throws<BusinessException>(() => _service.GetRawByAddress(-1, 1, 20));
|
||||
Assert.Equal(ErrorCode.BadRequest, ex.Code);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetRawByAddress_无效页码_抛出BadRequest异常()
|
||||
{
|
||||
var ex = Assert.Throws<BusinessException>(() => _service.GetRawByAddress(1, 0, 20));
|
||||
Assert.Equal(ErrorCode.BadRequest, ex.Code);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetRawByAddress_无效页大小_抛出BadRequest异常()
|
||||
{
|
||||
var ex = Assert.Throws<BusinessException>(() => _service.GetRawByAddress(1, 1, 0));
|
||||
Assert.Equal(ErrorCode.BadRequest, ex.Code);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetRawByAddress_无数据_返回空分页()
|
||||
{
|
||||
// 先插入一个采集地址
|
||||
TestDb.Execute(@"INSERT INTO cnc_collect_address (name, url, brand_id, collect_interval, is_enabled, created_at, updated_at)
|
||||
VALUES ('测试地址', 'http://test', 1, 30, 1, NOW(), NOW())");
|
||||
|
||||
var result = _service.GetRawByAddress(1, 1, 20);
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(0, result.Total);
|
||||
}
|
||||
|
||||
// ======== GetLatestRaw ========
|
||||
|
||||
[Fact]
|
||||
public void GetLatestRaw_无效地址ID_抛出BadRequest异常()
|
||||
{
|
||||
var ex = Assert.Throws<BusinessException>(() => _service.GetLatestRaw(0));
|
||||
Assert.Equal(ErrorCode.BadRequest, ex.Code);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetLatestRaw_无数据_返回null()
|
||||
{
|
||||
TestDb.Execute(@"INSERT INTO cnc_collect_address (name, url, brand_id, collect_interval, is_enabled, created_at, updated_at)
|
||||
VALUES ('测试地址', 'http://test', 1, 30, 1, NOW(), NOW())");
|
||||
|
||||
var result = _service.GetLatestRaw(1);
|
||||
Assert.Null(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetLatestRaw_有数据_返回最新记录()
|
||||
{
|
||||
TestDb.Execute(@"INSERT INTO cnc_collect_address (name, url, brand_id, collect_interval, is_enabled, created_at, updated_at)
|
||||
VALUES ('测试地址', 'http://test', 1, 30, 1, NOW(), NOW())");
|
||||
TestDb.Execute("INSERT INTO log_collect_raw (collect_address_id, request_time, response_time, is_success, raw_json, created_at) VALUES (1, NOW(), NOW(), 1, '{\"\"test\"\":1}', NOW())");
|
||||
|
||||
var result = _service.GetLatestRaw(1);
|
||||
Assert.NotNull(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,149 @@
|
||||
using System;
|
||||
using CncModels.Dto.Dashboard;
|
||||
using CncService;
|
||||
using CncService.Impl;
|
||||
using Xunit;
|
||||
|
||||
namespace CncService.Tests
|
||||
{
|
||||
/// <summary>
|
||||
/// DashboardService 仪表盘测试
|
||||
/// 测试场景:汇总查询、车间产量、机床排名、工人排名、趋势、状态分布、采集器状态
|
||||
/// </summary>
|
||||
[Collection("Database")]
|
||||
public class DashboardServiceTests : IDisposable
|
||||
{
|
||||
private readonly DashboardService _service;
|
||||
|
||||
public DashboardServiceTests()
|
||||
{
|
||||
TestDb.TruncateAll();
|
||||
_service = ServiceFactory.CreateDashboardService();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
TestDb.TruncateAll();
|
||||
}
|
||||
|
||||
// ======== GetSummary ========
|
||||
|
||||
[Fact]
|
||||
public void GetSummary_无数据_返回默认汇总()
|
||||
{
|
||||
var summary = _service.GetSummary();
|
||||
Assert.NotNull(summary);
|
||||
}
|
||||
|
||||
// ======== GetWorkshopProduction ========
|
||||
|
||||
[Fact]
|
||||
public void GetWorkshopProduction_无数据_返回空列表()
|
||||
{
|
||||
var result = _service.GetWorkshopProduction(null, null);
|
||||
Assert.NotNull(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetWorkshopProduction_指定日期范围()
|
||||
{
|
||||
var start = new DateTime(2026, 1, 1);
|
||||
var end = new DateTime(2026, 12, 31);
|
||||
var result = _service.GetWorkshopProduction(start, end);
|
||||
Assert.NotNull(result);
|
||||
}
|
||||
|
||||
// ======== GetMachineRank ========
|
||||
|
||||
[Fact]
|
||||
public void GetMachineRank_无数据_返回空列表()
|
||||
{
|
||||
var result = _service.GetMachineRank(null, null, 10);
|
||||
Assert.NotNull(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetMachineRank_指定Top数量()
|
||||
{
|
||||
var result = _service.GetMachineRank(null, null, 5);
|
||||
Assert.NotNull(result);
|
||||
}
|
||||
|
||||
// ======== GetWorkerRank ========
|
||||
|
||||
[Fact]
|
||||
public void GetWorkerRank_无数据_返回空列表()
|
||||
{
|
||||
var result = _service.GetWorkerRank(null, null, 10);
|
||||
Assert.NotNull(result);
|
||||
}
|
||||
|
||||
// ======== GetProductionTrend ========
|
||||
|
||||
[Fact]
|
||||
public void GetProductionTrend_默认7天()
|
||||
{
|
||||
var result = _service.GetProductionTrend();
|
||||
Assert.NotNull(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetProductionTrend_指定天数()
|
||||
{
|
||||
var result = _service.GetProductionTrend(30);
|
||||
Assert.NotNull(result);
|
||||
}
|
||||
|
||||
// ======== GetMachineStatusDistribution ========
|
||||
|
||||
[Fact]
|
||||
public void GetMachineStatusDistribution_无数据_返回结果()
|
||||
{
|
||||
var result = _service.GetMachineStatusDistribution();
|
||||
Assert.NotNull(result);
|
||||
}
|
||||
|
||||
// ======== GetRecentAlerts ========
|
||||
|
||||
[Fact]
|
||||
public void GetRecentAlerts_无数据_返回空列表()
|
||||
{
|
||||
var result = _service.GetRecentAlerts(5);
|
||||
Assert.NotNull(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetRecentAlerts_有告警数据()
|
||||
{
|
||||
TestDb.Execute(@"INSERT INTO cnc_collect_address (name, url, brand_id, collect_interval, is_enabled, created_at, updated_at)
|
||||
VALUES ('测试地址', 'http://test', 1, 30, 1, NOW(), NOW())");
|
||||
TestDb.Execute(@"INSERT INTO cnc_machine (device_code, name, workshop_id, collect_address_id, ip_address, brand_id, is_enabled, is_online, created_at, updated_at)
|
||||
VALUES ('M001', '机床1', 1, 1, '0.0.0.0', 1, 1, 0, NOW(), NOW())");
|
||||
TestDb.Execute(@"INSERT INTO cnc_alert (alert_type, machine_id, title, is_resolved, created_at)
|
||||
VALUES ('offline', 1, '告警1', 0, NOW()),
|
||||
('offline', 1, '告警2', 0, NOW())");
|
||||
|
||||
var result = _service.GetRecentAlerts(5);
|
||||
Assert.True(result.Count >= 2);
|
||||
}
|
||||
|
||||
// ======== GetCollectorStatus ========
|
||||
|
||||
[Fact]
|
||||
public void GetCollectorStatus_无心跳_返回未运行()
|
||||
{
|
||||
var result = _service.GetCollectorStatus();
|
||||
Assert.NotNull(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetCollectorStatus_有最近心跳_返回运行中()
|
||||
{
|
||||
TestDb.Execute(@"INSERT INTO log_collector_heartbeat (service_id, status, last_collect_time, success_count, fail_count, created_at)
|
||||
VALUES ('collector-service', 'running', NOW(), 1, 0, NOW())");
|
||||
|
||||
var result = _service.GetCollectorStatus();
|
||||
Assert.NotNull(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,138 @@
|
||||
using System;
|
||||
using CncModels.Constants;
|
||||
using CncModels.Dto;
|
||||
using CncModels.Dto.Production;
|
||||
using CncService;
|
||||
using CncService.Impl;
|
||||
using Xunit;
|
||||
|
||||
namespace CncService.Tests
|
||||
{
|
||||
/// <summary>
|
||||
/// ProductionService 产量管理测试
|
||||
/// 测试场景:查询、日汇总、日期范围总产量、产量修正、参数校验
|
||||
/// </summary>
|
||||
[Collection("Database")]
|
||||
public class ProductionServiceTests : IDisposable
|
||||
{
|
||||
private readonly ProductionService _service;
|
||||
|
||||
public ProductionServiceTests()
|
||||
{
|
||||
TestDb.TruncateAll();
|
||||
_service = ServiceFactory.CreateProductionService();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
TestDb.TruncateAll();
|
||||
}
|
||||
|
||||
// ======== GetList ========
|
||||
|
||||
[Fact]
|
||||
public void GetList_无数据_返回空列表()
|
||||
{
|
||||
var result = _service.GetList(new ProductionQuery { Page = 1, PageSize = 20 });
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(0, result.Total);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetList_查询参数为null_抛出BadRequest异常()
|
||||
{
|
||||
var ex = Assert.Throws<BusinessException>(() => _service.GetList(null));
|
||||
Assert.Equal(ErrorCode.BadRequest, ex.Code);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetList_有产量数据_返回分页结果()
|
||||
{
|
||||
// 插入机床+日产量数据
|
||||
TestDb.Execute(@"INSERT INTO cnc_collect_address (name, url, brand_id, collect_interval, is_enabled, created_at, updated_at)
|
||||
VALUES ('测试地址', 'http://test', 1, 30, 1, NOW(), NOW())");
|
||||
TestDb.Execute(@"INSERT INTO cnc_machine (device_code, name, workshop_id, collect_address_id, ip_address, brand_id, is_enabled, is_online, created_at, updated_at)
|
||||
VALUES ('M001', '机床1', 1, 1, '0.0.0.0', 1, 1, 0, NOW(), NOW())");
|
||||
TestDb.Execute(@"INSERT INTO cnc_daily_production (machine_id, production_date, program_name, total_quantity, created_at, updated_at)
|
||||
VALUES (1, CURDATE(), 'O0001', 100, NOW(), NOW())");
|
||||
|
||||
var result = _service.GetList(new ProductionQuery { Page = 1, PageSize = 20 });
|
||||
Assert.Equal(1, result.Total);
|
||||
}
|
||||
|
||||
// ======== GetSummary ========
|
||||
|
||||
[Fact]
|
||||
public void GetSummary_今日无产量_返回0()
|
||||
{
|
||||
var summary = _service.GetSummary(null, null);
|
||||
Assert.NotNull(summary);
|
||||
Assert.Equal(0, summary.TotalQuantity);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetSummary_指定日期有产量_返回正确数量()
|
||||
{
|
||||
TestDb.Execute(@"INSERT INTO cnc_collect_address (name, url, brand_id, collect_interval, is_enabled, created_at, updated_at)
|
||||
VALUES ('测试地址', 'http://test', 1, 30, 1, NOW(), NOW())");
|
||||
TestDb.Execute(@"INSERT INTO cnc_machine (device_code, name, workshop_id, collect_address_id, ip_address, brand_id, is_enabled, is_online, created_at, updated_at)
|
||||
VALUES ('M001', '机床1', 1, 1, '0.0.0.0', 1, 1, 0, NOW(), NOW())");
|
||||
TestDb.Execute(@"INSERT INTO cnc_daily_production (machine_id, production_date, program_name, total_quantity, created_at, updated_at)
|
||||
VALUES (1, CURDATE(), 'O0001', 150, NOW(), NOW())");
|
||||
|
||||
var summary = _service.GetSummary(DateTime.Today, null);
|
||||
Assert.Equal(150, summary.TotalQuantity);
|
||||
}
|
||||
|
||||
// ======== GetTotalByDateRange ========
|
||||
|
||||
[Fact]
|
||||
public void GetTotalByDateRange_范围内无数据_返回0()
|
||||
{
|
||||
var total = _service.GetTotalByDateRange(
|
||||
new DateTime(2020, 1, 1),
|
||||
new DateTime(2020, 1, 31),
|
||||
null);
|
||||
Assert.Equal(0m, total);
|
||||
}
|
||||
|
||||
// ======== Adjust ========
|
||||
|
||||
[Fact]
|
||||
public void Adjust_正常修正_返回true()
|
||||
{
|
||||
var result = _service.Adjust(new ProductionAdjustRequest
|
||||
{
|
||||
TargetTable = "cnc_daily_production",
|
||||
TargetId = 1,
|
||||
FieldName = "total_quantity",
|
||||
NewValue = "200",
|
||||
Reason = "数据修正测试"
|
||||
});
|
||||
Assert.True(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Adjust_请求为null_抛出BadRequest异常()
|
||||
{
|
||||
var ex = Assert.Throws<BusinessException>(() => _service.Adjust(null));
|
||||
Assert.Equal(ErrorCode.BadRequest, ex.Code);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Adjust_修正记录已插入数据库()
|
||||
{
|
||||
_service.Adjust(new ProductionAdjustRequest
|
||||
{
|
||||
TargetTable = "cnc_daily_production",
|
||||
TargetId = 1,
|
||||
FieldName = "total_quantity",
|
||||
NewValue = "200",
|
||||
Reason = "测试原因"
|
||||
});
|
||||
|
||||
var count = TestDb.QuerySingle<int>("SELECT COUNT(*) FROM cnc_production_adjustment");
|
||||
Assert.Equal(1, count);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,160 @@
|
||||
using System;
|
||||
using CncModels.Constants;
|
||||
using CncModels.Entity;
|
||||
using CncService;
|
||||
using CncService.Impl;
|
||||
using Xunit;
|
||||
|
||||
namespace CncService.Tests
|
||||
{
|
||||
/// <summary>
|
||||
/// ScreenService 大屏配置测试
|
||||
/// 测试场景:获取/更新配置、筛选CRUD、汇总数据、参数校验
|
||||
/// </summary>
|
||||
[Collection("Database")]
|
||||
public class ScreenServiceTests : IDisposable
|
||||
{
|
||||
private readonly ScreenService _service;
|
||||
|
||||
public ScreenServiceTests()
|
||||
{
|
||||
TestDb.TruncateAll();
|
||||
_service = ServiceFactory.CreateScreenService();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
TestDb.TruncateAll();
|
||||
}
|
||||
|
||||
// ======== GetSummary ========
|
||||
|
||||
[Fact]
|
||||
public void GetSummary_返回默认汇总()
|
||||
{
|
||||
var summary = _service.GetSummary();
|
||||
Assert.NotNull(summary);
|
||||
Assert.Equal(0, summary.MachineCount);
|
||||
Assert.Equal(0, summary.ProductionToday);
|
||||
Assert.Equal(0, summary.AlertCount);
|
||||
Assert.Equal(0, summary.OnlineCount);
|
||||
}
|
||||
|
||||
// ======== GetConfigs ========
|
||||
|
||||
[Fact]
|
||||
public void GetConfigs_无数据_返回空列表()
|
||||
{
|
||||
var configs = _service.GetConfigs();
|
||||
Assert.NotNull(configs);
|
||||
}
|
||||
|
||||
// ======== UpdateConfig ========
|
||||
|
||||
[Fact]
|
||||
public void UpdateConfig_请求为null_抛出BadRequest异常()
|
||||
{
|
||||
var ex = Assert.Throws<BusinessException>(() => _service.UpdateConfig(null));
|
||||
Assert.Equal(ErrorCode.BadRequest, ex.Code);
|
||||
}
|
||||
|
||||
// ======== GetFilters ========
|
||||
|
||||
[Fact]
|
||||
public void GetFilters_screenKey为空_抛出BadRequest异常()
|
||||
{
|
||||
var ex = Assert.Throws<BusinessException>(() => _service.GetFilters(""));
|
||||
Assert.Equal(ErrorCode.BadRequest, ex.Code);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetFilters_screenKey为null_抛出BadRequest异常()
|
||||
{
|
||||
var ex = Assert.Throws<BusinessException>(() => _service.GetFilters(null));
|
||||
Assert.Equal(ErrorCode.BadRequest, ex.Code);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetFilters_screenKey为空格_抛出BadRequest异常()
|
||||
{
|
||||
var ex = Assert.Throws<BusinessException>(() => _service.GetFilters(" "));
|
||||
Assert.Equal(ErrorCode.BadRequest, ex.Code);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetFilters_存在的screenKey_返回列表()
|
||||
{
|
||||
// 先插入筛选数据
|
||||
TestDb.Execute(@"INSERT INTO cnc_screen_filter (screen_key, filter_type, filter_value, is_default, sort_order)
|
||||
VALUES ('dashboard', 'workshop', '1', 1, 1)");
|
||||
|
||||
var filters = _service.GetFilters("dashboard");
|
||||
Assert.NotNull(filters);
|
||||
Assert.True(filters.Count >= 1);
|
||||
}
|
||||
|
||||
// ======== CreateFilter ========
|
||||
|
||||
[Fact]
|
||||
public void CreateFilter_正常创建_返回自增ID()
|
||||
{
|
||||
var id = _service.CreateFilter(new ScreenFilter
|
||||
{
|
||||
ScreenKey = "dashboard",
|
||||
FilterType = "workshop",
|
||||
FilterValue = "1",
|
||||
IsDefault = 1,
|
||||
SortOrder = 1
|
||||
});
|
||||
Assert.True(id > 0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CreateFilter_请求为null_抛出BadRequest异常()
|
||||
{
|
||||
var ex = Assert.Throws<BusinessException>(() => _service.CreateFilter(null));
|
||||
Assert.Equal(ErrorCode.BadRequest, ex.Code);
|
||||
}
|
||||
|
||||
// ======== UpdateFilter ========
|
||||
|
||||
[Fact]
|
||||
public void UpdateFilter_请求为null_抛出BadRequest异常()
|
||||
{
|
||||
var ex = Assert.Throws<BusinessException>(() => _service.UpdateFilter(null));
|
||||
Assert.Equal(ErrorCode.BadRequest, ex.Code);
|
||||
}
|
||||
|
||||
// ======== DeleteFilter ========
|
||||
|
||||
[Fact]
|
||||
public void DeleteFilter_无效ID_抛出BadRequest异常()
|
||||
{
|
||||
var ex = Assert.Throws<BusinessException>(() => _service.DeleteFilter(0));
|
||||
Assert.Equal(ErrorCode.BadRequest, ex.Code);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DeleteFilter_负数ID_抛出BadRequest异常()
|
||||
{
|
||||
var ex = Assert.Throws<BusinessException>(() => _service.DeleteFilter(-1));
|
||||
Assert.Equal(ErrorCode.BadRequest, ex.Code);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DeleteFilter_存在的ID_返回true()
|
||||
{
|
||||
var id = _service.CreateFilter(new ScreenFilter
|
||||
{
|
||||
ScreenKey = "dashboard",
|
||||
FilterType = "workshop",
|
||||
FilterValue = "1",
|
||||
IsDefault = 1,
|
||||
SortOrder = 1
|
||||
});
|
||||
|
||||
var result = _service.DeleteFilter(id);
|
||||
Assert.True(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,56 @@
|
||||
using System;
|
||||
using CncModels.Constants;
|
||||
using CncModels.Dto;
|
||||
using CncModels.Dto.Log;
|
||||
using CncService;
|
||||
using CncService.Impl;
|
||||
using Xunit;
|
||||
|
||||
namespace CncService.Tests
|
||||
{
|
||||
/// <summary>
|
||||
/// SystemLogService 系统日志测试
|
||||
/// 测试场景:分页查询、参数校验
|
||||
/// </summary>
|
||||
[Collection("Database")]
|
||||
public class SystemLogServiceTests : IDisposable
|
||||
{
|
||||
private readonly SystemLogService _service;
|
||||
|
||||
public SystemLogServiceTests()
|
||||
{
|
||||
TestDb.TruncateAll();
|
||||
_service = ServiceFactory.CreateSystemLogService();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
TestDb.TruncateAll();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetList_无数据_返回空列表()
|
||||
{
|
||||
var result = _service.GetList(new SystemLogQuery { Page = 1, PageSize = 20 });
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(0, result.Total);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetList_查询参数为null_抛出BadRequest异常()
|
||||
{
|
||||
var ex = Assert.Throws<BusinessException>(() => _service.GetList(null));
|
||||
Assert.Equal(ErrorCode.BadRequest, ex.Code);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetList_有日志数据_返回分页结果()
|
||||
{
|
||||
TestDb.Execute(@"INSERT INTO log_system (log_level, source, message, stack_trace, extra_data, created_at)
|
||||
VALUES ('INFO', 'AuthService', '登录系统', '', '{}', NOW())");
|
||||
|
||||
var result = _service.GetList(new SystemLogQuery { Page = 1, PageSize = 20 });
|
||||
Assert.Equal(1, result.Total);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue