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

Skip to content

Commit fe4b993

Browse files
committed
actualized ut_utils tests to current state of old tests
1 parent 746eb07 commit fe4b993

2 files changed

Lines changed: 315 additions & 176 deletions

File tree

test/ut_utils/test_ut_utils.pkb

Lines changed: 261 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -1,130 +1,287 @@
1-
create or replace package body test_ut_utils as
1+
create or replace package body test_ut_utils is
22

3-
procedure clob_to_table_test(
4-
a_clob clob, a_expected ut3.ut_varchar2_list,
5-
a_delimiter varchar2 := ',',
6-
a_overflow_limit integer := 1000
7-
) is
8-
l_result ut3.ut_varchar2_list;
9-
begin
10-
--Act
11-
l_result := ut3.ut_utils.clob_to_table(a_clob, a_overflow_limit, a_delimiter);
12-
--Assert
13-
ut.expect(anydata.convertCollection(l_result)).to_( equal(anydata.convertCollection(a_expected)) );
14-
end;
3+
gv_nls_value nls_session_parameters.value%type;
154

16-
procedure clob_to_table_by_delim is
5+
procedure common_clob_to_table_exec(p_clob varchar2, p_delimiter varchar2, p_expected_list ut3.ut_varchar2_list, p_limit number) is
6+
begin
7+
execute immediate 'declare
8+
l_clob clob := '''||p_clob||''';
9+
l_delimiter varchar2(1) := '''||p_delimiter||''';
10+
l_expected ut3.ut_varchar2_list := :p_expected_list;
11+
l_result ut3.ut_varchar2_list;
12+
l_limit integer := '||p_limit||q'[;
13+
l_result_str varchar2(32767);
14+
l_errors integer := 0;
15+
function compare_element(a_element_id integer, a_expected ut3.ut_varchar2_list, a_actual ut3.ut_varchar2_list) return integer is
1716
begin
18-
clob_to_table_test( a_clob => 'a,b,c,d', a_expected => ut3.ut_varchar2_list('a','b','c','d') );
17+
if a_expected.exists(a_element_id) and a_actual.exists(a_element_id) then
18+
if a_expected(a_element_id) = a_actual(a_element_id) or a_expected(a_element_id) is null and a_actual(a_element_id) is null then
19+
return 0;
20+
else
21+
dbms_output.put('a_expected('||a_element_id||')='||a_expected(a_element_id)||' | a_actual('||a_element_id||')='||a_actual(a_element_id));
22+
end if;
23+
end if;
24+
if not a_expected.exists(a_element_id) then
25+
dbms_output.put('a_expected('||a_element_id||') does not exist ');
26+
end if;
27+
if not a_actual.exists(a_element_id) then
28+
dbms_output.put('a_actual('||a_element_id||') does not exist ');
29+
end if;
30+
dbms_output.put_line(null);
31+
return 1;
32+
end;
33+
begin
34+
--Act
35+
select column_value bulk collect into l_result from table( ut3.ut_utils.clob_to_table(l_clob, l_limit, l_delimiter) );
36+
for i in 1 .. l_result.count loop
37+
l_result_str := l_result_str||''''||l_result(i)||''''||l_delimiter;
38+
end loop;
39+
l_result_str := rtrim(l_result_str,l_delimiter);
40+
--Assert
41+
for i in 1 .. greatest(l_expected.count, l_result.count) loop
42+
l_errors := l_errors + compare_element(i, l_expected, l_result);
43+
end loop;
44+
ut.expect(l_errors).to_equal(0);
45+
end;]' using p_expected_list;
1946
end;
2047

