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

Skip to content

Commit 1b0b8f8

Browse files
committed
Refactored expectations on compound data
With this change the reporting of differences is unified across: `unordered`, `ordered` and `join_by` comparison.
1 parent 101bcbf commit 1b0b8f8

3 files changed

Lines changed: 90 additions & 69 deletions

File tree

source/expectations/data_values/ut_compound_data_helper.pkb

Lines changed: 78 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -417,73 +417,99 @@ create or replace package body ut_compound_data_helper is
417417
l_join_xpath varchar2(32767) := ut_utils.to_xpath(a_join_by_list);
418418
l_results tt_row_diffs;
419419
l_sql varchar2(32767);
420+
l_column_order varchar2(100);
420421
begin
422+
if a_enforce_column_order then
423+
l_column_order := 'col_no';
424+
else
425+
l_column_order := 'col_name';
426+
end if;
421427
l_sql := q'[
422-
with exp as (
428+
with
429+
exp_rows as(
430+
select
431+
exp_data_id, extract( exp_item_data, :column_path ) exp_item_data, item_no, diff_id
432+
from ut_compound_data_diff_tmp
433+
where diff_id = :diff_id
434+
and exp_data_id = :self_guid
435+
),
436+
act_rows as (
437+
select
438+
act_data_id, extract( act_item_data, :column_path ) act_item_data, item_no, diff_id
439+
from ut_compound_data_diff_tmp ucd
440+
where diff_id = :diff_id
441+
and ucd.act_data_id = :other_guid
442+
),
443+
exp_cols as (
423444
select
424-
exp_item_data, exp_data_id, item_no rn, rownum col_no, pk_value,
445+
exp_item_data, exp_data_id, item_no rn, rownum col_no, diff_id,
425446
s.column_value col, s.column_value.getRootElement() col_name,
426447
nvl(s.column_value.getclobval(),empty_clob()) col_val
427-
from (
428-
select
429-
exp_data_id, extract( ucd.exp_item_data, :column_path ) exp_item_data, item_no,
430-
replace( extract( ucd.exp_item_data, :join_by ).getclobval(), chr(10) ) pk_value
431-
from ut_compound_data_diff_tmp ucd
432-
where diff_id = :diff_id
433-
and ucd.exp_data_id = :self_guid
434-
) i,
435-
table( xmlsequence( extract(i.exp_item_data,:extract_path) ) ) s
448+
from exp_rows i,
449+
table( xmlsequence( extract( i.exp_item_data, :extract_path ) ) ) s
436450
),
437-
act as (
451+
act_cols as (
438452
select
439-
act_item_data, act_data_id, item_no rn, rownum col_no, pk_value,
453+
act_item_data, act_data_id, item_no rn, rownum col_no, diff_id,
440454
s.column_value col, s.column_value.getRootElement() col_name,
441455
nvl(s.column_value.getclobval(),empty_clob()) col_val
456+
from act_rows i,
457+
table( xmlsequence( extract( i.act_item_data, :extract_path ) ) ) s
458+
),
459+
data_diff as (
460+
select rn, diff_type, xmlserialize(content data_item no indent) diffed_row, null pk_value, col_name, diff_id
442461
from (
443-
select
444-
act_data_id, extract( ucd.act_item_data, :column_path ) act_item_data, item_no,
445-
replace( extract( ucd.act_item_data, :join_by ).getclobval(), chr(10) ) pk_value
446-
from ut_compound_data_diff_tmp ucd
447-
where diff_id = :diff_id
448-
and ucd.act_data_id = :other_guid
449-
) i,
450-
table( xmlsequence( extract(i.act_item_data,:extract_path) ) ) s
462+
select nvl(exp.rn, act.rn) rn,
463+
exp.diff_id diff_id,
464+
xmlagg(exp.col order by exp.col_no) exp_item,
465+
xmlagg(act.col order by nvl(exp.col_no, act.col_no)) act_item,
466+
max(nvl(exp.col_name,act.col_name)) col_name
467+
from exp_cols exp
468+
join act_cols act
469+
on exp.rn = act.rn and exp.col_name = act.col_name
470+
where dbms_lob.compare(exp.col_val, act.col_val) != 0
471+
group by nvl(exp.rn, act.rn), exp.diff_id
472+
)
473+
unpivot ( data_item for diff_type in (exp_item as 'Expected:', act_item as 'Actual:') )
474+
),
475+
unordered_diff as (
476+
select rn, diff_type, xmlserialize(content data_item no indent) diffed_row, null pk_value, col_name, diff_id
477+
from (
478+
select nvl(exp.rn, act.rn) rn,
479+
exp.diff_id diff_id,
480+
xmlagg(exp.col order by exp.col_name) exp_item,
481+
xmlagg(act.col order by act.col_name) act_item,
482+
max(nvl(exp.col_name,act.col_name)) col_name
483+
from exp_cols exp
484+
join act_cols act
485+
on exp.rn = act.rn and exp.col_name = act.col_name
486+
where dbms_lob.compare(exp.col_val, act.col_val) != 0
487+
group by nvl(exp.rn, act.rn), exp.diff_id
488+
)
489+
unpivot ( data_item for diff_type in (exp_item as 'Expected:', act_item as 'Actual:') )
451490
)
452-
select rn, diff_type, diffed_row, pk_value pk_value
491+
select rn, diff_type, diffed_row, pk_value
453492
from (
454493
select rn, diff_type, diffed_row, pk_value,
455494
case when diff_type = 'Actual:' then 1 else 2 end rnk,
456495
1 final_order,
457496
col_name
458497
from ( ]'
459498
|| case when a_unordered then q'[
460-
select rn, diff_type, xmlserialize(content data_item no indent) diffed_row, pk_value, col_name
461-
from (
462-
select nvl(exp.rn, act.rn) rn,
463-
nvl(exp.pk_value, act.pk_value) pk_value,
464-
exp.col exp_item,
465-
act.col act_item,
466-
nvl(exp.col_name,act.col_name) col_name
467-
from exp
468-
join act
469-
on exp.rn = act.rn and exp.col_name = act.col_name
470-
where dbms_lob.compare(exp.col_val, act.col_val) != 0
471-
)
472-
unpivot ( data_item for diff_type in (exp_item as 'Expected:', act_item as 'Actual:') ) ]'
499+
select u.rn, u.diff_type, u.diffed_row,
500+
replace(
501+
extract( case when i.exp_data_id is null then i.act_item_data else i.exp_item_data end, :join_by ).getclobval(),
502+
chr(10)
503+
) pk_value,
504+
u.col_name
505+
from data_diff u
506+
join ut_compound_data_diff_tmp i
507+
on i.diff_id = u.diff_id
508+
and i.item_no = u.rn]'
473509
else q'[
474-
select rn, diff_type, xmlserialize(content data_item no indent) diffed_row, null pk_value, col_name
475-
from (
476-
select nvl(exp.rn, act.rn) rn,
477-
xmlagg(exp.col order by exp.col_no) exp_item,
478-
xmlagg(act.col order by act.col_no) act_item,
479-
max(nvl(exp.col_name,act.col_name)) col_name
480-
from exp exp
481-
join act act
482-
on exp.rn = act.rn and exp.col_name = act.col_name
483-
where dbms_lob.compare(exp.col_val, act.col_val) != 0
484-
group by (exp.rn, act.rn)
485-
)
486-
unpivot ( data_item for diff_type in (exp_item as 'Expected:', act_item as 'Actual:') ) ]'
510+
select rn, diff_type, diffed_row, pk_value, col_name
511+
from data_diff
512+
where :join_by is null ]'
487513
end ||q'[
488514
)
489515
union all
@@ -522,14 +548,13 @@ create or replace package body ut_compound_data_helper is
522548
case when final_order = 1 then to_char(rn) else col_name end,
523549
case when final_order = 1 then to_char(rnk) else col_name end
524550
]'
525-
else
526-
null
527551
end;
528552
execute immediate l_sql
529553
bulk collect into l_results
530-
using l_exp_extract_xpath, l_join_xpath, a_diff_id, a_expected_dataset_guid,a_extract_path,
531-
l_act_extract_xpath, l_join_xpath, a_diff_id, a_actual_dataset_guid,a_extract_path,
532-
l_join_xpath, l_join_xpath, a_diff_id;
554+
using l_exp_extract_xpath, a_diff_id, a_expected_dataset_guid,
555+
l_act_extract_xpath, a_diff_id, a_actual_dataset_guid,
556+
a_extract_path, a_extract_path,
557+
l_join_xpath, l_join_xpath, l_join_xpath, a_diff_id;
533558
return l_results;
534559
end;
535560

