Delimiter //
/**
 * Test of LOADER_MATCHINGCODE_NORMALIZE_MAP_MATCHINGCODE_UPDATE_OR_CREATE
 * @test
 */
Drop Procedure If Exists TEST_LOADER_MATCHINGCODE_NORMALIZE_MAP_MATCHINGCODE_UC//
Create Procedure TEST_LOADER_MATCHINGCODE_NORMALIZE_MAP_MATCHINGCODE_UC(
)
Begin

    -- Context
    call LOADER_TEST_MOCK_MATCHINGCODES_CLEAN();
    call LOADER_TEST_MOCK_MATCHINGCODES_PUSH(1, 'ID_ENTRIES = 1, MATCHINGCODES_CODE = ''AAA'', MATCHINGCODES_DATE = ''2012-06-02'' , ACCOUNTS_REF = '''', ENTRIES_ACC_EXT_REF = '''' ', 1, Null);
    call LOADER_TEST_MOCK_MATCHINGCODES_PUSH(2, 'ID_ENTRIES = 4, MATCHINGCODES_CODE = ''CCC'', MATCHINGCODES_DATE = ''2012-05-01'' , ACCOUNTS_REF = '''', ENTRIES_ACC_EXT_REF = '''' ', 2, Null);

    Call LOADER_TEST_MOCK_BUSINESS_CLEAN('MATCHINGCODES');
    Call LOADER_TEST_MOCK_BUSINESS_PUSH('MATCHINGCODES', 'ID_MATCHINGCODES = 1, MATCHINGCODES_CODE = ''AAA'', MATCHINGCODES_DATE = ''2012-07-03''' );
    Call LOADER_TEST_MOCK_BUSINESS_PUSH('MATCHINGCODES', 'ID_MATCHINGCODES = 2, MATCHINGCODES_CODE = ''BBB'', MATCHINGCODES_DATE = ''2012-07-03''' );

    Call LOADER_TEST_MOCK_BUSINESS_CLEAN('ENTRIES_MATCHCOD');
    Call LOADER_TEST_MOCK_BUSINESS_PUSH('ENTRIES_MATCHCOD', 'ID_ENTRIES_MATCHCOD = 1, ENTRIES_ID_ENTRIES = 1, MATCHCOD_ID_MATCHCOD = 1' );
    Call LOADER_TEST_MOCK_BUSINESS_PUSH('ENTRIES_MATCHCOD', 'ID_ENTRIES_MATCHCOD = 2, ENTRIES_ID_ENTRIES = 2, MATCHCOD_ID_MATCHCOD = 1' );
    Call LOADER_TEST_MOCK_BUSINESS_PUSH('ENTRIES_MATCHCOD', 'ID_ENTRIES_MATCHCOD = 3, ENTRIES_ID_ENTRIES = 3, MATCHCOD_ID_MATCHCOD = 2' );
    
    -- Tested procedure
    Call  LOADER_MATCHINGCODE_NORMALIZE_MAP_MATCHINGCODE_UPDATE_OR_CREATE(1);

    -- Assertions
    Call utAssert_eq('Matching code : we never update a matching code, we delete it then add a new - step delete'
        , 1
        , (Select Count(*) From LOADER_MATCHINGCODES_MATCHINGCODES_TOREMOVE)
    );
    Call utAssert_eq('Matching code : we never update a matching code, we delete it then add a new - step add'
        , 'AAA'
        , (Select MATCHINGCODES_CODE From LOADER_MATCHINGCODES_MATCHINGCODES Where ID_LOADER_MATCHINGCODES_MATCHINGCODES = 1)
    );
    Call utAssert_eq('Matching code is added in the database'
        , 2
        , (Select Count(*) From LOADER_MATCHINGCODES_MATCHINGCODES)
    );
    Call utAssert_eq('Matching code : we never update a relation beetween matching code and an entry, we delete it then add a new - step add'
        , 2
        , (Select Count(*) From LOADER_MATCHINGCODES_ENTRIES_TOREMOVE)
    );
    Call utAssert_eq('When existent entry receives a matching code, the matching is added'
        , 1
        , (Select ID_LOADER_MATCHINGCODES_MATCHINGCODES From LOADER_MATCHINGCODES_MATCHINGCODES Where ID_LOADER_MATCHINGCODES_MATCHINGCODES = 1 )
    );

    -- Clean
    Call LOADER_TEST_MOCK_BUSINESS_CLEAN('ENTRIES_MATCHCOD');
    Call LOADER_TEST_MOCK_BUSINESS_CLEAN('MATCHINGCODES');
    Call LOADER_TEST_MOCK_MATCHINGCODES_CLEAN();

End//
Delimiter ;