Delimiter //
/**
 * Test of LOADER_GUARANTEED_NORMALIZE_MAP_CREATE_OR_UPDATE()
 * @test
 */
Drop Procedure If Exists TEST_LOADER_GUARANTEED_NORMALIZE_MAP_CREATE_OR_UPDATE//
Create Procedure TEST_LOADER_GUARANTEED_NORMALIZE_MAP_CREATE_OR_UPDATE(
)
Begin

    -- initial dataset
    Call LOADER_GUARANTEED_CLEAN();

    Call LOADER_GUARANTEED_PUSH(1, 'ACCOUNTS_ID_ACCOUNTS = 1, GUARANTEED_NAME = ''TEST'', GUARANTEED_AMOUNT = 66011.0 ', 1, NULL); -- Pass
    Call LOADER_GUARANTEED_PUSH(2, 'ACCOUNTS_ID_ACCOUNTS = 1, GUARANTEED_NAME = ''TEST'', GUARANTEED_AMOUNT = 66011.0, GUARANTEED_VAL_DATE = ''2014-12-10'' ', 2, NULL); -- Histo
    Call LOADER_GUARANTEED_PUSH(3, 'ACCOUNTS_ID_ACCOUNTS = 1, GUARANTEED_NAME = ''TEST'', GUARANTEED_AMOUNT = 54622.0 ', 3, NULL); -- New

    -- Business tables
    Call LOADER_TEST_MOCK_BUSINESS_CLEAN('GUARANTEED');
    Call LOADER_TEST_MOCK_BUSINESS_PUSH('GUARANTEED', 
        'ID_GUARANTEED = 1
        , ACCOUNTS_ID_ACCOUNTS = 1
        , GUARANTEED_NAME = ''TEST''
        , GUARANTEED_AMOUNT = 42042.0
        , GUARANTEED_VAL_DATE = ''2015-01-01'''
    );
    Call LOADER_TEST_MOCK_BUSINESS_PUSH('GUARANTEED', 
        'ID_GUARANTEED = 2
        , ACCOUNTS_ID_ACCOUNTS = 1
        , GUARANTEED_NAME = ''TEST''
        , GUARANTEED_AMOUNT = 24024.0
        , GUARANTEED_VAL_DATE = ''2015-02-01'''
    );
    Call LOADER_TEST_MOCK_BUSINESS_PUSH('GUARANTEED', 
        'ID_GUARANTEED = 3
        , GUARANTEED_NAME = ''TEST''
        , ACCOUNTS_ID_ACCOUNTS = 1
        , GUARANTEED_AMOUNT = 66011.0'
    );

    -- Tested procedure
    Call LOADER_GUARANTEED_NORMALIZE_MAP_CREATE_OR_UPDATE(1);

    -- Assertions
    Call utAssert_eq('When the amount is the same as the active one, no creation is mapped'
        , Null
        , (Select ID_GUARANTEED_NEW From LOADER_GUARANTEED Where ID_LOADER_GUARANTEED = 1)
    );
    Call utAssert_eq('When the amount is the same as the active one, no update is mapped'
        , Null
        , (Select ID_GUARANTEED From LOADER_GUARANTEED Where ID_LOADER_GUARANTEED = 1)
    );
    Call utAssert_eq('When the validity date has passed, creation is mapped'
        , 1
        , (Select ID_GUARANTEED_NEW From LOADER_GUARANTEED Where ID_LOADER_GUARANTEED = 2)
    );
    Call utAssert_eq('When the validity date has passed, no update is mapped'
        , Null
        , (Select ID_GUARANTEED From LOADER_GUARANTEED Where ID_LOADER_GUARANTEED = 2)
    );
    Call utAssert_eq('When the amount is different from the active one, creation is mapped'
        , 2
        , (Select ID_GUARANTEED_NEW From LOADER_GUARANTEED Where ID_LOADER_GUARANTEED = 3)
    );
    Call utAssert_eq('When the amount is different from the active one, update is mapped'
        , 3
        , (Select ID_GUARANTEED From LOADER_GUARANTEED Where ID_LOADER_GUARANTEED = 3)
    );

    -- Clean
    Call LOADER_TEST_MOCK_BUSINESS_CLEAN('GUARANTEED');
    Call LOADER_GUARANTEED_CLEAN();

End//
Delimiter ;