Delimiter //
/**
 * @test
 * Test For LOADER_ENTRIES_NORMALIZE_MAP_ENTRY_CREATE_OR_UPDATE()
 */
Drop Procedure IF EXISTS TEST_LOADER_ENTRIES_MAP_ENTRY_CREATE_OR_UPDATE//
Create Procedure TEST_LOADER_ENTRIES_MAP_ENTRY_CREATE_OR_UPDATE()
Begin

    -- initial dataset
    Call LOADER_TEST_MOCK_ENTRIES_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_BUSINESS_CLEAN('ENTRIES_has_ACCOUNTS');

    Call LOADER_TEST_MOCK_ENTRIES_PUSH(1, 'ACCOUNTS_REF = ''ACCNT-REF-1'', ACCOUNTS_ID_ACCOUNTS = 1, ENTRIES_ACC_EXT_REF = ''ENTACCEXT-REF-1''', 1, Null);
    Call LOADER_TEST_MOCK_ENTRIES_PUSH(2, 'ACCOUNTS_REF = ''ACCNT-REF-1'', ACCOUNTS_ID_ACCOUNTS = 1, ENTRIES_ACC_EXT_REF = ''ENTACCEXT-REF-X''', 2, Null);
    Call LOADER_TEST_MOCK_ENTRIES_PUSH(3, 'ACCOUNTS_REF = ''ACCNT-REF-1'', ACCOUNTS_ID_ACCOUNTS = 1, ENTRIES_ACC_EXT_REF = ''ENTACCEXT-REF-Y''', 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'
    );
    Call LOADER_TEST_MOCK_BUSINESS_PUSH('ENTRIES_has_ACCOUNTS', 
            'ID_ENTRIES_has_ACCOUNTS = 1
            , ENTRIES_ID_ENTRIES = 1
            , ACCOUNTS_ID_ACCOUNTS = 1
            , ENTRIES_ACC_EXT_REF = ''ENTACCEXT-REF-1'''
    );

    -- Procedure
    Call LOADER_ENTRIES_NORMALIZE_MAP_ENTRY_CREATE_OR_UPDATE(1);

    -- Assertions
    Call utAssert_eq('Existing entries receive their id'
        , 1
        , (SELECT ID_ENTRIES FROM LOADER_ENTRIES 
            WHERE LOADER_STATUS_ID_LOADER_STATUS = 1)
    );
    Call utAssert_eq('New entries receive a new id'
        , 2
        , (SELECT ID_ENTRIES_NEW FROM LOADER_ENTRIES 
            WHERE LOADER_STATUS_ID_LOADER_STATUS = 3)
    );
    Call utAssert_eq('New entries have a relation with the account'
        , 2
        , (SELECT ID_ENTRIES_has_ACCOUNTS FROM LOADER_ENTRIES_ENTRIES_has_ACCOUNTS 
            WHERE LOADER_STATUS_ID_LOADER_STATUS = 3)
    );

    -- clean
    Call LOADER_TEST_MOCK_BUSINESS_CLEAN('ENTRIES_has_ACCOUNTS');
    Call LOADER_TEST_MOCK_BUSINESS_CLEAN('ACCOUNTS');
    Call LOADER_TEST_MOCK_BUSINESS_CLEAN('ENTITIES');
    Call LOADER_TEST_MOCK_INFO_CLEAN();
    Call LOADER_TEST_MOCK_ENTRIES_CLEAN();

End //
Delimiter ;
