Skip to content

Commit

Permalink
Added test scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
simasch committed Jul 10, 2023
1 parent ad79d59 commit 2b13d6d
Show file tree
Hide file tree
Showing 13 changed files with 265 additions and 10 deletions.
10 changes: 5 additions & 5 deletions scripts/sql/create_app_objects.sql
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
whenever sqlerror exit failure rollback
whenever oserror exit failure rollback

@scripts/sql/TO_TEST_ME.sql
@scripts/sql/APP.PKG_TEST_ME.pks
@scripts/sql/APP.PKG_TEST_ME.pkb
@scripts/sql/simple/scripts/sources/TO_TEST_ME.sql
@scripts/sql/simple/scripts/sources/APP.PKG_TEST_ME.pks
@scripts/sql/simple/scripts/sources/APP.PKG_TEST_ME.pkb

@scripts/sql/APP.TEST_PKG_TEST_ME.pks
@scripts/sql/APP.TEST_PKG_TEST_ME.pkb
@scripts/sql/simple/scripts/tests/APP.TEST_PKG_TEST_ME.pks
@scripts/sql/simple/scripts/tests/APP.TEST_PKG_TEST_ME.pkb
6 changes: 3 additions & 3 deletions scripts/sql/create_source_owner_objects.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
whenever sqlerror exit failure rollback
whenever oserror exit failure rollback

@src/test/resources-its/org/utplsql/maven/plugin/UtPlsqlMojoIT/owner_param/scripts/sources/foo/tables/TO_TEST_ME.sql
@src/test/resources-its/org/utplsql/maven/plugin/UtPlsqlMojoIT/owner_param/scripts/sources/foo/packages/PKG_TEST_ME.pks
@src/test/resources-its/org/utplsql/maven/plugin/UtPlsqlMojoIT/owner_param/scripts/sources/foo/package_bodies/PKG_TEST_ME.pkb
@scripts/sql/owner_param/scripts/sources/foo/tables/TO_TEST_ME.sql
@scripts/sql/owner_param/scripts/sources/foo/packages/PKG_TEST_ME.pks
@scripts/sql/owner_param/scripts/sources/foo/package_bodies/PKG_TEST_ME.pkb
4 changes: 2 additions & 2 deletions scripts/sql/create_tests_owner_objects.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ whenever oserror exit failure rollback
create synonym TO_TEST_ME for CODE_OWNER.TO_TEST_ME;
create synonym PKG_TEST_ME for CODE_OWNER.PKG_TEST_ME;

@src/test/resources-its/org/utplsql/maven/plugin/UtPlsqlMojoIT/owner_param/scripts/test/bar/packages/TEST_PKG_TEST_ME.pks
@src/test/resources-its/org/utplsql/maven/plugin/UtPlsqlMojoIT/owner_param/scripts/test/bar/package_bodies/TEST_PKG_TEST_ME.pkb
@scripts/sql/owner_param/scripts/test/bar/packages/TEST_PKG_TEST_ME.pks
@scripts/sql/owner_param/scripts/test/bar/package_bodies/TEST_PKG_TEST_ME.pkb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
CREATE OR REPLACE PACKAGE BODY CODE_OWNER.PKG_TEST_ME IS
--
-- This
--
FUNCTION FC_TEST_ME(PPARAM1 IN VARCHAR2) RETURN NUMBER IS
BEGIN
IF PPARAM1 IS NULL THEN
RETURN NULL;
ELSIF PPARAM1 = '1' THEN
RETURN 1;
ELSE
RETURN 0;
END IF;
END FC_TEST_ME;

PROCEDURE PR_TEST_ME(PSNAME IN VARCHAR2) IS
BEGIN
IF PSNAME IS NULL THEN
NULL;
ELSE
INSERT INTO TO_TEST_ME (SNAME) VALUES (PSNAME);
COMMIT;
END IF;
END PR_TEST_ME;

