from flask_app.货物标.技术参数要求提取后处理函数 import restructure_data

#注意！这里的期待输出有误
def test_restructure_data():
    print("开始测试 restructure_data 函数...\n")

    # 测试用例 1: 所有顶层键的值都是列表（二层结构）
    data1 = {
        "fruits": ["apple", "banana", "cherry"],
        "vegetables": ["carrot", "lettuce", "spinach"],
        "grains": ["rice", "wheat", "barley"]
    }
    expected1 = data1.copy()
    result1 = restructure_data(data1)
    print("测试用例 1: 所有顶层键的值都是列表（二层结构）")
    print("输入:", data1)
    print("预期输出:", expected1)
    print("实际输出:", result1)
    print("测试通过:", result1 == expected1, "\n")

    # 测试用例 2: 所有顶层键的值都是字典，且已经是三层结构
    data2 = {
        "fruits": {
            "citrus": ["orange", "lemon"],
            "berries": ["strawberry", "blueberry"]
        },
        "vegetables": {
            "root": ["carrot", "beet"],
            "leafy": ["lettuce", "spinach"]
        }
    }
    expected2 = data2.copy()
    result2 = restructure_data(data2)
    print("测试用例 2: 所有顶层键的值都是字典，且已经是三层结构")
    print("输入:", data2)
    print("预期输出:", expected2)
    print("实际输出:", result2)
    print("测试通过:", result2 == expected2, "\n")

    # 测试用例 3: 混合的两层和三层结构
    data3 = {
        "fruits": ["apple", "banana"],
        "vegetables": {
            "root": ["carrot", "beet"],
            "leafy": ["lettuce", "spinach"]
        },
        "grains": ["rice", "wheat"]
    }
    expected3 = {
        "fruits": {"fruits": ["apple", "banana"]},
        "vegetables": {
            "root": ["carrot", "beet"],
            "leafy": ["lettuce", "spinach"]
        },
        "grains": {"grains": ["rice", "wheat"]}
    }
    result3 = restructure_data(data3)
    print("测试用例 3: 混合的两层和三层结构")
    print("输入:", data3)
    print("预期输出:", expected3)
    print("实际输出:", result3)
    print("测试通过:", result3 == expected3, "\n")

    # 测试用例 4: 超过三层嵌套
    data4 = {
        "fruits": {
            "citrus": {
                "tropical": ["orange", "lemon"]
            },
            "berries": ["strawberry", "blueberry"]
        },
        "vegetables": ["carrot", "lettuce"]
    }
    expected4 = {
        "fruits.citrus": {
            "tropical": ["orange", "lemon"]
        },
        "fruits.berries": {
            "berries": ["strawberry", "blueberry"]
        },
        "vegetables": {
            "vegetables": ["carrot", "lettuce"]
        }
    }
    result4 = restructure_data(data4)
    print("测试用例 4: 超过三层嵌套")
    print("输入:", data4)
    print("预期输出:", expected4)
    print("实际输出:", result4)
    print("测试通过:", result4 == expected4, "\n")

    # 测试用例 5: 超过四层嵌套
    data5 = {
        "animals": {
            "mammals": {
                "primates": {
                    "humans": ["Alice", "Bob"],
                    "monkeys": ["Charlie", "Dave"]
                },
                "carnivores": ["Lion", "Tiger"]
            },
            "birds": ["Eagle", "Parrot"]
        },
        "plants": ["Oak", "Pine"]
    }
    expected5 = {
        "animals.mammals.primates": {
            "humans": ["Alice", "Bob"],
            "monkeys": ["Charlie", "Dave"]
        },
        "animals.mammals.carnivores": ["Lion", "Tiger"],
        "animals.birds": ["Eagle", "Parrot"],
        "plants": {"plants": ["Oak", "Pine"]}
    }
    result5 = restructure_data(data5)
    print("测试用例 5: 超过四层嵌套")
    print("输入:", data5)
    print("预期输出:", expected5)
    print("实际输出:", result5)
    print("测试通过:", result5 == expected5, "\n")

    # 测试用例 6: 空字典
    data6 = {}
    expected6 = {}
    result6 = restructure_data(data6)
    print("测试用例 6: 空字典")
    print("输入:", data6)
    print("预期输出:", expected6)
    print("实际输出:", result6)
    print("测试通过:", result6 == expected6, "\n")

    # 测试用例 7: 顶层键的值类型无效
    data7 = {
        "fruits": "apple",
        "vegetables": ["carrot", "lettuce"]
    }
    print("测试用例 7: 顶层键的值类型无效")
    print("输入:", data7)
    try:
        restructure_data(data7)
        print("预期结果: 引发 ValueError")
        print("实际输出: 无异常")
        print("测试通过: False\n")
    except ValueError as ve:
        print("预期结果: 引发 ValueError")
        print("实际输出: 引发 ValueError:", ve)
        print("测试通过:", "数据格式异常" in str(ve), "\n")

    # 测试用例 8: 嵌套字典中存在无效的值类型
    data8 = {
        "fruits": {
            "citrus": "orange",
            "berries": ["strawberry", "blueberry"]
        },
        "vegetables": ["carrot", "lettuce"]
    }
    print("测试用例 8: 嵌套字典中存在无效的值类型")
    print("输入:", data8)
    try:
        restructure_data(data8)
        print("预期结果: 引发 ValueError")
        print("实际输出: 无异常")
        print("测试通过: False\n")
    except ValueError as ve:
        print("预期结果: 引发 ValueError")
        print("实际输出: 引发 ValueError:", ve)
        print("测试通过:", "数据格式异常" in str(ve), "\n")

    # 测试用例 9: 复杂的嵌套结构
    data9 = {
        "fruits": {
            "citrus": ["orange", "lemon"],
            "berries": {
                "summer": ["strawberry", "blueberry"],
                "winter": ["cranberry"]
            }
        },
        "vegetables": ["carrot", "lettuce"],
        "grains": {
            "whole": ["rice", "wheat"],
            "refined": ["white rice"]
        }
    }
    expected9 = {
        "fruits.berries": {
            "summer": ["strawberry", "blueberry"],
            "winter": ["cranberry"]
        },
        "fruits.citrus": {
            "citrus": ["orange", "lemon"]
        },
        "vegetables": {
            "vegetables": ["carrot", "lettuce"]
        },
        "grains.whole": {
            "whole": ["rice", "wheat"]
        },
        "grains.refined": {
            "refined": ["white rice"]
        }
    }
    result9 = restructure_data(data9)
    print("测试用例 9: 复杂的嵌套结构")
    print("输入:", data9)
    print("预期输出:", expected9)
    print("实际输出:", result9)
    print("测试通过:", result9 == expected9, "\n")

    print("所有测试用例已完成。")

# 运行测试
if __name__ == "__main__":
    test_restructure_data()