test/ut3_user/expectations/test_expectation_anydata.pkb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -864,10 +864,8 @@ Rows: [ 60 differences, showing first 20 ]
864864
l_expected_message := q'[%Actual: ut3_tester_helper.test_dummy_object_list [ count = 2 ] was expected to equal: ut3_tester_helper.test_dummy_object_list [ count = 2 ]
865865
%Diff:
866866
%Rows: [ 3 differences ]
867-
%PK <ID>2</ID> - Actual: <name>Something 2</name>
868-
%PK <ID>2</ID> - Actual: <Value>2</Value>
869-
%PK <ID>2</ID> - Expected: <name>Something 1</name>
870-
%PK <ID>2</ID> - Expected: <Value>1</Value>
867+
%PK <ID>2</ID> - Actual: <name>Something 2</name><Value>2</Value>
868+
%PK <ID>2</ID> - Expected: <name>Something 1</name><Value>1</Value>
871869
%PK <ID>1</ID> - Extra: <TEST_DUMMY_OBJECT><ID>1</ID><name>Something 1</name><Value>1</Value></TEST_DUMMY_OBJECT>
872870
%PK <ID>4</ID> - Missing: <TEST_DUMMY_OBJECT><ID>4</ID><name>Something 2</name><Value>2</Value></TEST_DUMMY_OBJECT>]';
873871
l_actual_message := ut3_tester_helper.main_helper.get_failed_expectations(1);

