Delimiter //
/**
 * Test of TEST_TEST_LOADER_MATCHINGCODE_CHECK_ENTRY_EXISTS
 * @test
 */
Drop Procedure If Exists TEST_LOADER_MATCHINGCODE_CHECK_ENTRY_EXISTS//
Create Procedure TEST_LOADER_MATCHINGCODE_CHECK_ENTRY_EXISTS(
)
Begin

    -- Context
    call LOADER_TEST_MOCK_MATCHINGCODES_CLEAN();
    call LOADER_TEST_MOCK_MATCHINGCODES_PUSH(1, 'ACCOUNTS_REF = ''ACC1'', ENTRIES_ACC_EXT_REF = ''ENT1''  ', 1, Null);
    call LOADER_TEST_MOCK_MATCHINGCODES_PUSH(2, 'ACCOUNTS_REF = ''ACC2'', ENTRIES_ACC_EXT_REF = ''ENT1''  ', 2, Null);
    call LOADER_TEST_MOCK_MATCHINGCODES_PUSH(3, 'ACCOUNTS_REF = ''ACC1'', ENTRIES_ACC_EXT_REF = ''ENT2''  ', 3, 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'
    );
    Call LOADER_TEST_MOCK_BUSINESS_CLEAN('ENTRIES_has_ACCOUNTS');
    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 = ''ENT1'''
    );
    

    -- Tested procedure
	Call LOADER_MATCHINGCODE_NORMALIZE_MAP_ENTRY(1);
    Call LOADER_MATCHINGCODE_NORMALIZE_CHECK_ENTRY_EXISTS(1);

    -- Assertions
    Call utAssert_eq('During a matching code import, When a valid entry is provided, it doesn''t receive any error'
        , 0
        , (Select LOADER_STATUS_ERRCOUNT From LOADER_STATUS Where ID_LOADER_STATUS = 1)
    );
    Call utAssert_eq('During a matching code import, When a invalid entry ref is provided, it receives an error'
        , 1
        , (Select LOADER_STATUS_ERRCOUNT From LOADER_STATUS Where ID_LOADER_STATUS = 3)
    );
    Call utAssert_eq('During a matching code import, When a invalid account ref is provided, it receives an error'
        , 1
        , (Select LOADER_STATUS_ERRCOUNT From LOADER_STATUS Where ID_LOADER_STATUS = 2)
    );

    -- 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_MATCHINGCODES_CLEAN();

End//
Delimiter ;