utPLSQL icon indicating copy to clipboard operation
utPLSQL copied to clipboard

--%throws annotation named package constant/variable not recognized

Open jgebal opened this issue 3 years ago • 1 comments

When a test is executed, the named constant is being searched in scope of current user (user executing the tests. This is not how it is expected to work.

Example of the problem

create or replace package user1.error_codes as
  some_error constant integer := -20001;
end;
/

create or replace package user1.main_package as
  function the_proc (a number , b number ) return number;
end;
/

create or replace package body user1.main_package as
  function the_proc(a number , b number ) return number is
  begin
     if b = 0 then
        raise_application_error(error_codes.some_error, 'Cannot use "0"');
     end if;
     return a/b;
  end;
end;
/

create or replace package user1.ut_the_code as
  --%suite

  --%test
  --%throws(error_codes.some_error)
  procedure the_test;

end;
/

create or replace package body user1.ut_the_code as
  procedure the_test is
    y number;
  begin
    y := main_package.the_proc (1,0);
  end;
end;
/

The above test will succeed when called from schema user1.

ut_the_code
  the_test [.019 sec]

Finished in .027852 seconds
1 tests, 0 failed, 0 errored, 0 disabled, 0 warning(s)

If called from user2,

ut_the_code
  the_test [.007 sec] (FAILED - 1)

Failures:

  1) the_test
      ORA-20001: Cannot use "0"
      ORA-06512: at "USER1.MAIN_PACKAGE", line 5
      ORA-06512: at "USER1.UT_THE_CODE", line 5
      ORA-06512: at line 6

Warnings:

  1) ut_the_code.the_test
      Invalid parameter value "error_codes.some_error" for "--%throws" annotation. Parameter ignored.
      at package "USER1.UT_THE_CODE.THE_TEST", line 4

Finished in .010336 seconds
1 tests, 0 failed, 1 errored, 0 disabled, 1 warning(s)

This is because the value of --%throws annotation is evaluated at runtime within the scope of the user running the code.

A change is needed so that the value is evaluated in scope of the executed UT package schema.

Ideally, we should also allow for below syntax to be valid:

create or replace package user1.ut_the_code as
  --%suite

  some_error constant integer := -20003;
  
  --%test
  --%throws(some_error)
  procedure the_test;

end;
/

jgebal avatar Aug 02 '22 19:08 jgebal

Currently a workaround is possible. Instead of --%throws(error_codes.some_error) use --%throws(user1.error_codes.some_error) This will work regardless of user running the test

jgebal avatar Aug 02 '22 19:08 jgebal