21-
--%test(clob_to_table returns empty table for null clob)
22-
procedure clob_to_table_null_data is
48+
procedure test_clob_to_table is
49+
begin
50+
common_clob_to_table_exec('a,b,c,d', ',', ut3.ut_varchar2_list('a','b','c','d'), 1000);
51+
common_clob_to_table_exec( '', ',', ut3.ut_varchar2_list(), 1000);
52+
common_clob_to_table_exec( '1,b,c,d', '', ut3.ut_varchar2_list('1,b,','c,d'), 4);
53+
common_clob_to_table_exec( 'abcdefg,hijk,axa,a', ',', ut3.ut_varchar2_list('abc','def','g','hij','k','axa','a'), 3);
54+
common_clob_to_table_exec( ',a,,c,d,', ',', ut3.ut_varchar2_list('','a','','c','d',''), 1000);
55+
end;
56+
57+
procedure test_to_char is
58+
begin
59+
ut.expect(ut3.ut_utils.test_result_to_char(-1),'test unknown').to_equal('Unknown(-1)');
60+
ut.expect(ut3.ut_utils.test_result_to_char(null),'test unknown').to_equal('Unknown(NULL)');
61+
ut.expect(ut3.ut_utils.test_result_to_char(ut3.ut_utils.tr_success),'test unknown').to_equal(ut3.ut_utils.tr_success_char);
62+
end;
63+
64+
procedure test_to_string_blob is
65+
l_text varchar2(32767) := 'A test char';
66+
l_value blob := utl_raw.cast_to_raw(l_text);
67+
l_expected varchar2(32767) := ''''||rawtohex(l_value)||'''';
68+
l_result varchar2(32767);
2369
begin
24-
clob_to_table_test( a_clob => null, a_expected => ut3.ut_varchar2_list() );
70+
l_result := ut3.ut_utils.to_String(l_value);
71+
ut.expect(l_result).to_equal(l_expected);
2572
end;
73+
74+
procedure test_to_string_clob is
75+
l_value clob := 'A test char';
76+
l_expected varchar2(32767) := ''''||l_value||'''';
77+
l_result varchar2(32767);
78+
begin
79+
l_result := ut3.ut_utils.to_String(l_value);
80+
ut.expect(l_result).to_equal(l_expected);
81+
end;
82+
83+
procedure test_to_string_date is
84+
l_value date := to_date('2016-12-31 23:59:59', 'yyyy-mm-dd hh24:mi:ss');
85+
l_expected varchar2(100) := '2016-12-31T23:59:59';
86+
l_result varchar2(32767);
87+
begin
88+
l_result := ut3.ut_utils.to_String(l_value);
89+
ut.expect(l_result).to_equal(l_expected);
90+
end;
91+
92+
procedure to_string_null is
93+
begin
94+
ut.expect(ut3.ut_utils.to_String(to_blob(NULL))).to_equal('NULL');
95+
ut.expect(ut3.ut_utils.to_String(to_clob(NULL))).to_equal('NULL');
96+
ut.expect(ut3.ut_utils.to_String(to_date(NULL))).to_equal('NULL');
97+
ut.expect(ut3.ut_utils.to_String(to_number(NULL))).to_equal('NULL');
98+
ut.expect(ut3.ut_utils.to_String(to_timestamp(NULL))).to_equal('NULL');
99+
end;
100+
101+
procedure to_string is
102+
l_value timestamp(9) := to_timestamp('2016-12-31 23:59:59.123456789', 'yyyy-mm-dd hh24:mi:ss.ff');
103+
l_value2 timestamp(9) with local time zone:= to_timestamp('2016-12-31 23:59:59.123456789', 'yyyy-mm-dd hh24:mi:ss.ff');
104+
l_value3 timestamp(9) with time zone := to_timestamp_tz('2016-12-31 23:59:59.123456789 -8:00', 'yyyy-mm-dd hh24:mi:ss.ff tzh:tzm');
105+
l_value4 varchar2(20) := 'A test char';
106+
l_expected varchar2(100);
107+
l_result varchar2(100);
108+
l_delimiter varchar2(10);
109+
begin
110+
select substr(value, 1, 1) into l_delimiter from nls_session_parameters t where t.parameter = 'NLS_NUMERIC_CHARACTERS';
111+
l_expected := '2016-12-31T23:59:59'||l_delimiter||'123456789';
112+
113+
l_result := ut3.ut_utils.to_String(l_value);
114+
ut.expect(l_result,'Returns a full string representation of a timestamp with maximum precission').to_equal(l_expected);
115+
116+
l_expected := '2016-12-31T23:59:59'||l_delimiter||'123456789';
117+
l_result := ut3.ut_utils.to_String(l_value2);
118+
ut.expect(l_result,'Returns a full string representation of a timestamp with maximum precission').to_equal(l_expected);
26119

