Files
DMS/DMS.Infrastructure.UnitTests/Repository_Test/DeviceRepositoryTests.cs

92 lines
2.6 KiB
C#
Raw Normal View History

using Xunit;
using Moq;
using AutoMapper;
using DMS.Infrastructure.Data;
using DMS.Infrastructure.Entities;
using DMS.Infrastructure.Repositories;
using SqlSugar;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace DMS.Infrastructure.UnitTests.Repository_Test
{
public class DeviceRepositoryTests:BaseRepositoryTests
{
private readonly DeviceRepository _deviceRepository;
public DeviceRepositoryTests() : base()
{
_deviceRepository = new DeviceRepository(_sqlSugarDbContext);
}
[Fact]
2025-07-19 15:16:28 +08:00
public async Task GetAllAsync_Test()
{
// Act
var result = await _deviceRepository.GetAllAsync();
// Assert
Assert.NotNull(result);
}
2025-07-19 15:16:28 +08:00
[Fact]
public async Task UpdateByIdAsync_Test()
{
var device = await _deviceRepository.GetByIdAsync(33);
device.Name = "<22>ŷ<EFBFBD>";
// Act
var result = await _deviceRepository.UpdateAsync(device);
// Assert
//Assert.NotNull(result);
Assert.Equal(result, 1);
}
[Fact]
public async Task DeleteAsync_Test()
{
var device = await _deviceRepository.GetByIdAsync(33);
// Act
var result = await _deviceRepository.DeleteAsync(device);
// Assert
//Assert.NotNull(result);
Assert.Equal(result, 1);
}
[Fact]
public async Task AddAsync_Test()
{
2025-07-19 15:16:28 +08:00
try
{
2025-07-19 15:16:28 +08:00
//await _sqlSugarDbContext.BeginTranAsync();
for (var i = 0; i < 10; i++)
{
var dbDevice = FakerHelper.FakeDbDevice();
//await _sqlSugarDbContext.GetInstance().Insertable(dbDevice).ExecuteCommandAsync();
2025-07-19 15:16:28 +08:00
// Act
var result = await _deviceRepository.AddAsync(dbDevice);
}
throw new Exception(<><C4A3><EFBFBD><EFBFBD><EFBFBD>󡣡<EFBFBD><F3A1A3A1><EFBFBD>");
//await _sqlSugarDbContext.CommitTranAsync();
}
catch (Exception e)
{
//await _sqlSugarDbContext.RollbackTranAsync();
Console.WriteLine($"<22><><EFBFBD><EFBFBD><EFBFBD>豸ʱ<E8B1B8><CAB1><EFBFBD><EFBFBD><EFBFBD>˴<EFBFBD><CBB4><EFBFBD><EFBFBD><EFBFBD>{e}");
}
2025-07-19 15:16:28 +08:00
// Assert
//Assert.NotNull(result);
//Assert.Contains(result, d => d.Name == testDevice.Name);
// Clean up after the test
//await _sqlSugarDbContext.GetInstance().Deleteable<DbDevice>().ExecuteCommandAsync();
}
}
}