Delimiter //
/**
 * Test of LOADER_SUMMARIES_NORMALIZE_MAP_ACCOUNTS
 * @test
 */
Drop Procedure If Exists TEST_LOADER_SUMMARIES_NORMALIZE_MAP_ENTRIES//
Create Procedure TEST_LOADER_SUMMARIES_NORMALIZE_MAP_ENTRIES(
)
Begin

    -- initial dataset
    CALL LOADER_SUMMARIES_CLEAN();

    CALL LOADER_SUMMARIES_PUSH(1, 'ACCOUNTS_ID_ACCOUNTS=1, ENTRIES_ACC_EXT_REF=''ENT1'' ', 1, NULL);
    CALL LOADER_SUMMARIES_PUSH(2, 'ACCOUNTS_ID_ACCOUNTS=1 ', 2, NULL);
    CALL LOADER_SUMMARIES_PUSH(3, 'ACCOUNTS_ID_ACCOUNTS=1, ENTRIES_ACC_EXT_REF=''NOTHING'' ', 1, NULL);
    CALL LOADER_SUMMARIES_PUSH(4, 'ACCOUNTS_ID_ACCOUNTS=2, ENTRIES_ACC_EXT_REF=''ENT1'' ', 2, NULL);

    -- Business tables
    Call LOADER_TEST_MOCK_INFO_PUSH('ID_ENTITIES', 1);

    call LOADER_TEST_MOCK_BUSINESS_CLEAN('ENTITIES');
    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_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 = 1
            , ACCOUNTS_REF = ''ACC1''
            , 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('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 = 1
            , ACCOUNTS_REF = ''ACC2''
            , ACCOUNTS_CREATED = NOW()
            , ACCOUNTS_LAST_EVENT_UPDATE = NOW()
            , ACCOUNTS_PROCESSUS_SCAN_TYPE = 1
            , ACCOUNTS_ACC_MNGT_LAST_LVL = 0'
    );
    
	Call LOADER_TEST_MOCK_BUSINESS_CLEAN('ENTRIES_has_ACCOUNTS');
    Call LOADER_TEST_MOCK_BUSINESS_PUSH('ENTRIES_has_ACCOUNTS', 
            'ID_ENTRIES_has_ACCOUNTS = 1
            , ENTRIES_ID_ENTRIES = 1
            , ACCOUNTS_ID_ACCOUNTS = 1
            , ENTRIES_ACC_EXT_REF = ''ENT1'' '
    );


    -- Tested procedure
    Call LOADER_SUMMARIES_NORMALIZE_MAP_ENTRIES(1);

    -- Assertions
    Call utAssert_eq('During load SUMMARIES, when a valid ENTRIES is provided, row receives its id'
        , 1
        , (Select ENTRIES_ID_ENTRIES From LOADER_SUMMARIES Where ID_LOADER_SUMMARIES = 1)
    );
	Call utAssert_eq('During load SUMMARIES, When ENTRIES is not provided, row receive no id'
        , Null
        , (Select ENTRIES_ID_ENTRIES From LOADER_SUMMARIES Where ID_LOADER_SUMMARIES = 2)
    );
    Call utAssert_eq('During load SUMMARIES, When an invalid ENTRIES is provided, row receive no id'
        , Null
        , (Select ENTRIES_ID_ENTRIES From LOADER_SUMMARIES Where ID_LOADER_SUMMARIES = 3)
    );
    
    Call utAssert_eq('During load SUMMARIES, When a valid ENTRIES is provided but not assotiated with the right ACCOUNT, row receive no id'
        , Null
        , (Select ENTRIES_ID_ENTRIES From LOADER_SUMMARIES Where ID_LOADER_SUMMARIES = 4)
    );
    

    -- Clean
	Call LOADER_TEST_MOCK_BUSINESS_CLEAN('ENTRIES_has_ACCOUNTS');
    Call LOADER_TEST_MOCK_BUSINESS_CLEAN('ACCOUNTS');
    Call LOADER_TEST_MOCK_BUSINESS_CLEAN('ENTITIES');
    Call LOADER_TEST_MOCK_INFO_CLEAN();
    Call LOADER_SUMMARIES_CLEAN();

End//
Delimiter ;