You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
150 lines
4.6 KiB
C#
150 lines
4.6 KiB
C#
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);
|
|
}
|
|
}
|
|
}
|