Delimiter //
/**
 * @test
 * Test For 
 *   - LOADER_CONTACTS_PHONES_UPDATE() 
 *   - LOADER_CONTACTS_PHONES_INSERT() 
 */
Drop Procedure if exists TEST_LOADER_CONTACTS_COPY_CONTACTS_PHONES //
Create Procedure TEST_LOADER_CONTACTS_COPY_CONTACTS_PHONES ()
Begin
    
    -- initial dataset
    Call LOADER_TEST_MOCK_INFO_CLEAN();
    Call LOADER_TEST_MOCK_CONTACTS_PHONES_CLEAN();
    Call LOADER_TEST_MOCK_CONTACTS_CLEAN();
-- 
    Call LOADER_TEST_MOCK_BUSINESS_START('CONTACTS_PHONES');
    Call LOADER_TEST_MOCK_BUSINESS_CLEAN('CONTACTS');
    Call LOADER_TEST_MOCK_BUSINESS_CLEAN('ACCOUNTS');    
    Call LOADER_TEST_MOCK_BUSINESS_CLEAN('ENTITIES');

    CALL LOADER_TEST_MOCK_CONTACTS_PUSH(1, '`ACCOUNTS_ID_ACCOUNTS` = 1, `ID_CONTACTS` = 1, `ID_CONTACTS_NEW` = null, `IS_MAIN_CONTACT` = 0, `DF_LANGUAGES_ID_DF_LANGUAGES` = 1, `DF_SAND_STATE_ID_DF_SAND_STATE` = 1', 1, NULL);
    CALL LOADER_TEST_MOCK_CONTACTS_PUSH(2, '`ACCOUNTS_ID_ACCOUNTS` = 1, `ID_CONTACTS` = Null, `ID_CONTACTS_NEW` = 10, `IS_MAIN_CONTACT` = 0, `DF_LANGUAGES_ID_DF_LANGUAGES` = 1, `DF_SAND_STATE_ID_DF_SAND_STATE` = 1', 2, NULL);
    CALL LOADER_TEST_MOCK_CONTACTS_PUSH(3, '`ACCOUNTS_ID_ACCOUNTS` = 1, `ID_CONTACTS` = Null, `ID_CONTACTS_NEW` = 2, `IS_MAIN_CONTACT` = 0, `DF_LANGUAGES_ID_DF_LANGUAGES` = 1, `DF_SAND_STATE_ID_DF_SAND_STATE` = 1', 3, NULL);

    Call LOADER_TEST_MOCK_PUSH('CONTACTS_PHONES', 2
            , '`DF_PHONE_TYP_ID_DF_PHONE_TYP` = 1
            , `DF_CHECK_STAT_ID_DF_CHECK_STAT` = 2
            , `CONTACTS_PHONE_NUMBER` = ''01-02-03-04-05''
            , `CONTACTS_PHONE_NUMERIC_ONLY` = ''0102030405''
            , `CONTACTS_PHONE_PREFERENCE` = 0
            , `CONTACTS_PHONES_TRASHED` = 0
            , `CONTACTS_PHONES_LAST_UPDATE` = ''2012-01-08 08:00:00'''
            ,2, null
        );
    Call LOADER_TEST_MOCK_PUSH('CONTACTS_PHONES', 3
            , '`DF_PHONE_TYP_ID_DF_PHONE_TYP` = 1
            , `DF_CHECK_STAT_ID_DF_CHECK_STAT` = 2
            , `CONTACTS_PHONE_NUMBER` = ''01-01-01-01-01''
            , `CONTACTS_PHONE_NUMERIC_ONLY` = ''0101010101''
            , `CONTACTS_PHONE_PREFERENCE` = 0
            , `CONTACTS_PHONES_TRASHED` = 0
            , `CONTACTS_PHONES_LAST_UPDATE` = ''2012-01-08 08:00:00'''
            ,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('CONTACTS', 
            'ID_CONTACTS = 1
            , ACCOUNTS_ID_ACCOUNTS = 1
            , DF_LANGUAGES_ID_DF_LANGUAGE = 1
            , DF_SAND_STATE_ID_DF_SAND_STATE = 2
            , CONTACTS_REF = ''CNTCT-REF-1''
            , CONTACTS_TRASHED = 0
            , CONTACTS_LAST_UPDATE = NOW()'
    );
    Call LOADER_TEST_MOCK_BUSINESS_PUSH('CONTACTS', 
            'ID_CONTACTS = 11
            , ACCOUNTS_ID_ACCOUNTS = 1
            , DF_LANGUAGES_ID_DF_LANGUAGE = 1
            , DF_SAND_STATE_ID_DF_SAND_STATE = 2
            , CONTACTS_REF = ''CNTCT-REF-2''
            , CONTACTS_TRASHED = 0
            , CONTACTS_LAST_UPDATE = NOW()'
    );
    Call LOADER_TEST_MOCK_BUSINESS_PUSH('CONTACTS', 
            'ID_CONTACTS = 3
            , ACCOUNTS_ID_ACCOUNTS = 1
            , DF_LANGUAGES_ID_DF_LANGUAGE = 1
            , DF_SAND_STATE_ID_DF_SAND_STATE = 2
            , CONTACTS_REF = ''CNTCT-REF-3''
            , CONTACTS_TRASHED = 0
            , CONTACTS_LAST_UPDATE = NOW()'
    );


    Call LOADER_TEST_MOCK_BUSINESS_PUSH('CONTACTS_PHONES'
            , '`CONTACTS_ID_CONTACTS` = 1
            , `DF_PHONE_TYP_ID_DF_PHONE_TYP` = 1
            , `DF_CHECK_STAT_ID_DF_CHECK_STAT` = 2
            , `CONTACTS_PHONE_NUMBER` = ''01-23-45-67-89''
            , `CONTACTS_PHONE_NUMERIC_ONLY` = ''0123456789''
            , `CONTACTS_PHONE_PREFERENCE` = 0
            , `CONTACTS_PHONES_TRASHED` = 0
            , `CONTACTS_PHONES_LAST_UPDATE` = ''2009-02-22 14:26:10'''
        );

    call LOADER_CONTACTS_PHONES_UPDATE(1);
    call LOADER_CONTACTS_PHONES_INSERT(1, 1);

    Call utAssert_eq('New contacts phones are Added to existants Contacts phones'
            , 3
            , (Select count(*) From CONTACTS_PHONES)
        );
    Call utAssert_eq('Not null Fields on existing  contacts phones Are updated'
            , '01-02-03-04-05'
            , (Select CONTACTS_PHONE_NUMBER From CONTACTS_PHONES Where CONTACTS_ID_CONTACTS = 11)
        );


    -- clean
    Call LOADER_TEST_MOCK_CONTACTS_PHONES_CLEAN();
    Call LOADER_TEST_MOCK_CONTACTS_CLEAN();
    Call LOADER_TEST_MOCK_INFO_CLEAN();

    Call LOADER_TEST_MOCK_BUSINESS_CLEAN('CONTACTS_PHONES');
    Call LOADER_TEST_MOCK_BUSINESS_CLEAN('CONTACTS');
    Call LOADER_TEST_MOCK_BUSINESS_CLEAN('ACCOUNTS');
    Call LOADER_TEST_MOCK_BUSINESS_CLEAN('ENTITIES');
    

End //

Delimiter ;