Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b5f1281

Browse files
committed
Merge branch 'version3' into feature/running_with_many_reporters
2 parents ff846d1 + 980d36d commit b5f1281

13 files changed

Lines changed: 330 additions & 16 deletions

examples/RunAllExamples.sql

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,9 @@ prompt RunExampleTestAnnotationsHugePackage
2121
@@RunExampleTestAnnotationsHugePackage.sql
2222
prompt RunExpectations
2323
@@RunExpectations.sql
24-
prompt RunWithDocumentationReporter
24+
2525
@@RunWithDocumentationReporter.sql
26+
27+
@@award_bonus/run_award_bonus_test.sql
28+
@@between_string/run_betwnstr_test.sql
29+
@@remove_rooms_by_name/run_remove_rooms_by_name_test.sql
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: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
@@employees_test.sql
2+
@@award_bonus.sql
3+
@@test_award_bonus.pkg
4+
5+
set serveroutput on size unlimited format truncated
6+
7+
exec ut.run(user||'.test_award_bonus',ut_documentation_reporter());
8+
9+
drop package test_award_bonus;
10+
drop procedure award_bonus;
11+
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_test 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+
/
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
create or replace function betwnstr( a_string varchar2, a_start_pos integer, a_end_pos integer ) return varchar2 is
2+
l_start_pos pls_integer := a_start_pos;
3+
begin
4+
if l_start_pos = 0 then
5+
l_start_pos := 1;
6+
end if;
7+
return substr( a_string, l_start_pos, a_end_pos - l_start_pos + 1);
8+
end;
9+
/
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
@@betwnstr.sql
2+
@@test_betwnstr.pkg
3+
4+
set serveroutput on size unlimited format truncated
5+
6+
exec ut.run(user||'.test_betwnstr',ut_documentation_reporter());
7+
8+
drop package test_betwnstr;
9+
drop function betwnstr;
10+
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
create or replace package test_betwnstr as
2+
3+
-- %suite(Between string function)
4+
5+
-- %test(Returns substring from start position to end position)
6+
procedure normal_case;
7+
8+
-- %test(Returns substring when start position is zero)
9+
procedure zero_start_position;
10+
11+
-- %test(Returns string until end if end position is greated than string length)
12+
procedure big_end_position;
13+
14+
-- %test(Returns null for null inlut srting value)
15+
procedure null_string;
16+
end;
17+
/
18+
19+
create or replace package body test_betwnstr as
20+
21+
procedure normal_case is
22+
begin
23+
ut.expect( betwnstr( '1234567', 2, 5 ) ).to_( equal('2345') );
24+
end;
25+
26+
procedure zero_start_position is
27+
begin
28+
ut.expect( betwnstr( '1234567', 0, 5 ) ).to_( equal('12345') );
29+
end;
30+
31+
32+
procedure big_end_position is
33+
begin
34+
ut.expect( betwnstr( '1234567', 0, 500 ) ).to_( equal('1234567') );
35+
end;
36+
37+
procedure null_string is
38+
begin
39+
ut.expect( betwnstr( null, 2, 5 ) ).to_( be_null );
40+
end;
41+
42+
end;
43+
/

examples/demo_expectations.pck

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ create or replace package body demo_expectations is
147147
ut.expect( l_actual_timestamp_tz ).to_( equal( l_expected_timestamp_tz ) );
148148
ut.expect( l_actual_varchar2 ).to_( equal( l_expected_varchar2 ) );
149149

150-
open l_actual_cursor for select * from user_objects where rownum <100;
150+
open l_actual_cursor for select * from user_objects where rownum <6;
151151
open l_expected_cursor for select * from user_objects where rownum <5;
152152

153153
ut.expect(l_actual_cursor).to_equal(l_expected_cursor);
@@ -178,7 +178,7 @@ create or replace package body demo_expectations is
178178
ut.expect( l_timestamp ).to_equal( l_timestamp_tz );
179179
ut.expect( l_timestamp_ltz ).to_equal( l_date );
180180
ut.expect( l_varchar2 ).to_equal( l_clob );
181-
open l_cursor for select * from user_objects where rownum <100;
181+
open l_cursor for select * from user_objects where rownum <5;
182182
ut.expect( l_cursor ).to_equal( l_varchar2 );
183183

184184
--using the to_( equal( ) ) matcher
@@ -192,7 +192,7 @@ create or replace package body demo_expectations is
192192
ut.expect( l_timestamp ).to_( equal( l_timestamp_tz ) );
193193
ut.expect( l_timestamp_ltz ).to_( equal( l_date ) );
194194
ut.expect( l_varchar2 ).to_( equal( l_clob ) );
195-
open l_cursor for select * from user_objects where rownum <100;
195+
open l_cursor for select * from user_objects where rownum <5;
196196
ut.expect( l_varchar2 ).to_( equal( l_cursor ) );
197197

198198
end;
@@ -223,8 +223,8 @@ create or replace package body demo_expectations is
223223
ut.expect( l_timestamp_ltz ).to_equal( l_timestamp_ltz );
224224
ut.expect( l_timestamp_tz ).to_equal( l_timestamp_tz );
225225
ut.expect( l_varchar2 ).to_equal( l_varchar2 );
226-
open l_cursor1 for select * from all_objects;
227-
open l_cursor2 for select * from all_objects;
226+
open l_cursor1 for select * from all_objects where rownum <=50;
227+
open l_cursor2 for select * from all_objects where rownum <=50;
228228
ut.expect( l_cursor1 ).to_equal( l_cursor2 );
229229

