exception-data-generator v1.2.0
异常数据生成工具
使用此工具,通过配置,来生成相应数据的各种异常状态。
举例:用户配置模板数据为:
{"a": "int|undefined,null|1"}
说明:
"a"为数据字段名,字段值分为三部分:
第一个参数"int"表明这是一个int类型的字段
第二个参数"undefined,null"表示异常值可能返回undefined,也可能是返回null(支持留空,表示永远信任此字段)
第三个参数"1",为开发自定义的合法值(int、str、bool支持多枚举值输出到正常数据)
模板参数说明:
整理了数据契约中常见的字段类型,以下是推荐配置
int类型:{"a": "int|undefined,null|1"} - undefined,null
float类型:{"b": "float|undefined,null|2.3"} - undefined,null
str类型:{"c": "string|undefined,null,''|'test'"} - undefined,null,""
bool类型:{"d": "bool|undefined,null|true"} - undefined,null
binary类型:{"e": "binary|undefined,null,''|合法字节流"} - undefined,null,""
obj类型:{"f": {...}} - 工具自动输出“undefined,null,{}”的异常,{...}为合法值
intArray类型:{"g": 1,2,3] - 工具自动输出“undefined,null,[]”的异常,1,2,3为合法值
strArray类型:{"h": 'a','b','c'] - 工具自动输出“undefined,null,[]”的异常,'a','b','c'为合法值
objArray类型:{"i": {...}] - 工具自动输出“undefined,null,[]”的异常,{...}为合法值
模板语法更新说明:
1.0.6版本更新:
string / int / float / bool类型数据支持跳过模板语法直接配置,例如:"a": 123,效果等同于 "a":"int||123",简化配置。(但要注意,配置多个枚举值的情况还是要按照模板语法撰写)
1.1.0版本更新:
objArray类型支持多对象配置:{"i": {...},{...},...]
1.2.0版本更新:
getTestDataSet & getAbnormalTestCase方法支持传入options对象,初步添加trustedItemList字段,通过配置二维数组的方式,使异常数据列表不返回对应路径下对象的异常值(完全信任所配路径下的对象)
实际项目举例:
const tpl = {
"productList": [
{
"productID": "int|undefined,null|123",
"productName": "string|undefined,null,''|'testProductName'",
"tag": ['a','b','c'],
"starCity": "string|undefined,null,''|'Shanghai'",
"score": "float|undefined,null|2.3",
"salePrice": "float|undefined,null|11.1",
"netPrice": "float|undefined,null|12.1",
"coverImage": "string|undefined,null,''|'https://dimg04.c-ctrip.com/images/fd/activity/g4/M00/D7/C4/CggYHVaMv62AQLlYAAF2rH5eBek422_C_260_195.jpg'",
"currency": "string|undefined,null,''|'CNY'",
"currencyCode": "string|undefined,null,''|'CNY'",
"brandType": "int|undefined,null|1,2", // 注意这边为多个枚举值
"brandName": "string|undefined,null,''|'testBrandName'"
}
],
"isBst": "bool|undefined,null|true,false",
"priceType": "int|undefined,null|1,2",
"location": {
"query": {
"ssr": 0
}
}
}
工具暴露4个方法:
1、getTestDataSet——返回三个参数mockSkeletonData, mockNormalDataList, mockAbnormalDataList,分别为“第一个正常值的骨架数据”、“所有正常枚举值循环出来的正常数据列表”、“所有异常枚举值循环出来的异常数据列表”。
2、getSkeletonData——第一个正常值的骨架数据
3、getNormalTestCase——所有正常枚举值循环出来的正常数据列表
4、getAbnormalTestCase——所有异常枚举值循环出来的异常数据列表
代码实例:
import {
getTestDataSet,
getSkeletonData,
getNormalTestCase,
getAbnormalTestCase,
} from 'exception-data-generator'
const [mockSkeletonData, mockNormalDataList, mockAbnormalDataList] = getTestDataSet(tpl, '数据描述', {
trustedItemList: [
['location'], // location对象不返回异常
['location', 'query'] // location->query对象不返回异常
]
})
// or
const skeletonData = getSkeletonData(tpl, '数据描述')
const getNormalTestCase = getNormalTestCase(tpl, '数据描述')
const getAbnormalTestCase = getAbnormalTestCase(tpl, '数据描述', {
trustedItemList: [
['location'], // location对象不返回异常
['location', 'query'] // location->query对象不返回异常
]
})