修复所有Repository SQL列名为snake_case,新增CncRepository.Tests(37个测试全部通过)
- 修复20个Repository文件中SQL列名从PascalCase改为snake_case匹配数据库 - 新增TestDb.cs测试辅助类(DELETE+重置自增+种子数据) - 新增5个测试类(Alert/Brand/Machine/SysConfig/Workshop)共37个测试 - 添加[Collection(Database)]串行执行避免并行竞争 - 解决TRUNCATE外键约束问题改用DELETE FROMmain
parent
03aaeb11c2
commit
3a1912f4a1
@ -0,0 +1,106 @@
|
|||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using CncModels.Dto.Alert;
|
||||||
|
using CncModels.Entity;
|
||||||
|
using CncRepository.Impl;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace CncRepository.Tests
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 告警仓储测试
|
||||||
|
/// </summary>
|
||||||
|
[Collection("Database")]
|
||||||
|
public class AlertRepositoryTests : IDisposable
|
||||||
|
{
|
||||||
|
private readonly AlertRepository _repo;
|
||||||
|
|
||||||
|
public AlertRepositoryTests()
|
||||||
|
{
|
||||||
|
_repo = new AlertRepository(TestDb.ConnectionString);
|
||||||
|
TestDb.TruncateAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
TestDb.TruncateAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
private Alert CreateTestAlert(string alertType = "collect_fail")
|
||||||
|
{
|
||||||
|
return new Alert
|
||||||
|
{
|
||||||
|
AlertType = alertType,
|
||||||
|
Title = "测试告警",
|
||||||
|
MachineId = null,
|
||||||
|
CollectAddressId = null,
|
||||||
|
Detail = "测试详情",
|
||||||
|
IsResolved = 0,
|
||||||
|
CreatedAt = DateTime.Now
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Create_新增告警_返回自增ID()
|
||||||
|
{
|
||||||
|
var id = _repo.Create(CreateTestAlert());
|
||||||
|
Assert.True(id > 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetById_存在的ID_返回告警()
|
||||||
|
{
|
||||||
|
var id = _repo.Create(CreateTestAlert());
|
||||||
|
var result = _repo.GetById(id);
|
||||||
|
Assert.NotNull(result);
|
||||||
|
Assert.Equal("collect_fail", result.AlertType);
|
||||||
|
Assert.Equal(0, result.IsResolved);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetList_分页查询_返回分页结果()
|
||||||
|
{
|
||||||
|
for (int i = 0; i < 5; i++)
|
||||||
|
_repo.Create(CreateTestAlert());
|
||||||
|
var query = new AlertQuery { Page = 1, PageSize = 3 };
|
||||||
|
var result = _repo.GetList(query);
|
||||||
|
Assert.Equal(5, result.Total);
|
||||||
|
Assert.Equal(3, result.Items.Count);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Resolve_处理告警_返回true()
|
||||||
|
{
|
||||||
|
var id = _repo.Create(CreateTestAlert());
|
||||||
|
var result = _repo.Resolve(id);
|
||||||
|
Assert.True(result);
|
||||||
|
var alert = _repo.GetById(id);
|
||||||
|
Assert.Equal(1, alert.IsResolved);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void BatchResolve_批量处理_返回处理数量()
|
||||||
|
{
|
||||||
|
var id1 = _repo.Create(CreateTestAlert());
|
||||||
|
var id2 = _repo.Create(CreateTestAlert());
|
||||||
|
var id3 = _repo.Create(CreateTestAlert());
|
||||||
|
var count = _repo.BatchResolve(new System.Collections.Generic.List<long> { id1, id2 });
|
||||||
|
Assert.Equal(2, count);
|
||||||
|
Assert.Equal(1, _repo.GetById(id1).IsResolved);
|
||||||
|
Assert.Equal(1, _repo.GetById(id2).IsResolved);
|
||||||
|
Assert.Equal(0, _repo.GetById(id3).IsResolved);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetStatistics_返回未处理统计()
|
||||||
|
{
|
||||||
|
_repo.Create(CreateTestAlert("collect_fail"));
|
||||||
|
_repo.Create(CreateTestAlert("collect_fail"));
|
||||||
|
_repo.Create(CreateTestAlert("device_offline"));
|
||||||
|
var stats = _repo.GetStatistics();
|
||||||
|
Assert.True(stats.UnresolvedCount >= 3);
|
||||||
|
Assert.True(stats.UnresolvedByType.ContainsKey("collect_fail"));
|
||||||
|
Assert.True(stats.UnresolvedByType["collect_fail"] >= 2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,106 @@
|
|||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using CncModels.Entity;
|
||||||
|
using CncRepository.Impl;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace CncRepository.Tests
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 品牌仓储测试
|
||||||
|
/// </summary>
|
||||||
|
[Collection("Database")]
|
||||||
|
public class BrandRepositoryTests : IDisposable
|
||||||
|
{
|
||||||
|
private readonly BrandRepository _repo;
|
||||||
|
|
||||||
|
public BrandRepositoryTests()
|
||||||
|
{
|
||||||
|
_repo = new BrandRepository(TestDb.ConnectionString);
|
||||||
|
TestDb.TruncateAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
TestDb.TruncateAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetAll_返回所有品牌()
|
||||||
|
{
|
||||||
|
var result = _repo.GetAll();
|
||||||
|
Assert.NotNull(result);
|
||||||
|
Assert.True(result.Count >= 1);
|
||||||
|
Assert.Equal("FANUC", result[0].BrandName);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetById_存在的ID_返回品牌()
|
||||||
|
{
|
||||||
|
var result = _repo.GetById(1);
|
||||||
|
Assert.NotNull(result);
|
||||||
|
Assert.Equal("FANUC", result.BrandName);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Create_新增品牌_返回自增ID()
|
||||||
|
{
|
||||||
|
var entity = new Brand
|
||||||
|
{
|
||||||
|
BrandName = "Siemens",
|
||||||
|
DeviceField = "device",
|
||||||
|
TagsPath = "tags",
|
||||||
|
IsEnabled = 1,
|
||||||
|
CreatedAt = DateTime.Now,
|
||||||
|
UpdatedAt = DateTime.Now
|
||||||
|
};
|
||||||
|
var id = _repo.Create(entity);
|
||||||
|
Assert.True(id > 0);
|
||||||
|
Assert.Equal("Siemens", _repo.GetById(id).BrandName);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Update_修改品牌_返回true()
|
||||||
|
{
|
||||||
|
var entity = _repo.GetById(1);
|
||||||
|
entity.BrandName = "FANUC-修改";
|
||||||
|
entity.UpdatedAt = DateTime.Now;
|
||||||
|
var result = _repo.Update(entity);
|
||||||
|
Assert.True(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Delete_存在的ID_返回true()
|
||||||
|
{
|
||||||
|
var id = _repo.Create(new Brand
|
||||||
|
{
|
||||||
|
BrandName = "待删除", DeviceField = "d", TagsPath = "t", IsEnabled = 1,
|
||||||
|
CreatedAt = DateTime.Now, UpdatedAt = DateTime.Now
|
||||||
|
});
|
||||||
|
Assert.True(_repo.Delete(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void ToggleEnabled_切换启用状态()
|
||||||
|
{
|
||||||
|
var before = _repo.GetById(1).IsEnabled;
|
||||||
|
_repo.ToggleEnabled(1);
|
||||||
|
var after = _repo.GetById(1).IsEnabled;
|
||||||
|
Assert.NotEqual(before, after);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetFieldMappingCount_返回字段映射数量()
|
||||||
|
{
|
||||||
|
var count = _repo.GetFieldMappingCount(1);
|
||||||
|
Assert.True(count >= 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetCollectAddressCount_返回关联地址数量()
|
||||||
|
{
|
||||||
|
var count = _repo.GetCollectAddressCount(1);
|
||||||
|
Assert.True(count >= 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,10 @@
|
|||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace CncRepository.Tests
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 数据库测试集合定义 —— 所有测试类共享同一个数据库,必须串行执行
|
||||||
|
/// </summary>
|
||||||
|
[CollectionDefinition("Database", DisableParallelization = true)]
|
||||||
|
public class DatabaseCollection { }
|
||||||
|
}
|
||||||
@ -0,0 +1,138 @@
|
|||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using CncModels.Dto;
|
||||||
|
using CncModels.Dto.Machine;
|
||||||
|
using CncModels.Entity;
|
||||||
|
using CncRepository.Impl;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace CncRepository.Tests
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 机床仓储测试
|
||||||
|
/// </summary>
|
||||||
|
[Collection("Database")]
|
||||||
|
public class MachineRepositoryTests : IDisposable
|
||||||
|
{
|
||||||
|
private readonly MachineRepository _repo;
|
||||||
|
|
||||||
|
public MachineRepositoryTests()
|
||||||
|
{
|
||||||
|
_repo = new MachineRepository(TestDb.ConnectionString);
|
||||||
|
TestDb.TruncateAll();
|
||||||
|
// 插入一个采集地址供机床引用
|
||||||
|
TestDb.Execute(@"INSERT INTO cnc_collect_address (id, name, url, brand_id, collect_interval, is_enabled, created_at, updated_at)
|
||||||
|
VALUES (1, '测试地址', 'http://10.1.1.1/', 1, 30, 1, NOW(), NOW())");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
TestDb.TruncateAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
private Machine CreateTestMachine(string deviceCode = "TEST_001")
|
||||||
|
{
|
||||||
|
return new Machine
|
||||||
|
{
|
||||||
|
DeviceCode = deviceCode,
|
||||||
|
Name = "测试机床",
|
||||||
|
WorkshopId = 1,
|
||||||
|
CollectAddressId = 1,
|
||||||
|
BrandId = 1,
|
||||||
|
IpAddress = "10.1.1.8",
|
||||||
|
IsEnabled = 1,
|
||||||
|
IsOnline = 0,
|
||||||
|
CreatedAt = DateTime.Now,
|
||||||
|
UpdatedAt = DateTime.Now
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Create_新增机床_返回自增ID()
|
||||||
|
{
|
||||||
|
var entity = CreateTestMachine();
|
||||||
|
var id = _repo.Create(entity);
|
||||||
|
Assert.True(id > 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetById_存在的ID_返回机床实体()
|
||||||
|
{
|
||||||
|
var id = _repo.Create(CreateTestMachine());
|
||||||
|
var result = _repo.GetById(id);
|
||||||
|
Assert.NotNull(result);
|
||||||
|
Assert.Equal("TEST_001", result.DeviceCode);
|
||||||
|
Assert.Equal("测试机床", result.Name);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetById_不存在的ID_返回null()
|
||||||
|
{
|
||||||
|
var result = _repo.GetById(99999);
|
||||||
|
Assert.Null(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetByDeviceCode_存在的编码_返回机床()
|
||||||
|
{
|
||||||
|
_repo.Create(CreateTestMachine("UNIQUE_CODE"));
|
||||||
|
var result = _repo.GetByDeviceCode("UNIQUE_CODE");
|
||||||
|
Assert.NotNull(result);
|
||||||
|
Assert.Equal("UNIQUE_CODE", result.DeviceCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetList_分页查询_返回分页结果()
|
||||||
|
{
|
||||||
|
// 插入几条数据
|
||||||
|
for (int i = 1; i <= 3; i++)
|
||||||
|
{
|
||||||
|
_repo.Create(CreateTestMachine($"CODE_{i:D3}"));
|
||||||
|
}
|
||||||
|
var query = new MachineQuery { Page = 1, PageSize = 2 };
|
||||||
|
var result = _repo.GetList(query);
|
||||||
|
Assert.True(result.Total >= 3);
|
||||||
|
Assert.True(result.Items.Count <= 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Update_修改机床_返回true()
|
||||||
|
{
|
||||||
|
var id = _repo.Create(CreateTestMachine());
|
||||||
|
var entity = _repo.GetById(id);
|
||||||
|
entity.Name = "修改后";
|
||||||
|
entity.UpdatedAt = DateTime.Now;
|
||||||
|
var result = _repo.Update(entity);
|
||||||
|
Assert.True(result);
|
||||||
|
Assert.Equal("修改后", _repo.GetById(id).Name);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Delete_存在的ID_返回true()
|
||||||
|
{
|
||||||
|
var id = _repo.Create(CreateTestMachine("DEL_001"));
|
||||||
|
var result = _repo.Delete(id);
|
||||||
|
Assert.True(result);
|
||||||
|
Assert.Null(_repo.GetById(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void ToggleEnabled_切换启用状态()
|
||||||
|
{
|
||||||
|
var id = _repo.Create(CreateTestMachine());
|
||||||
|
var before = _repo.GetById(id).IsEnabled;
|
||||||
|
_repo.ToggleEnabled(id);
|
||||||
|
var after = _repo.GetById(id).IsEnabled;
|
||||||
|
Assert.NotEqual(before, after);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetEnabledByAddressId_返回该地址下启用的机床()
|
||||||
|
{
|
||||||
|
_repo.Create(CreateTestMachine("ADDR_001"));
|
||||||
|
var result = _repo.GetEnabledByAddressId(1);
|
||||||
|
Assert.NotNull(result);
|
||||||
|
Assert.True(result.Count > 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,132 @@
|
|||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using CncModels.Dto;
|
||||||
|
using CncModels.Dto.Settings;
|
||||||
|
using CncModels.Entity;
|
||||||
|
using CncRepository.Impl;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace CncRepository.Tests
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 车间仓储测试
|
||||||
|
/// </summary>
|
||||||
|
[Collection("Database")]
|
||||||
|
public class WorkshopRepositoryTests : IDisposable
|
||||||
|
{
|
||||||
|
private readonly WorkshopRepository _repo;
|
||||||
|
|
||||||
|
public WorkshopRepositoryTests()
|
||||||
|
{
|
||||||
|
_repo = new WorkshopRepository(TestDb.ConnectionString);
|
||||||
|
TestDb.TruncateAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
TestDb.TruncateAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetAll_返回所有车间()
|
||||||
|
{
|
||||||
|
var result = _repo.GetAll();
|
||||||
|
Assert.NotNull(result);
|
||||||
|
Assert.True(result.Count >= 2, "种子数据至少有2个车间");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetById_存在的ID_返回车间实体()
|
||||||
|
{
|
||||||
|
var result = _repo.GetById(1);
|
||||||
|
Assert.NotNull(result);
|
||||||
|
Assert.Equal("A栋", result.Name);
|
||||||
|
Assert.Equal(1, result.SortOrder);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetById_不存在的ID_返回null()
|
||||||
|
{
|
||||||
|
var result = _repo.GetById(99999);
|
||||||
|
Assert.Null(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetList_无关键字_返回分页结果()
|
||||||
|
{
|
||||||
|
var result = _repo.GetList(null);
|
||||||
|
Assert.NotNull(result);
|
||||||
|
Assert.True(result.Total >= 2);
|
||||||
|
Assert.NotNull(result.Items);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetList_关键字搜索_返回匹配结果()
|
||||||
|
{
|
||||||
|
var result = _repo.GetList("A");
|
||||||
|
Assert.True(result.Items.All(w => w.Name.Contains("A")));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Create_新增车间_返回自增ID()
|
||||||
|
{
|
||||||
|
var entity = new Workshop
|
||||||
|
{
|
||||||
|
Name = "C栋",
|
||||||
|
SortOrder = 3,
|
||||||
|
IsEnabled = 1,
|
||||||
|
CreatedAt = DateTime.Now,
|
||||||
|
UpdatedAt = DateTime.Now
|
||||||
|
};
|
||||||
|
var id = _repo.Create(entity);
|
||||||
|
Assert.True(id > 0);
|
||||||
|
|
||||||
|
var created = _repo.GetById(id);
|
||||||
|
Assert.Equal("C栋", created.Name);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Update_修改车间_返回true()
|
||||||
|
{
|
||||||
|
var entity = _repo.GetById(1);
|
||||||
|
entity.Name = "A栋-修改";
|
||||||
|
entity.UpdatedAt = DateTime.Now;
|
||||||
|
var result = _repo.Update(entity);
|
||||||
|
Assert.True(result);
|
||||||
|
|
||||||
|
var updated = _repo.GetById(1);
|
||||||
|
Assert.Equal("A栋-修改", updated.Name);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Delete_存在的ID_返回true()
|
||||||
|
{
|
||||||
|
// 先插入一个独立的车间
|
||||||
|
var id = _repo.Create(new Workshop
|
||||||
|
{
|
||||||
|
Name = "待删除", SortOrder = 99, IsEnabled = 1,
|
||||||
|
CreatedAt = DateTime.Now, UpdatedAt = DateTime.Now
|
||||||
|
});
|
||||||
|
var result = _repo.Delete(id);
|
||||||
|
Assert.True(result);
|
||||||
|
Assert.Null(_repo.GetById(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void ToggleEnabled_切换启用状态()
|
||||||
|
{
|
||||||
|
var before = _repo.GetById(1);
|
||||||
|
var beforeState = before.IsEnabled;
|
||||||
|
_repo.ToggleEnabled(1);
|
||||||
|
var after = _repo.GetById(1);
|
||||||
|
Assert.NotEqual(beforeState, after.IsEnabled);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetMachineCount_无机床_返回0()
|
||||||
|
{
|
||||||
|
var count = _repo.GetMachineCount(1);
|
||||||
|
Assert.Equal(0, count);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue