基本修改完毕开始进行单元测试

This commit is contained in:
2025-07-19 14:36:34 +08:00
parent ca67d2e6df
commit aaf8bdf08a
23 changed files with 843 additions and 353 deletions

View File

@@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Bogus" Version="35.6.3" />
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="xunit" Version="2.5.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" />
</ItemGroup>
<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DMS.Infrastructure\DMS.Infrastructure.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,18 @@
using Xunit;
namespace DMS.Infrastructure.UnitTests
{
public class VariableTableRepositoryTests
{
public VariableTableRepositoryTests()
{
}
[Fact]
public void AddAsync()
{
Assert.True(true);
}
}
}

View File

@@ -0,0 +1,59 @@
using Bogus;
using DMS.Infrastructure.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DMS.Infrastructure.UnitTests
{
public static class FakerHelper
{
public static DbDevice FakeDbDevice()
{
var dbDevice = new Faker<DbDevice>()
.RuleFor(d => d.Name, f => f.Commerce.ProductName())
.RuleFor(d => d.Description, f => f.Commerce.ProductDescription())
.RuleFor(d => d.Ip, f => f.Internet.Ip())
.Generate();
dbDevice.Prot = 102;
dbDevice.ProtocolType = Core.Enums.ProtocolType.S7;
dbDevice.Slot = 1;
dbDevice.Rack = 0;
dbDevice.CpuType = S7.Net.CpuType.S71200;
dbDevice.DeviceType = Core.Enums.DeviceType.SiemensPLC;
return dbDevice;
}
public static DbVariableTable FakeDbVariableTable()
{
var dbVarTable = new Faker<DbVariableTable>()
.RuleFor(d => d.Name, f => f.Commerce.ProductName())
.RuleFor(d => d.Description, f => f.Commerce.ProductDescription())
.Generate();
dbVarTable.ProtocolType = Core.Enums.ProtocolType.S7;
dbVarTable.IsActive=true;
return dbVarTable;
}
public static DbVariable FakeDbVariable()
{
var dbVariable = new Faker<DbVariable>()
.RuleFor(d => d.Name, f => f.Commerce.ProductName())
.RuleFor(d => d.Description, f => f.Commerce.ProductDescription())
.RuleFor(d => d.S7Address, f => f.Internet.DomainWord())
.RuleFor(d => d.DataValue, f => f.Commerce.Price())
.Generate();
dbVariable.ProtocolType = Core.Enums.ProtocolType.S7;
dbVariable.IsActive = true;
dbVariable.SignalType=Core.Enums.SignalType.RunSignal;
dbVariable.UpdateTime=DateTime.Now;
dbVariable.DataType = "String";
return dbVariable;
}
}
}

View File

@@ -0,0 +1,28 @@
using DMS.Infrastructure.Data;
using DMS.Infrastructure.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DMS.Infrastructure.UnitTests.Repository_Test
{
public class BaseRepositoryTests
{
protected readonly SqlSugarDbContext _sqlSugarDbContext;
public BaseRepositoryTests()
{
// Load real connection settings
var connectionSettings = new Config.ConnectionSettings()
{
Database = "DMS_test"
};
_sqlSugarDbContext = new SqlSugarDbContext(connectionSettings);
_sqlSugarDbContext.GetInstance().DbMaintenance.CreateDatabase();
_sqlSugarDbContext.GetInstance().CodeFirst.InitTables<DbDevice>();
}
}
}

View File

@@ -0,0 +1,54 @@
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]
public async Task GetAllAsync_ShouldReturnListOfDbDevices()
{
// Act
var result = await _deviceRepository.GetAllAsync();
// Assert
Assert.NotNull(result);
}
[Fact]
public async Task AddAsync_Test()
{
for (var i = 0; i < 10; i++)
{
var dbDevice = FakerHelper.FakeDbDevice();
//await _sqlSugarDbContext.GetInstance().Insertable(dbDevice).ExecuteCommandAsync();
// Act
var result = await _deviceRepository.AddAsync(dbDevice);
}
// Assert
//Assert.NotNull(result);
//Assert.Contains(result, d => d.Name == testDevice.Name);
// Clean up after the test
//await _sqlSugarDbContext.GetInstance().Deleteable<DbDevice>().ExecuteCommandAsync();
}
}
}

View File

@@ -0,0 +1,13 @@
using Xunit;
namespace DMS.Infrastructure.UnitTests.Repository_Test
{
public class VariableTableRepositoryTests
{
[Fact]
public void Test1()
{
Assert.True(true);
}
}
}