Delimiter //
/**
 * Test of LOADER_ENTRIES_NORMALIZE_MAP_BALANCE_AMOUNT()
 * @test
 */
Drop Procedure If Exists TEST_LOADER_ENTRIES_MAP_AMOUNT_BALANCED//
Create Procedure TEST_LOADER_ENTRIES_MAP_AMOUNT_BALANCED() 
Begin

	-- Suite problème intégration IDVERDE, et confirmation règle metier avec Olivier
	-- ON NE TIENS COMPTE QUE DU SIGNE de montant pour les mapper en débit ou crédit
	-- ON NE DOIS PAS TENIR COMPTE DU TYPE DE L'ECRITURE
	-- le type de l'ecriture sera testé dans une autre procédure, qui décidera alors 
	-- s'il faut faut intervertir et les colonnes en fonction du type

    -- Infos
    Set @id_tpl_loader = 1;
    Call LOADER_TEST_MOCK_INFO_CLEAN();
    Call LOADER_TEST_MOCK_INFO_PUSH('ID_TEMPLATE_LOADER', @id_tpl_loader);

    -- context
    Call LOADER_TEST_MOCK_ENTRIES_CLEAN();
    Call LOADER_TEST_MOCK_ENTRIES_PUSH(1, 'ENTRIES_ENT_DEBIT=null, ENTRIES_ENT_CREDIT=null, ENT_DEBIT=null, ENT_CREDIT=null, ENT_AMOUNT = 10, DF_ENTR_TYPE_ID_DF_ENTR_TYPE = 1 ', 1, Null);
    Call LOADER_TEST_MOCK_ENTRIES_PUSH(2, 'ENTRIES_ENT_DEBIT=null, ENTRIES_ENT_CREDIT=null, ENT_DEBIT=null, ENT_CREDIT=null, ENT_AMOUNT = -10, DF_ENTR_TYPE_ID_DF_ENTR_TYPE = 2 ', 2, Null);
--     Call LOADER_TEST_MOCK_ENTRIES_PUSH(3, 'ENTRIES_ENT_DEBIT=20, ENTRIES_ENT_CREDIT=null, ENT_DEBIT=20, ENT_CREDIT=null, AMOUNT = 10, DF_ENTR_TYPE_ID_DF_ENTR_TYPE = 1 ', 1, Null);
--     Call LOADER_TEST_MOCK_ENTRIES_PUSH(4, 'ENTRIES_ENT_DEBIT=null, ENTRIES_ENT_CREDIT=30, ENT_DEBIT=null, ENT_CREDIT=30, AMOUNT = 10, DF_ENTR_TYPE_ID_DF_ENTR_TYPE = 2 ', 2, Null);

    -- business context
    Call LOADER_TEST_MOCK_BUSINESS_CLEAN('TEMPL_LOADER_has_ENTR_TYPE');
    Update TEMPL_LOADER_has_ENTR_TYPE Set TEMPL_LOADER_has_ENTR_TYP_CODE = '';
    Call LOADER_TEST_MOCK_BUSINESS_PUSH('TEMPL_LOADER_has_ENTR_TYPE', Concat('TEMPL_LOADER_has_ENTR_TYP_CODE = ''DEBIT'', TEMPL_LOADER_ID_TEMPL_LOADER = ',@id_tpl_loader,', DF_ENTR_TYPE_ID_DF_ENTR_TYPE = 1, ID_TEMPL_LOADER_has_ENTR_TYPE = 1 ' ));
    Call LOADER_TEST_MOCK_BUSINESS_PUSH('TEMPL_LOADER_has_ENTR_TYPE', Concat('TEMPL_LOADER_has_ENTR_TYP_CODE = ''CREDIT'', TEMPL_LOADER_ID_TEMPL_LOADER = ',@id_tpl_loader,', DF_ENTR_TYPE_ID_DF_ENTR_TYPE = 2, ID_TEMPL_LOADER_has_ENTR_TYPE = 2 ' ));
    
    -- tested procedure
    Call LOADER_ENTRIES_NORMALIZE_MAP_BALANCE_AMOUNT(1);

    -- assertions
    Call utAssert_eq('When positive amount provided, amout was balanced on DEBIT'
        , 10.0000
        , (Select ENTRIES_ENT_DEBIT From LOADER_ENTRIES Where ID_LOADER_ENTRIES = 1)
    );
    -- assertions
    Call utAssert_eq('When positive amount provided, CREDIT is null'
        , 0.0000
        , (Select Coalesce(ENTRIES_ENT_CREDIT, 0.0000) From LOADER_ENTRIES Where ID_LOADER_ENTRIES = 1)
    );
    -- assertions
    Call utAssert_eq('When negative provided, absolute value of amout was balanced on CREDIT'
        , 10.0000
        , (Select ENTRIES_ENT_CREDIT From LOADER_ENTRIES Where ID_LOADER_ENTRIES = 2)
    );
    -- assertions
    Call utAssert_eq('When entries provided type is "CREDIT", DEBIT is null'
        , 0.0000
        , (Select Coalesce(ENTRIES_ENT_DEBIT, 0.0000) From LOADER_ENTRIES Where ID_LOADER_ENTRIES = 2)
    );
-- 
--     Call utAssert_eq('When "DEBIT" was not null, amount was not balanced'
--         , 20.0000
--         , (Select ENTRIES_ENT_DEBIT From LOADER_ENTRIES Where ID_LOADER_ENTRIES = 3)
--     );
--     Call utAssert_eq('When "CREDIT" was not null, amount was not balanced'
--         , 30.0000
--         , (Select ENTRIES_ENT_CREDIT From LOADER_ENTRIES Where ID_LOADER_ENTRIES = 4)
--     );


    -- Clean
    Call LOADER_TEST_MOCK_BUSINESS_CLEAN('EMPL_LOADER_has_ENTR_TYPE');
    Call LOADER_TEST_MOCK_ENTRIES_CLEAN();
    Call LOADER_TEST_MOCK_INFO_CLEAN();

End//
Delimiter ;