From 16016d0df773a4921f917a7704de127d82dac5c1 Mon Sep 17 00:00:00 2001
From: haoliang <821644@qq.com>
Date: Tue, 28 Apr 2026 22:52:48 +0800
Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9ECncWebApi.Tests=EF=BC=9A14?=
=?UTF-8?q?=E4=B8=AA=E6=8E=A7=E5=88=B6=E5=99=A8127=E4=B8=AA=E6=B5=8B?=
=?UTF-8?q?=E8=AF=95=E5=85=A8=E9=83=A8=E9=80=9A=E8=BF=87?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
- ControllerFactory:封装14个Controller的创建(含完整Repository→Service→Controller依赖链)
- TestDb:测试数据库辅助(TruncateAll+SeedData+SetRealPasswordHash+辅助查询)
- DatabaseCollection:xUnit串行测试集合(共享cnc_test库)
- 14个Controller测试文件覆盖所有API端点:
Health(1) + Auth(6) + Brand(15) + Machine(10) + CollectAddress(10) +
Worker(13) + Dashboard(9) + Settings(15) + Production(6) + Alert(7) +
Log(4) + ScreenConfig(12) + Screen(11) + Option(8) = 127个测试
- 直接实例化Controller调用方法,不经过HTTP管线
- 使用真实数据库(cnc_test库),与Service.Tests共享同一测试库
---
tests/CncWebApi.Tests/AlertControllerTests.cs | 145 +++++++++
tests/CncWebApi.Tests/AuthControllerTests.cs | 151 +++++++++
tests/CncWebApi.Tests/BrandControllerTests.cs | 303 ++++++++++++++++++
.../CollectAddressControllerTests.cs | 194 +++++++++++
tests/CncWebApi.Tests/ControllerFactory.cs | 135 ++++++++
.../DashboardControllerTests.cs | 164 ++++++++++
tests/CncWebApi.Tests/DatabaseCollection.cs | 10 +
.../CncWebApi.Tests/HealthControllerTests.cs | 37 +++
tests/CncWebApi.Tests/LogControllerTests.cs | 85 +++++
.../CncWebApi.Tests/MachineControllerTests.cs | 204 ++++++++++++
.../CncWebApi.Tests/OptionControllerTests.cs | 156 +++++++++
.../ProductionControllerTests.cs | 146 +++++++++
.../ScreenConfigControllerTests.cs | 241 ++++++++++++++
.../CncWebApi.Tests/ScreenControllerTests.cs | 186 +++++++++++
.../SettingsControllerTests.cs | 245 ++++++++++++++
tests/CncWebApi.Tests/TestDb.cs | 131 ++++++++
.../CncWebApi.Tests/WorkerControllerTests.cs | 193 +++++++++++
17 files changed, 2726 insertions(+)
create mode 100644 tests/CncWebApi.Tests/AlertControllerTests.cs
create mode 100644 tests/CncWebApi.Tests/AuthControllerTests.cs
create mode 100644 tests/CncWebApi.Tests/BrandControllerTests.cs
create mode 100644 tests/CncWebApi.Tests/CollectAddressControllerTests.cs
create mode 100644 tests/CncWebApi.Tests/ControllerFactory.cs
create mode 100644 tests/CncWebApi.Tests/DashboardControllerTests.cs
create mode 100644 tests/CncWebApi.Tests/DatabaseCollection.cs
create mode 100644 tests/CncWebApi.Tests/HealthControllerTests.cs
create mode 100644 tests/CncWebApi.Tests/LogControllerTests.cs
create mode 100644 tests/CncWebApi.Tests/MachineControllerTests.cs
create mode 100644 tests/CncWebApi.Tests/OptionControllerTests.cs
create mode 100644 tests/CncWebApi.Tests/ProductionControllerTests.cs
create mode 100644 tests/CncWebApi.Tests/ScreenConfigControllerTests.cs
create mode 100644 tests/CncWebApi.Tests/ScreenControllerTests.cs
create mode 100644 tests/CncWebApi.Tests/SettingsControllerTests.cs
create mode 100644 tests/CncWebApi.Tests/TestDb.cs
create mode 100644 tests/CncWebApi.Tests/WorkerControllerTests.cs
diff --git a/tests/CncWebApi.Tests/AlertControllerTests.cs b/tests/CncWebApi.Tests/AlertControllerTests.cs
new file mode 100644
index 0000000..4b4e111
--- /dev/null
+++ b/tests/CncWebApi.Tests/AlertControllerTests.cs
@@ -0,0 +1,145 @@
+using System;
+using System.Collections.Generic;
+using CncModels.Dto;
+using CncModels.Dto.Alert;
+using CncService;
+using CncWebApi.Controllers;
+using Xunit;
+
+namespace CncWebApi.Tests
+{
+ ///
+ /// AlertController单元测试
+ /// 告警CRUD + 统计 + 批量处理
+ ///
+ [Collection("Database")]
+ public class AlertControllerTests
+ {
+ private readonly AlertController _controller;
+
+ public AlertControllerTests()
+ {
+ TestDb.TruncateAll();
+ // 预置机床(告警外键依赖)
+ 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())");
+ _controller = ControllerFactory.CreateAlertController();
+ }
+
+ #region 辅助方法
+
+ ///
+ /// 插入一条告警
+ ///
+ private int InsertAlert(string type = "offline", string title = "告警测试")
+ {
+ TestDb.Execute(@"INSERT INTO cnc_alert (machine_id, alert_type, title, is_resolved, created_at)
+ VALUES (1, @type, @title, 0, NOW())", new { type, title });
+ return TestDb.QuerySingle("SELECT MAX(id) FROM cnc_alert");
+ }
+
+ #endregion
+
+ #region GetList - 告警列表
+
+ ///
+ /// 测试:空数据库返回空列表
+ ///
+ [Fact]
+ public void GetList_EmptyDb_ShouldReturnEmpty()
+ {
+ var result = _controller.GetList(new AlertQuery());
+ var response = ControllerFactory.Extract>(result);
+ ControllerFactory.AssertSuccess(response);
+ Assert.Empty(response.Data.Items);
+ }
+
+ ///
+ /// 测试:有告警数据时返回列表
+ ///
+ [Fact]
+ public void GetList_WithData_ShouldReturnItems()
+ {
+ InsertAlert();
+ var result = _controller.GetList(new AlertQuery());
+ var response = ControllerFactory.Extract>(result);
+ Assert.NotEmpty(response.Data.Items);
+ }
+
+ #endregion
+
+ #region GetStatistics - 告警统计
+
+ ///
+ /// 测试:告警统计数据
+ ///
+ [Fact]
+ public void GetStatistics_ShouldReturnStats()
+ {
+ InsertAlert();
+ var result = _controller.GetStatistics();
+ var response = ControllerFactory.Extract(result);
+ ControllerFactory.AssertSuccess(response);
+ Assert.NotNull(response.Data);
+ }
+
+ #endregion
+
+ #region Resolve - 处理告警
+
+ ///
+ /// 测试:处理单条告警成功
+ ///
+ [Fact]
+ public void Resolve_Existing_ShouldSuccess()
+ {
+ int alertId = InsertAlert();
+ var result = _controller.Resolve(alertId);
+ ControllerFactory.AssertSuccess(ControllerFactory.Extract