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

Skip to content

Commit ac042d0

Browse files
committed
Added failure on missing join by key in cursor.
1 parent fdff62a commit ac042d0

3 files changed

Lines changed: 91 additions & 8 deletions

File tree

source/expectations/data_values/ut_compound_data_helper.pkb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,48 @@ create or replace package body ut_compound_data_helper is
464464
return l_cols_hash;
465465
end;
466466

467+
function is_pk_exists(a_expected_cursor xmltype,a_actual_cursor xmltype, a_exclude_xpath varchar2, a_include_xpath varchar2,a_join_by_xpath varchar2)
468+
return tt_missing_pk is
469+
l_pk_xpath_tabs ut_varchar2_list := ut_varchar2_list();
470+
l_column_filter varchar2(32767);
471+
l_no_missing_keys tt_missing_pk := tt_missing_pk();
472+
begin
473+
if a_join_by_xpath is not null then
474+
l_pk_xpath_tabs := ut_utils.string_to_table(a_join_by_xpath,'|');
475+
l_column_filter := get_columns_row_filter(a_exclude_xpath, a_include_xpath);
476+
477+
execute immediate q'[
478+
with xpaths_tab as (select column_value xpath from table(:xpath_tabs)),
479+
expected_column_info as ( select :expected as item_data from dual ),
480+
actual_column_info as ( select :actual as item_data from dual )
481+
select xpath,diif_type from
482+
(
483+
(select xpath,'e' diif_type from xpaths_tab
484+
minus
485+
select xpath,'e' diif_type
486+
from ( select ]'||l_column_filter||q'[ from expected_column_info ucd) x
487+
,xpaths_tab
488+
where xmlexists (xpaths_tab.xpath passing x.item_data)
489+
)
490+
union all
491+
(select xpath,'a' diif_type from xpaths_tab
492+
minus
493+
select xpath,'a' diif_type
494+
from ( select ]'||l_column_filter||q'[ from actual_column_info ucd) x
495+
,xpaths_tab
496+
where xmlexists (xpaths_tab.xpath passing x.item_data)
497+
)
498+
)]' bulk collect into l_no_missing_keys
499+
using l_pk_xpath_tabs,a_expected_cursor,a_actual_cursor,
500+
a_exclude_xpath, a_include_xpath,
501+
a_exclude_xpath, a_include_xpath;
502+
503+
end if;
504+
505+
return l_no_missing_keys;
506+
end;
507+
508+
467509
begin
468510
g_type_name_map( dbms_sql.binary_bouble_type ) := 'BINARY_DOUBLE';
469511
g_type_name_map( dbms_sql.bfile_type ) := 'BFILE';

source/expectations/data_values/ut_compound_data_helper.pks

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ create or replace package ut_compound_data_helper authid definer is
2828

2929
type tt_column_diffs is table of t_column_diffs;
3030

31+
type t_missing_pk is record(
32+
missingxpath varchar2(250),
33+
diff_type varchar2(1)
34+
);
35+
36+
type tt_missing_pk is table of t_missing_pk;
37+
3138
type t_row_diffs is record(
3239
rn integer,
3340
diff_type varchar2(250),
@@ -72,6 +79,9 @@ create or replace package ut_compound_data_helper authid definer is
7279
a_data_value_cursor ut_data_value_refcursor, a_exclude_xpath varchar2, a_include_xpath varchar2,
7380
a_hash_type binary_integer := dbms_crypto.hash_sh1
7481
) return t_hash;
82+
83+
function is_pk_exists(a_expected_cursor xmltype, a_actual_cursor xmltype, a_exclude_xpath varchar2, a_include_xpath varchar2,a_join_by_xpath varchar2)
84+
return tt_missing_pk;
7585

7686
end;
7787
/

