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.

268 lines
10 KiB
C#

using System;
using System.Threading.Tasks;
using FluentAssertions;
using Microsoft.AspNetCore.Mvc;
using Moq;
using Xunit;
using Haoliang.Api.Controllers;
using Haoliang.Core.Services;
using Haoliang.Models.Production;
namespace Haoliang.Tests.Controllers
{
public class ProductionControllerTests
{
private readonly Mock<IProductionService> _productionService = new Mock<IProductionService>();
private readonly Mock<IAlarmService> _alarmService = new Mock<IAlarmService>();
private readonly Mock<ILoggingService> _loggingService = new Mock<ILoggingService>();
private readonly Mock<ISystemConfigService> _configService = new Mock<ISystemConfigService>();
private readonly ProductionController _controller;
public ProductionControllerTests()
{
_controller = new ProductionController(
_productionService.Object,
_alarmService.Object,
_loggingService.Object,
_configService.Object);
}
// 1. GetAllProduction (2 tests)
[Fact]
public async Task GetAllProduction_WhenSuccess_ReturnsOk()
{
var date = DateTime.Today;
_productionService.Setup(s => s.GetProductionSummaryAsync(date)).ReturnsAsync(new ProductionSummary());
var result = await _controller.GetAllProduction(date);
result.Should().BeOfType<OkObjectResult>();
}
[Fact]
public async Task GetAllProduction_WhenException_Returns500()
{
_productionService.Setup(s => s.GetProductionSummaryAsync(It.IsAny<DateTime>())).ThrowsAsync(new Exception("t"));
var result = await _controller.GetAllProduction(default);
result.Should().BeOfType<ObjectResult>().Which.StatusCode.Should().Be(500);
}
// 2. GetProductionStatistics (2 tests)
[Fact]
public async Task GetProductionStatistics_WhenSuccess_ReturnsOk()
{
var date = DateTime.Today;
_productionService.Setup(s => s.GetProductionStatisticsAsync(date)).ReturnsAsync(new ProductionStatistics());
var result = await _controller.GetProductionStatistics(date);
result.Should().BeOfType<OkObjectResult>();
}
[Fact]
public async Task GetProductionStatistics_WhenException_Returns500()
{
_productionService.Setup(s => s.GetProductionStatisticsAsync(It.IsAny<DateTime>())).ThrowsAsync(new Exception("t"));
var result = await _controller.GetProductionStatistics(default);
result.Should().BeOfType<ObjectResult>().Which.StatusCode.Should().Be(500);
}
// 3. GetDeviceProduction (2 tests)
[Fact]
public async Task GetDeviceProduction_WhenSuccess_ReturnsOk()
{
_productionService.Setup(s => s.GetTodayProductionAsync(1)).ReturnsAsync(new ProductionRecord());
var result = await _controller.GetDeviceProduction(1, DateTime.Today);
result.Should().BeOfType<OkObjectResult>();
}
[Fact]
public async Task GetDeviceProduction_WhenException_Returns500()
{
_productionService.Setup(s => s.GetTodayProductionAsync(It.IsAny<int>())).ThrowsAsync(new Exception("t"));
var result = await _controller.GetDeviceProduction(1, DateTime.Today);
result.Should().BeOfType<ObjectResult>().Which.StatusCode.Should().Be(500);
}
// 4. GetDeviceProductionStatistics (2 tests)
[Fact]
public async Task GetDeviceProductionStatistics_WhenSuccess_ReturnsOk()
{
_productionService.Setup(s => s.GetProductionStatisticsAsync(1, It.IsAny<DateTime>())).ReturnsAsync(new ProductionStatistics());
var result = await _controller.GetDeviceProductionStatistics(1, DateTime.Today);
result.Should().BeOfType<OkObjectResult>();
}
[Fact]
public async Task GetDeviceProductionStatistics_WhenException_Returns500()
{
_productionService.Setup(s => s.GetProductionStatisticsAsync(It.IsAny<int>(), It.IsAny<DateTime>())).ThrowsAsync(new Exception("t"));
var result = await _controller.GetDeviceProductionStatistics(1, DateTime.Today);
result.Should().BeOfType<ObjectResult>().Which.StatusCode.Should().Be(500);
}
// 5. GetDeviceQualityRate (2 tests)
[Fact]
public async Task GetDeviceQualityRate_WhenSuccess_ReturnsOk()
{
_productionService.Setup(s => s.GetQualityRateAsync(1, It.IsAny<DateTime>())).ReturnsAsync(95.5m);
var result = await _controller.GetDeviceQualityRate(1, DateTime.Today);
result.Should().BeOfType<OkObjectResult>();
}
[Fact]
public async Task GetDeviceQualityRate_WhenException_Returns500()
{
_productionService.Setup(s => s.GetQualityRateAsync(It.IsAny<int>(), It.IsAny<DateTime>())).ThrowsAsync(new Exception("t"));
var result = await _controller.GetDeviceQualityRate(1, DateTime.Today);
result.Should().BeOfType<ObjectResult>().Which.StatusCode.Should().Be(500);
}
// 6. CalculateProduction (2 tests)
[Fact]
public async Task CalculateProduction_WhenSuccess_ReturnsOk()
{
_productionService.Setup(s => s.CalculateAllProductionAsync()).Returns(Task.CompletedTask);
var result = await _controller.CalculateProduction();
result.Should().BeOfType<OkObjectResult>();
}
[Fact]
public async Task CalculateProduction_WhenException_Returns500()
{
_productionService.Setup(s => s.CalculateAllProductionAsync()).ThrowsAsync(new Exception("t"));
var result = await _controller.CalculateProduction();
result.Should().BeOfType<ObjectResult>().Which.StatusCode.Should().Be(500);
}
// 7. CalculateDeviceProduction (2 tests)
[Fact]
public async Task CalculateDeviceProduction_WhenSuccess_ReturnsOk()
{
_productionService.Setup(s => s.CalculateProductionAsync(1)).Returns(Task.CompletedTask);
var result = await _controller.CalculateDeviceProduction(1);
result.Should().BeOfType<OkObjectResult>();
}
[Fact]
public async Task CalculateDeviceProduction_WhenException_Returns500()
{
_productionService.Setup(s => s.CalculateProductionAsync(It.IsAny<int>())).ThrowsAsync(new Exception("t"));
var result = await _controller.CalculateDeviceProduction(1);
result.Should().BeOfType<ObjectResult>().Which.StatusCode.Should().Be(500);
}
// 8. GetProductionPrograms (2 tests)
[Fact]
public async Task GetProductionPrograms_WhenSuccess_ReturnsOk()
{
_productionService.Setup(s => s.GetProductionProgramsAsync(It.IsAny<DateTime>())).ReturnsAsync(new[] { "PROG1" });
var result = await _controller.GetProductionPrograms(DateTime.Today);
result.Should().BeOfType<OkObjectResult>();
}
[Fact]
public async Task GetProductionPrograms_WhenException_Returns500()
{
_productionService.Setup(s => s.GetProductionProgramsAsync(It.IsAny<DateTime>())).ThrowsAsync(new Exception("t"));
var result = await _controller.GetProductionPrograms(DateTime.Today);
result.Should().BeOfType<ObjectResult>().Which.StatusCode.Should().Be(500);
}
// 9. GetProgramProduction (2 tests)
[Fact]
public async Task GetProgramProduction_WhenSuccess_ReturnsOk()
{
_productionService.Setup(s => s.GetProgramProductionAsync("PROG1", It.IsAny<DateTime>()))
.ReturnsAsync(new ProgramProductionSummary());
var result = await _controller.GetProgramProduction("PROG1", DateTime.Today);
result.Should().BeOfType<OkObjectResult>();
}
[Fact]
public async Task GetProgramProduction_WhenException_Returns500()
{
_productionService.Setup(s => s.GetProgramProductionAsync(It.IsAny<string>(), It.IsAny<DateTime>()))
.ThrowsAsync(new Exception("t"));
var result = await _controller.GetProgramProduction("PROG1", DateTime.Today);
result.Should().BeOfType<ObjectResult>().Which.StatusCode.Should().Be(500);
}
// 10. ExportProductionData (2 tests)
[Fact]
public async Task ExportProductionData_WhenSuccess_ReturnsFile()
{
_productionService.Setup(s => s.ExportProductionDataAsync(It.IsAny<DateTime>(), It.IsAny<DateTime>()))
.ReturnsAsync(new byte[] { 1, 2, 3 });
var result = await _controller.ExportProductionData(DateTime.Today, DateTime.Today);
result.Should().BeOfType<FileContentResult>();
}
[Fact]
public async Task ExportProductionData_WhenException_Returns500()
{
_productionService.Setup(s => s.ExportProductionDataAsync(It.IsAny<DateTime>(), It.IsAny<DateTime>()))
.ThrowsAsync(new Exception("t"));
var result = await _controller.ExportProductionData(default, default);
result.Should().BeOfType<ObjectResult>().Which.StatusCode.Should().Be(500);
}
// 11. ArchiveProductionData (2 tests)
[Fact]
public async Task ArchiveProductionData_WhenSuccess_ReturnsOk()
{
_productionService.Setup(s => s.ArchiveProductionDataAsync(It.IsAny<int>())).Returns(Task.CompletedTask);
var result = await _controller.ArchiveProductionData(90);
result.Should().BeOfType<OkObjectResult>();
}
[Fact]
public async Task ArchiveProductionData_WhenException_Returns500()
{
_productionService.Setup(s => s.ArchiveProductionDataAsync(It.IsAny<int>())).ThrowsAsync(new Exception("t"));
var result = await _controller.ArchiveProductionData();
result.Should().BeOfType<ObjectResult>().Which.StatusCode.Should().Be(500);
}
}
}