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

Skip to content

Commit 7fec0f9

Browse files
committed
Added tests for 0 value of actual and expected and actual greater than expected (not only smaller).
Updated documentation.
1 parent 5b5a5c0 commit 7fec0f9

3 files changed

Lines changed: 63 additions & 21 deletions

File tree

docs/userguide/expectations.md

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,13 +1096,17 @@ SUCCESS
10961096
10971097
## to_be_within of
10981098
1099-
This matcher determines wheter expected value is within range from another value.
1099+
Determines wheter expected value is within range (tolerance) from another value.
11001100
1101-
The logical formual used for calcuating the matcher is:
1102-
`abs( expected - actual ) <= distance`
1103-
The matcher will succeed if the `expected` and `actual` are not more than `distance` apart from each other.
1101+
The logical formual used for calcuating the matcher is:
1102+
```
1103+
result := ( abs( expected - actual ) <= distance )
1104+
```
1105+
The actual formula used for calculation is more complex to handle different data-types of expected/actual values as well as differnet types of distance value.
1106+
The matcher will fail if the `expected` and `actual` are more than `distance` apart from each other.
1107+
The matcher will fail if the dataypes of `expected` and `actual` are not the same.
11041108
1105-
The matcher works with data-type number, date, timestamp, timestamp with time zone, timestamp with local time zone.
1109+
The matcher works with data-types: `number`, `date`, `timestamp`, `timestamp with time zone`, `timestamp with local time zone`
11061110
The data-types of compared values must match exactly and if type does not match, the expectation will fail.
11071111
11081112
| expected/actual<br>data-type | distance data-type |
@@ -1118,12 +1122,13 @@ The data-types of compared values must match exactly and if type does not match,
11181122
| timestamp with local time zone | interval year to month |
11191123
11201124
1121-
The distance can be expressed as a postive number or positive interval.
1125+
The distance must be expressed as a non-negative number or non-negative interval.
11221126
11231127
>Note:
11241128
> Interval year-to-moth as a distance is giving sucess if the distance between the given dates/timestamps evaluates to value less or equal of the specified interval
1125-
> Keep in mind that a distance of `interval '0-1' year to month` will actuall be successful if the distance isnot greater than a month and a half.
1126-
> This is due to how oracle evaluates conversion between timestamp difference converted to `year to month interval`.
1129+
> Keep in mind that a checking for distance of `interval '0-1' year to month` will actuall be successful if the distance is less than a month and 15 days.
1130+
> This is due to how oracle evaluates conversion between timestamp difference converted to `year to month interval`.
1131+
> The behavior is similar to a call to `months_between()` function with results rounded to full monts ie. round(months_between(date, date))
11271132
11281133
**Example 1.**
11291134
```sql
@@ -1172,17 +1177,14 @@ Failures:
11721177
11731178
## to_be_within_pct of
11741179
1175-
This matcher is created to determine wheter expected value is approximately equal or "close" to another value within percentage value of expected.
1176-
1177-
Matcher will allow to compare numbers.
1178-
1179-
When comparing a number the tolerance / distance can be expressed as another postive number or a percentage.
1180-
1181-
When comparing a two dates tolerance can be expressed in interval time either Day-To-Second or Year-To-Month.
1182-
1183-
Matcher for numbers will calculate a absolute distance between expected and actual and check whether that value is within a tolerance.
1180+
Determines wheter actual value is within percentage range of expected value.
1181+
The matcher only works with `number` data-type.
11841182
1185-
When comparing a date a distance is measured in interval, the check is done that actual value is within date range of expected taking into account interval plus and minus.
1183+
The percentage deviation (distance) must be expressed as a non-negative number.
1184+
The formula used for calcuation of expectation is:
1185+
```
1186+
result := ( ( distance ) * expected >= abs( expected - actual ) * 100 )
1187+
```
11861188
11871189
**Example 1.**
11881190
```sql
@@ -1192,6 +1194,11 @@ end;
11921194
/
11931195
```
11941196
1197+
```
1198+
SUCCESS
1199+
Actual: 9 (number) was expected to be within 10 % of 10 (number)
1200+
```
1201+
11951202
11961203
## Comparing cursors, object types, nested tables and varrays
11971204

source/expectations/matchers/ut_be_within_pct.tpb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,8 @@ create or replace type body ut_be_within_pct as
5252
if self.expected.data_type = a_actual.data_type then
5353
if self.expected is of (ut_data_value_number) then
5454
l_result :=
55-
treat(self.distance_from_expected as ut_data_value_number).data_value
56-
>= ( ( treat(self.expected as ut_data_value_number).data_value - treat(a_actual as ut_data_value_number).data_value ) * 100 ) /
57-
treat(self.expected as ut_data_value_number).data_value;
55+
abs(treat(self.distance_from_expected as ut_data_value_number).data_value) * treat(self.expected as ut_data_value_number).data_value
56+
>= abs( ( treat(self.expected as ut_data_value_number).data_value - treat(a_actual as ut_data_value_number).data_value ) * 100 );
5857
end if;
5958
else
6059
l_result := (self as ut_matcher).run_matcher(a_actual);

test/ut3_user/expectations/binary/test_to_be_within_pct.pkb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,27 @@ create or replace package body test_to_be_within_pct is
125125

126126
ut3_develop.ut.expect( 2.987654321 ).not_to( ut3_develop.be_within_pct( 0.1 ).of_(3) );
127127
expect_success;
128+
129+
ut3_develop.ut.expect( 3.012345679 ).to_be_within_pct( 1 ).of_(3);
130+
expect_success;
131+
132+
ut3_develop.ut.expect( 3.012345679 ).to_( ut3_develop.be_within_pct( 1 ).of_(3) );
133+
expect_success;
134+
135+
ut3_develop.ut.expect( 3.012345679 ).not_to_be_within_pct( 0.1 ).of_(3);
136+
expect_success;
137+
138+
ut3_develop.ut.expect( 3.012345679 ).not_to( ut3_develop.be_within_pct( 0.1 ).of_(3) );
139+
expect_success;
140+
141+
ut3_develop.ut.expect( 0 ).to_be_within_pct( 10 ).of_( 0 );
142+
expect_success;
143+
144+
ut3_develop.ut.expect( 0 ).to_be_within_pct( 100 ).of_( 1 );
145+
expect_success;
146+
147+
ut3_develop.ut.expect( -1 ).to_be_within_pct( 200 ).of_( 1 );
148+
expect_success;
128149
end;
129150

130151
procedure failed_tests is
@@ -140,6 +161,21 @@ create or replace package body test_to_be_within_pct is
140161

141162
ut3_develop.ut.expect( 2.987654321 ).not_to( ut3_develop.be_within_pct( 1 ).of_(3) );
142163
expect_failure;
164+
165+
ut3_develop.ut.expect( 3.012345679 ).to_be_within_pct( 0.1 ).of_(3);
166+
expect_failure;
167+
168+
ut3_develop.ut.expect( 3.012345679 ).to_( ut3_develop.be_within_pct( 0.1 ).of_(3) );
169+
expect_failure;
170+
171+
ut3_develop.ut.expect( 3.012345679 ).not_to_be_within_pct( 1 ).of_(3);
172+
expect_failure;
173+
174+
ut3_develop.ut.expect( 3.012345679 ).not_to( ut3_develop.be_within_pct( 1 ).of_(3) );
175+
expect_failure;
176+
177+
ut3_develop.ut.expect( 0.1 ).to_be_within_pct( 10 ).of_( 0 );
178+
expect_failure;
143179
end;
144180

145181
procedure fail_for_number_not_within is

0 commit comments

Comments
 (0)