Delimiter //
/**
 * Test of LOADER_SUMMARIES_NORMALIZE_MAP_ACCOUNTS
 * @test
 */
Drop Procedure If Exists TEST_LOADER_SUMMARIES_NORMALIZE_MAP_ACCOUNTS//
Create Procedure TEST_LOADER_SUMMARIES_NORMALIZE_MAP_ACCOUNTS(
)
Begin

    -- initial dataset
    CALL LOADER_SUMMARIES_CLEAN();

    CALL LOADER_SUMMARIES_PUSH(1, 'ACCOUNTS_REF = ''ACC1'' ', 1, NULL);
    CALL LOADER_SUMMARIES_PUSH(2, 'ACCOUNTS_REF = ''ACC-NOTHING'' ', 2, NULL);

    -- Business tables
    Call LOADER_TEST_MOCK_INFO_PUSH('ID_ENTITIES', 1);

    call LOADER_TEST_MOCK_BUSINESS_CLEAN('ENTITIES');
    Call LOADER_TEST_MOCK_BUSINESS_PUSH('ENTITIES', 
            'ID_ENTITIES = 1
            , DF_CURRENCIES_ID_DF_CURR = 1
            , ADMIN_ID_ADMIN = 1
            , CONT_BUSINESS_ID_CONT_BUSINESS = 1
            , ENTITIES_LOCK_STATE = 0
            , ENTITIES_CODE = 1'
    );
    call LOADER_TEST_MOCK_BUSINESS_CLEAN('ACCOUNTS');
    Call LOADER_TEST_MOCK_BUSINESS_PUSH('ACCOUNTS', 
            'ID_ACCOUNTS = 1
            , ENTITIES_ID_ENTITIES = 1
            , DF_CURRENCIES_ID_DF_CURRENCIES = 1
            , DF_ACC_CATEG_ID_DF_ACC_CATEG = 3
            , DF_PRIORITY_ID_DF_PRIORITY = 2
            , DF_SAND_STATE_ID_DF_SAND_STATE = 2
            , ACCOUNTS_TYPE = 1
            , CONTACTS_ID_CONTACTS = 1
            , ACCOUNTS_REF = ''ACC1''
            , ACCOUNTS_CREATED = NOW()
            , ACCOUNTS_LAST_EVENT_UPDATE = NOW()
            , ACCOUNTS_PROCESSUS_SCAN_TYPE = 1
            , ACCOUNTS_ACC_MNGT_LAST_LVL = 0'
    );
    

    -- Tested procedure
    Call LOADER_SUMMARIES_NORMALIZE_MAP_ACCOUNTS(1);

    -- Assertions
    Call utAssert_eq('During load summaries, when a valid account is provided, row receives its id'
        , 1
        , (Select ACCOUNTS_ID_ACCOUNTS From LOADER_SUMMARIES Where ID_LOADER_SUMMARIES = 1)
    );
    Call utAssert_eq('During load summaries, When an invalid account ref is provided, row receive no id'
        , Null
        , (Select ACCOUNTS_ID_ACCOUNTS From LOADER_SUMMARIES Where ID_LOADER_SUMMARIES = 2)
    );

	-- Assertions
    CALL utAssert_eq('data with empty invalid accouts ref are flagged' 
        , 1
        , (SELECT LOADER_STATUS_ERRCOUNT FROM LOADER_STATUS WHERE ID_LOADER_STATUS = 2) 
    );
    CALL utAssert_eq('data with not valid accouts ref are not flagged' 
        , 0
        , (SELECT LOADER_STATUS_ERRCOUNT FROM LOADER_STATUS WHERE ID_LOADER_STATUS = 1) 
    );



    -- Clean
    Call LOADER_TEST_MOCK_BUSINESS_CLEAN('ACCOUNTS');
    Call LOADER_TEST_MOCK_BUSINESS_CLEAN('ENTITIES');
    Call LOADER_TEST_MOCK_INFO_CLEAN();
    Call LOADER_SUMMARIES_CLEAN();

End//
Delimiter ;