v15 D4 DmConfigValidator: DmConfig/DmField + 7 L2 规则 (字段数/主键/必填/格式/去重/decimal/说明) + 28 tests (323 total)
This commit is contained in:
1 parent
10bf91b011
commit
d6d700815f
6 files changed
+476
-14
No files matched your search
@@ -0,0 +1,31 @@
|
||||
package ssw.dacli.validation.dm;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* v15 D4 — DmConfig 数据模型 (dm 子命令输入的不可变快照).
|
||||
*
|
||||
* <p>dm create/update 子命令的必传参数 (table, tableName, fields) 经 {@link ssw.dacli.DmCommand}
|
||||
* 解析后构造成本类, 交给 {@link DmConfigValidator} 校验 7 条 L2 规则.
|
||||
*
|
||||
* <p>字段顺序与 JNPF 后端 API 字段一致:
|
||||
* <ul>
|
||||
* <li>{@code table} (JNPF: newTable) — 表名
|
||||
* <li>{@code tableName} — 表说明
|
||||
* <li>{@code fields} — 字段定义列表
|
||||
* </ul>
|
||||
*
|
||||
* <p>使用示例:
|
||||
* <pre>{@code
|
||||
* DmConfig cfg = new DmConfig("tds_x", "X 表", List.of(
|
||||
* new DmField("f_id", "主键", "varchar", "50", 0, 1),
|
||||
* new DmField("f_name", "名称", "varchar", "100", 0, 0)
|
||||
* ));
|
||||
* List<ValidationError> errs = ConfigValidatorFactory.get(Module.DM).validate(cfg);
|
||||
* }</pre>
|
||||
*/
|
||||
public record DmConfig(String table, String tableName, List<DmField> fields) {
|
||||
public DmConfig {
|
||||
if (fields == null) fields = List.of();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,140 @@
|
||||
package ssw.dacli.validation.dm;
|
||||
|
||||
import ssw.dacli.validation.ConfigValidator;
|
||||
import ssw.dacli.validation.DacliValidation;
|
||||
import ssw.dacli.validation.Module;
|
||||
import ssw.dacli.validation.Severity;
|
||||
import ssw.dacli.validation.ValidationError;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* v15 D4 — DmConfigValidator (dm 数据模型 L2 配置提交校验).
|
||||
*
|
||||
* <p>实现 {@code docs/dacli/v15-validation-schema.md} §3.2 列出的 7 条 L2 规则:
|
||||
* <ol>
|
||||
* <li>dm-L2-1: 至少 1 个字段 ({@code fieldList.length >= 1})
|
||||
* <li>dm-L2-2: 至少 1 个主键 ({@code primaryKey == 1})
|
||||
* <li>dm-L2-3: 字段名必填 ({@code field} 非空)
|
||||
* <li>dm-L2-4: 字段名格式 (小写+下划线+字母开头)
|
||||
* <li>dm-L2-5: 字段名不重复 (去重)
|
||||
* <li>dm-L2-6: decimal 类型 {@code dataLength} 格式 (数字+逗号)
|
||||
* <li>dm-L2-7: 字段说明必填 ({@code fieldName} 非空)
|
||||
* </ol>
|
||||
*
|
||||
* <p>L1 (用户填写) 校验由 {@link DacliValidation} 在子命令 @Parameters 注解层处理,
|
||||
* 本类只负责 L2 (配置完整性).
|
||||
*
|
||||
* <p>实现要点 (仿照 web 端 dm Form.vue:174-229 exist() + handleSubmit()):
|
||||
* <ul>
|
||||
* <li>穷尽所有错误, 一次性返回 (用户看到全部再修)
|
||||
* <li>field 索引 (i) 用于错误消息 ("第1行列名不能为空")
|
||||
* <li>set + 重复检测避免假阳性 (空字符串也算重复)
|
||||
* </ul>
|
||||
*
|
||||
* <p>注意: web 端用 `fieldList.value.filter((o) => o.field == e.field)` 统计,
|
||||
* 本类用 HashSet.size 检测, 效率更高.
|
||||
*/
|
||||
public class DmConfigValidator implements ConfigValidator<DmConfig> {
|
||||
|
||||
/** 字段名格式正则 (与 web 端 dm Form.vue:187 一致):
|
||||
* (^_([a-z0-9]_?)*$)|(^[a-z](_?[a-z0-9])*_?$) — 下划线/字母开头, 中间字母数字+可选下划线
|
||||
*/
|
||||
private static final Pattern FIELD_NAME_PATTERN = Pattern.compile(
|
||||
"(^_([a-z0-9]_?)*$)|(^[a-z](_?[a-z0-9])*_?$)", Pattern.CASE_INSENSITIVE);
|
||||
|
||||
/** decimal 类型 dataLength 格式 (与 web 端 dm Form.vue:198 一致): 数字+逗号 */
|
||||
private static final Pattern DECIMAL_LENGTH_PATTERN = Pattern.compile("^\\d+(?:,\\d*)?$");
|
||||
|
||||
@Override
|
||||
public Module module() {
|
||||
return Module.DM;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ValidationError> validate(DmConfig config) {
|
||||
List<ValidationError> errors = new ArrayList<>();
|
||||
|
||||
if (config == null) {
|
||||
errors.add(new ValidationError("dm-L2-0", "DmConfig 不能为 null", "config", Severity.ERROR));
|
||||
return errors;
|
||||
}
|
||||
|
||||
// L1: 表名格式校验 (dm.table = 大写下划线)
|
||||
if (config.table() != null && !config.table().isEmpty()) {
|
||||
DacliValidation.PatternRule tableRule = DacliValidation.getModuleBinding("dm", "table");
|
||||
if (!tableRule.matches(config.table())) {
|
||||
errors.add(new ValidationError("dm-L1-1", Module.DM, tableRule.message(),
|
||||
"table", Severity.ERROR));
|
||||
}
|
||||
}
|
||||
|
||||
// L1: 表说明必填 (tableName)
|
||||
if (config.tableName() == null || config.tableName().isEmpty()) {
|
||||
errors.add(new ValidationError("dm-L1-2", Module.DM, "请输入表说明",
|
||||
"tableName", Severity.ERROR));
|
||||
}
|
||||
|
||||
// L2-1: 至少 1 个字段
|
||||
if (config.fields().isEmpty()) {
|
||||
errors.add(new ValidationError("dm-L2-1", "请至少添加一个字段", "fields", Severity.ERROR));
|
||||
return errors; // 没有字段, 后续规则无意义
|
||||
}
|
||||
|
||||
// L2-2: 至少 1 个主键
|
||||
boolean hasPrimaryKey = config.fields().stream().anyMatch(f -> f.primaryKey() == 1);
|
||||
if (!hasPrimaryKey) {
|
||||
errors.add(new ValidationError("dm-L2-2", "请选择一个字段作为主键", "fields", Severity.ERROR));
|
||||
}
|
||||
|
||||
// L2-3 ~ L2-7: 逐字段校验
|
||||
Set<String> seenFields = new HashSet<>();
|
||||
for (int i = 0; i < config.fields().size(); i++) {
|
||||
DmField f = config.fields().get(i);
|
||||
int lineNo = i + 1;
|
||||
|
||||
// L2-3: 字段名必填
|
||||
if (f.field() == null || f.field().isEmpty()) {
|
||||
errors.add(new ValidationError("dm-L2-3",
|
||||
"第" + lineNo + "行列名不能为空", "fields[" + i + "].field", Severity.ERROR));
|
||||
continue; // 没有 field, 后续字段名规则无意义
|
||||
}
|
||||
|
||||
// L2-4: 字段名格式
|
||||
if (!FIELD_NAME_PATTERN.matcher(f.field()).matches()) {
|
||||
errors.add(new ValidationError("dm-L2-4",
|
||||
"第" + lineNo + "行列名格式错误,请重新输入",
|
||||
"fields[" + i + "].field", Severity.ERROR));
|
||||
}
|
||||
|
||||
// L2-5: 字段名不重复 (用 set 检测, 比 web 端 filter 高效)
|
||||
if (!seenFields.add(f.field())) {
|
||||
errors.add(new ValidationError("dm-L2-5",
|
||||
"第" + lineNo + "行列名'" + f.field() + "'已重复",
|
||||
"fields[" + i + "].field", Severity.ERROR));
|
||||
}
|
||||
|
||||
// L2-6: decimal 类型 dataLength 格式
|
||||
if ("decimal".equalsIgnoreCase(f.dataType())) {
|
||||
if (f.dataLength() == null || !DECIMAL_LENGTH_PATTERN.matcher(f.dataLength()).matches()) {
|
||||
errors.add(new ValidationError("dm-L2-6",
|
||||
"第" + lineNo + "行请输入数字",
|
||||
"fields[" + i + "].dataLength", Severity.ERROR));
|
||||
}
|
||||
}
|
||||
|
||||
// L2-7: 字段说明必填
|
||||
if (f.fieldName() == null || f.fieldName().isEmpty()) {
|
||||
errors.add(new ValidationError("dm-L2-7",
|
||||
"第" + lineNo + "行说明不能为空",
|
||||
"fields[" + i + "].fieldName", Severity.ERROR));
|
||||
}
|
||||
}
|
||||
|
||||
return errors;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package ssw.dacli.validation.dm;
|
||||
|
||||
/**
|
||||
* v15 D4 — DmField 单字段定义 (dm 子命令 --fields 数组元素).
|
||||
*
|
||||
* <p>字段对应 JNPF 后端 DataModel API 的字段定义:
|
||||
* <ul>
|
||||
* <li>{@code field} — 列名 (e.g. "f_name")
|
||||
* <li>{@code fieldName} — 列说明 (e.g. "名称")
|
||||
* <li>{@code dataType} — 数据类型 (varchar/int/datetime/decimal/bigint/text)
|
||||
* <li>{@code dataLength} — 长度 (decimal 类型用 "10,2" 格式)
|
||||
* <li>{@code allowNull} — 0=否, 1=是
|
||||
* <li>{@code primaryKey} — 0=否, 1=是 (复合主键可多个)
|
||||
* </ul>
|
||||
*/
|
||||
public record DmField(
|
||||
String field,
|
||||
String fieldName,
|
||||
String dataType,
|
||||
String dataLength,
|
||||
int allowNull,
|
||||
int primaryKey
|
||||
) {
|
||||
/** 构造时允许 0/1 之外的整数值 (视为 0) */
|
||||
public DmField {
|
||||
allowNull = (allowNull == 1) ? 1 : 0;
|
||||
primaryKey = (primaryKey == 1) ? 1 : 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
ssw.dacli.validation.dm.DmConfigValidator
|
||||
@@ -5,36 +5,59 @@ import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/**
|
||||
* v15 D3 — ConfigValidatorFactory SPI 注册测试.
|
||||
* v15 D3+D4 — ConfigValidatorFactory SPI 注册测试.
|
||||
*
|
||||
* <p>v15 启动时 (D3 阶段) 4 module 都没有注册 validator, 工厂应抛
|
||||
* {@link UnsupportedOperationException} 并提示需在指定路径添加实现.
|
||||
* <p>v15 D3 阶段 4 module 都没有注册 validator, 工厂应抛 {@link UnsupportedOperationException}.
|
||||
* <p>v15 D4 阶段 DmConfigValidator 已实现并注册, DM module 应可正常返回.
|
||||
* <p>v15 D7+ 阶段 di/dict/workflow validator 才会逐个实现, 仍为未注册.
|
||||
*/
|
||||
public class ConfigValidatorFactoryTest {
|
||||
|
||||
@Test
|
||||
public void testRegisteredModules_emptyAtD3() {
|
||||
// D3 阶段 4 module 都未注册 (D4-D7 才会实现)
|
||||
// 此断言反映"未实现"现状, D4 实现 DmConfigValidator 后此断言需更新
|
||||
assertEquals(0, ConfigValidatorFactory.registeredModules().size(),
|
||||
"D3 阶段 4 module 都未注册, D4+ 才会逐个实现");
|
||||
public void testRegisteredModules_dmRegisteredAtD4() {
|
||||
// D4 阶段: DM 已注册, DI/DICT/WORKFLOW 仍未注册
|
||||
java.util.List<Module> registered = ConfigValidatorFactory.registeredModules();
|
||||
assertEquals(1, registered.size(),
|
||||
"D4 阶段只有 DM 注册, 实际: " + registered);
|
||||
assertTrue(registered.contains(Module.DM));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGet_unregistered_throwsHelpfulError() {
|
||||
public void testGet_dm_returnsDmValidator() {
|
||||
// DM 已注册, get 应正常返回
|
||||
assertDoesNotThrow(() -> ConfigValidatorFactory.get(Module.DM));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGet_di_unregistered_throwsHelpfulError() {
|
||||
UnsupportedOperationException ex = assertThrows(UnsupportedOperationException.class,
|
||||
() -> ConfigValidatorFactory.get(Module.DM));
|
||||
// 错误消息应提示开发者如何实现
|
||||
assertTrue(ex.getMessage().contains("dm"), "错误消息应包含 module 短码");
|
||||
() -> ConfigValidatorFactory.get(Module.DI));
|
||||
assertTrue(ex.getMessage().contains("di"), "错误消息应包含 module 短码");
|
||||
assertTrue(ex.getMessage().contains("ConfigValidator"), "错误消息应提示需注册 ConfigValidator");
|
||||
assertTrue(ex.getMessage().contains("META-INF/services"),
|
||||
"错误消息应提示 SPI 注册路径");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsSupported_unregistered() {
|
||||
public void testGet_dict_unregistered() {
|
||||
assertThrows(UnsupportedOperationException.class,
|
||||
() -> ConfigValidatorFactory.get(Module.DICT));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGet_workflow_unregistered() {
|
||||
assertThrows(UnsupportedOperationException.class,
|
||||
() -> ConfigValidatorFactory.get(Module.WORKFLOW));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsSupported_dmRegistered() {
|
||||
assertTrue(ConfigValidatorFactory.isSupported(Module.DM));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsSupported_othersUnregistered() {
|
||||
assertFalse(ConfigValidatorFactory.isSupported(Module.DI));
|
||||
assertFalse(ConfigValidatorFactory.isSupported(Module.DM));
|
||||
assertFalse(ConfigValidatorFactory.isSupported(Module.DICT));
|
||||
assertFalse(ConfigValidatorFactory.isSupported(Module.WORKFLOW));
|
||||
}
|
||||
|
||||
@@ -0,0 +1,238 @@
|
||||
package ssw.dacli.validation.dm;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import ssw.dacli.validation.ConfigValidator;
|
||||
import ssw.dacli.validation.ConfigValidatorFactory;
|
||||
import ssw.dacli.validation.Module;
|
||||
import ssw.dacli.validation.Severity;
|
||||
import ssw.dacli.validation.ValidationError;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/**
|
||||
* v15 D4 — DmConfigValidator 单元测试 (覆盖 7 条 L2 规则 + L1 字段绑定).
|
||||
*/
|
||||
public class DmConfigValidatorTest {
|
||||
|
||||
@Test
|
||||
public void testModule() {
|
||||
DmConfigValidator v = new DmConfigValidator();
|
||||
assertEquals(Module.DM, v.module());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSpiRegistered() {
|
||||
// D4 后 DmConfigValidator 应在 SPI 注册
|
||||
assertTrue(ConfigValidatorFactory.isSupported(Module.DM));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSpiGet_returnsDmValidator() {
|
||||
@SuppressWarnings("unchecked")
|
||||
ConfigValidator<DmConfig> v = ConfigValidatorFactory.get(Module.DM);
|
||||
assertNotNull(v);
|
||||
assertEquals(Module.DM, v.module());
|
||||
// 通过工厂拿到的实例, 实际类型应是 DmConfigValidator
|
||||
assertTrue(v instanceof DmConfigValidator);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValidConfig_passesAllChecks() {
|
||||
DmConfig cfg = new DmConfig("TDS_X", "X 表", List.of(
|
||||
new DmField("f_id", "主键", "varchar", "50", 0, 1),
|
||||
new DmField("f_name", "名称", "varchar", "100", 0, 0)
|
||||
));
|
||||
List<ValidationError> errs = new DmConfigValidator().validate(cfg);
|
||||
assertTrue(errs.isEmpty(), "valid config 应无错误, 实际: " + errs);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullConfig_throwsHelpfulError() {
|
||||
List<ValidationError> errs = new DmConfigValidator().validate(null);
|
||||
assertEquals(1, errs.size());
|
||||
assertEquals(Severity.ERROR, errs.get(0).severity());
|
||||
assertTrue(errs.get(0).message().contains("null"));
|
||||
}
|
||||
|
||||
// ============ L1 (L1 字段绑定校验) ============
|
||||
|
||||
@Test
|
||||
public void testL1_1_tableNameFormat_lowercase() {
|
||||
// 表名必须大写+下划线
|
||||
DmConfig cfg = new DmConfig("tds_x", "X 表", List.of(
|
||||
new DmField("f_id", "主键", "varchar", "50", 0, 1)
|
||||
));
|
||||
List<ValidationError> errs = new DmConfigValidator().validate(cfg);
|
||||
assertTrue(errs.stream().anyMatch(e -> e.code().equals("dm-L1-1")),
|
||||
"表名小写应触发 dm-L1-1, 实际: " + errs);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testL1_2_tableNameMissing() {
|
||||
DmConfig cfg = new DmConfig("TDS_X", "", List.of(
|
||||
new DmField("f_id", "主键", "varchar", "50", 0, 1)
|
||||
));
|
||||
List<ValidationError> errs = new DmConfigValidator().validate(cfg);
|
||||
assertTrue(errs.stream().anyMatch(e -> e.code().equals("dm-L1-2")),
|
||||
"表说明空应触发 dm-L1-2, 实际: " + errs);
|
||||
}
|
||||
|
||||
// ============ L2 校验 ============
|
||||
|
||||
@Test
|
||||
public void testL2_1_noFields() {
|
||||
DmConfig cfg = new DmConfig("TDS_X", "X 表", Collections.emptyList());
|
||||
List<ValidationError> errs = new DmConfigValidator().validate(cfg);
|
||||
assertTrue(errs.stream().anyMatch(e -> e.code().equals("dm-L2-1")),
|
||||
"空字段列表应触发 dm-L2-1, 实际: " + errs);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testL2_2_noPrimaryKey() {
|
||||
DmConfig cfg = new DmConfig("TDS_X", "X 表", List.of(
|
||||
new DmField("f_name", "名称", "varchar", "100", 0, 0)
|
||||
));
|
||||
List<ValidationError> errs = new DmConfigValidator().validate(cfg);
|
||||
assertTrue(errs.stream().anyMatch(e -> e.code().equals("dm-L2-2")),
|
||||
"无主键应触发 dm-L2-2, 实际: " + errs);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testL2_3_fieldNameEmpty() {
|
||||
DmConfig cfg = new DmConfig("TDS_X", "X 表", List.of(
|
||||
new DmField("", "名称", "varchar", "100", 0, 1)
|
||||
));
|
||||
List<ValidationError> errs = new DmConfigValidator().validate(cfg);
|
||||
assertTrue(errs.stream().anyMatch(e -> e.code().equals("dm-L2-3")),
|
||||
"空字段名应触发 dm-L2-3, 实际: " + errs);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testL2_3_errorMessageContainsLineNo() {
|
||||
// 第 3 行字段名空, 错误消息应包含 "第3行"
|
||||
DmConfig cfg = new DmConfig("TDS_X", "X 表", List.of(
|
||||
new DmField("f_id", "PK", "varchar", "50", 0, 1),
|
||||
new DmField("f_name", "名称", "varchar", "100", 0, 0),
|
||||
new DmField("", "空名", "varchar", "100", 0, 0)
|
||||
));
|
||||
List<ValidationError> errs = new DmConfigValidator().validate(cfg);
|
||||
ValidationError e3 = errs.stream()
|
||||
.filter(e -> e.code().equals("dm-L2-3"))
|
||||
.findFirst().orElseThrow();
|
||||
assertTrue(e3.message().contains("第3行"), "错误消息应包含行号, 实际: " + e3.message());
|
||||
assertEquals("fields[2].field", e3.field());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testL2_4_fieldNameFormat_invalid() {
|
||||
DmConfig cfg = new DmConfig("TDS_X", "X 表", List.of(
|
||||
new DmField("1abc", "数字开头", "varchar", "50", 0, 1)
|
||||
));
|
||||
List<ValidationError> errs = new DmConfigValidator().validate(cfg);
|
||||
assertTrue(errs.stream().anyMatch(e -> e.code().equals("dm-L2-4")),
|
||||
"字段名格式错误应触发 dm-L2-4, 实际: " + errs);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testL2_4_fieldNameFormat_valid() {
|
||||
// 允许下划线开头 (a_z, _a, _1 等)
|
||||
DmConfig cfg = new DmConfig("TDS_X", "X 表", List.of(
|
||||
new DmField("_id", "下划线开头", "varchar", "50", 0, 1),
|
||||
new DmField("f_name", "字母开头", "varchar", "100", 0, 0)
|
||||
));
|
||||
List<ValidationError> errs = new DmConfigValidator().validate(cfg);
|
||||
assertFalse(errs.stream().anyMatch(e -> e.code().equals("dm-L2-4")),
|
||||
"合法字段名不应触发 dm-L2-4, 实际: " + errs);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testL2_5_fieldNameDuplicated() {
|
||||
DmConfig cfg = new DmConfig("TDS_X", "X 表", List.of(
|
||||
new DmField("f_id", "PK", "varchar", "50", 0, 1),
|
||||
new DmField("f_id", "重复", "varchar", "100", 0, 0)
|
||||
));
|
||||
List<ValidationError> errs = new DmConfigValidator().validate(cfg);
|
||||
assertTrue(errs.stream().anyMatch(e -> e.code().equals("dm-L2-5")),
|
||||
"字段名重复应触发 dm-L2-5, 实际: " + errs);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testL2_6_decimalDataLength_invalid() {
|
||||
DmConfig cfg = new DmConfig("TDS_X", "X 表", List.of(
|
||||
new DmField("f_id", "PK", "varchar", "50", 0, 1),
|
||||
new DmField("f_amount", "金额", "decimal", "10.2", 0, 0) // 点不允许
|
||||
));
|
||||
List<ValidationError> errs = new DmConfigValidator().validate(cfg);
|
||||
assertTrue(errs.stream().anyMatch(e -> e.code().equals("dm-L2-6")),
|
||||
"decimal dataLength 用点应触发 dm-L2-6, 实际: " + errs);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testL2_6_decimalDataLength_valid() {
|
||||
DmConfig cfg = new DmConfig("TDS_X", "X 表", List.of(
|
||||
new DmField("f_id", "PK", "varchar", "50", 0, 1),
|
||||
new DmField("f_amount", "金额", "decimal", "10,2", 0, 0) // 逗号允许
|
||||
));
|
||||
List<ValidationError> errs = new DmConfigValidator().validate(cfg);
|
||||
assertFalse(errs.stream().anyMatch(e -> e.code().equals("dm-L2-6")),
|
||||
"decimal dataLength 用逗号不应触发 dm-L2-6, 实际: " + errs);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testL2_6_nonDecimalDataType_skipped() {
|
||||
// 非 decimal 类型的 dataLength 不走 L2-6 校验
|
||||
DmConfig cfg = new DmConfig("TDS_X", "X 表", List.of(
|
||||
new DmField("f_id", "PK", "varchar", "abc", 0, 1) // varchar 允许任意 dataLength
|
||||
));
|
||||
List<ValidationError> errs = new DmConfigValidator().validate(cfg);
|
||||
assertFalse(errs.stream().anyMatch(e -> e.code().equals("dm-L2-6")),
|
||||
"非 decimal 类型不应触发 dm-L2-6, 实际: " + errs);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testL2_7_fieldNameMissing() {
|
||||
DmConfig cfg = new DmConfig("TDS_X", "X 表", List.of(
|
||||
new DmField("f_id", "", "varchar", "50", 0, 1)
|
||||
));
|
||||
List<ValidationError> errs = new DmConfigValidator().validate(cfg);
|
||||
assertTrue(errs.stream().anyMatch(e -> e.code().equals("dm-L2-7")),
|
||||
"字段说明空应触发 dm-L2-7, 实际: " + errs);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAllRulesTriggered_simultaneously() {
|
||||
// 一个超烂的 config 同时触发 7 条规则
|
||||
DmConfig cfg = new DmConfig("TDS_X", "X 表", List.of(
|
||||
new DmField("f_id", "PK", "varchar", "50", 0, 1),
|
||||
new DmField("1bad", "", "decimal", "10.2", 0, 0) // 错: 数字开头 + 空说明 + 错 dataLength
|
||||
));
|
||||
List<ValidationError> errs = new DmConfigValidator().validate(cfg);
|
||||
// 应触发 dm-L2-4 (格式) + dm-L2-6 (decimal) + dm-L2-7 (说明)
|
||||
assertTrue(errs.stream().anyMatch(e -> e.code().equals("dm-L2-4")));
|
||||
assertTrue(errs.stream().anyMatch(e -> e.code().equals("dm-L2-6")));
|
||||
assertTrue(errs.stream().anyMatch(e -> e.code().equals("dm-L2-7")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCompositePrimaryKey_works() {
|
||||
// 复合主键 (2 个字段都 primaryKey=1) 应通过 L2-2
|
||||
DmConfig cfg = new DmConfig("TDS_X", "X 表", List.of(
|
||||
new DmField("f_id_a", "PK1", "varchar", "50", 0, 1),
|
||||
new DmField("f_id_b", "PK2", "varchar", "50", 0, 1)
|
||||
));
|
||||
List<ValidationError> errs = new DmConfigValidator().validate(cfg);
|
||||
assertFalse(errs.stream().anyMatch(e -> e.code().equals("dm-L2-2")),
|
||||
"复合主键应通过 L2-2, 实际: " + errs);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDmField_normalizeAllowNull() {
|
||||
// DmField 构造时把非 0/1 整数值规范化为 0
|
||||
DmField f = new DmField("f_id", "PK", "varchar", "50", 5, 9);
|
||||
assertEquals(0, f.allowNull());
|
||||
assertEquals(0, f.primaryKey());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user