END PKG_TEST_ME;
/
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
--
-- This package is used TO demonstrate the utPL/SQL possibilities
--
CREATE OR REPLACE PACKAGE CODE_OWNER.PKG_TEST_ME AS
FUNCTION FC_TEST_ME(PPARAM1 IN VARCHAR2) RETURN NUMBER;
PROCEDURE PR_TEST_ME(PSNAME IN VARCHAR2);
END PKG_TEST_ME;
/
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
CREATE OR REPLACE PACKAGE BODY TESTS_OWNER.TEST_PKG_TEST_ME AS

---------------------------------------------------------------------------
PROCEDURE SETUP_GLOBAL IS
BEGIN
-- Put here the code which is valid for all tests and that should be
-- executed once.
NULL;
END SETUP_GLOBAL;

---------------------------------------------------------------------------
PROCEDURE TEARDOWN_GLOBAL IS
BEGIN
-- Put here the code that should be called only once after all the test
-- have executed
NULL;
END TEARDOWN_GLOBAL;

---------------------------------------------------------------------------
PROCEDURE SETUP_TEST IS
BEGIN
-- Nothing to clean up globally
NULL;
END SETUP_TEST;

PROCEDURE TEARDOWN_TEST IS
BEGIN
-- Nothing to clean up globally
NULL;
END TEARDOWN_TEST;

PROCEDURE TEST_FC_INPUT_1 IS
BEGIN
-- Ok this is a real test where I check that the function return 1
-- when called with a '1' parameter
UT.EXPECT(PKG_TEST_ME.FC_TEST_ME('1')).TO_EQUAL(1);
END;

PROCEDURE SETUP_TEST_FC_INPUT_1 IS
BEGIN
-- Nothing to be done really
NULL;
END;

PROCEDURE TEARDOWN_TEST_FC_INPUT_1 IS
BEGIN
-- Nothing to be done really
NULL;
END;

PROCEDURE TEST_FC_INPUT_0 IS
BEGIN
-- Ok this is a real test where I check that the function return 0
-- when called with a '0' parameter
UT.EXPECT(PKG_TEST_ME.FC_TEST_ME('0')).TO_EQUAL(0);
END;

PROCEDURE TEST_FC_INPUT_NULL IS
BEGIN
-- Ok I check that the function return NULL
-- when called with a NULL parameter
UT.EXPECT(PKG_TEST_ME.FC_TEST_ME(NULL)).TO_BE_NULL;
END TEST_FC_INPUT_NULL;

PROCEDURE TEST_PR_TEST_ME_NULL IS
VNCOUNT1 PLS_INTEGER;
VNCOUNT2 PLS_INTEGER;
BEGIN
-- In this example I check that the procedure does
-- not insert anything when passing it a NULL parameter
SELECT COUNT(1) INTO VNCOUNT1 FROM TO_TEST_ME;
PKG_TEST_ME.PR_TEST_ME(NULL);
SELECT COUNT(1) INTO VNCOUNT2 FROM TO_TEST_ME;
UT.EXPECT(VNCOUNT1).TO_EQUAL(VNCOUNT2);
END;

PROCEDURE TEST_PR_TEST_ME_NOT_NULL IS
VNCOUNT1 PLS_INTEGER;
VNCOUNT2 PLS_INTEGER;
VSNAME TO_TEST_ME.SNAME%TYPE;
BEGIN
-- In this test I will check that I do insert a value
-- when the parameter is not null. I futher check that
-- the procedure has inserted the value I specified.
SELECT COUNT(1) INTO VNCOUNT1 FROM TO_TEST_ME;
VSNAME := TO_CHAR(VNCOUNT1);
PKG_TEST_ME.PR_TEST_ME(VSNAME);
SELECT COUNT(1) INTO VNCOUNT2 FROM TO_TEST_ME;

-- Check that I have inserted the value
UT.EXPECT(VNCOUNT1 + 1).TO_EQUAL(VNCOUNT2);
SELECT COUNT(1) INTO VNCOUNT2 FROM TO_TEST_ME T WHERE T.SNAME = VSNAME;

-- Check that I inserted the one I said I would insert
UT.EXPECT(VNCOUNT2).TO_EQUAL(1);
DELETE FROM TO_TEST_ME T WHERE T.SNAME = VSNAME;
COMMIT;
END;

