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.

98 lines
3.3 KiB
C#

using Xunit;
using Moq;
using FluentAssertions;
using Microsoft.Extensions.Logging;
using Haoliang.Core.Services;
using Haoliang.Models.Device;
using Haoliang.Models.Production;
using Haoliang.Data.Repositories;
namespace Haoliang.Tests;
public class DataStorageServiceTests
{
private readonly Mock<ILogger<DataStorageService>> _mockLogger;
private readonly Mock<IDeviceRepository> _mockDeviceRepository;
private readonly DataStorageService _dataStorageService;
public DataStorageServiceTests()
{
_mockLogger = new Mock<ILogger<DataStorageService>>();
_mockDeviceRepository = new Mock<IDeviceRepository>();
_dataStorageService = new DataStorageService(
_mockLogger.Object,
_mockDeviceRepository.Object);
}
[Fact]
public async Task StoreDeviceDataAsync_CallsUpdateDeviceStatus()
{
var data = new ParsedDeviceData
{
DeviceId = 1,
DeviceName = "Test Device",
Timestamp = DateTime.Now
};
_mockDeviceRepository.Setup(r => r.UpdateDeviceStatusAsync(1, true, true)).Returns(Task.CompletedTask);
await _dataStorageService.StoreDeviceDataAsync(data);
_mockDeviceRepository.Verify(r => r.UpdateDeviceStatusAsync(1, true, true), Times.Once);
}
[Fact]
public async Task StoreDeviceDataAsync_CompletesSuccessfully()
{
var data = new ParsedDeviceData { DeviceId = 1, DeviceName = "Test" };
_mockDeviceRepository.Setup(r => r.UpdateDeviceStatusAsync(It.IsAny<int>(), It.IsAny<bool>(), It.IsAny<bool>())).Returns(Task.CompletedTask);
var action = async () => await _dataStorageService.StoreDeviceDataAsync(data);
await action.Should().NotThrowAsync();
}
[Fact]
public async Task StoreDeviceDataBatchAsync_ProcessesAllDevices()
{
var dataList = new List<ParsedDeviceData>
{
new ParsedDeviceData { DeviceId = 1, DeviceName = "Device 1" },
new ParsedDeviceData { DeviceId = 2, DeviceName = "Device 2" },
new ParsedDeviceData { DeviceId = 3, DeviceName = "Device 3" }
};
_mockDeviceRepository.Setup(r => r.UpdateDeviceStatusAsync(It.IsAny<int>(), It.IsAny<bool>(), It.IsAny<bool>())).Returns(Task.CompletedTask);
await _dataStorageService.StoreDeviceDataBatchAsync(dataList);
_mockDeviceRepository.Verify(r => r.UpdateDeviceStatusAsync(1, true, true), Times.Once);
_mockDeviceRepository.Verify(r => r.UpdateDeviceStatusAsync(2, true, true), Times.Once);
_mockDeviceRepository.Verify(r => r.UpdateDeviceStatusAsync(3, true, true), Times.Once);
}
[Fact]
public async Task StoreDeviceDataBatchAsync_HandlesEmptyList()
{
var dataList = new List<ParsedDeviceData>();
var action = async () => await _dataStorageService.StoreDeviceDataBatchAsync(dataList);
await action.Should().NotThrowAsync();
}
[Fact]
public async Task StoreProductionRecordAsync_CompletesSuccessfully()
{
var record = new ProductionRecord
{
DeviceId = 1,
ProgramName = "PROGRAM001",
Quantity = 100,
ProductionDate = DateTime.Now
};
var action = async () => await _dataStorageService.StoreProductionRecordAsync(record);
await action.Should().NotThrowAsync();
}
}