27-
--%test(clob_to_table splits table by char limit when no delimiter)
28-
procedure clob_to_table_char_limit is
29-
begin
30-
clob_to_table_test(
31-
a_clob => '1,2,3,4',
32-
a_expected => ut3.ut_varchar2_list('1,2,','3,4'),
33-
a_delimiter => '',
34-
a_overflow_limit => 4
35-
);
120+
l_expected := '2016-12-31T23:59:59'||l_delimiter||'123456789 -08:00';
121+
122+
l_result := ut3.ut_utils.to_String(l_value3);
123+
ut.expect(l_result,'Returns a full string representation of a timestamp with maximum precission').to_equal(l_expected);
124+
125+
l_expected := ''''||l_value4||'''';
126+
l_result := ut3.ut_utils.to_String(l_value4);
127+
ut.expect(l_result,'Returns a varchar2 eclosed in quotes').to_equal(l_expected);
128+
36129
end;
130+
131+
procedure to_string_big_blob is
132+
l_text clob := lpad('A test char',32767,'1')||lpad('1',32767,'1');
133+
l_value blob;
134+
l_result varchar2(32767);
135+
function clob_to_blob(p_clob clob) return blob
136+
as
137+
l_blob blob;
138+
l_dest_offset integer := 1;
139+
l_source_offset integer := 1;
140+
l_lang_context integer := dbms_lob.default_lang_ctx;
141+
l_warning integer := dbms_lob.warn_inconvertible_char;
142+
begin
143+
dbms_lob.createtemporary(l_blob, true);
144+
dbms_lob.converttoblob(
145+
dest_lob =>l_blob,
146+
src_clob =>p_clob,
147+
amount =>DBMS_LOB.LOBMAXSIZE,
148+
dest_offset =>l_dest_offset,
149+
src_offset =>l_source_offset,
150+
blob_csid =>DBMS_LOB.DEFAULT_CSID,
151+
lang_context=>l_lang_context,
152+
warning =>l_warning
153+
);
154+
return l_blob;
155+
end;
156+
begin
157+
l_value := clob_to_blob(l_text);
158+
--Act
159+
l_result := ut3.ut_utils.to_String(l_value);
160+
--Assert
161+
ut.EXPECT(length(l_result)).to_equal(ut3.ut_utils.gc_max_output_string_length);
162+
ut.EXPECT(l_result).to_be_like('%'||ut3.ut_utils.gc_more_data_string);
37163

38-
--%test(clob_to_table splits table by char limit on overflow and continues by delimiter)
39-
procedure clob_to_table_char_limit_delim is
164+
end;
165+
166+
procedure to_string_big_clob is
167+
l_value clob := lpad('A test char',32767,'1')||lpad('1',32767,'1');
168+
l_result varchar2(32767);
40169
begin
41-
clob_to_table_test(
42-
a_clob => 'abcdefg,hijk,axa,a',
43-
a_expected => ut3.ut_varchar2_list('abc','def','g','hij','k','axa','a'),
44-
a_overflow_limit => 3
45-
);
170+
--Act
171+
l_result := ut3.ut_utils.to_String(l_value);
172+
--Assert
173+
ut.EXPECT(length(l_result)).to_equal(ut3.ut_utils.gc_max_output_string_length);
174+
ut.EXPECT(l_result).to_be_like('%'||ut3.ut_utils.gc_more_data_string);
46175
end;
47-
48-
--%test(clob_to_table returns empty lines for null data between delimiter)
49-
procedure clob_to_table_empty_lines is
176+
177+
procedure to_string_big_number is
178+
l_value number := 1234567890123456789012345678901234567890;
179+
l_expected varchar2(100) := '1234567890123456789012345678901234567890';
180+
l_result varchar2(100);
50181
begin
51-
clob_to_table_test(
52-
a_clob => ',a,,c,d,',
53-
a_expected => ut3.ut_varchar2_list('','a','','c','d','')
54-
);
182+
--Act
183+
l_result := ut3.ut_utils.to_String(l_value);
184+
--Assert
185+
ut.expect(l_result).TO_equal(l_expected);
55186
end;
56-
57-
--%test(test_result_to_char)
58-
procedure test_result_to_char is
187+
188+
procedure to_string_big_varchar2 is
189+
l_value varchar2(32767) := lpad('A test char',32767,'1');
190+
l_result varchar2(32767);
59191
begin
60-
ut.expect( ut3.ut_utils.test_result_to_char(NULL)).to_equal('Unknown(NULL)');
61-
ut.expect( ut3.ut_utils.test_result_to_char(-1)).to_equal('Unknown(-1)');
62-
ut.expect( ut3.ut_utils.test_result_to_char(ut3.ut_utils.tr_disabled)).to_equal(ut3.ut_utils.tr_disabled_char);
63-
ut.expect( ut3.ut_utils.test_result_to_char(ut3.ut_utils.tr_success)).to_equal(ut3.ut_utils.tr_success_char);
64-
ut.expect( ut3.ut_utils.test_result_to_char(ut3.ut_utils.tr_failure)).to_equal(ut3.ut_utils.tr_failure_char);
65-
ut.expect( ut3.ut_utils.test_result_to_char(ut3.ut_utils.tr_error)).to_equal(ut3.ut_utils.tr_error_char);
192+
--Act
193+
l_result := ut3.ut_utils.to_String(l_value);
194+
--Assert
195+
ut.EXPECT(length(l_result)).to_equal(ut3.ut_utils.gc_max_output_string_length);
196+
ut.EXPECT(l_result).to_be_like('%'||ut3.ut_utils.gc_more_data_string);
66197
end;
67198