source/expectations/data_values/ut_data_value_refcursor.tpb

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ create or replace type body ut_data_value_refcursor as
115115
l_actual ut_data_value_refcursor;
116116
l_column_diffs ut_compound_data_helper.tt_column_diffs := ut_compound_data_helper.tt_column_diffs();
117117
l_exclude_xpath varchar2(32767) := a_exclude_xpath;
118-
118+
l_missing_pk ut_compound_data_helper.tt_missing_pk := ut_compound_data_helper.tt_missing_pk();
119+
119120
function get_col_diff_text(a_col ut_compound_data_helper.t_column_diffs) return varchar2 is
120121
begin
121122
return
@@ -130,7 +131,18 @@ create or replace type body ut_data_value_refcursor as
130131
' Column <'||a_col.actual_name||'> is misplaced. Expected position: '||a_col.expected_pos||',' ||' actual position: '||a_col.actual_pos||'.'
131132
end;
132133
end;
133-
134+
135+
function get_missing_key_message(a_missing_keys ut_compound_data_helper.t_missing_pk) return varchar2 is
136+
begin
137+
return
138+
case a_missing_keys.diff_type
139+
when 'a' then
140+
' Unknown key to join by in actual:'||a_missing_keys.missingxpath
141+
when 'e' then
142+
' Unknown key to join by in expected:'||a_missing_keys.missingxpath
143+
end;
144+
end;
145+
134146
function add_incomparable_cols_to_xpath(
135147
a_column_diffs ut_compound_data_helper.tt_column_diffs, a_exclude_xpath varchar2
136148
) return varchar2 is
@@ -151,6 +163,7 @@ create or replace type body ut_data_value_refcursor as
151163
end if;
152164
return l_result;
153165
end;
166+
154167
begin
155168
if not a_other is of (ut_data_value_refcursor) then
156169
raise value_error;
@@ -176,12 +189,20 @@ create or replace type body ut_data_value_refcursor as
176189
end if;
177190

178191
--diff rows and row elements
179-
if a_join_by_xpath is not null then
180-
ut_utils.append_to_clob(l_result, self.get_data_diff(a_other, a_exclude_xpath, a_include_xpath, a_join_by_xpath));
181-
elsif a_unordered then
182-
ut_utils.append_to_clob(l_result, self.get_data_diff(a_other, l_exclude_xpath, a_include_xpath, a_unordered));
192+
if (a_join_by_xpath is not null) or not(a_unordered) then
193+
-- Check if pk filter exists
194+
l_missing_pk := ut_compound_data_helper.is_pk_exists(self.columns_info, l_actual.columns_info, a_exclude_xpath, a_include_xpath,a_join_by_xpath);
195+
if l_missing_pk.count = 0 then
196+
ut_utils.append_to_clob(l_result, self.get_data_diff(a_other, a_exclude_xpath, a_include_xpath, a_join_by_xpath));
197+
else
198+
ut_utils.append_to_clob(l_result,chr(10) || 'Unable to join sets:' || chr(10));
199+
for i in 1 .. l_missing_pk.count loop
200+
l_results.extend;
201+
ut_utils.append_to_clob(l_result, get_missing_key_message(l_missing_pk(i)));
202+
end loop;
203+
end if;
183204
else
184-
ut_utils.append_to_clob(l_result, self.get_data_diff(a_other, l_exclude_xpath, a_include_xpath, a_join_by_xpath));
205+
ut_utils.append_to_clob(l_result, self.get_data_diff(a_other, l_exclude_xpath, a_include_xpath, a_unordered));
185206
end if;
186207

187208
l_result_string := ut_utils.to_string(l_result,null);
@@ -211,19 +232,29 @@ create or replace type body ut_data_value_refcursor as
211232
overriding member function compare_implementation (a_other ut_data_value, a_exclude_xpath varchar2, a_include_xpath varchar2, a_join_by_xpath varchar2, a_unordered boolean) return integer is
212233
l_result integer := 0;
213234
l_other ut_data_value_refcursor;
235+
l_missing_pk ut_compound_data_helper.tt_missing_pk := ut_compound_data_helper.tt_missing_pk();
214236
begin
215237
if not a_other is of (ut_data_value_refcursor) then
216238
raise value_error;
217239
end if;
218240

219241
l_other := treat(a_other as ut_data_value_refcursor);
242+
243+
l_missing_pk := ut_compound_data_helper.is_pk_exists(self.columns_info, l_other.columns_info, a_exclude_xpath, a_include_xpath,a_join_by_xpath);
244+
--if we join by key and key is missing fail and report error
245+
if a_join_by_xpath is not null and l_missing_pk.count > 0 then
246+
l_result := 1;
247+
return l_result;
248+
end if;
249+
220250
--if column names/types are not equal - build a diff of column names and types
221251
if ut_compound_data_helper.columns_hash( self, a_exclude_xpath, a_include_xpath )
222252
!= ut_compound_data_helper.columns_hash( l_other, a_exclude_xpath, a_include_xpath )
223253
then
224254
l_result := 1;
225255
end if;
226-
l_result := l_result + (self as ut_compound_data_value).compare_implementation(a_other, a_exclude_xpath, a_include_xpath, a_join_by_xpath, a_unordered);
256+
257+
l_result := l_result + (self as ut_compound_data_value).compare_implementation(a_other, a_exclude_xpath, a_include_xpath, a_join_by_xpath, a_unordered);
227258
return l_result;
228259
end;
229260

0 commit comments

Comments
 (0)