1- # Concept of expectation and matcher
1+ # Concepts
22
33Validation of the code under test (the tested logic of procedure/function etc.) is performed by comparing the actual data against the expected data.
44To do that we use concept of expectation and a matcher to perform the check on the data.
@@ -14,66 +14,213 @@ Expectation is a set of the expected value(s), actual values(s) and the matcher(
1414
1515Matcher is defining the comparison operation to be performed on expected and actual values.
1616
17- # List of currently build-in matchers
18- - ` match `
19- - ` equal `
20- - ` be_true `
21- - ` be_null `
22- - ` be_not_null `
23- - ` be_like `
24- - ` be_less_than `
25- - ` be_less_or_equal `
26- - ` be_greater_than `
27- - ` be_greater_or_equal `
28- - ` be_false `
17+ # Matchers
18+ utPLSQL provides following matchers to perform checks on the expected and actual values.
2919- ` be_between `
3020- ` be_empty `
21+ - ` be_false `
22+ - ` be_greater_than `
23+ - ` be_greater_or_equal `
24+ - ` be_less_or_equal `
25+ - ` be_less_than `
26+ - ` be_like `
27+ - ` be_not_null `
28+ - ` be_null `
29+ - ` be_true `
30+ - ` equal `
31+ - ` match `
3132
32- ## match
33- Allows regexp_like validations to be executed against the following datatypes:
34- - ` clob `
35- - ` varchar2 `
33+ ## be_between
34+ Validates that the actual value is between the lower and upper bound.
35+
36+ Usage:
37+ ``` sql
38+ ut .expect ( a_actual {mulitple data- types} ).to_( be_between( a_lower_bound {mulitple data- types}, a_upper_bound {mulitple data- types}) );
39+ ```
40+
41+ ## be_empty
42+ Unary matcher that validates if the provided dataset is empty.
43+
44+ Usage:
45+ ``` sql
46+ ut .expect ( a_actual {mulitple data- types} ).to_( be_empty() );
47+ ```
48+
49+ When used with anydata, it is only valid for collection data types.
50+
51+ ## be_false
52+ Unary matcher that validates if the provided value is false.
53+
54+ Usage:
55+ ``` sql
56+ ut .expect ( a_actual buulean ).to_( be_false() );
57+ ```
58+
59+ ## be_greater_or_equal
60+ Allows to check if the actual value is greater or equal than the expected.
61+
62+ Usage:
63+ ``` sql
64+ ut .expect ( a_actual {mulitple data- types} ).to_( be_greater_or_equal( a_expected {mulitple data- types}) );
65+ ```
66+
67+ ## be_greater_than
68+ Allows to check if the actual value is greater than the expected.
69+
70+ Usage:
71+ ``` sql
72+ ut .expect ( a_actual {mulitple data- types} ).to_( be_greater_than( a_expected {mulitple data- types}) );
73+ ```
74+
75+ ## be_less_or_equal
76+ Allows to check if the actual value is less or equal than the expected.
77+
78+ Usage:
79+ ``` sql
80+ ut .expect ( a_actual {mulitple data- types} ).to_( be_less_or_equal( a_expected {mulitple data- types}) );
81+ ```
82+
83+ ## be_less_than
84+ Allows to check if the actual value is less than the expected.
3685
3786Usage:
3887``` sql
39- ut .expect ( a_actual ).to_( match( a_pattern in varchar2 , a_modifiers in varchar2 : = null ) )
88+ ut .expect ( a_actual {mulitple data - types} ).to_( be_less_than( a_expected {mulitple data - types} ) );
4089```
4190
42- Parameters ` a_pattern ` and ` a_modifiers ` represent a valid regexp pattern accepted by [ Oracle regexp_like function] ( http://docs.oracle.com/database/121/SQLRF/conditions007.htm#SQLRF00501 )
91+
92+ ## be_like
93+ Validates that the actual value is like the expected expression.
94+
95+ Usage:
96+ ``` sql
97+ ut .expect ( a_actual {mulitple data- types} ).to_( be_like( a_mask in varchar2 , a_escape_char in varchar2 := null ) );
98+ ```
99+
100+ Parameters ` a_mask ` and ` a_escape_char ` represent a valid parameters of the [ Oracle like function] ( https://docs.oracle.com/database/121/SQLRF/conditions007.htm#SQLRF52142 )
101+
102+
103+ ## be_not_null
104+ Unary matcher that validates if the actual value is not null.
105+
106+ Usage:
107+ ``` sql
108+ ut .expect ( a_actual {mulitple data- types} ).to_( be_not_null() );
109+ ```
110+
111+ ## be_null
112+ Unary matcher that validates if the actual value is null.
113+
114+ Usage:
115+ ``` sql
116+ ut .expect ( a_actual {mulitple data- types} ).to_( be_null() );
117+ ```
118+
119+ ## be_true
120+ Unary matcher that validates if the provided value is false.
121+ - ` boolean `
122+
123+ Usage:
124+ ``` sql
125+ ut .expect ( a_actual buulean ).to_( be_false() );
126+ ```
43127
44128## equal
45129
46- The equal matcher is a very restrictive matcher.
47- It only returns true, if compared data-types.
130+ The equal matcher is a very restrictive matcher. It only returns true, if compared data-types.
48131That means, that comparing varchar2 to a number will fail even if the varchar2 contains the same number.
49132This matcher is designed to capture changes of data-type, so that if you expect your variable to be number and is now something else,
50133 the test will fail and give you early indication of potential problem.
51134
52135Usage:
53136``` sql
54- ut .expect ( a_actual ).to_( equal( a_expected {mulitple data- types}, a_nulls_are_equal boolean := null ) )
137+ ut .expect ( a_actual {mulitple data - types} ).to_( equal( a_expected {mulitple data- types}, a_nulls_are_equal boolean := null ) );
55138```
139+ The ` a_nulls_are_equal ` parameter decides on the behavior of ` null=null ` comparison (** this comparison by default is true!** )
56140
141+ The ` anydata ` data type is used to compare user defined object and collections.
142+
143+ Example usage of anydata to compare user defined types.
144+ ``` sql
145+ create type department as object(name varchar2 (30 ));
146+ /
147+ create or replace package demo_dept as
148+ -- %suite(demo)
57149
58- The equal matcher accepts a_expected of following data-types.
59- - ` anydata `
60- - ` blob `
61- - ` boolean `
62- - ` clob `
63- - ` date `
64- - ` number `
65- - ` sys_refcursor `
66- - ` timestamp_unconstrained `
67- - ` timestamp_tz_unconstrained `
68- - ` timestamp_ltz_unconstrained `
69- - ` varchar2 `
70- - ` yminterval_unconstrained `
71- - ` dsinterval_unconstrained `
150+ -- %test(demo_dept)
151+ procedure test_department
152+ end;
153+ /
154+
155+ create or replace package body demo_dept as
156+ procedure test_department is
157+ v_expected department;
158+ v_actual department;
159+ begin
160+ v_expected := department(' HR' );
161+ ut .expect ( anydata .convertObject (v_expected) ).to_( equal( anydata .convertObject (v_actual) ) );
162+ end;
163+ end;
164+ /
165+ ```
166+
167+ This test will fail as the ` v_acutal ` is not equal ` v_expected ` .
168+
169+ ## match
170+ Validates that the actual value is matching the expected regular expression.
171+
172+ Usage:
173+ ``` sql
174+ ut .expect ( a_actual {mulitple data- types} ).to_( match( a_pattern in varchar2 , a_modifiers in varchar2 := null ) );
175+ ```
176+
177+ Parameters ` a_pattern ` and ` a_modifiers ` represent a valid regexp pattern accepted by [ Oracle regexp_like function] ( https://docs.oracle.com/database/121/SQLRF/conditions007.htm#SQLRF00501 )
178+
179+
180+
181+ # Supported data types
182+
183+ Below matrix illustrates the data types supported by different matchers.
72184
73- The second parameter decides on the behavior of ` null=null ` comparison (** this comparison by default is true!** )
74-
185+ | | be_between | be_empty | be_false | be_greater_than | be_greater_or_equal | be_less_or_equal | be_less_than | be_like | be_not_null | be_null | be_true | equal | match |
186+ | :------------------------------| :----------:| :--------:| :--------:| :---------------:| :-------------------:| :----------------:| :------------:| :-------:| :-----------:| :-------:| :-------:| :-----:| :-----:|
187+ | anydata | | X | | | | | | | X | X | | X | |
188+ | blob | | | | | | | | | X | X | | X | |
189+ | boolean | | | X | | | | | | X | X | X | X | |
190+ | clob | | | | | | | | X | X | X | | X | X |
191+ | date | X | | | X | X | X | X | | X | X | | X | |
192+ | number | X | | | X | X | X | X | | X | X | | X | |
193+ | refcursor | | X | | | | | | | X | X | | X | |
194+ | timestamp | X | | | X | X | X | X | | X | X | | X | |
195+ | timestamp with timezone | X | | | X | X | X | X | | X | X | | X | |
196+ | timestamp with local timezone | X | | | X | X | X | X | | X | X | | X | |
197+ | varchar2 | X | | | | | | | X | X | X | | X | X |
198+ | interval year to month | X | | | X | X | X | X | | X | X | | X | |
199+ | interval day to second | X | | | X | X | X | X | | X | X | | X | |
200+
201+
202+
203+ # Negating the matcher
204+ Expectations provide a very convenient way to check for a negative of the expectation.
205+
206+ Syntax of check for matcher evaluating to true:
207+ ``` sql
208+ ut .expect ( a_actual {mulitple data- types} ).to_( {matcher} );
209+ ```
210+
211+ Syntax of check for matcher evaluating to false:
212+ ``` sql
213+ ut .expect ( a_actual {mulitple data- types} ).not_to( {matcher} );
214+ ```
215+
216+ If a matcher evaluated to NULL, then both ` to_ ` and ` not_to ` will cause the expectation to report failure.
217+
218+ Example:
219+ ``` sql
220+ ut .expect ( null ).to_( be_true() );
221+ ut .expect ( null ).not_to( be_true() );
222+ ```
75223
76- A test procedure will contain one or more checks to verify the the test performed as expected. These checks are called assertion. utPLSQL provides a robust and extensible assertion library .
224+ Since NULL is neither true not it is not true, both expectations will report failure .
77225
78226
79- TODO: Finish Expectations concepts
0 commit comments