Files
zszq-trs/UnitTestProject/Modules/RiskEngine/RiskRuleApplicationStatusDecoupleContractTest.cs
T

96 lines
3.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using YLErp.DBModels;
using YLErp.Modules.RiskEngine.Dto;
namespace YLErp.Modules.RiskEngine
{
[TestClass]
public class RiskRuleApplicationStatusDecoupleContractTest
{
private static readonly string[] AvailabilityPropertyNames =
{
"AssociatedRuleCount",
"EffectiveRuleCount",
"CanEnable",
"CanDisable",
"EnableButtonTooltip",
"DisableButtonTooltip"
};
[TestMethod]
public void DisableRuleResult_UsesDecoupledContract()
{
AssertProperty<DisableRuleResult>("RuleId", typeof(long));
AssertProperty<DisableRuleResult>("AffectedActiveAppCount", typeof(int));
Assert.IsNull(typeof(DisableRuleResult).GetProperty("CascadedApplicationIds"));
}
[TestMethod]
public void DeleteRuleResult_ReportsAffectedApplications()
{
AssertProperty<DeleteRuleResult>("RuleId", typeof(long));
AssertProperty<DeleteRuleResult>("AffectedActiveAppCount", typeof(int));
AssertProperty<DeleteRuleResult>("CausedNoAvailableRuleAppIds", typeof(List<long>));
}
[TestMethod]
public void ApplicationDtos_ExposeAvailabilityFields()
{
AssertAvailabilityProperties<RiskApplicationListItem>();
AssertAvailabilityProperties<RiskApplicationDetail>();
}
[TestMethod]
public void BatchOperationResult_ExposesDecoupledApplicationImpactFields()
{
AssertProperty<BatchOperationItemResult>("AffectedApplicationIds", typeof(List<long>));
AssertProperty<BatchOperationItemResult>("CausedNoAvailableRuleApplicationIds", typeof(List<long>));
AssertProperty<BatchOperationResult>("AffectedApplicationIds", typeof(List<long>));
AssertProperty<BatchOperationResult>("CausedNoAvailableRuleApplicationIds", typeof(List<long>));
}
[TestMethod]
public void VariableDeleteReferenceMessage_ListsDistinctSortedRuleIds()
{
var method = typeof(RiskRuleService).GetMethod(
"BuildVariableDeleteReferenceMessage", BindingFlags.NonPublic | BindingFlags.Static);
Assert.IsNotNull(method);
var rules = new List<glms_risk_rule>
{
new glms_risk_rule { id = 12 },
new glms_risk_rule { id = 10 },
new glms_risk_rule { id = 12 }
};
var message = (string)method.Invoke(null, new object[] { rules });
Assert.AreEqual("该变量被 2 个规则引用(规则ID:10、12),无法删除", message);
}
private static void AssertAvailabilityProperties<T>()
{
foreach (var propertyName in AvailabilityPropertyNames)
{
var expectedType = propertyName.EndsWith("Tooltip", StringComparison.Ordinal)
? typeof(string)
: propertyName.StartsWith("Can", StringComparison.Ordinal)
? typeof(bool)
: typeof(int);
AssertProperty<T>(propertyName, expectedType);
}
}
private static void AssertProperty<T>(string propertyName, Type expectedType)
{
var property = typeof(T).GetProperty(propertyName);
Assert.IsNotNull(property, $"{typeof(T).Name}.{propertyName} should exist.");
Assert.AreEqual(expectedType, property.PropertyType, $"{typeof(T).Name}.{propertyName} type mismatch.");
}
}
}