Skip to content

Commit ede9fa1

Browse files
committed
1 parent 463f64b commit ede9fa1

4 files changed

Lines changed: 119 additions & 0 deletions

File tree

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--https://docs.oracle.com/database/sql-developer-4.2/RPTUG/sql-developer-unit-testing.htm#RPTUG45065
2+
3+
4+
create or replace procedure award_bonus (emp_id number, sales_amt number) as
5+
commission real;
6+
comm_missing exception;
7+
begin
8+
select commission_pct into commission
9+
from employees_test
10+
where employee_id = emp_id;
11+
12+
if commission is null then
13+
raise comm_missing;
14+
else
15+
update employees_test
16+
set salary = nvl(salary,0) + sales_amt*commission
17+
where employee_id = emp_id;
18+
end if;
19+
end;
20+
/
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
create table employees_test (employee_id number primary key, commission_pct number, salary number);
2+
insert into employees_test values (1001, 0.2, 8400);
3+
insert into employees_test values (1002, 0.25, 6000);
4+
insert into employees_test values (1003, 0.3, 5000);
5+
-- next employee is not in the sales department, thus is not on commission.
6+
insert into employees_test values (1004, null, 10000);
7+
commit;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
@@employees.sql
2+
@@award_bonus.sql
3+
@@test_award_bonus.pkg
4+
5+
set serveroutput on
6+
exec ut.run(user||'.test_award_bonus',ut_documentation_reporter());
7+
8+
drop package test_award_bonus;
9+
drop procedure award_bonus;
10+
drop table employees_test;
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
create or replace package test_award_bonus as
2+
3+
-- %suite(Award bonus)
4+
5+
-- %test(Sets new salary as pct commision * sales amount)
6+
-- %testsetup(add_test_employee)
7+
procedure update_employee_salary;
8+
9+
-- %test(Raises exception if null bonus is passed)
10+
-- %testsetup(add_employee_with_null_comm)
11+
procedure fail_on_null_bonus;
12+
13+
procedure add_test_employee;
14+
15+
procedure add_employee_with_null_comm;
16+
17+
end;
18+
/
19+
20+
create or replace package body test_award_bonus as
21+
22+
gc_test_employee constant integer := -1;
23+
gc_salary constant number := 4500;
24+
gc_commision_pct constant number := 0.2;
25+
26+
procedure update_employee_salary is
27+
results sys_refcursor;
28+
expected sys_refcursor;
29+
not_affected sys_refcursor;
30+
c_sales_amount constant number := 1000;
31+
begin
32+
--arrange
33+
open expected for
34+
select (salary + c_sales_amount * gc_commision_pct) as new_salary
35+
from employees_test where employee_id = gc_test_employee;
36+
37+
open not_affected for
38+
select * from employees where employee_id <> gc_test_employee;
39+
40+
--act
41+
award_bonus(emp_id => gc_test_employee, sales_amt => c_sales_amount);
42+
43+
--assert
44+
open results for
45+
select salary as new_salary
46+
from employees_test where employee_id = gc_test_employee;
47+
48+
ut.expect( results ).to_( equal( expected ) );
49+
50+
open results for
51+
select * from employees_test where employee_id != gc_test_employee;
52+
53+
ut.expect( results ).to_( equal( not_affected ) );
54+
end;
55+
56+
procedure fail_on_null_bonus is
57+
begin
58+
award_bonus(emp_id => gc_test_employee, sales_amt => null);
59+
ut.expect( sqlcode ).not_to( equal( 0 ) );
60+
exception
61+
when others then
62+
ut.expect( sqlcode ).not_to( equal( 0 ) );
63+
end;
64+
65+
procedure add_employee( emp_id number, comm_pct number, sal number ) is
66+
begin
67+
insert into employees_test (employee_id, commission_pct, salary)
68+
values (emp_id, comm_pct, sal);
69+
end;
70+
71+
procedure add_test_employee is
72+
begin
73+
add_employee(gc_test_employee, 0.2, gc_salary);
74+
end;
75+
76+
procedure add_employee_with_null_comm is
77+
begin
78+
add_employee(gc_test_employee, null, gc_salary);
79+
end;
80+
81+
end;
82+
/

0 commit comments

Comments
 (0)