test/ut3_user/expectations/test_expectations_cursor.pkb

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ create or replace package body test_expectations_cursor is
371371
l_expected_message := q'[Actual: refcursor [ count = 1 ] was expected to equal: refcursor [ count = 1 ]
372372
%Diff:
373373
%Rows: [ 1 differences ]
374-
%Row No. 1 - Actual: <COL_4>40</COL_4><COL_3>30</COL_3>
374+
%Row No. 1 - Actual: <COL_3>30</COL_3><COL_4>40</COL_4>
375375
%Row No. 1 - Expected: <COL_3>3</COL_3><COL_4>4</COL_4>]';
376376
l_actual_message := ut3_tester_helper.main_helper.get_failed_expectations(1);
377377
--Assert
@@ -394,7 +394,7 @@ create or replace package body test_expectations_cursor is
394394
l_expected_message := q'[Actual: refcursor [ count = 1 ] was expected to equal: refcursor [ count = 1 ]
395395
%Diff:
396396
%Rows: [ 1 differences ]
397-
%Row No. 1 - Actual: <COL_4>40</COL_4><COL_3>30</COL_3>
397+
%Row No. 1 - Actual: <COL_3>30</COL_3><COL_4>40</COL_4>
398398
%Row No. 1 - Expected: <COL_3>3</COL_3><COL_4>4</COL_4>]';
399399
l_actual_message := ut3_tester_helper.main_helper.get_failed_expectations(1);
400400
--Assert
@@ -860,9 +860,9 @@ Columns:
860860
Rows: [ 4 differences ]
861861
Row No. 1 - Actual: <SALARY>25000</SALARY>
862862
Row No. 1 - Expected: <SALARY>10000</SALARY>
863-
Row No. 2 - Actual: <FIRST_NAME>TONY</FIRST_NAME><LAST_NAME>STARK</LAST_NAME><ID>3</ID><SALARY>100000</SALARY>
863+
Row No. 2 - Actual: <ID>3</ID><FIRST_NAME>TONY</FIRST_NAME><LAST_NAME>STARK</LAST_NAME><SALARY>100000</SALARY>
864864
Row No. 2 - Expected: <ID>2</ID><FIRST_NAME>LUKE</FIRST_NAME><LAST_NAME>SKYWALKER</LAST_NAME><SALARY>1000</SALARY>
865-
Row No. 3 - Actual: <FIRST_NAME>JESSICA</FIRST_NAME><LAST_NAME>JONES</LAST_NAME><ID>4</ID><SALARY>2345</SALARY>
865+
Row No. 3 - Actual: <ID>4</ID><FIRST_NAME>JESSICA</FIRST_NAME><LAST_NAME>JONES</LAST_NAME><SALARY>2345</SALARY>
866866
Row No. 3 - Expected: <ID>3</ID><FIRST_NAME>TONY</FIRST_NAME><LAST_NAME>STARK</LAST_NAME><SALARY>100000</SALARY>
867867
Row No. 4 - Extra: <GENDER>M</GENDER><FIRST_NAME>LUKE</FIRST_NAME><LAST_NAME>SKYWALKER</LAST_NAME><ID>2</ID><SALARY>1000</SALARY>]';
868868
l_actual_message := ut3_tester_helper.main_helper.get_failed_expectations(1);
@@ -896,9 +896,9 @@ Columns:
896896
Rows: [ 4 differences ]
897897
Row No. 1 - Actual: <SALARY>25000</SALARY>
898898
Row No. 1 - Expected: <SALARY>10000</SALARY>
899-
Row No. 2 - Actual: <FIRST_NAME>TONY</FIRST_NAME><LAST_NAME>STARK</LAST_NAME><ID>3</ID><SALARY>100000</SALARY>
899+
Row No. 2 - Actual: <ID>3</ID><FIRST_NAME>TONY</FIRST_NAME><LAST_NAME>STARK</LAST_NAME><SALARY>100000</SALARY>
900900
Row No. 2 - Expected: <ID>2</ID><FIRST_NAME>LUKE</FIRST_NAME><LAST_NAME>SKYWALKER</LAST_NAME><SALARY>1000</SALARY>
901-
Row No. 3 - Actual: <FIRST_NAME>JESSICA</FIRST_NAME><LAST_NAME>JONES</LAST_NAME><ID>4</ID><SALARY>2345</SALARY>
901+
Row No. 3 - Actual: <ID>4</ID><FIRST_NAME>JESSICA</FIRST_NAME><LAST_NAME>JONES</LAST_NAME><SALARY>2345</SALARY>
902902
Row No. 3 - Expected: <ID>3</ID><FIRST_NAME>TONY</FIRST_NAME><LAST_NAME>STARK</LAST_NAME><SALARY>100000</SALARY>
903903
Row No. 4 - Extra: <GENDER>M</GENDER><FIRST_NAME>LUKE</FIRST_NAME><LAST_NAME>SKYWALKER</LAST_NAME><ID>2</ID><SALARY>1000</SALARY>]';
904904
l_actual_message := ut3_tester_helper.main_helper.get_failed_expectations(1);
@@ -1237,10 +1237,8 @@ Rows: [ 4 differences ]
12371237
l_expected_message := q'[Actual: refcursor [ count = 1 ] was expected to equal: refcursor [ count = 1 ]
12381238
%Diff:
12391239
%Rows: [ 1 differences ]
1240-
%PK <COL_1>1</COL_1> - Actual: <COL_3>30</COL_3>
1241-
%PK <COL_1>1</COL_1> - Expected: <COL_3>3</COL_3>
1242-
%PK <COL_1>1</COL_1> - Actual: <COL_4>40</COL_4>
1243-
%PK <COL_1>1</COL_1> - Expected: <COL_4>4</COL_4>]';
1240+
%PK <COL_1>1</COL_1> - Actual: <COL_3>30</COL_3><COL_4>40</COL_4>
1241+
%PK <COL_1>1</COL_1> - Expected: <COL_3>3</COL_3><COL_4>4</COL_4>]';
12441242
l_actual_message := ut3_tester_helper.main_helper.get_failed_expectations(1);
12451243
--Assert
12461244
ut.expect(l_actual_message).to_be_like(l_expected_message);
@@ -2511,7 +2509,7 @@ Diff:%
25112509
begin
25122510
l_exp_message :='ORA-20218: SQL exception thrown when fetching data from cursor:
25132511
ORA-01476: divisor is equal to zero
2514-
at "UT3$USER#.TEST_EXPECTATIONS_CURSOR%", line 2522 ut3.ut.expect(l_actual).to_equal(l_expected);%
2512+
at "UT3$USER#.TEST_EXPECTATIONS_CURSOR%", line % ut3.ut.expect(l_actual).to_equal(l_expected);%
25152513
Check the query and data for errors.';
25162514

25172515
open l_actual for
@@ -2536,7 +2534,7 @@ Check the query and data for errors.';
25362534

25372535
l_exp_message :='ORA-20218: SQL exception thrown when fetching data from cursor:
25382536
ORA-01476: divisor is equal to zero
2539-
at "UT3$USER#.TEST_EXPECTATIONS_CURSOR%", line 2547 ut3.ut.expect(l_actual).to_equal(l_expected);%
2537+
at "UT3$USER#.TEST_EXPECTATIONS_CURSOR%", line % ut3.ut.expect(l_actual).to_equal(l_expected);%
25402538
Check the query and data for errors.';
25412539

25422540
open l_expected for

0 commit comments

Comments
 (0)