CREATE OR REPLACE FUNCTION br001_crtl_plate_chk_test 
(IN p_sample_id          TECH_ID
,IN p_sample_code        CODE
,IN p_saty_id            TECH_ID
,IN p_pers_id_registerer TECH_ID
,IN p_expect_failure     BOOLEAN) 

RETURNS  varchar

AS $br001_crtl_plate_chk_test$

DECLARE
   v_sample_inserted       BOOLEAN default FALSE;

BEGIN

-- Attempt an insert

   insert into samples (id, code, saty_id, pers_id_registerer) 
      values (p_sample_id
             ,p_sample_code
             ,p_saty_id 
             ,p_pers_id_registerer) ;


   v_sample_inserted := TRUE;

   if p_expect_failure then
       RAISE EXCEPTION 'Test Failed in FUNCTION "br001_crtl_plate_chk_test" as failure expected, but: "Sample inserted successfully".';
   else
       RETURN 'Test Passed: A new Sample was inserted, as expected.';
   end if;


EXCEPTION
   WHEN raise_exception OR others THEN
      if NOT v_sample_inserted then
         if p_expect_failure then
            -- Failure expected, so Test Passed
            RETURN 'Test Passed: Exception thrown successfully in trigger "br001_crtl_plate_chk". ';
         else
            RAISE EXCEPTION 'Test Failed: due to unexpected result, %', sqlerrm;
         end if;
      else
         -- Test Failed in current FUNCTION "br001_crtl_plate_chk_test", continue to raise the exception
         RAISE EXCEPTION '%', sqlerrm;
      end if;

END;
$br001_crtl_plate_chk_test$ LANGUAGE plpgsql;