Delimiter //
/**
 * @test
 * Test For LOADER_ACCOUNTS_COPY() 
 */
Drop Procedure if exists TEST_LOADER_ACCOUNTS_COPY_ACCOUNTS //
Create Procedure TEST_LOADER_ACCOUNTS_COPY_ACCOUNTS()
Begin
-- initial dataset
    Call LOADER_TEST_MOCK_INFO_CLEAN();
    Call LOADER_TEST_MOCK_ACCOUNTS_CLEAN();

    
    Call LOADER_TEST_MOCK_BUSINESS_START('CONTACTS');

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

    Call LOADER_TEST_MOCK_ACCOUNTS_PUSH(1, '`ACCOUNTS_REF` = ''ACCNT-REF-1'', `ID_ACCOUNTS` = 1, `ID_ACCOUNTS_NEW` = Null, `ACCOUNTS_TYPE` = Null, `DF_ACCOUNTS_TYPES_ID_DF_ACCOUNTS_TYPES` = Null, `ACCOUNTS_CREATED` = ''2012-09-19'', `ACCOUNTS_LAST_EVENT_UPDATE` = ''2012-09-19'', `DF_CURRENCIES_CODE` = Null, `DF_CURRENCIES_ID_DF_CURRENCIES` = Null, `DF_PRIORITY_ID_DF_PRIORITY` = 1, `DF_SAND_STATE_ID_DF_SAND_STATE` = Null, `DF_MNGT_LVL_ID_DF_MNGT_LVL` = Null, `DF_SCAN_TYPE_ID_DF_SCAN_TYPE` = Null, `DF_ACC_CATEG_ID_DF_ACC_CATEG` = 1, `ENTITIES_AGENCIES_ID_ENTITIES_AGENCIES` = Null', 1, Null);
    Call LOADER_TEST_MOCK_ACCOUNTS_PUSH(2, '`ACCOUNTS_REF` = ''ACCNT-REF-2'', `ID_ACCOUNTS` = Null, `ID_ACCOUNTS_NEW` = 10, `ACCOUNTS_TYPE` = NULL, `DF_ACCOUNTS_TYPES_ID_DF_ACCOUNTS_TYPES` = 1, `ACCOUNTS_CREATED` = ''2012-09-19'', `ACCOUNTS_LAST_EVENT_UPDATE` = ''2012-09-19'', `DF_CURRENCIES_CODE` = Null, `DF_CURRENCIES_ID_DF_CURRENCIES` = 1, `DF_PRIORITY_ID_DF_PRIORITY` = 2, `DF_SAND_STATE_ID_DF_SAND_STATE` = 2, `DF_MNGT_LVL_ID_DF_MNGT_LVL` = 1, `DF_SCAN_TYPE_ID_DF_SCAN_TYPE` = 1, `DF_ACC_CATEG_ID_DF_ACC_CATEG` = 2, `ENTITIES_AGENCIES_ID_ENTITIES_AGENCIES` = Null', 2, Null);
    Call LOADER_TEST_MOCK_ACCOUNTS_PUSH(3, '`ACCOUNTS_REF` = ''ACCNT-REF-3'', `ID_ACCOUNTS` = Null, `ID_ACCOUNTS_NEW` = 2, `ACCOUNTS_TYPE` = Null, `DF_ACCOUNTS_TYPES_ID_DF_ACCOUNTS_TYPES` = 1, `ACCOUNTS_CREATED` = ''2012-09-19'', `ACCOUNTS_LAST_EVENT_UPDATE` = ''2012-09-19'', `DF_CURRENCIES_CODE` = Null, `DF_CURRENCIES_ID_DF_CURRENCIES` = 1, `DF_PRIORITY_ID_DF_PRIORITY` = 2, `DF_SAND_STATE_ID_DF_SAND_STATE` = 2, `DF_MNGT_LVL_ID_DF_MNGT_LVL` = 1, `DF_SCAN_TYPE_ID_DF_SCAN_TYPE` = 1, `DF_ACC_CATEG_ID_DF_ACC_CATEG` = 3, `ENTITIES_AGENCIES_ID_ENTITIES_AGENCIES` = Null', 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_ACCOUNTS_COPY(1);

    Call utAssert_eq('New accounts are Added to existants Accounts'
            , 3
            , (Select count(*) From ACCOUNTS)
        );

    Call utAssert_eq('Existing Accounts Are preserved'
            , 1
            , (Select ID_ACCOUNTS From ACCOUNTS Where ENTITIES_ID_ENTITIES = 1 And ACCOUNTS_REF = 'ACCNT-REF-1')
        );

    Call utAssert_eq('Not null Fields on existing Accounts are updated'
            , 1
            , (Select DF_PRIORITY_ID_DF_PRIORITY From ACCOUNTS Where ENTITIES_ID_ENTITIES = 1 And ACCOUNTS_REF = 'ACCNT-REF-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 Accounts ID are expected values (1)'
            , 11
            , (Select ID_ACCOUNTS From ACCOUNTS Where ENTITIES_ID_ENTITIES = 1 And ACCOUNTS_REF = 'ACCNT-REF-2')
        );
    Call utAssert_eq('new Accounts ID are expected values (2)'
            , 3
            , (Select ID_ACCOUNTS From ACCOUNTS Where ENTITIES_ID_ENTITIES = 1 And ACCOUNTS_REF = 'ACCNT-REF-3')
        );


    -- clean
    Call LOADER_TEST_MOCK_ACCOUNTS_CLEAN();
    Call LOADER_TEST_MOCK_INFO_CLEAN();

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

End //

Delimiter ;