接口测试的时候,经常遇到[{},{},{}]格式的数据,需要diff两套环境的返回数据;
举例:
data01=[{"path":"/Users/Library/Fonts/1610424977302997.ttf","postscriptName":"STHupo-Regular","usedPostScriptName":"STHupo","width":5,"weight":400,"style":"Regular","family":"STHupo","localizedFamily":"华文琥珀","localizedStyle":"Regular","italic":false,"monospace":false},{"path":"/Users/Library/Fonts/RobotoFlex[GRAD,XOPQ,XTRA,YOPQ,YTAS,YTDE,YTFI,YTLC,YTUC,opsz,slnt,wdth,wght].ttf","postscriptName":"RobotoFlex-Black","usedPostScriptName":"RobotoFlex-Regular","width":100,"weight":900,"style":"Black","family":"Roboto Flex","localizedFamily":"Roboto Flex","localizedStyle":"Black","italic":false,"monospace":false}]
data02=[{"path":"/Users/Library/Fonts/1610424977302997.ttf","postscriptName":"STHupo","usedPostScriptName":"STHupo","width":5,"weight":400,"style":"Regular","family":"STHupo","localizedFamily":"华文琥珀","localizedStyle":"Regular","italic":false,"monospace":false},{"path":"/Users/Library/Fonts/RobotoFlex[GRAD,XOPQ,XTRA,YOPQ,YTAS,YTDE,YTFI,YTLC,YTUC,opsz,slnt,wdth,wght].ttf","postscriptName":"RobotoFlex","usedPostScriptName":"RobotoFlex-Regular","width":100,"weight":900,"style":"Black","family":"Roboto Flex","localizedFamily":"Roboto Flex","localizedStyle":"Black","italic":false,"monospace":false},{"path":"/Users/Library/Fonts/RobotoFlex[GRAD,XOPQ,XTRA,YOPQ,YTAS,YTDE,YTFI,YTLC,YTUC,opsz,slnt,wdth,wght].ttf","postscriptName":"RobotoFlex","usedPostScriptName":"RobotoFlex-Regular","width":100,"weight":900,"style":"Black","family":"Roboto Flex","localizedFamily":"Roboto Flex","localizedStyle":"Black","italic":false,"monospace":false},{"path":"/Users/Library/Fonts/RobotoFlex[GRAD,XOPQ,XTRA,YOPQ,YTAS,YTDE,YTFI,YTLC,YTUC,opsz,slnt,wdth,wght].ttf","postscriptName":"RobotoFlex","usedPostScriptName":"RobotoFlex-Regular","width":100,"weight":900,"style":"Black","family":"Roboto Flex","localizedFamily":"Roboto Flex","localizedStyle":"Black","italic":false,"monospace":false}]
对比1:获取data02中的重复数据
def json_object_filter(f: str, data: list[dict[str, any]] = None, ret: bool = False) -> list[dict[str, any]] or None:
"""
从给定的json文件或数据列表过滤重复的对象
:param f: 文件地址,文件内容必须是json
:param data: 数据列表
:param ret: 是否返回数据,若为False则直接打印结果,否则返回结果
:return:
"""
if f is None and data is None:
raise RuntimeError("数据文件与数据列表不能都为空")
if f is not None:
data = json.loads((open(f, 'r')).read())
mapping: dict[str, dict[str, any]] = {}
for item in data:
keys = list(item.keys())
keys.sort()
value = ""
for key in keys:
value += "{}".format(item.get(key))
if mapping.get(value) is None:
# 默认不要数据,只统计value
mapping[value] = {"count": 1}
else:
# 只有数据在重复时才取出数据
if ret:
mapping[value]["data"] = item
elif 1 == mapping[value]["count"]:
# 只需要打印一次
print(item)
mapping[value]["count"] += 1
if ret is False:
return None
ret: list[dict[str, any]] = []
for item in mapping.values():
if item["count"] > 1:
ret.append(item["data"])
return ret
对比2:指定某个字段,对比data01和data02返回的字体,输出data01和data02中不一样的数据
def get_diff_data(f_01: str, f_02: str, data_01: list[dict[str, any]] = None, data_02: list[dict[str, any]] = None, field: str = None):
"""
指定某个字段,对比data01和data02返回的字体,输出data01和data02中不一样的数据
:param f_01: 文件地址,文件内容必须是json
:param f_02: 文件地址,文件内容必须是json
:param data_01: 数据列表
:param data_02: 数据列表
:param field: 需要对比的字段
:return:
"""
if f_01 is None and data_01 is None:
raise RuntimeError("data01数据文件与数据列表不能都为空")
if f_02 is None and data_02 is None:
raise RuntimeError("data02数据文件与数据列表不能都为空")
if f_01 is not None:
data_01 = json.loads((open(f_01, 'r')).read())
if f_02 is not None:
data_02 = json.loads((open(f_02, 'r')).read())
print("文件1中一共有{}数据".format(len(data_01)))
print("文件2中一共有{}数据".format(len(data_02)))
field_01_list = [i[field] for i in data_01]
field_02_list = [i[field] for i in data_02]
remove_duplicate_field_01_list = list(set(field_01_list))
remove_duplicate_field_02_list = list(set(field_02_list))
print("字段{}在文件01中一共有{}数据,去重之后还有{}条数据".format(field, len(field_01_list), len(remove_duplicate_field_01_list)))
print("字段{}在文件02中一共有{}数据,去重之后还有{}条数据".format(field, len(field_02_list), len(remove_duplicate_field_02_list)))
in_01_not_in_02 = []
for i in remove_duplicate_field_01_list:
if i not in remove_duplicate_field_02_list:
in_01_not_in_02.append(i)
in_02_not_in_01 = []
for i in remove_duplicate_field_02_list:
if i not in remove_duplicate_field_01_list:
in_02_not_in_01.append(i)
print("=========================结果===========================")
print("字段{}在文件01中存在,但是在02中不存在:".format(field))
print(in_01_not_in_02)
print("字段{}在文件02中存在,但是在01中不存在:".format(field))
print(in_02_not_in_01)