230230
--using the to_( equal( ) ) matcher
@@ -239,8 +239,8 @@ create or replace package body demo_expectations is
239239
ut.expect( l_timestamp_tz ).to_( equal( l_timestamp_tz ) );
240240
ut.expect( l_varchar2 ).to_( equal( l_varchar2 ) );
241241

242-
open l_cursor1 for select * from all_objects;
243-
open l_cursor2 for select * from all_objects;
242+
open l_cursor1 for select * from all_objects where rownum <=50;
243+
open l_cursor2 for select * from all_objects where rownum <=50;
244244
ut.expect( l_cursor1 ).to_( equal( l_cursor2 ) );
245245
end;
246246

@@ -291,7 +291,7 @@ create or replace package body demo_expectations is
291291
l_timestamp_tz timestamp with time zone := l_timestamp;
292292
l_varchar2 varchar2(100) := 'a string';
293293
begin
294-
open l_cursor for select * from user_objects where rownum <100;
294+
open l_cursor for select * from user_objects where rownum <5;
295295
--using the to_be_null() matcher
296296
ut.expect( l_anydata ).to_be_null();
297297
ut.expect( l_blob ).to_be_null();
@@ -306,7 +306,7 @@ create or replace package body demo_expectations is
306306
ut.expect( l_cursor ).to_be_null;
307307

308308
--using the to_( be_null() ) matcher
309-
open l_cursor for select * from user_objects where rownum <100;
309+
open l_cursor for select * from user_objects where rownum <5;
310310
ut.expect( l_anydata ).to_( be_null() );
311311
ut.expect( l_blob ).to_( be_null() );
312312
ut.expect( l_boolean ).to_( be_null() );
@@ -432,7 +432,7 @@ create or replace package body demo_expectations is
432432
l_timestamp_tz timestamp with time zone := sysdate;
433433
l_varchar2 varchar2(100) := 'a string';
434434
begin
435-
open l_cursor for select * from user_objects where rownum <100;
435+
open l_cursor for select * from user_objects where rownum <5;
436436
--using the to_be_not_null() matcher
437437
ut.expect( l_anydata ).to_be_not_null();
438438
ut.expect( l_blob ).to_be_not_null();
@@ -447,7 +447,7 @@ create or replace package body demo_expectations is
447447
ut.expect( l_cursor ).to_be_not_null;
448448

449449
--using the to_( be_not_null() ) matcher
450-
open l_cursor for select * from user_objects where rownum <100;
450+
open l_cursor for select * from user_objects where rownum <5;
451451
ut.expect( l_anydata ).to_( be_not_null() );
452452
ut.expect( l_blob ).to_( be_not_null() );
453453
ut.expect( l_boolean ).to_( be_not_null() );
@@ -462,7 +462,7 @@ create or replace package body demo_expectations is
462462
end;
463463

464464
procedure demo_to_match_failure is
465-
l_clob clob := rpad('a',32767,'a')||'Stephen';
465+
l_clob clob := 'There was a guy named Stephen';
466466
begin
467467
ut.expect( 'STEPHEN' ).to_match('^Stephen$');
468468
ut.expect( 'stephen ' ).to_match('^Stephen$', 'i'); --case insensitive
@@ -479,7 +479,7 @@ create or replace package body demo_expectations is
479479
end;
480480

481481
procedure demo_to_match_success is
482-
l_clob clob := rpad('a',32767,'a')||'STEPHEN';
482+
l_clob clob := 'There was a guy named STEPHEN';
483483
begin
484484
ut.expect( 'Hi, I am Stephen' ).to_match('Stephen$');
485485
ut.expect( 'stephen' ).to_match('^Stephen$', 'i'); --case insensitive
@@ -492,7 +492,7 @@ create or replace package body demo_expectations is
492492
end;
493493

494494
procedure demo_to_be_like_failure is
495-
l_clob clob := rpad('a',32767,'a')||'Stephen';
495+
l_clob clob := 'There was a guy named Stephen';
496496
begin
497497
ut.expect( 'STEPHEN' ).to_be_like('Stephen');
498498
ut.expect( 'Stephen ' ).to_be_like('Stephen\_', '\'); --escape wildcards with '\'
@@ -509,7 +509,7 @@ create or replace package body demo_expectations is
509509
end;
510510

511511
procedure demo_to_be_like_success is
512-
l_clob clob := rpad('a',32767,'a')||'STEPHEN_';
512+
l_clob clob := 'There was a guy named STEPHEN_';
513513
begin
514514
ut.expect( 'Hi, I am Stephen' ).to_be_like('%Stephen');
515515
ut.expect( 'stephen_' ).to_be_like('_tephen\_', '\'); --escape wildcards with '\'
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
create or replace procedure remove_rooms_by_name( l_name rooms.name%type ) is
2+
begin
3+
if l_name is null then
4+
raise program_error;
5+
end if;
6+
delete from rooms where name like l_name;
7+
end;
8+
/

0 commit comments

Comments
 (0)