68-
--%test(to_test_result converts boolean value to test result integer)
69-
procedure to_test_result is
199+
procedure to_string_big_tiny_number is
200+
l_value number := 0.123456789012345678901234567890123456789;
201+
l_expected varchar2(100);
202+
l_result varchar2(100);
203+
l_delimiter varchar2(1);
70204
begin
71-
ut.expect( ut3.ut_utils.to_test_result(true)).to_equal(ut3.ut_utils.tr_success);
72-
ut.expect( ut3.ut_utils.to_test_result(false)).to_equal(ut3.ut_utils.tr_failure);
73-
ut.expect( ut3.ut_utils.to_test_result(null)).to_equal(ut3.ut_utils.tr_failure);
74-
end;
205+
--Act
206+
select substr(value, 1, 1) into l_delimiter from nls_session_parameters t where t.parameter = 'NLS_NUMERIC_CHARACTERS';
207+
l_expected := l_delimiter||'123456789012345678901234567890123456789';
208+
209+
l_result := ut3.ut_utils.to_String(l_value);
210+
211+
--Assert
212+
ut.expect(l_result).TO_equal(l_expected);
75213

76-
--%test(to_string on null blob)
77-
procedure to_string_null_blob is
214+
end;
215+
216+
procedure test_table_to_clob is
217+
procedure exec_table_to_clob(a_list ut3.ut_varchar2_list, a_delimiter varchar2, a_expected clob) is
218+
l_result clob;
219+
begin
220+
l_result := ut3.ut_utils.table_to_clob(a_list, a_delimiter);
221+
222+
ut.expect(l_result).to_equal(a_expected, a_nulls_are_equal => true);
223+
end;
78224
begin
79-
ut.expect( ut3.ut_utils.to_string(to_blob(null)) ).to_equal('NULL');
225+
exec_table_to_clob(null, ',', '');
226+
exec_table_to_clob(ut3.ut_varchar2_list(), ',', '');
227+
exec_table_to_clob(ut3.ut_varchar2_list('a', 'b', 'c', 'd'), ',', 'a,b,c,d');
228+
exec_table_to_clob(ut3.ut_varchar2_list('1,b,', 'c,d'), ',', '1,b,,c,d');
229+
exec_table_to_clob(ut3.ut_varchar2_list('', 'a', '', 'c', 'd', ''), ',', ',a,,c,d,');
80230
end;
81231

