Is there a way to access private plsql procedures for testing purposes?

后端 未结 4 1808
悲&欢浪女
悲&欢浪女 2021-01-31 11:14

I\'m working on a project with a lot of plsql code and would like to add more specific unit-tests to our codebase. Some of the procedures/functions I like to test aren\'t in the

4条回答
  •  抹茶落季
    2021-01-31 11:39

    There is a way to do this, providing you are on 10g or higher. It's called Conditional Compilation. This is a highly neat feature which provides special syntax so we can change our PL/SQL code at compilation time.

    As it happens I have been using this feature precisely to expose private packages in a spec so I can run UTPLSQL tests against them.

    Here is the special syntax:

    create or replace package my_pkg
    as
    
        $IF $$dev_env_test $THEN
    
        PROCEDURE private_proc;
    
        $END
    
        FUNCTION public_function return date;
    
    end my_pkg;
    /
    

    That variable with the double-dollar sign is a Conditional Compilation flag.

    If I describe the package we can only see the public package:

    SQL> desc my_pkg
    FUNCTION PUBLIC_FUNCTION RETURNS DATE
    
    SQL>
    

    Now I set the conditional flag and re-compile the package, and as if by magic ...

    SQL> alter session set plsql_ccflags='dev_env_test:true'
      2  /
    
    Session altered.
    
    SQL> alter package my_pkg compile
      2  /
    
    Package altered.
    
    SQL> desc my_pkg
    PROCEDURE PRIVATE_PROC
    FUNCTION PUBLIC_FUNCTION RETURNS DATE
    
    SQL>
    

    Privatising the functions is as simple as you think it would be:

    SQL> alter session set plsql_ccflags='dev_env_test:false'
      2  /
    
    Session altered.
    
    SQL> alter package my_pkg compile
      2  /
    
    Package altered.
    
    SQL> desc my_pkg
    FUNCTION PUBLIC_FUNCTION RETURNS DATE
    
    SQL>
    

    We can do lots more with conditional compilation. It's covered in the docs. Find out more.

提交回复
热议问题