using System.Collections.Generic;
using System.Linq;
using CncModels.Dto;
using CncModels.Dto.Brand;
using CncService;
using CncWebApi.Controllers;
using Xunit;
namespace CncWebApi.Tests
{
///
/// BrandController单元测试
/// 品牌CRUD + 复制 + 启停 + 标准字段
///
[Collection("Database")]
public class BrandControllerTests
{
private readonly BrandController _controller;
public BrandControllerTests()
{
TestDb.TruncateAll();
_controller = ControllerFactory.CreateBrandController();
}
#region GetList - 品牌列表
///
/// 测试:获取品牌列表,种子数据包含FANUC
///
[Fact]
public void GetList_ShouldReturnBrandList()
{
// Act
var result = _controller.GetList();
// Assert
var response = ControllerFactory.Extract>(result);
ControllerFactory.AssertSuccess(response);
Assert.NotNull(response.Data);
Assert.Single(response.Data); // 种子数据只有FANUC
Assert.Equal("FANUC", response.Data[0].BrandName);
}
///
/// 测试:新增品牌后列表数量增加
///
[Fact]
public void GetList_AfterCreate_ShouldHaveTwoBrands()
{
// Arrange - 新增一个品牌
_controller.Create(new CreateBrandRequest
{
BrandName = "Siemens",
DeviceField = "device",
TagsPath = "tags"
});
// Act
var result = _controller.GetList();
// Assert
var response = ControllerFactory.Extract>(result);
Assert.Equal(2, response.Data.Count);
}
#endregion
#region GetById - 品牌详情
///
/// 测试:获取FANUC品牌详情
///
[Fact]
public void GetById_ExistingBrand_ShouldReturnDetail()
{
// Act
var result = _controller.GetById(1);
// Assert
var response = ControllerFactory.Extract(result);
ControllerFactory.AssertSuccess(response);
Assert.NotNull(response.Data);
Assert.Equal("FANUC", response.Data.BrandName);
Assert.NotNull(response.Data.Mappings);
}
///
/// 测试:获取不存在的品牌抛出异常
///
[Fact]
public void GetById_NotExisting_ShouldThrowBusinessException()
{
// Act & Assert
var ex = Assert.Throws(() => _controller.GetById(999));
Assert.Equal("品牌不存在", ex.Message);
}
#endregion
#region Create - 新增品牌
///
/// 测试:新增品牌成功
///
[Fact]
public void Create_ValidRequest_ShouldReturnNewId()
{
// Arrange
var request = new CreateBrandRequest
{
BrandName = "Mitsubishi",
DeviceField = "device",
TagsPath = "tags"
};
// Act
var result = _controller.Create(request);
// Assert
var response = ControllerFactory.Extract