|
| 1 | +# Advanced data comparison |
| 2 | + |
| 3 | +utPLSQL expectations incorporates advanced data comparison options when comparing compound data-types: |
| 4 | + |
| 5 | +- refcursor |
| 6 | +- object type |
| 7 | +- nested table and varray |
| 8 | + |
| 9 | +Advanced data-comparison options are available for the [`equal`](expectations.md#equal) matcher. |
| 10 | + |
| 11 | +## Syntax |
| 12 | + |
| 13 | +``` |
| 14 | + ut.expect( a_actual {data-type} ).to_( equal( a_expected {data-type})[.extendend_option()[.extendend_option()[...]]]); |
| 15 | + ut.expect( a_actual {data-type} ).not_to( equal( a_expected {data-type})[.extendend_option()[.extendend_option()[...]]]) ); |
| 16 | + ut.expect( a_actual {data-type} ).to_equal( a_expected {data-type})[.extendend_option()[.extendend_option()[...]]]); |
| 17 | + ut.expect( a_actual {data-type} ).not_to_equal( a_expected {data-type})[.extendend_option()[.extendend_option()[...]]] ); |
| 18 | +``` |
| 19 | + |
| 20 | +`extended_option` can be one of: |
| 21 | + |
| 22 | + - `include(a_items varchar2)` - item or comma separated list of items to include |
| 23 | + - `exclude(a_items varchar2)` - item or comma separated list of items to exclude |
| 24 | + - `include(a_items ut_varchar2_list)` - table of items to include |
| 25 | + - `exclude(a_items ut_varchar2_list)` - table of items to exclude |
| 26 | + |
| 27 | +Each item in the comma separated list can be: |
| 28 | +- a column name of cursor to be compared |
| 29 | +- an attribute name of object type to be compared |
| 30 | +- an attribute name of object type within a table of objects to be compared |
| 31 | +- an [XPath](http://zvon.org/xxl/XPathTutorial/Output/example1.html) expression representing column/attribute |
| 32 | + |
| 33 | +Each element in `ut_varchar2_list` nested table can be an item or a comma separated list of items. |
| 34 | + |
| 35 | +When specifying column/attribute names, keep in mind that the names are **case sensitive**. |
| 36 | + |
| 37 | +**XPath expressions with comma are not supported.** |
| 38 | + |
| 39 | +## Excluding elements from data comparison |
| 40 | + |
| 41 | +Consider the following example |
| 42 | +```sql |
| 43 | +procedure test_cursors_skip_columns is |
| 44 | + l_expected sys_refcursor; |
| 45 | + l_actual sys_refcursor; |
| 46 | +begin |
| 47 | + open l_expected for select 'text' ignore_me, d.* from user_tables d; |
| 48 | + open l_actual for select sysdate "ADate", d.* from user_tables d; |
| 49 | + ut.expect( l_actual ).to_equal( l_expected ).exclude( 'IGNORE_ME,ADate' ); |
| 50 | +end; |
| 51 | +``` |
| 52 | + |
| 53 | +Columns 'ignore_me' and "ADate" will get excluded from cursor comparison. |
| 54 | +The cursor data is equal, when those columns are excluded. |
| 55 | + |
| 56 | +This option is useful in scenarios, when you need to exclude incomparable/unpredictable column data like CREATE_DATE of a record that is maintained by default value on a table column. |
| 57 | + |
| 58 | +## Selecting columns for data comparison |
| 59 | + |
| 60 | +Consider the following example |
| 61 | +```sql |
| 62 | +procedure include_columns_as_csv is |
| 63 | + l_actual sys_refcursor; |
| 64 | + l_expected sys_refcursor; |
| 65 | +begin |
| 66 | + open l_expected for select rownum as rn, 'a' as "A_Column", 'x' SOME_COL from dual a connect by level < 4; |
| 67 | + open l_actual for select rownum as rn, 'a' as "A_Column", 'x' SOME_COL, a.* from all_objects a where rownum < 4; |
| 68 | + ut.expect( l_actual ).to_equal( l_expected ).include( 'RN,A_Column,SOME_COL' ); |
| 69 | +end; |
| 70 | +``` |
| 71 | + |
| 72 | +## Combining include/exclude options |
| 73 | +You can chain the advanced options in an expectation and mix the `varchar2` with `ut_varchar2_list` arguments. |
| 74 | +When doing so, the fianl list of items to include/exclude will be a concatenation of all items. |
| 75 | + |
| 76 | +```sql |
| 77 | +procedure include_columns_as_csv is |
| 78 | + l_actual sys_refcursor; |
| 79 | + l_expected sys_refcursor; |
| 80 | +begin |
| 81 | + open l_expected for select rownum as rn, 'a' as "A_Column", 'x' SOME_COL from dual a connect by level < 4; |
| 82 | + open l_actual for select rownum as rn, 'a' as "A_Column", 'Y' SOME_COL, a.* from all_objects a where rownum < 4; |
| 83 | + ut.expect( l_actual ).to_equal( l_expected ) |
| 84 | + .include( 'RN') |
| 85 | + .include( ut_varchar2_list( 'A_Column', 'SOME_COL' ) ) |
| 86 | + .exclude( 'SOME_COL' ); |
| 87 | +end; |
| 88 | +``` |
| 89 | + |
| 90 | +Only the columns 'RN', "A_Column" will be compared. Column 'SOME_COL' is excluded. |
| 91 | + |
| 92 | +This option can be useful in scenarios where you need to narrow-down the scope of test so that the test is only focused on very specific data. |
| 93 | + |
| 94 | +## Defining item as XPath |
| 95 | +When using XPath expression, keep in mind the following: |
| 96 | + |
| 97 | +- cursor columns are nested under `<ROW>` element |
| 98 | +- object type attributes are nested under `<OBJECTY_TYPE>` element |
| 99 | +- nested table and varray items type attributes are nested under `<ARRAY><OBJECTY_TYPE>` elements |
| 100 | + |
| 101 | +```sql |
| 102 | +procedure include_columns_as_xpath is |
| 103 | + l_actual sys_refcursor; |
| 104 | + l_expected sys_refcursor; |
| 105 | +begin |
| 106 | + open l_expected for select rownum as rn, 'a' as "A_Column", 'x' SOME_COL from dual a connect by level < 4; |
| 107 | + open l_actual for select rownum as rn, 'a' as "A_Column", 'x' SOME_COL, a.* from all_objects a where rownum < 4; |
| 108 | + ut.expect( l_actual ).to_equal( l_expected ).include( '/ROW/RN|/ROW/A_Column|/ROW/SOME_COL' ); |
| 109 | +end; |
| 110 | +``` |
0 commit comments