Delimiter //
/**
 * @test
 * Test For LOADER_CONTACTS_NORMALIZE_MAP_ACCOUNT_REF_PROVIDED() 
 */
Drop Procedure IF EXISTS TEST_LOADER_CONTACTS_MAP_ACCOUNT_REF_PROVIDED//

Create Procedure TEST_LOADER_CONTACTS_MAP_ACCOUNT_REF_PROVIDED()
Begin

    -- initial dataset
    Call LOADER_TEST_MOCK_INFO_CLEAN();
    Call LOADER_TEST_MOCK_CONTACTS_CLEAN();
    Call LOADER_TEST_MOCK_BUSINESS_CLEAN('ACCOUNTS');

    Call LOADER_TEST_MOCK_INFO_PUSH('LOADER_TYPE', 'CONTACTS');
	Call LOADER_TEST_MOCK_INFO_PUSH('ID_ENTITIES', 1);
    Call LOADER_TEST_MOCK_CONTACTS_PUSH(1, 'ACCOUNTS_REF = ''ACCNT-REF-1''', 1, Null);
    Call LOADER_TEST_MOCK_CONTACTS_PUSH(2, 'ACCOUNTS_REF = ''beurk''', 2, Null);
    Call LOADER_TEST_MOCK_CONTACTS_PUSH(3, 'ACCOUNTS_REF = NULL', 3, Null);

    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 = ''ACCNT-REF-1''
            , ACCOUNTS_CREATED = NOW()
            , ACCOUNTS_LAST_EVENT_UPDATE = NOW()
            , ACCOUNTS_PROCESSUS_SCAN_TYPE = 1
            , ACCOUNTS_ACC_MNGT_LAST_LVL = 0'
    );

    -- Procedure
    CALL LOADER_CONTACTS_NORMALIZE_MAP_ACCOUNT_REF_PROVIDED(1);

    -- Assertions
    CALL utAssert_eq('Contacts with an account ref that exists, receive the corresponding account id'
        , 1
        , (SELECT ACCOUNTS_ID_ACCOUNTS FROM LOADER_CONTACTS WHERE LOADER_STATUS_ID_LOADER_STATUS = 1)
    );
    CALL utAssert_eq('Contacts with an account ref that does not exist, do not receive account id' 
        , NULL
        , (SELECT ACCOUNTS_ID_ACCOUNTS FROM LOADER_CONTACTS WHERE LOADER_STATUS_ID_LOADER_STATUS = 2) 
    );
    Call utAssert_eq('Contacts with an account ref that does not exist, have an error' 
        , 1
        , (SELECT LOADER_STATUS_ERRCOUNT FROM LOADER_STATUS WHERE ID_LOADER_STATUS = 2) 
    );
    CALL utAssert_eq('Contacts without account ref do not receive account id' 
        , NULL
        , (SELECT ACCOUNTS_ID_ACCOUNTS FROM LOADER_CONTACTS WHERE LOADER_STATUS_ID_LOADER_STATUS = 3) 
    );

    -- clean
    Call LOADER_TEST_MOCK_BUSINESS_CLEAN('ACCOUNTS');
    Call LOADER_TEST_MOCK_CONTACTS_CLEAN();
    Call LOADER_TEST_MOCK_INFO_CLEAN();
    
End //
Delimiter ;
