| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310 |
- using Admin.NET.Plugin.AiDOP.Controllers.S0.Quality;
- using Admin.NET.Plugin.AiDOP.Dto.S0.Quality;
- using Admin.NET.Plugin.AiDOP.Entity.S0.Quality;
- using Xunit;
- namespace Admin.NET.Plugin.AiDOP.Tests.S0.Quality;
- /// <summary>
- /// 检验规范聚合必填契约(UAT-S0-10 拍板,2026-09-02)。
- /// 新增严格 / 编辑「历史兼容、禁止劣化」是两套规则,分别覆盖。
- /// </summary>
- public class AdoS0QmsSpecAggregateGuardTests
- {
- private static (string, string?)[] RawFields(string? file, string? mat, string? name, string? ver) =>
- [("文件编号", file), ("物料编码", mat), ("原材料名称", name), ("版本", ver)];
- // ==================== 新增 ====================
- [Fact]
- public void Create_WhenAllRequiredPresentAndHasItem_Passes()
- {
- var error = AdoS0QmsSpecAggregateGuard.ValidateCreate(RawFields("F-001", "M-001", "钢板", "V1"), 1);
- Assert.Null(error);
- }
- /// <summary>UAT-S0-10 原始场景:只填文件编号、其余必填全空。</summary>
- [Fact]
- public void Create_WhenOnlyFileNumberFilled_IsRejected()
- {
- var error = AdoS0QmsSpecAggregateGuard.ValidateCreate(RawFields("UATA-BAD-SPEC", null, null, null), 0);
- Assert.Equal("物料编码不能为空", error);
- }
- [Theory]
- [InlineData(null)]
- [InlineData("")]
- [InlineData(" ")]
- public void Create_TreatsWhitespaceAsMissing(string? blank)
- {
- var error = AdoS0QmsSpecAggregateGuard.ValidateCreate(RawFields("F-001", "M-001", blank, "V1"), 1);
- Assert.Equal("原材料名称不能为空", error);
- }
- /// <summary>头字段齐全但 0 条明细,仍必须拒绝——Create / Import 两条入口共用本规则。</summary>
- [Fact]
- public void Create_WhenNoDetail_IsRejected()
- {
- var error = AdoS0QmsSpecAggregateGuard.ValidateCreate(RawFields("F-001", "M-001", "钢板", "V1"), 0);
- Assert.Equal("检验明细至少需要 1 条", error);
- }
- // ==================== 编辑:历史兼容 ====================
- [Fact]
- public void Update_WhenHistoricalFieldWasAlreadyBlank_AllowsStayingBlank()
- {
- // 实测 aidopdev 有 72 条历史记录缺必填字段;它们必须仍可编辑其他字段。
- var error = AdoS0QmsSpecAggregateGuard.ValidateUpdate(
- [("物料编码", null, null), ("版本", "V1", "V2")], existingMeaningfulItemCount: 3, incomingMeaningfulItemCount: 3);
- Assert.Null(error);
- }
- [Fact]
- public void Update_WhenHistoricalRecordHadNoDetail_AllowsStayingZero()
- {
- // 实测 22 条历史记录 0 明细。
- var error = AdoS0QmsSpecAggregateGuard.ValidateUpdate(
- [("物料编码", "M-001", "M-001")], existingMeaningfulItemCount: 0, incomingMeaningfulItemCount: 0);
- Assert.Null(error);
- }
- [Fact]
- public void Update_WhenHistoricalBlankFieldGetsFilled_Passes()
- {
- var error = AdoS0QmsSpecAggregateGuard.ValidateUpdate(
- [("物料编码", "", "M-001")], existingMeaningfulItemCount: 1, incomingMeaningfulItemCount: 1);
- Assert.Null(error);
- }
- // ==================== 编辑:禁止劣化 ====================
- [Fact]
- public void Update_WhenClearingAPopulatedRequiredField_IsRejected()
- {
- var error = AdoS0QmsSpecAggregateGuard.ValidateUpdate(
- [("物料编码", "M-001", " ")], existingMeaningfulItemCount: 1, incomingMeaningfulItemCount: 1);
- Assert.Equal("物料编码原本已有值,不允许清空", error);
- }
- [Fact]
- public void Update_WhenDeletingAllExistingDetails_IsRejected()
- {
- var error = AdoS0QmsSpecAggregateGuard.ValidateUpdate(
- [("物料编码", "M-001", "M-001")], existingMeaningfulItemCount: 5, incomingMeaningfulItemCount: 0);
- Assert.Equal("该检规原有 5 条检验明细,不允许全部删除", error);
- }
- // ==================== 文件编号:必填已从 [Required] 迁入守卫 ====================
- /// <summary>阶段 1.1 · 场景 1:历史 FileNumber 为空的记录,改其他字段必须能存。</summary>
- [Fact]
- public void Update_WhenHistoricalFileNumberIsNull_AllowsEditingOtherFields()
- {
- // 对应实测的 3 行 qms_gcjygf.wjbh IS NULL。
- var error = AdoS0QmsSpecAggregateGuard.ValidateUpdate(
- [
- ("文件编号", null, null),
- ("物料编码", "M-001", "M-002"),
- ("版本", "V1", "V1")
- ],
- existingMeaningfulItemCount: 2, incomingMeaningfulItemCount: 2);
- Assert.Null(error);
- }
- /// <summary>阶段 1.1 · 场景 2:原本有文件编号,Update 清空必须拒绝。</summary>
- [Fact]
- public void Update_WhenClearingAnExistingFileNumber_IsRejected()
- {
- var error = AdoS0QmsSpecAggregateGuard.ValidateUpdate(
- [("文件编号", "F-001", "")], existingMeaningfulItemCount: 1, incomingMeaningfulItemCount: 1);
- Assert.Equal("文件编号原本已有值,不允许清空", error);
- }
- [Fact]
- public void Create_WhenFileNumberMissing_IsRejected()
- {
- var error = AdoS0QmsSpecAggregateGuard.ValidateCreate(RawFields(null, "M-001", "钢板", "V1"), 1);
- Assert.Equal("文件编号不能为空", error);
- }
- // ==================== 明细归一化:全空占位不落库 ====================
- /// <summary>阶段 1.1 · 场景 3:1 实 + 1 空 → 只保留 1 条真实明细。</summary>
- [Fact]
- public void Meaningful_DropsBlankPlaceholderRows_KeepingOnlyRealDetails()
- {
- var incoming = new List<AdoS0QmsRawInspectionSpecEntryDto>
- {
- new() { InspectionItem = "外观", UpperLimit = "10" },
- new() { InspectionItem = "", InspectionStandard = " ", UpperLimit = null }, // 前端 addItem 的空占位
- };
- var kept = AdoS0QmsSpecAggregateGuard.Meaningful(incoming);
- Assert.Single(kept);
- Assert.Equal("外观", kept[0].InspectionItem);
- // 过滤结果同时用于数量校验,保证「校验看到的」与「写进库的」一致
- Assert.Null(AdoS0QmsSpecAggregateGuard.ValidateCreate(RawFields("F-001", "M-001", "钢板", "V1"), kept.Count));
- }
- /// <summary>阶段 1.1 · 场景 4:明细全为空占位 → Create 按 0 明细拒绝。</summary>
- [Fact]
- public void Create_WhenAllDetailsAreBlankPlaceholders_IsRejected()
- {
- var kept = AdoS0QmsSpecAggregateGuard.Meaningful(new List<AdoS0QmsRawInspectionSpecEntryDto> { new(), new() });
- Assert.Empty(kept);
- Assert.Equal("检验明细至少需要 1 条",
- AdoS0QmsSpecAggregateGuard.ValidateCreate(RawFields("F-001", "M-001", "钢板", "V1"), kept.Count));
- }
- /// <summary>阶段 1.1 · 场景 5:历史 0 明细 + UI 空占位 → 放行,且不得写出任何明细。</summary>
- [Fact]
- public void Update_WhenHistoricalHadNoDetailAndUiAddsBlankPlaceholder_WritesNothing()
- {
- // UI 新补的空占位没有 Id → 不进写入集
- var written = AdoS0QmsSpecAggregateGuard.ForUpdateWrite(new List<AdoS0QmsProcessInspectionSpecEntryDto> { new() });
- Assert.Empty(written); // 写库用的就是这份 → 不会新增全空明细
- Assert.Null(AdoS0QmsSpecAggregateGuard.ValidateUpdate(
- [("物料编码", "M-001", "M-001")], existingMeaningfulItemCount: 0, incomingMeaningfulItemCount: 0));
- }
- /// <summary>
- /// 阶段 1.1 · 场景 10:Update 是 delete-all + 全量重建语义,
- /// 因此写入集必须保留**历史已持久化**的空明细(带 Id),否则用户编辑一次头字段
- /// 就会静默删掉库里的空行——实测 aidopdev 有 10 条这样的行,分布在 9 张真实主单上。
- /// </summary>
- [Fact]
- public void Update_KeepsPersistedEmptyDetail_ButDropsNewEmptyPlaceholder()
- {
- var incoming = new List<AdoS0QmsRawInspectionSpecEntryDto>
- {
- new() { Id = 101, InspectionItem = "外观" }, // 历史真实明细
- new() { Id = 102 }, // 历史已持久化的空明细 → 必须保留
- new() { InspectionItem = " " }, // 本次 UI 新补的空占位 → 必须丢弃
- };
- var written = AdoS0QmsSpecAggregateGuard.ForUpdateWrite(incoming);
- Assert.Equal(2, written.Count);
- Assert.Equal(new long?[] { 101, 102 }, written.Select(x => x.Id).ToArray());
- // 数量校验仍只认 meaningful:这里只有 1 条有内容
- Assert.Single(AdoS0QmsSpecAggregateGuard.Meaningful(incoming));
- }
- /// <summary>Create 侧相反:任何空明细都没有 Id,一律不落库。</summary>
- [Fact]
- public void Create_NeverPersistsEmptyDetail_EvenIfClientSuppliesAnId()
- {
- var incoming = new List<AdoS0QmsRawInspectionSpecEntryDto> { new() { InspectionItem = "外观" }, new() { Id = 999 } };
- // Create 走 Meaningful,不走 ForUpdateWrite → 带 Id 的空行同样不落库
- Assert.Single(AdoS0QmsSpecAggregateGuard.Meaningful(incoming));
- }
- /// <summary>
- /// 「原有 ≥1 明细不许删空」必须按 meaningful 计。
- /// 若按 raw count,一张明细全为空的历史主单会被自己的空占位永久锁死、无法保存。
- /// </summary>
- [Fact]
- public void Update_WhenAllExistingDetailsAreEmpty_IsNotLockedOut()
- {
- var existingRows = new List<AdoS0QmsRawInspectionSpecEntry> { new(), new() }; // 2 条空行
- var existingMeaningful = AdoS0QmsSpecAggregateGuard.CountMeaningful(existingRows);
- Assert.Equal(0, existingMeaningful);
- Assert.Null(AdoS0QmsSpecAggregateGuard.ValidateUpdate(
- [("物料编码", "M-001", "M-001")], existingMeaningful, incomingMeaningfulItemCount: 0));
- }
- [Fact]
- public void CountMeaningful_OnEntities_MatchesDtoSemantics()
- {
- Assert.Equal(1, AdoS0QmsSpecAggregateGuard.CountMeaningful(new List<AdoS0QmsRawInspectionSpecEntry>
- {
- new() { InspectionItem = "外观" }, new() { InspectionItem = " " }, new()
- }));
- Assert.Equal(2, AdoS0QmsSpecAggregateGuard.CountMeaningful(new List<AdoS0QmsProcessInspectionSpecEntry>
- {
- new() { OperationCode = "OP10" }, new() { PeelingForce = 5 }, new()
- }));
- }
- /// <summary>阶段 1.1 · 场景 6:原有真实明细被删到只剩空占位 → 按「删到 0」拒绝。</summary>
- [Fact]
- public void Update_WhenRealDetailsReducedToBlankPlaceholderOnly_IsRejected()
- {
- var kept = AdoS0QmsSpecAggregateGuard.Meaningful(new List<AdoS0QmsFqcInspectionSpecEntryDto> { new() });
- Assert.Empty(kept);
- Assert.Equal("该检规原有 3 条检验明细,不允许全部删除",
- AdoS0QmsSpecAggregateGuard.ValidateUpdate(
- [("物料编码", "M-001", "M-001")], existingMeaningfulItemCount: 3, incomingMeaningfulItemCount: kept.Count));
- }
- /// <summary>阶段 1.1 · 场景 7:导入侧口径与实体侧同源——空明细行不落库,全空 aggregate 拒绝。</summary>
- [Fact]
- public void Import_BlankDetailRowsAreNotCountedAndAllBlankAggregateIsRejected()
- {
- var headerOnly = new AdoS0QmsRawInspectionSpecImportRow { FileNumber = "F-001", MaterialCode = "M-001", RawMaterialName = "钢板", VersionNo = "V1" };
- var blankDetail = new AdoS0QmsRawInspectionSpecImportRow { FileNumber = "F-001" };
- var realDetail = new AdoS0QmsRawInspectionSpecImportRow { FileNumber = "F-001", InspectionItem = "外观" };
- Assert.False(AdoS0QmsSpecAggregateGuard.HasDetailValue(headerOnly));
- Assert.False(AdoS0QmsSpecAggregateGuard.HasDetailValue(blankDetail));
- Assert.True(AdoS0QmsSpecAggregateGuard.HasDetailValue(realDetail));
- // 只有表头行 + 空行的分组 → 0 条明细 → 整份拒绝
- var group = new[] { headerOnly, blankDetail };
- Assert.Equal("检验明细至少需要 1 条",
- AdoS0QmsSpecAggregateGuard.ValidateCreate(
- RawFields("F-001", "M-001", "钢板", "V1"),
- group.Count(AdoS0QmsSpecAggregateGuard.HasDetailValue)));
- }
- [Fact]
- public void Import_ProcessLikeBlankRowIsNotADetail()
- {
- Assert.False(AdoS0QmsSpecAggregateGuard.HasDetailValue(
- new AdoS0QmsProcessLikeInspectionSpecImportRow { FileNumber = "F-001", MaterialCode = "M-001", VersionNo = "V1" }));
- Assert.True(AdoS0QmsSpecAggregateGuard.HasDetailValue(
- new AdoS0QmsProcessLikeInspectionSpecImportRow { FileNumber = "F-001", OperationCode = "OP10" }));
- }
- // ==================== 明细「有内容」判据 ====================
- [Fact]
- public void HasValue_WhenRawEntryIsEntirelyBlank_ReturnsFalse()
- {
- // 前端 openCreate 会自动补一行空明细;它不得让「≥1 条」形同虚设。
- Assert.False(AdoS0QmsSpecAggregateGuard.HasValue(new AdoS0QmsRawInspectionSpecEntryDto
- {
- InspectionItem = "", InspectionStandard = " ", UpperLimit = null
- }));
- }
- [Fact]
- public void HasValue_WhenRawEntryHasAnyContent_ReturnsTrue()
- {
- Assert.True(AdoS0QmsSpecAggregateGuard.HasValue(new AdoS0QmsRawInspectionSpecEntryDto { InspectionItem = "外观" }));
- Assert.True(AdoS0QmsSpecAggregateGuard.HasValue(new AdoS0QmsRawInspectionSpecEntryDto { Seq = 1 }));
- }
- [Fact]
- public void HasValue_WhenProcessLikeEntryIsEntirelyBlank_ReturnsFalse()
- {
- Assert.False(AdoS0QmsSpecAggregateGuard.HasValue(new AdoS0QmsProcessInspectionSpecEntryDto()));
- Assert.False(AdoS0QmsSpecAggregateGuard.HasValue(new AdoS0QmsFqcInspectionSpecEntryDto()));
- Assert.False(AdoS0QmsSpecAggregateGuard.HasValue(new AdoS0QmsOqcInspectionSpecEntryDto()));
- }
- [Fact]
- public void HasValue_WhenProcessLikeEntryHasAnyContent_ReturnsTrue()
- {
- Assert.True(AdoS0QmsSpecAggregateGuard.HasValue(new AdoS0QmsProcessInspectionSpecEntryDto { OperationCode = "OP10" }));
- Assert.True(AdoS0QmsSpecAggregateGuard.HasValue(new AdoS0QmsFqcInspectionSpecEntryDto { InspectionItem = "尺寸" }));
- Assert.True(AdoS0QmsSpecAggregateGuard.HasValue(new AdoS0QmsOqcInspectionSpecEntryDto { PeelingForce = 5 }));
- }
- }
|