|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using CncModels.Dto;
|
|
|
using CncModels.Dto.Dashboard;
|
|
|
using CncWebApi.Controllers;
|
|
|
using Xunit;
|
|
|
|
|
|
namespace CncWebApi.Tests
|
|
|
{
|
|
|
/// <summary>
|
|
|
/// DashboardController单元测试
|
|
|
/// 8个仪表盘统计接口
|
|
|
/// </summary>
|
|
|
[Collection("Database")]
|
|
|
public class DashboardControllerTests
|
|
|
{
|
|
|
private readonly DashboardController _controller;
|
|
|
|
|
|
public DashboardControllerTests()
|
|
|
{
|
|
|
TestDb.TruncateAll();
|
|
|
_controller = ControllerFactory.CreateDashboardController();
|
|
|
}
|
|
|
|
|
|
#region GetSummary - 统计卡片
|
|
|
|
|
|
/// <summary>
|
|
|
/// 测试:空数据库也能返回统计(各指标为0)
|
|
|
/// </summary>
|
|
|
[Fact]
|
|
|
public void GetSummary_EmptyDb_ShouldReturnZeros()
|
|
|
{
|
|
|
var result = _controller.GetSummary();
|
|
|
var response = ControllerFactory.Extract<DashboardSummaryResponse>(result);
|
|
|
ControllerFactory.AssertSuccess(response);
|
|
|
Assert.NotNull(response.Data);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 测试:有数据时统计正确
|
|
|
/// </summary>
|
|
|
[Fact]
|
|
|
public void GetSummary_WithData_ShouldReturnStats()
|
|
|
{
|
|
|
// 预置机床+产量数据
|
|
|
PrepareProductionData();
|
|
|
|
|
|
var result = _controller.GetSummary();
|
|
|
var response = ControllerFactory.Extract<DashboardSummaryResponse>(result);
|
|
|
ControllerFactory.AssertSuccess(response);
|
|
|
Assert.NotNull(response.Data);
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
#region GetWorkshopProduction - 车间产量
|
|
|
|
|
|
[Fact]
|
|
|
public void GetWorkshopProduction_ShouldReturnList()
|
|
|
{
|
|
|
var result = _controller.GetWorkshopProduction(null, null);
|
|
|
var response = ControllerFactory.Extract<List<WorkshopProductionResponse>>(result);
|
|
|
ControllerFactory.AssertSuccess(response);
|
|
|
Assert.NotNull(response.Data);
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
#region GetMachineRank - 机床排行
|
|
|
|
|
|
[Fact]
|
|
|
public void GetMachineRank_ShouldReturnList()
|
|
|
{
|
|
|
var result = _controller.GetMachineRank(null, null, 10);
|
|
|
var response = ControllerFactory.Extract<List<MachineRankResponse>>(result);
|
|
|
ControllerFactory.AssertSuccess(response);
|
|
|
Assert.NotNull(response.Data);
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
#region GetWorkerRank - 工人排行
|
|
|
|
|
|
[Fact]
|
|
|
public void GetWorkerRank_ShouldReturnList()
|
|
|
{
|
|
|
var result = _controller.GetWorkerRank(null, null, 10);
|
|
|
var response = ControllerFactory.Extract<List<WorkerRankResponse>>(result);
|
|
|
ControllerFactory.AssertSuccess(response);
|
|
|
Assert.NotNull(response.Data);
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
#region GetProductionTrend - 产量趋势
|
|
|
|
|
|
[Fact]
|
|
|
public void GetProductionTrend_ShouldReturnData()
|
|
|
{
|
|
|
var result = _controller.GetProductionTrend(7);
|
|
|
Assert.NotNull(result);
|
|
|
var content = result.GetType().GetProperty("Content")?.GetValue(result);
|
|
|
Assert.NotNull(content);
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
#region GetMachineStatusDistribution - 机床状态分布
|
|
|
|
|
|
[Fact]
|
|
|
public void GetMachineStatusDistribution_ShouldReturnData()
|
|
|
{
|
|
|
var result = _controller.GetMachineStatusDistribution();
|
|
|
Assert.NotNull(result);
|
|
|
var content = result.GetType().GetProperty("Content")?.GetValue(result);
|
|
|
Assert.NotNull(content);
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
#region GetRecentAlerts - 最新告警
|
|
|
|
|
|
[Fact]
|
|
|
public void GetRecentAlerts_ShouldReturnList()
|
|
|
{
|
|
|
var result = _controller.GetRecentAlerts(5);
|
|
|
var response = ControllerFactory.Extract<List<AlertListItem>>(result);
|
|
|
ControllerFactory.AssertSuccess(response);
|
|
|
Assert.NotNull(response.Data);
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
#region GetCollectorStatus - 采集服务状态
|
|
|
|
|
|
[Fact]
|
|
|
public void GetCollectorStatus_ShouldReturnData()
|
|
|
{
|
|
|
var result = _controller.GetCollectorStatus();
|
|
|
Assert.NotNull(result);
|
|
|
var content = result.GetType().GetProperty("Content")?.GetValue(result);
|
|
|
Assert.NotNull(content);
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
#region 辅助方法
|
|
|
|
|
|
/// <summary>
|
|
|
/// 预置产量数据(机床+日产量)
|
|
|
/// </summary>
|
|
|
private void PrepareProductionData()
|
|
|
{
|
|
|
TestDb.Execute(@"INSERT INTO cnc_collect_address (name, url, brand_id, collect_interval, is_enabled, created_at, updated_at)
|
|
|
VALUES ('测试地址', 'http://192.168.1.1', 1, 5, 1, NOW(), NOW())");
|
|
|
TestDb.Execute(@"INSERT INTO cnc_machine (device_code, name, workshop_id, collect_address_id, ip_address, brand_id, is_enabled, created_at, updated_at)
|
|
|
VALUES ('CNC001', '机床1', 1, 1, '192.168.1.100', 1, 1, NOW(), NOW())");
|
|
|
TestDb.Execute(@"INSERT INTO cnc_daily_production (machine_id, production_date, program_name, total_quantity, segment_count, created_at, updated_at)
|
|
|
VALUES (1, CURDATE(), 'O0001', 100, 1, NOW(), NOW())");
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
}
|
|
|
}
|