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

Skip to content

Commit 570a341

Browse files
committed
Refactored the ut_run.sql script.
Added ut_runner package. Removed run procedures from ut_suite_manager. Fixed a compilation optimization issue in UT_OUTPUT_STREAM.
1 parent b5f1281 commit 570a341

9 files changed

Lines changed: 206 additions & 215 deletions

File tree

source/core/types/ut_output_stream.tpb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
alter session set plsql_optimize_level=1;
2+
--we need to use plsql optimize level=1 to prevent
3+
-- Oracle from changing the for loop to bulk collect into
4+
-- The row-by-row approach is needed to get the visible progress ot unit tests outputs
5+
-- as the tests get executed.
16
create or replace type body ut_output_stream as
27

38
overriding final member procedure close(self in out nocopy ut_output_stream) is
@@ -23,3 +28,5 @@ create or replace type body ut_output_stream as
2328

2429
end;
2530
/
31+
--going back to the "default" level 2?
32+
alter session set plsql_optimize_level=2;

source/core/ut_runner.pkb

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
create or replace package body ut_runner is
2+
3+
type t_call_param is record (
4+
ut_reporter_name varchar2(4000),
5+
output_file_name varchar2(4000),
6+
output_to_screen varchar2(3) := 'off',
7+
output_id varchar2(4000)
8+
);
9+
type tt_call_params is table of t_call_param;
10+
11+
g_call_params tt_call_params;
12+
13+
g_ut_paths varchar2(4000);
14+
15+
function get_streamed_output_type return varchar2 is
16+
l_result varchar2(255);
17+
begin
18+
select type_name
19+
into l_result
20+
from user_types where supertype_name = 'UT_OUTPUT_STREAM';
21+
return lower(l_result)||'()';
22+
end;
23+
24+
procedure run(a_paths in ut_varchar2_list, a_reporter in ut_reporter) is
25+
l_objects_to_run ut_objects_list;
26+
l_reporter ut_reporter := a_reporter;
27+
ut_running_suite ut_test_suite;
28+
begin
29+
ut_suite_manager.configure_execution_by_path(a_paths,l_objects_to_run);
30+
31+
if l_objects_to_run.count > 0 then
32+
l_reporter.before_run(a_suites => l_objects_to_run);
33+
for i in 1 .. l_objects_to_run.count loop
34+
35+
ut_running_suite := treat(l_objects_to_run(i) as ut_test_suite);
36+
ut_running_suite.do_execute(l_reporter);
37+
l_objects_to_run(i) := ut_running_suite;
38+
39+
end loop;
40+
l_reporter.after_run(a_suites => l_objects_to_run);
41+
end if;
42+
end;
43+
44+
45+
procedure run(a_path in varchar2, a_reporter in ut_reporter) is
46+
begin
47+
run(ut_varchar2_list(coalesce(a_path, sys_context('userenv', 'current_schema'))), a_reporter);
48+
end run;
49+
50+
function get_optional_params_script(a_params_count integer := 100) return ut_varchar2_list pipelined is
51+
l_sql_columns varchar2(4000);
52+
l_params varchar2(4000);
53+
begin
54+
for i in 1 .. a_params_count loop
55+
pipe row ('column '||i||' new_value '||i);
56+
end loop;
57+
for i in 1 .. a_params_count loop
58+
l_sql_columns := l_sql_columns ||'null as "'||i||'",';
59+
l_params := l_params || '''&&'||i||''',';
60+
end loop;
61+
pipe row ('select '||rtrim(l_sql_columns, ',') ||' from dual where rownum = 0;');
62+
pipe row ('' );
63+
pipe row ('exec ut_runner.set_call_params(ut_varchar2_list('||rtrim(l_params, ',')||'));' );
64+
return;
65+
end;
66+
67+
procedure set_call_params(a_params ut_varchar2_list) is
68+
l_call_param t_call_param;
69+
begin
70+
g_call_params := tt_call_params();
71+
for param in
72+
( with
73+
param_vals as(
74+
select regexp_substr(column_value,'-([fos])\=?(.*)',1,1,'c',1) param_type,
75+
regexp_substr(column_value,'-([fos])\=(.*)',1,1,'c',2) param_value
76+
from table(a_params)
77+
where column_value is not null)
78+
select param_type, param_value
79+
from param_vals
80+
where param_type is not null)
81+
loop
82+
if param.param_type = 'f' then
83+
g_call_params.extend;
84+
g_call_params(g_call_params.last) := l_call_param;
85+
g_call_params(g_call_params.last).ut_reporter_name := param.param_value;
86+
elsif g_call_params.last is not null then
87+
if param.param_type = 'o' then
88+
g_call_params(g_call_params.last).output_file_name := param.param_value;
89+
elsif param.param_type = 's' then
90+
g_call_params(g_call_params.last).output_to_screen := 'on';
91+
end if;
92+
end if;
93+
end loop;
94+
95+
begin
96+
select ''''||replace(ut_paths,',',''',''')||''''
97+
into g_ut_paths
98+
from (select regexp_substr(column_value,'-p\=(.*)',1,1,'c',1) as ut_paths from table(a_params) )
99+
where ut_paths is not null;
100+
exception
101+
when no_data_found then
102+
g_ut_paths := 'user';
103+
when too_many_rows then
104+
raise_application_error(-20000, 'Parameter "-p=ut_paths" defined more than once. Only one "-p=ut_paths" parameter can be used.');
105+
end;
106+
107+
end;
108+
109+
function get_run_in_background_script return ut_varchar2_list pipelined is
110+
l_output_id varchar2(128);
111+
l_output_type varchar2(256);
112+
begin
113+
114+
l_output_type := get_streamed_output_type();
115+
pipe row( 'set serveroutput on size unlimited format truncated');
116+
pipe row( 'set pagesize 0');
117+
pipe row( 'set linesize 4000');
118+
pipe row( 'spool run_background.log');
119+
pipe row( 'declare');
120+
pipe row( ' v_reporter ut_reporter;');
121+
pipe row( ' v_reporters_list ut_reporters_list := ut_reporters_list();');
122+
pipe row( 'begin');
123+
for i in 1 .. cardinality(g_call_params) loop
124+
execute immediate 'begin :l_output_id := '||l_output_type||'.generate_output_id(); end;'
125+
using out g_call_params(i).output_id;
126+
pipe row(' v_reporter := '||g_call_params(i).ut_reporter_name||'('||l_output_type||');');
127+
pipe row(' v_reporter.output.output_id := '''||g_call_params(i).output_id||''';');
128+
pipe row(' v_reporters_list.extend; v_reporters_list(v_reporters_list.last) := v_reporter;');
129+
end loop;
130+
pipe row( ' ut.run( ut_varchar2_list('||g_ut_paths||'), ut_composite_reporter( v_reporters_list ) );');
131+
pipe row( 'end;');
132+
pipe row( '/');
133+
pipe row( 'exit');
134+
135+
return;
136+
end;
137+
138+
function get_outputs_script return ut_varchar2_list pipelined is
139+
begin
140+
for i in 1 .. cardinality(g_call_params) loop
141+
pipe row('set termout '||g_call_params(i).output_to_screen);
142+
if g_call_params(i).output_file_name is not null then
143+
pipe row('spool '||g_call_params(i).output_file_name);
144+
pipe row('select * from table( '||get_streamed_output_type()||'.get_lines('''||g_call_params(i).output_id||''') );');
145+
pipe row('spool off');
146+
else
147+
pipe row('select * from table( '||get_streamed_output_type()||'.get_lines('''||g_call_params(i).output_id||''') );');
148+
end if;
149+
end loop;
150+
end;
151+
152+
end ut_runner;
153+
/

source/core/ut_runner.pks

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
create or replace package ut_runner authid definer is
2+
3+
procedure run(a_path in varchar2, a_reporter in ut_reporter);
4+
5+
-- implementation to be changed
6+
procedure run(a_paths in ut_varchar2_list, a_reporter in ut_reporter);
7+
8+
9+
10+
----------------------------
11+
-- SQLPlus executor helper procedures and functions.
12+
13+
--returns a text to be executed by sqlplus in order to make all the script call parameters optional
14+
--@param a_params_count - determines the number of parameters to be made optional (default is 100)
15+
function get_optional_params_script(a_params_count integer := 100) return ut_varchar2_list pipelined;
16+
17+
procedure set_call_params(a_params ut_varchar2_list);
18+
19+
function get_run_in_background_script return ut_varchar2_list pipelined;
20+
21+
function get_outputs_script return ut_varchar2_list pipelined;
22+
23+
end ut_runner;
24+
/

source/core/ut_suite_manager.pkb

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -453,31 +453,5 @@ create or replace package body ut_suite_manager is
453453
end loop;
454454
end configure_execution_by_path;
455455

456-
procedure run(a_paths in ut_varchar2_list, a_reporter in ut_reporter) is
457-
l_objects_to_run ut_objects_list;
458-
l_reporter ut_reporter := a_reporter;
459-
ut_running_suite ut_test_suite;
460-
begin
461-
configure_execution_by_path(a_paths,l_objects_to_run);
462-
463-
if l_objects_to_run.count > 0 then
464-
l_reporter.before_run(a_suites => l_objects_to_run);
465-
for i in 1 .. l_objects_to_run.count loop
466-
467-
ut_running_suite := treat(l_objects_to_run(i) as ut_test_suite);
468-
ut_running_suite.do_execute(l_reporter);
469-
l_objects_to_run(i) := ut_running_suite;
470-
471-
end loop;
472-
l_reporter.after_run(a_suites => l_objects_to_run);
473-
end if;
474-
end;
475-
476-
477-
procedure run(a_path in varchar2, a_reporter in ut_reporter) is
478-
begin
479-
run(ut_varchar2_list(coalesce(a_path, sys_context('userenv', 'current_schema'))), a_reporter);
480-
end run;
481-
482456
end ut_suite_manager;
483457
/

source/core/ut_suite_manager.pks

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,7 @@ create or replace package ut_suite_manager authid definer is
44

55
procedure config_schema(a_owner_name varchar2);
66

7-
procedure run(a_path in varchar2, a_reporter in ut_reporter);
8-
-- implementation to be changed
9-
procedure run(a_paths in ut_varchar2_list, a_reporter in ut_reporter);
10-
11-
--INTERNAL USE
7+
--INTERNAL USE
128
procedure configure_execution_by_path(a_paths in ut_varchar2_list, a_objects_to_run out nocopy ut_objects_list);
139

1410
end ut_suite_manager;

source/expectations/ut.pkb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ create or replace package body ut is
22

33
procedure run(a_path in varchar2, a_reporter in ut_reporter) is
44
begin
5-
ut_suite_manager.run(a_path, a_reporter);
5+
ut_runner.run(a_path, a_reporter);
66
end;
77

88
procedure run(a_paths in ut_varchar2_list, a_reporter in ut_reporter) is
99
begin
10-
ut_suite_manager.run(a_paths, a_reporter);
10+
ut_runner.run(a_paths, a_reporter);
1111
end;
1212

1313
function expect(a_actual in anydata, a_message varchar2 := null) return ut_expectation_anydata is

source/install.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ whenever oserror exit failure rollback
4343
--suite manager
4444
@@core/ut_suite_manager.pks
4545
@@core/ut_suite_manager.pkb
46+
--test runner
47+
@@core/ut_runner.pks
48+
@@core/ut_runner.pkb
4649

4750
--assertios execution state interface
4851
@@core/ut_assert_processor.pks

source/uninstall.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ prompt Uninstalling utplsql framework
22

33
drop package ut_teamcity_reporter_helper;
44

5+
drop package ut_runner;
6+
57
drop package ut_suite_manager;
68

79
drop package ut_assert;

0 commit comments

Comments
 (0)