from database import *

def test_black_list_expand():
    #  如下的参考数据来源于 原本的脚本里面的 BLACK_LIST_DEFAULT
    ref = """
    sdk/interface/include/aio/
	sdk/interface/include/ai/
	sdk/interface/src/ai/
	sdk/impl/ai/
	sdk/interface/src/aio
	sdk/impl/aio
	sdk/interface/include/ao/
	sdk/interface/src/ao/
	sdk/impl/ao/
	sdk/interface/include/alsa/
	sdk/interface/src/alsa/
	sdk/impl/alsa/
	sdk/interface/include/cipher/
	sdk/interface/src/cipher/
	sdk/impl/cipher/
	sdk/interface/src/common
	sdk/impl/common
	sdk/interface/include/cus3a/
	sdk/interface/src/cus3a/
	sdk/interface/include/debug
	sdk/interface/src/debug
	sdk/impl/debug
	sdk/interface/include/dummy/
	sdk/interface/src/dummy/
	sdk/impl/dummy/
	sdk/interface/include/disp
	sdk/interface/src/disp
	sdk/impl/disp
	sdk/interface/include/dpu/
	sdk/interface/src/dpu/
	sdk/impl/dpu/
	sdk/interface/include/dsp/
	sdk/interface/src/dsp/
	sdk/impl/dsp/
	sdk/interface/include/fb/
	sdk/interface/src/fb/
	sdk/impl/fb/
	sdk/interface/include/gfx/
	sdk/interface/src/gfx/
	sdk/impl/gfx/
	sdk/interface/include/hdmi/
	sdk/interface/src/hdmi/
	sdk/impl/hdmi/
	sdk/interface/include/hdmirx
	sdk/interface/src/hdmirx
	sdk/impl/hdmirx
	sdk/interface/include/hvp
	sdk/interface/src/hvp
	sdk/impl/hvp
	sdk/interface/include/ipu
	sdk/interface/src/ipu
	sdk/impl/ipu
	sdk/interface/include/iqserver/
	sdk/interface/src/iqserver/
	sdk/interface/include/ive
	sdk/interface/src/ive
	sdk/impl/ive
	sdk/interface/include/jpd/
	sdk/interface/src/jpd/
	sdk/impl/jpd
	sdk/interface/include/mipitx/
	sdk/interface/src/mipitx/
	sdk/impl/mipitx/
	sdk/interface/include/nir
	sdk/interface/src/nir
	sdk/impl/nir
	sdk/interface/include/panel/
	sdk/interface/src/panel/
	sdk/impl/panel/
	sdk/interface/include/pcie/
	sdk/interface/src/pcie/
	sdk/impl/pcie/
	sdk/interface/include/pspi
	sdk/interface/src/pspi
	sdk/impl/pspi
	sdk/interface/include/rgn
	sdk/interface/src/rgn
	sdk/impl/rgn
	sdk/interface/include/scl
	sdk/interface/src/scl
	sdk/impl/scl
	sdk/interface/include/sed/
	sdk/interface/src/sed/
	sdk/interface/include/sensor
	sdk/interface/src/sensor
	sdk/impl/sensor
	sdk/interface/include/shadow/
	sdk/interface/src/shadow/
	sdk/impl/shadow/
	sdk/impl/sys
	sdk/interface/include/vdec/
	sdk/interface/src/vdec/
	sdk/impl/vdec/
	sdk/interface/include/md/
	sdk/interface/include/od/
	sdk/interface/include/vg/
	sdk/interface/include/vdf/
	sdk/interface/src/vdf/
	sdk/interface/src/vcodec
	sdk/interface/include/vdisp/
	sdk/interface/src/vdisp/
	sdk/impl/vdisp/
	sdk/interface/include/wlan/
	sdk/interface/src/wlan/
	rtos/proj/sc/driver/sysdriver/dualos
	rtos/proj/sc/driver/sysdriver/otp
	rtos/proj/sc/driver/sysdriver/usb_gadget/udc/usb30
	rtos/proj/sc/system/rtos/freertos
	rtos/proj/sc_priv/arch/arm/cortex-a/v7_aarch32/context_switch
	rtos/proj/sc_priv/arch/arm/cortex-a/v7_aarch32/lh_monitor
	rtos/proj/sc_priv/driver/sysdriver/dualos
	rtos/proj/sc_priv/driver/sysdriver/otp
	rtos/proj/sc_priv/driver/sysdriver/usb_gadget/udc/usb30
	rtos/proj/sc_priv/system/rtos/freertos
	pm_rtos/proj/sc/driver/sysdriver/usb_gadget/udc/usb30
	pm_rtos/proj/sc/driver/camdriver/common
	pm_rtos/proj/sc/driver/camdriver/pm_aio
	pm_rtos/proj/sc/driver/camdriver/pm_idle
	pm_rtos/proj/sc/driver/camdriver/pm_jpe
	pm_rtos/proj/sc/driver/camdriver/pm_md
	pm_rtos/proj/sc/driver/camdriver/pm_radar
	pm_rtos/proj/sc/driver/camdriver/pm_usbpoc
	pm_rtos/proj/sc/driver/camdriver/pm_vif
    """
    BlackListDefault.check()
    ret = sorted(set(config_expand(BlackListDefault)))
    ret = [_.strip('/') for _ in ret]
    # with open("temp/test_expand.txt", "w") as f:
    #     for item in ret:
    #         item = item.strip('/')
    #         f.write(item + "\n")
    exp = [_.strip() for _ in ref.split()]
    exp = sorted(set(exp))
    exp = [_.strip('/') for _ in exp]
    # with open("temp/test_expand_ref.txt", "w") as f:
    #     for item in exp:
    #         f.write(item + "\n")
    assert ret == exp
    #print("BLACK_LIST_DEFAULT.expand: ", ret)

