Delimiter //
/**
 * @test
 * Test For LOADER_ACCOUNTS_NORMALIZE_MAP_DATES_PROVIDED_OR_NOT()
 */
Drop Procedure IF EXISTS TEST_LOADER_ACCOUNTS_MAP_DATES_PROVIDED_OR_NOT//
Create Procedure TEST_LOADER_ACCOUNTS_MAP_DATES_PROVIDED_OR_NOT()
Begin

    -- initial dataset
    Call LOADER_TEST_MOCK_ACCOUNTS_CLEAN();
    Call LOADER_TEST_MOCK_ACCOUNTS_PUSH(1, 'ID_ACCOUNTS_NEW = 1, ACCOUNTS_CREATED = ''2001-01-01''', 1, Null);
    Call LOADER_TEST_MOCK_ACCOUNTS_PUSH(2, 'ID_ACCOUNTS = 1, ACCOUNTS_LAST_EVENT_UPDATE = ''2002-02-02''', 2, Null);
    Call LOADER_TEST_MOCK_ACCOUNTS_PUSH(3, 'ID_ACCOUNTS = 2, ACCOUNTS_CREATED = NULL', 3, Null);
    Call LOADER_TEST_MOCK_ACCOUNTS_PUSH(4, 'ID_ACCOUNTS_NEW = 2, ACCOUNTS_LAST_EVENT_UPDATE = NULL', 4, Null);

    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 = Null
            , ACCOUNTS_REF = ''ACCNT-REF-1''
            , ACCOUNTS_CREATED = ''2009-09-09''
            , ACCOUNTS_LAST_EVENT_UPDATE = ''2010-10-10''
            , ACCOUNTS_PROCESSUS_SCAN_TYPE = 1
            , ACCOUNTS_ACC_MNGT_LAST_LVL = 0'
    );
    Call LOADER_TEST_MOCK_BUSINESS_PUSH('ACCOUNTS', 
            'ID_ACCOUNTS = 2
            , 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 = Null
            , ACCOUNTS_REF = ''ACCNT-REF-2''
            , ACCOUNTS_CREATED = ''2011-11-11''
            , ACCOUNTS_LAST_EVENT_UPDATE = ''2012-12-12''
            , ACCOUNTS_PROCESSUS_SCAN_TYPE = 1
            , ACCOUNTS_ACC_MNGT_LAST_LVL = 0'
    );

    -- Procedure
    Call LOADER_ACCOUNTS_NORMALIZE_MAP_DATES_PROVIDED_OR_NOT(1);

    -- Assertions
    Call utAssert_eq('New accounts with a creation date receive this date'
        , '2001-01-01'
        , (SELECT ACCOUNTS_CREATED FROM LOADER_ACCOUNTS 
            WHERE LOADER_STATUS_ID_LOADER_STATUS = 1)
    );
    Call utAssert_eq('Updated accounts with a last event update date receive this date'
        , '2002-02-02'
        , (SELECT ACCOUNTS_LAST_EVENT_UPDATE FROM LOADER_ACCOUNTS 
            WHERE LOADER_STATUS_ID_LOADER_STATUS = 2)
    );
    Call utAssert_eq('Updated accounts with no creation date keep their date'
        , '2011-11-11'
        , (SELECT ACCOUNTS_CREATED FROM LOADER_ACCOUNTS 
            WHERE LOADER_STATUS_ID_LOADER_STATUS = 3)
    );
    Call utAssert_eq('New accounts with no last event update date receive the current date'
        , (SELECT CURDATE())
        , (SELECT ACCOUNTS_LAST_EVENT_UPDATE FROM LOADER_ACCOUNTS 
            WHERE LOADER_STATUS_ID_LOADER_STATUS = 4)
    );

    -- clean
    Call LOADER_TEST_MOCK_BUSINESS_CLEAN('ACCOUNTS');
    Call LOADER_TEST_MOCK_ACCOUNTS_CLEAN();
    
End //
Delimiter ;
