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 _productionService = new Mock(); private readonly Mock _alarmService = new Mock(); private readonly Mock _loggingService = new Mock(); private readonly Mock _configService = new Mock(); 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(); } [Fact] public async Task GetAllProduction_WhenException_Returns500() { _productionService.Setup(s => s.GetProductionSummaryAsync(It.IsAny())).ThrowsAsync(new Exception("t")); var result = await _controller.GetAllProduction(default); result.Should().BeOfType().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(); } [Fact] public async Task GetProductionStatistics_WhenException_Returns500() { _productionService.Setup(s => s.GetProductionStatisticsAsync(It.IsAny())).ThrowsAsync(new Exception("t")); var result = await _controller.GetProductionStatistics(default); result.Should().BeOfType().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(); } [Fact] public async Task GetDeviceProduction_WhenException_Returns500() { _productionService.Setup(s => s.GetTodayProductionAsync(It.IsAny())).ThrowsAsync(new Exception("t")); var result = await _controller.GetDeviceProduction(1, DateTime.Today); result.Should().BeOfType().Which.StatusCode.Should().Be(500); } // 4. GetDeviceProductionStatistics (2 tests) [Fact] public async Task GetDeviceProductionStatistics_WhenSuccess_ReturnsOk() { _productionService.Setup(s => s.GetProductionStatisticsAsync(1, It.IsAny())).ReturnsAsync(new ProductionStatistics()); var result = await _controller.GetDeviceProductionStatistics(1, DateTime.Today); result.Should().BeOfType(); } [Fact] public async Task GetDeviceProductionStatistics_WhenException_Returns500() { _productionService.Setup(s => s.GetProductionStatisticsAsync(It.IsAny(), It.IsAny())).ThrowsAsync(new Exception("t")); var result = await _controller.GetDeviceProductionStatistics(1, DateTime.Today); result.Should().BeOfType().Which.StatusCode.Should().Be(500); } // 5. GetDeviceQualityRate (2 tests) [Fact] public async Task GetDeviceQualityRate_WhenSuccess_ReturnsOk() { _productionService.Setup(s => s.GetQualityRateAsync(1, It.IsAny())).ReturnsAsync(95.5m); var result = await _controller.GetDeviceQualityRate(1, DateTime.Today); result.Should().BeOfType(); } [Fact] public async Task GetDeviceQualityRate_WhenException_Returns500() { _productionService.Setup(s => s.GetQualityRateAsync(It.IsAny(), It.IsAny())).ThrowsAsync(new Exception("t")); var result = await _controller.GetDeviceQualityRate(1, DateTime.Today); result.Should().BeOfType().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(); } [Fact] public async Task CalculateProduction_WhenException_Returns500() { _productionService.Setup(s => s.CalculateAllProductionAsync()).ThrowsAsync(new Exception("t")); var result = await _controller.CalculateProduction(); result.Should().BeOfType().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(); } [Fact] public async Task CalculateDeviceProduction_WhenException_Returns500() { _productionService.Setup(s => s.CalculateProductionAsync(It.IsAny())).ThrowsAsync(new Exception("t")); var result = await _controller.CalculateDeviceProduction(1); result.Should().BeOfType().Which.StatusCode.Should().Be(500); } // 8. GetProductionPrograms (2 tests) [Fact] public async Task GetProductionPrograms_WhenSuccess_ReturnsOk() { _productionService.Setup(s => s.GetProductionProgramsAsync(It.IsAny())).ReturnsAsync(new[] { "PROG1" }); var result = await _controller.GetProductionPrograms(DateTime.Today); result.Should().BeOfType(); } [Fact] public async Task GetProductionPrograms_WhenException_Returns500() { _productionService.Setup(s => s.GetProductionProgramsAsync(It.IsAny())).ThrowsAsync(new Exception("t")); var result = await _controller.GetProductionPrograms(DateTime.Today); result.Should().BeOfType().Which.StatusCode.Should().Be(500); } // 9. GetProgramProduction (2 tests) [Fact] public async Task GetProgramProduction_WhenSuccess_ReturnsOk() { _productionService.Setup(s => s.GetProgramProductionAsync("PROG1", It.IsAny())) .ReturnsAsync(new ProgramProductionSummary()); var result = await _controller.GetProgramProduction("PROG1", DateTime.Today); result.Should().BeOfType(); } [Fact] public async Task GetProgramProduction_WhenException_Returns500() { _productionService.Setup(s => s.GetProgramProductionAsync(It.IsAny(), It.IsAny())) .ThrowsAsync(new Exception("t")); var result = await _controller.GetProgramProduction("PROG1", DateTime.Today); result.Should().BeOfType().Which.StatusCode.Should().Be(500); } // 10. ExportProductionData (2 tests) [Fact] public async Task ExportProductionData_WhenSuccess_ReturnsFile() { _productionService.Setup(s => s.ExportProductionDataAsync(It.IsAny(), It.IsAny())) .ReturnsAsync(new byte[] { 1, 2, 3 }); var result = await _controller.ExportProductionData(DateTime.Today, DateTime.Today); result.Should().BeOfType(); } [Fact] public async Task ExportProductionData_WhenException_Returns500() { _productionService.Setup(s => s.ExportProductionDataAsync(It.IsAny(), It.IsAny())) .ThrowsAsync(new Exception("t")); var result = await _controller.ExportProductionData(default, default); result.Should().BeOfType().Which.StatusCode.Should().Be(500); } // 11. ArchiveProductionData (2 tests) [Fact] public async Task ArchiveProductionData_WhenSuccess_ReturnsOk() { _productionService.Setup(s => s.ArchiveProductionDataAsync(It.IsAny())).Returns(Task.CompletedTask); var result = await _controller.ArchiveProductionData(90); result.Should().BeOfType(); } [Fact] public async Task ArchiveProductionData_WhenException_Returns500() { _productionService.Setup(s => s.ArchiveProductionDataAsync(It.IsAny())).ThrowsAsync(new Exception("t")); var result = await _controller.ArchiveProductionData(); result.Should().BeOfType().Which.StatusCode.Should().Be(500); } } }