test_scene_split.py 5.55 KB
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
场景拆分脚本测试
用于验证脚本的基本功能
"""

import sys
import os

# 添加父目录到路径
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))

from scene_type_mapping import (
    SCENE_TYPE_MAPPING,
    SCENE_TYPE_0_SPLIT,
    get_base_scene_code,
    need_split,
    get_split_codes,
    get_copy_codes
)


def test_scene_type_mapping():
    """测试场景类型映射"""
    print("=" * 80)
    print("测试场景类型映射")
    print("=" * 80)

    # 测试 scene_type != 0 的映射
    print("\n1. scene_type != 0 的映射:")
    for scene_type, base_scene_code in SCENE_TYPE_MAPPING.items():
        print(f"   scene_type={scene_type} -> base_scene_code={base_scene_code}")

    # 测试 scene_type = 0 的拆分
    print("\n2. scene_type = 0 的拆分:")
    print(f"   拆分为: {SCENE_TYPE_0_SPLIT}")
    print(f"   原记录更新为: {SCENE_TYPE_0_SPLIT[0]}")
    print(f"   复制插入: {SCENE_TYPE_0_SPLIT[1:]}")

    # 测试辅助函数
    print("\n3. 辅助函数测试:")
    print(f"   get_base_scene_code(0) = {get_base_scene_code(0)}")
    print(f"   get_base_scene_code(1) = {get_base_scene_code(1)}")
    print(f"   need_split(0) = {need_split(0)}")
    print(f"   need_split(1) = {need_split(1)}")
    print(f"   get_split_codes(0) = {get_split_codes(0)}")
    print(f"   get_copy_codes(0) = {get_copy_codes(0)}")

    print("\n✓ 场景类型映射测试通过")


def test_import_modules():
    """测试模块导入"""
    print("\n" + "=" * 80)
    print("测试模块导入")
    print("=" * 80)

    try:
        from db_helper import DBHelper, get_db_helper
        print("✓ db_helper 模块导入成功")
    except Exception as e:
        print(f"✗ db_helper 模块导入失败: {e}")
        return False

    try:
        from id_mapping_manager import IDMappingManager
        print("✓ id_mapping_manager 模块导入成功")
    except Exception as e:
        print(f"✗ id_mapping_manager 模块导入失败: {e}")
        return False

    try:
        from scene_split_processor import SceneSplitProcessor
        print("✓ scene_split_processor 模块导入成功")
    except Exception as e:
        print(f"✗ scene_split_processor 模块导入失败: {e}")
        return False

    print("\n✓ 所有模块导入测试通过")
    return True


def test_config_file():
    """测试配置文件"""
    print("\n" + "=" * 80)
    print("测试配置文件")
    print("=" * 80)

    config_file = 'db_config.yaml'
    if os.path.exists(config_file):
        print(f"✓ 配置文件 {config_file} 存在")

        try:
            from common.dbase.yaml_config_loader import load_db_configs
            configs = load_db_configs(config_file)

            if 'test_smartcarlife_db' in configs:
                print("✓ 测试环境配置存在")
                test_config = configs['test_smartcarlife_db']
                print(f"   Host: {test_config.get('host')}")
                print(f"   Port: {test_config.get('port')}")
                print(f"   DB: {test_config.get('db')}")
            else:
                print("✗ 测试环境配置不存在")

            if 'prod_smartcarlife_db' in configs:
                print("✓ 生产环境配置存在")
            else:
                print("⚠ 生产环境配置不存在(可选)")

        except Exception as e:
            print(f"✗ 配置文件加载失败: {e}")
            return False
    else:
        print(f"✗ 配置文件 {config_file} 不存在")
        return False

    print("\n✓ 配置文件测试通过")
    return True


def test_sql_generation():
    """测试SQL生成逻辑"""
    print("\n" + "=" * 80)
    print("测试SQL生成逻辑")
    print("=" * 80)

    # 模拟生成更新SQL
    table_name = 'dj_car_life_merchant_settle_account_rule'
    scene_type = 1
    base_scene_code = '110101'

    update_sql = f"""
    UPDATE {table_name}
    SET base_scene_code = '{base_scene_code}',
        updated_by = 'DATA_MIGRATION',
        updated_date = NOW()
    WHERE deleted_flag = 'N'
      AND scene_type = {scene_type}
      AND (base_scene_code IS NULL OR base_scene_code = '');
    """

    print("1. 更新SQL示例:")
    print(update_sql)

    # 模拟生成插入SQL
    insert_sql = f"""
    INSERT INTO {table_name}
    (source, merchant_id, scene_type, base_scene_code, enable_flag, submit_flag,
     created_by, created_date, updated_by, updated_date, deleted_flag)
    VALUES
    (1, 12345, 0, '110102', 'N', 'N',
     'DATA_MIGRATION', NOW(), 'DATA_MIGRATION', NOW(), 'N');
    """

    print("\n2. 插入SQL示例:")
    print(insert_sql)

    print("\n✓ SQL生成逻辑测试通过")
    return True


def main():
    """主测试函数"""
    print("\n" + "=" * 80)
    print("场景拆分脚本测试")
    print("=" * 80)

    # 执行各项测试
    test_scene_type_mapping()

    if not test_import_modules():
        print("\n✗ 模块导入测试失败,请检查代码")
        return 1

    if not test_config_file():
        print("\n⚠ 配置文件测试失败,请检查 db_config.yaml")
        print("  提示:请根据实际环境修改数据库配置")

    test_sql_generation()

    print("\n" + "=" * 80)
    print("测试完成")
    print("=" * 80)
    print("\n提示:")
    print("1. 请确保 db_config.yaml 中的数据库配置正确")
    print("2. 执行前请备份数据库")
    print("3. 建议先在测试环境执行: python main_scene_split.py --env test")
    print("=" * 80)

    return 0


if __name__ == '__main__':
    sys.exit(main())