82-
--%test(to_string on blob)
83-
procedure to_string_blob is
84-
l_text varchar2(32767) := 'A test char';
85-
l_value blob := utl_raw.cast_to_raw(l_text);
86-
l_expected varchar2(32767) := ''''||rawtohex(l_value)||'''';
232+
procedure test_append_with_multibyte is
233+
l_lines sys.dbms_preprocessor.source_lines_t;
234+
l_result clob;
87235
begin
88-
ut.expect( ut3.ut_utils.to_string(l_value) ).to_equal(l_expected);
236+
l_lines := sys.dbms_preprocessor.get_post_processed_source(
237+
object_type => 'PACKAGE',
238+
schema_name => user,
239+
object_name => 'TST_CHARS'
240+
);
241+
242+
for i in 1..l_lines.count loop
243+
l_result := null;
244+
ut3.ut_utils.append_to_clob(l_result, l_lines(i));
245+
246+
--Assert
247+
ut.expect(dbms_lob.getlength(l_result),'Error for index '||i).to_equal(dbms_lob.getlength(l_lines(i)));
248+
end loop;
89249
end;
90250

91-
-- --%test(to_string on null clob)
92-
-- procedure to_string_null_clob;
93-
-- --%test(to_string on clob)
94-
-- procedure to_string_clob;
95-
-- --%test(to_string on clob no surrounding quotes)
96-
-- procedure to_string_clob_no_quotes;
97-
-- --%test(to_string on clob other surrounding quotes)
98-
-- procedure to_string_clob_other_quotes;
99-
--
100-
-- --%test(to_string on null number)
101-
-- procedure to_string_null_number;
102-
-- --%test(to_string on number)
103-
-- procedure to_string_number;
104-
--
105-
-- --%test(to_string on null timestamp)
106-
-- procedure to_string_null_timestamp;
107-
-- --%test(to_string on timestamp)
108-
-- procedure to_string_timestamp;
109-
--
110-
-- --%test(to_string on null timestamp with time zone)
111-
-- procedure to_string_null_timestamp_tz;
112-
-- --%test(to_string on timestamp with time zone)
113-
-- procedure to_string_timestamp_tz;
114-
--
115-
-- --%test(to_string on null timestamp with local time zone)
116-
-- procedure to_string_null_timestamp_ltz;
117-
-- --%test(to_string on timestamp with local time zone)
118-
-- procedure to_string_timestamp_ltz;
119-
--
120-
-- --%test(to_string on null varchar)
121-
-- procedure to_string_null_varchar;
122-
-- --%test(to_string on varchar)
123-
-- procedure to_string_varchar;
124-
-- --%test(to_string on varchar no surrounding quotes)
125-
-- procedure to_string_varchar_no_quotes;
126-
-- --%test(to_string on varchar non default surrounding quotes)
127-
-- procedure to_string_varchar_other_quotes;
251+
procedure setup_append_with_multibyte is
252+
pragma autonomous_transaction;
253+
begin
254+
select value into gv_nls_value from nls_session_parameters where parameter = 'NLS_DATE_LANGUAGE';
255+
execute immediate 'alter session set nls_date_language=ENGLISH';
256+
execute immediate 'create or replace package tst_chars as
257+
-- 2) Status of the process = �PE� with no linked data
258+
end;';
259+
execute immediate 'alter session set nls_date_language=RUSSIAN';
260+
261+
end;
262+
procedure clean_append_with_multibyte is
263+
pragma autonomous_transaction;
264+
begin
265+
execute immediate 'alter session set nls_date_language='||gv_nls_value;
266+
execute immediate 'drop package tst_chars';
267+
end;
268+
269+
procedure test_clob_to_table_multibyte is
270+
l_varchar2_byte_limit integer := 32767;
271+
l_workaround_byte_limit integer := 8191;
272+
l_singlebyte_string_max_size varchar2(32767 char) := rpad('x',l_varchar2_byte_limit,'x');
273+
l_twobyte_character char(1 char) := '�';
274+
l_clob_multibyte clob := l_twobyte_character||l_singlebyte_string_max_size; --here we have 32769(2+32767) bytes and 32768 chars
275+
l_expected ut3.ut_varchar2_list := ut3.ut_varchar2_list();
276+
l_result ut3.ut_varchar2_list;
277+
begin
278+
l_expected.extend(1);
279+
l_expected(1) := l_twobyte_character||substr(l_singlebyte_string_max_size,1,l_workaround_byte_limit-1);
280+
--Act
281+
l_result := ut3.ut_utils.clob_to_table(l_clob_multibyte);
282+
--Assert
283+
ut.expect(l_result(1)).to_equal(l_expected(1));
284+
end;
128285

129-
end;
130-
/
286+
end test_ut_utils;
287+
/

0 commit comments

Comments
 (0)