PROCEDURE TEST_PR_TEST_ME_EXISTS IS
BEGIN
-- In case the value exists the procedure should fail with an exception.
BEGIN
PKG_TEST_ME.PR_TEST_ME('EXISTS');
PKG_TEST_ME.PR_TEST_ME('EXISTS');
EXCEPTION
WHEN OTHERS THEN
UT.FAIL('Unexpected exception raised');
END;
END;

PROCEDURE TEST_PR_TEST_ME_CURSOR IS
TYPE REF_CURSOR IS REF CURSOR;
VEXPECTED REF_CURSOR;
VACTUAL REF_CURSOR;
BEGIN
EXECUTE IMMEDIATE 'TRUNCATE TABLE CODE_OWNER.TO_TEST_ME';
OPEN VEXPECTED FOR
SELECT T.SNAME FROM TO_TEST_ME T;
OPEN VACTUAL FOR
SELECT T.SNAME FROM TO_TEST_ME T;
UT.EXPECT(VEXPECTED).TO_(EQUAL(VACTUAL));
END;

END;
/
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
CREATE OR REPLACE PACKAGE TESTS_OWNER.TEST_PKG_TEST_ME AS
-- %suite(TEST_PKG_TEST_ME)
-- %suitepath(plsql.examples)
--
-- This package shows all the possibilities to unit test
-- your PL/SQL package. NOTE that it is not limited to
-- testing your package. You can do that on all your
-- procedure/functions...
--

/**
* This two parameters are used by the test framework in
* order to identify the test suite to run
*/

/*
* This method is invoked once before any other method.
* It should contain all the setup code that is relevant
* for all your test. It might be inserting a register,
* creating a type, etc...
*/
-- %beforeall
PROCEDURE SETUP_GLOBAL;

/*
* This method is invoked once after all other method.
* It can be used to clean up all the resources that
* you created in your script
*/
-- %afterall
PROCEDURE TEARDOWN_GLOBAL;

/*
* This method is called once before each test.
*/
-- %beforeeach
PROCEDURE SETUP_TEST;

/*
* This method is called once after each test.
*/
-- %aftereach
PROCEDURE TEARDOWN_TEST;

/**
* This is a real test. The main test can declare a setup
* and teardown method in order to setup and cleanup things
* for that specific test.
*/
-- %test
-- %displayname(Checking if function ('1') returns 1)
-- %beforetest(SETUP_TEST_FC_INPUT_1)
-- %aftertest(TEARDOWN_TEST_FC_INPUT_1)
PROCEDURE TEST_FC_INPUT_1;
PROCEDURE SETUP_TEST_FC_INPUT_1;
PROCEDURE TEARDOWN_TEST_FC_INPUT_1;

-- %test
-- %displayname(Checking if function ('0') returns 0)
PROCEDURE TEST_FC_INPUT_0;

-- %test
-- %displayname(Checking if function (NULL) returns NULL)
PROCEDURE TEST_FC_INPUT_NULL;

-- %test
-- %displayname(Checking if procedure (NULL) insert)
PROCEDURE TEST_PR_TEST_ME_NULL;

-- %test
-- %displayname(Checking if procedure (NOT NULL) insert)
-- %rollback(manual)
PROCEDURE TEST_PR_TEST_ME_NOT_NULL;

-- %test
-- %displayname(Checking if procedure (NOT NULL) insert while existing)
-- %rollback(manual)
PROCEDURE TEST_PR_TEST_ME_EXISTS;

-- %test
-- %displayname(Demonstrating the use of cursor)
-- %rollback(manual)
PROCEDURE TEST_PR_TEST_ME_CURSOR;

END;
/
File renamed without changes.
File renamed without changes.
8 changes: 8 additions & 0 deletions scripts/sql/simple/scripts/sources/TO_TEST_ME.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
--
-- This is a table used to demonstrate the UNIT test framework.
--
CREATE TABLE TO_TEST_ME
(
SNAME VARCHAR2(10)
)
/
File renamed without changes.
File renamed without changes.

0 comments on commit 2b13d6d

Please sign in to comment.