def test_black_list_internal_expand():
    BlackListInternal.check()
    ref = """
	sdk/interface/include/isp/
	sdk/interface/src/isp/
	sdk/impl/isp/
	sdk/interface/include/ispalgo/
	sdk/interface/src/ispalgo/
	sdk/interface/include/ldc/
	sdk/interface/src/ldc/
	sdk/impl/ldc/
	sdk/interface/include/venc
	sdk/interface/src/venc
	sdk/impl/venc
	sdk/interface/include/vif
	sdk/interface/src/vif
	sdk/impl/vif
	pm_rtos/proj/sc/driver/camdriver/pm_isp/
	pm_rtos/proj/sc/driver/camdriver/pm_ispalgo/
	pm_rtos/proj/sc/driver/camdriver/pm_radar_algo/
    """
    ret = sorted(set(config_expand(BlackListInternal)))
    ret = [_.strip('/') for _ in ret]
    # with open("temp/test_expand_internal.txt", "w") as f:
    #     for item in ret:
    #         f.write(item + "\n")
    exp = [_.strip() for _ in ref.split()]
    exp = sorted(set(exp))
    exp = [_.strip('/') for _ in exp]
    # with open("temp/test_expand_ref_internal.txt", "w") as f:
    #     for item in exp:
    #         f.write(item + "\n")
    assert ret == exp

def test_config_format():
    ReleaseOptions.SWBoardAlias = "iford"
    @AutoInstanceDecorator
    class TestAlias(BaseConfig):
        _config = [
            "preload_sample",
        ]
    ret = config_expand(TestAlias)
    assert ret == ["sdk/verify/sample_code/source/iford/preload_sample/rtos"]


def test_config_test_item():
    @AutoInstanceDecorator
    class TestAlias(BaseConfig):
        _config = [
            "preload_sample",
        ]
    TestAlias.update(["xxx/xxx/yyy", "zzz/zzz/www", "test_item"])
    ret = config_expand(TestAlias)
    # 使用list方式访问
    assert [_ for _ in ["xxx/xxx/yyy", "zzz/zzz/www", "test_item"] if _ in ret]
    # 使用dict方式访问
    assert TestAlias.get("xxx/xxx/yyy") == "xxx/xxx/yyy"
    assert TestAlias["zzz/zzz/www"] == "zzz/zzz/www"
    # 使用索引方式修改item 因为这是一个list类型的配置，所以修改了值其key也会跟着修改
    TestAlias["zzz/zzz/www"] = "zzz/zzz/www_new"
    assert TestAlias["zzz/zzz/www_new"] == "zzz/zzz/www_new"
    # 获取不存在的item
    assert TestAlias.get("zzz/zzz/www", 'default') == 'default'
    # 特殊访问方式。直接使用一个路径去访问，这样即使这个路径不存在，也不会抛出异常而是返回路径本身
    assert TestAlias["zzz/zzz/www"] == "zzz/zzz/www"
    # 通过索引的方式访问不存在的item 会抛出异常
    try:
        TestAlias["unexist_item"]
    except KeyError:
        assert True
    else:
        assert False
    # 使用点号方式访问
    assert TestAlias.test_item == "test_item"
    # 使用点号方式设置
    TestAlias.test_item = "test_item_new" # 会自动将test_item从_list中删除，并添加test_item_new
    assert TestAlias.test_item_new == "test_item_new"
    # 使用点号方式删除后，再次访问会抛出异常
    try:
        del TestAlias.test_item_new
        TestAlias.test_item_new
    except KeyError:
        assert True
    else:
        assert False
    # 删除后，使用get方式访问会返回默认值
    assert TestAlias.get("test_item", "default") == "default"
