Delimiter //
/**
 * @test
 * Test For 
 *   - LOADER_CONTACTS_INDIVIDUAL_UPDATE() 
 *   - LOADER_CONTACTS_INDIVIDUAL_INSERT() 
 */
Drop Procedure if exists TEST_LOADER_CONTACTS_COPY_CONTACTS_INDIVIDUAL //
Create Procedure TEST_LOADER_CONTACTS_COPY_CONTACTS_INDIVIDUAL ()
Begin
    
    -- initial dataset
    Call LOADER_TEST_MOCK_INFO_CLEAN();
    Call LOADER_TEST_MOCK_CONTACTS_CLEAN();
-- 
    Call LOADER_TEST_MOCK_BUSINESS_CLEAN('CONTACTS_INDIVIDUAL');
    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, `CONTACTS_REF` = ''CNTCT-REF-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, `CONTACTS_INDIVIDUAL_FIRST_NAME` = ''new value''', 1, NULL);
    CALL LOADER_TEST_MOCK_CONTACTS_PUSH(2, '`ACCOUNTS_ID_ACCOUNTS` = 1, `CONTACTS_REF` = ''CNTCT-REF-2'', `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, `CONTACTS_REF` = ''CNTCT-REF-3'', `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_CONTACTS_PUSH(1, '`ACCOUNTS_REF` = Null, `ACCOUNTS_ID_ACCOUNTS` = Null, `CONTACTS_REF` = Null, `ID_CONTACTS` = Null, `ID_CONTACTS_NEW` = Null, `IS_MAIN_CONTACT` = 0, `DF_COUNTRY_LABEL` = Null, `DF_COUNTRY_SHORT_CODE` = Null, `DF_COUNTRY_LONG_CODE` = Null, `DF_COUNTRY_ID_DF_COUNTRY` = Null, `CONTACTS_ADDRESSES_LINE_1` = Null, `CONTACTS_ADDRESSES_LINE_2` = Null, `CONTACTS_ADDRESSES_LINE_3` = Null, `CONTACTS_ADDRESSES_LINE_4` = Null, `CONTACTS_ADDRESSES_ZIP_CODE` = Null, `CONTACTS_ADDRESSES_CITY` = Null, `DF_CHECK_STAT_ID_DF_CHECK_STAT` = Null, `DF_TITLES_SHORT_TITLE` = Null, `DF_TITLES_FULL_TITLE` = Null, `DF_TITLES_ID_DF_TITLES` = Null, `CONTACTS_INDIVIDUAL_FIRST_NAME` = Null, `CONTACTS_INDIVIDUAL_LAST_NAME` = Null, `CONTACTS_INDIVIDUAL_BIRTH_NAME` = Null, `CONTACTS_PHONE1` = Null, `CONTACTS_PHONE2` = Null, `CONTACTS_PHONE3` = Null, `CONTACTS_EMAILS_EMAIL` = Null, `CONTACTS_BUSINESS_COMPANY_NAME` = Null, `CONTACTS_BUSINESS_BRAND_NAME` = Null, `CONTACTS_BUSINESS_SIRET` = Null, `DF_LANGUAGES_CODE` = Null, `DF_LANGUAGES_LABEL` = Null, `DF_LANGUAGES_ID_DF_LANGUAGES` = Null, `DF_FUNCTIONS_SHORT_LABEL` = Null, `DF_FUNCTIONS_ID_DF_FUNCTIONS` = Null, `DF_SAND_STATE_ID_DF_SAND_STATE` = Null, `CONTACTS_TRASHED` = Null, `CONTACTS_LAST_UPDATE` = Null',1)

    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_INDIVIDUAL', 
        '
        `ID_CONTACTS_INDIVIDUAL` = 1, 
        `CONTACTS_ID_CONTACTS` = 1, 
        `DF_TITLES_ID_DF_TITLES` = Null, 
        `DF_FUNCTIONS_ID_DF_FUNCTIONS` = Null, 
        `CONTACTS_INDIVIDUAL_FIRST_NAME` = ''initial value'', 
        `CONTACTS_INDIVIDUAL_LAST_NAME` = ''not changed'', 
        `CONTACTS_INDIVIDUAL_BIRTH_NAME` = Null, 
        `CONTACTS_INDIVIDUAL_BIRTH_DAY` = Null, 
        `CONTACTS_INDIVIDUAL_LAST_UPDAT` = ''2012-01-02 08:10:20'''
    );

    call LOADER_CONTACTS_INDIVIDUAL_UPDATE(1);
    call LOADER_CONTACTS_INDIVIDUAL_INSERT(1, 1);

    Call utAssert_eq('New individual contacts are Added to existants individual Contacts'
            , 3
            , (Select count(*) From CONTACTS_INDIVIDUAL)
        );

    Call utAssert_eq('Existing individual contacts Are preserved'
            , 'not changed'
            , (Select CONTACTS_INDIVIDUAL_LAST_NAME From CONTACTS_INDIVIDUAL Where ID_CONTACTS_INDIVIDUAL = 1)
        );

    Call utAssert_eq('Not null Fields on existing individual Accounts are updated'
            , 'new value'
            , (Select CONTACTS_INDIVIDUAL_FIRST_NAME From CONTACTS_INDIVIDUAL Where ID_CONTACTS_INDIVIDUAL = 1)
        );

-- --     Call utAssert_eq('Null Fields on existing Accounts are unchanged'
-- --             , 1
-- --             , (Select DF_CURRENCIES_ID_DF_CURRENCIES From ACCOUNTS Where ENTITIES_ID_ENTITIES = 1 And ACCOUNTS_REF = 'ACCNT-REF-1')
-- --         );
-- -- 
--     Call utAssert_eq('new Contacts ID are expected values (1)'
--             , 11
--             , (Select ID_CONTACTS From CONTACTS Where ACCOUNTS_ID_ACCOUNTS = 1 And CONTACTS_REF = 'CNTCT-REF-2')
--         );
--     Call utAssert_eq('new Accounts ID are expected values (2)'
--             , 3
--             , (Select ID_CONTACTS From CONTACTS Where ACCOUNTS_ID_ACCOUNTS = 1 And CONTACTS_REF = 'CNTCT-REF-3')
--         );


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

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

End //

Delimiter ;