Delimiter //
/**
 * @test
 * Test For LOADER_ACCOUNTS_NORMALIZE_MAP_ACCOUNT_CREATE_OR_UPDATE()
 */
Drop Procedure IF EXISTS TEST_LOADER_ACCOUNTS_MAP_ACCOUNT_CREATE_OR_UPDATE//

Create Procedure TEST_LOADER_ACCOUNTS_MAP_ACCOUNT_CREATE_OR_UPDATE()
Begin

    -- initial dataset
    Call LOADER_TEST_MOCK_ACCOUNTS_CLEAN();
    Call LOADER_TEST_MOCK_INFO_CLEAN();
    Call LOADER_TEST_MOCK_BUSINESS_CLEAN('ENTITIES');
    Call LOADER_TEST_MOCK_BUSINESS_CLEAN('ACCOUNTS');

    Call LOADER_TEST_MOCK_ACCOUNTS_PUSH(1, 'ACCOUNTS_REF = ''ACCNT-REF-1''', 1, Null);
    Call LOADER_TEST_MOCK_ACCOUNTS_PUSH(2, 'ACCOUNTS_REF = ''ACCNT-REF-2''', 2, Null);
    Call LOADER_TEST_MOCK_ACCOUNTS_PUSH(3, 'ACCOUNTS_REF = ''ACCNT-REF-3''', 3, Null);

    Call LOADER_TEST_MOCK_INFO_PUSH('ID_ENTITIES', 1);

    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_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_ACCOUNTS_NORMALIZE_MAP_ACCOUNT_CREATE_OR_UPDATE(1);

    -- Assertions
    Call utAssert_eq('Existing accounts receive their id'
        , 1
        , (SELECT ID_ACCOUNTS FROM LOADER_ACCOUNTS 
            WHERE LOADER_STATUS_ID_LOADER_STATUS = 1)
    );
    Call utAssert_eq('New accounts receive a new id'
        , 2
        , (SELECT ID_ACCOUNTS_NEW FROM LOADER_ACCOUNTS 
            WHERE LOADER_STATUS_ID_LOADER_STATUS = 3)
    );

    -- clean
    Call LOADER_TEST_MOCK_BUSINESS_CLEAN('ACCOUNTS');
    Call LOADER_TEST_MOCK_BUSINESS_CLEAN('ENTITIES');
    Call LOADER_TEST_MOCK_INFO_CLEAN();
    Call LOADER_TEST_MOCK_ACCOUNTS_CLEAN();

End //
Delimiter ;
