93 lines
3.1 KiB
Python
93 lines
3.1 KiB
Python
from flask_app.货物标.技术参数要求提取后处理函数 import extract_matching_keys
|
|
|
|
|
|
def test_extract_matching_keys():
|
|
# 定义测试数据
|
|
data = {
|
|
"fruits": ["apple", "banana"],
|
|
"fruits-1": ["orange"],
|
|
"vegetables": ["carrot"],
|
|
"系统功能": ["feature1"], # 特殊键,应被特殊处理或排除
|
|
"grains": {
|
|
"whole": ["rice", "wheat"],
|
|
"whole-1": ["barley"],
|
|
"refined": ["white rice"]
|
|
},
|
|
"misc": {
|
|
"fruits": ["strawberry"],
|
|
"fruits-1": ["blueberry"],
|
|
"系统 功能": ["feature2"], # 特殊键,带空格
|
|
"dairy": ["milk", "cheese"]
|
|
},
|
|
"beverages": {
|
|
"alcoholic": {
|
|
"beer": ["lager", "ale"],
|
|
"wine": ["red", "white"]
|
|
},
|
|
"non-alcoholic": ["juice", "soda"]
|
|
},
|
|
"snacks": [
|
|
{
|
|
"chips": ["potato", "tortilla"],
|
|
"nuts": ["almonds", "cashews"]
|
|
},
|
|
{
|
|
"chips-1": ["kettle", "baked"],
|
|
"candies": ["chocolate", "gummy"]
|
|
}
|
|
]
|
|
}
|
|
|
|
good_list = ["fruits", "vegetables", "grains", "chips"]
|
|
|
|
special_keys = ["系统功能", "dairy"] # 假设 'dairy' 也是特殊键
|
|
|
|
# 预期输出
|
|
expected_output = {
|
|
"fruits-a": ["apple", "banana"],
|
|
"fruits-b": ["orange"],
|
|
"vegetables": ["carrot"],
|
|
"grains-a": ["rice", "wheat"],
|
|
"grains-b": ["barley"],
|
|
"grains": {"refined": ["white rice"]},
|
|
"misc": {}, # 'fruits' and 'fruits-1' inside 'misc' should be processed
|
|
"chips-a": ["potato", "tortilla"],
|
|
"chips-b": ["kettle", "baked"]
|
|
}
|
|
|
|
# 注意:根据您的函数逻辑,特殊键会被排除,且嵌套的 'fruits' 会被处理
|
|
# 这里我们需要根据函数实际行为调整预期输出
|
|
# 让我们根据函数逻辑重新定义预期输出
|
|
|
|
# 函数会生成新的键名,对于重复的 'fruits' 会添加后缀
|
|
# 'grains' 内的 'whole' 和 'whole-1' 也会被处理为 'whole-a', 'whole-b'
|
|
# 'chips' 和 'chips-1' 会被处理为 'chips-a', 'chips-b'
|
|
# 'dairy' 是特殊键,应被排除
|
|
# '系统功能' 和 '系统 功能' 是特殊键,应被排除
|
|
|
|
expected_output_correct = {
|
|
"fruits-a": ["apple", "banana"],
|
|
"fruits-b": ["orange"],
|
|
"vegetables": ["carrot"],
|
|
"grains-a": ["rice", "wheat"],
|
|
"grains-b": ["barley"],
|
|
"chips-a": ["potato", "tortilla"],
|
|
"chips-b": ["kettle", "baked"]
|
|
}
|
|
|
|
# 运行函数
|
|
result = extract_matching_keys(data, good_list, special_keys)
|
|
|
|
# 打印结果
|
|
print("测试用例: 提取匹配键并处理各种情况")
|
|
print("输入数据:", data)
|
|
print("good_list:", good_list)
|
|
print("special_keys:", special_keys)
|
|
print("\n预期输出:", expected_output_correct)
|
|
print("实际输出:", result)
|
|
print("\n测试通过:", result == expected_output_correct)
|
|
|
|
# 运行测试
|
|
if __name__ == "__main__":
|
|
test_extract_matching_keys()
|