@isTest (SeeAllData=true)
private class PowerBICustomSettingTest {

    private static Map<String, Schema.SObjectType> allTableDesc;

    private static testMethod void CustomSettingCheck() {
        list<string> Errormess = new list<string>();
        Schema.SObjectType tablefrom = null;
        Schema.SObjectType tableType = null;
        boolean hasLookUpId = false;
        List<SObject> targeList = new List<SObject>();
        List<PowerBISyncDefine__c> pbDFList;

        if (allTableDesc == null){
            allTableDesc = Schema.getGlobalDescribe();
        }

        pbDFList = [SELECT Name, To_Table__c
                    FROM PowerBISyncDefine__c
                    WHERE Delete_Flag__c = false];

        if (pbDFList.size() == 0) {
            return;
        }

        for (PowerBISyncDefine__c pbDF: pbDFList){

            tablefrom = allTableDesc.get(pbDF.Name);
            if (tablefrom == null){
                throw new ControllerUtil.myException('名为 ' + pbDF.Name + ' 的表不存在!');
            }

            tableType = allTableDesc.get(pbDF.To_Table__c);
            if (tableType == null){
                throw new ControllerUtil.myException('没有设定 ' + pbDF.Name + '相对应的POWERBI表!');
            }

            if (!tableType.getDescribe().fields.getMap().keySet().contains('powerbi_source_id__c')) {
                throw new ControllerUtil.myException('在 ' + pbDF.Name + '内设定的POWERBI表名称错误!');
            }

            List<Schema.SObjectField> fieldList = tableType.getDescribe().fields.getMap().values();
            for (SObjectField fsm : fieldList) {
                if(!fsm.getDescribe().isCalculated() && fsm.getDescribe().isCustom()
                    && fsm.getDescribe().getName() != 'PowerBI_Source_Id__c'
                    && fsm.getDescribe().getName() != 'PowerBI_Source_LookUp_Id__c' ) {
                    if (!tablefrom.getDescribe().fields.getMap().keySet().contains(fsm.getDescribe().getName().tolowercase())) {
                        throw new ControllerUtil.myException('在 ' + pbDF.Name + '内设定的POWERBI表中,字段' + fsm.getDescribe().getName() + '设定错误!');
                    }
                }

            }
        }
    }



}