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

Skip to content

Commit 363c333

Browse files
committed
Fixed and simplified matcher
1 parent 1d771a5 commit 363c333

8 files changed

Lines changed: 90 additions & 64 deletions

File tree

source/expectations/data_values/ut_data_value_dsinterval.tps

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ create or replace type ut_data_value_dsinterval under ut_data_value(
1515
See the License for the specific language governing permissions and
1616
limitations under the License.
1717
*/
18-
data_value dsinterval_unconstrained,
18+
data_value interval day(9) to second(9),
1919
constructor function ut_data_value_dsinterval(self in out nocopy ut_data_value_dsinterval, a_value dsinterval_unconstrained) return self as result,
2020
overriding member function is_null return boolean,
2121
overriding member function to_string return varchar2,

source/expectations/data_values/ut_data_value_yminterval.tps

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ create or replace type ut_data_value_yminterval under ut_data_value(
1515
See the License for the specific language governing permissions and
1616
limitations under the License.
1717
*/
18-
data_value yminterval_unconstrained,
18+
data_value interval year(9) to month,
1919
constructor function ut_data_value_yminterval(self in out nocopy ut_data_value_yminterval, a_value yminterval_unconstrained) return self as result,
2020
overriding member function is_null return boolean,
2121
overriding member function to_string return varchar2,

source/expectations/matchers/ut_be_within.tpb

Lines changed: 6 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -107,64 +107,13 @@ create or replace type body ut_be_within as
107107
end;
108108

109109
overriding member function run_matcher(self in out nocopy ut_be_within, a_actual ut_data_value) return boolean is
110-
l_result boolean;
111-
function l_result_from_number return boolean is
112-
l_expected number := treat(self.expected as ut_data_value_number).data_value;
113-
l_actual number := treat(a_actual as ut_data_value_number).data_value;
114-
l_distance number := treat(self.distance_from_expected as ut_data_value_number).data_value;
115-
begin
116-
return abs(l_expected - l_actual) <= l_distance;
117-
end;
118-
119-
function l_result_from_date(a_distance ut_data_value) return boolean is
120-
l_expected date := treat(self.expected as ut_data_value_date).data_value;
121-
l_actual date := treat(a_actual as ut_data_value_date).data_value;
122-
l_distance_ym yminterval_unconstrained := case when self.distance_from_expected is of ( ut_data_value_yminterval)
123-
then treat(self.distance_from_expected as ut_data_value_yminterval).data_value
124-
end;
125-
l_distance_ds dsinterval_unconstrained := case when self.distance_from_expected is of ( ut_data_value_dsinterval)
126-
then treat(self.distance_from_expected as ut_data_value_dsinterval).data_value
127-
end;
128-
begin
129-
return case when l_distance_ym is not null
130-
then l_actual between l_expected - l_distance_ym and l_expected + l_distance_ym
131-
else l_actual between l_expected - l_distance_ds and l_expected + l_distance_ds
132-
end;
133-
end;
134-
135-
function l_result_from_timestamp (a_distance ut_data_value) return boolean is
136-
l_expected timestamp := case when self.expected is of ( ut_data_value_timestamp) then
137-
treat(self.expected as ut_data_value_timestamp).data_value
138-
when self.expected is of ( ut_data_value_timestamp_tz) then
139-
treat(self.expected as ut_data_value_timestamp_tz).data_value
140-
when self.expected is of ( ut_data_value_timestamp_ltz) then
141-
treat(self.expected as ut_data_value_timestamp_ltz).data_value
142-
end;
143-
l_actual timestamp with time zone := treat(a_actual as ut_data_value_date).data_value;
144-
l_distance_ym yminterval_unconstrained := case when self.distance_from_expected is of ( ut_data_value_yminterval)
145-
then treat(self.distance_from_expected as ut_data_value_yminterval).data_value
146-
end;
147-
l_distance_ds dsinterval_unconstrained := case when self.distance_from_expected is of ( ut_data_value_dsinterval)
148-
then treat(self.distance_from_expected as ut_data_value_dsinterval).data_value
149-
end;
150-
begin
151-
return case when l_distance_ym is not null
152-
then l_actual between l_expected - l_distance_ym and l_expected + l_distance_ym
153-
else l_actual between l_expected - l_distance_ds and l_expected + l_distance_ds
154-
end;
155-
end;
156-
157-
158-
begin
110+
l_result boolean;
111+
begin
159112
if self.expected.data_type = a_actual.data_type then
160-
if self.expected is of (ut_data_value_number) then
161-
l_result := l_result_from_number;
162-
elsif self.expected is of (ut_data_value_date)then
163-
l_result := l_result_from_date(self.distance_from_expected);
164-
elsif self.expected is of (ut_data_value_timestamp_tz)then
165-
l_result := l_result_from_date(self.distance_from_expected);
166-
elsif self.expected is of (ut_data_value_timestamp_ltz)then
167-
l_result := l_result_from_date(self.distance_from_expected);
113+
if self.expected is of (ut_data_value_date, ut_data_value_number, ut_data_value_timestamp, ut_data_value_timestamp_tz, ut_data_value_timestamp_ltz) then
114+
l_result := ut_be_within_helper.values_within_abs_distance(self.expected, a_actual, self.distance_from_expected) ;
115+
else
116+
l_result := (self as ut_matcher).run_matcher(a_actual);
168117
end if;
169118
else
170119
l_result := (self as ut_matcher).run_matcher(a_actual);
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
create or replace package body ut_be_within_helper as
2+
/*
3+
utPLSQL - Version 3
4+
Copyright 2016 - 2019 utPLSQL Project
5+
6+
Licensed under the Apache License, Version 2.0 (the "License"):
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
function values_within_abs_distance(
20+
a_value_1 ut_data_value, a_value_2 ut_data_value, a_distance ut_data_value
21+
) return boolean is
22+
l_result boolean;
23+
begin
24+
execute immediate q'[
25+
begin
26+
:result :=
27+
treat(:a_value_1 as ut3_develop.]'||a_value_1.self_type||q'[).data_value
28+
between
29+
treat(:a_value_2 as ut3_develop.]'||a_value_2.self_type||q'[).data_value
30+
- treat(:a_distance as ut3_develop.]'||a_distance.self_type||q'[).data_value
31+
and
32+
treat(:a_value_2 as ut3_develop.]'||a_value_2.self_type||q'[).data_value
33+
+ treat(:a_distance as ut3_develop.]'||a_distance.self_type||q'[).data_value;
34+
end;
35+
]'
36+
using out l_result, a_value_1, a_value_2, a_distance;
37+
return l_result;
38+
end;
39+
40+
end;
41+
/
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
create or replace package ut_be_within_helper authid definer as
2+
/*
3+
utPLSQL - Version 3
4+
Copyright 2016 - 2019 utPLSQL Project
5+
6+
Licensed under the Apache License, Version 2.0 (the "License"):
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
function values_within_abs_distance(
20+
a_value_1 ut_data_value, a_value_2 ut_data_value, a_distance ut_data_value
21+
) return boolean;
22+
23+
end;
24+
/

source/install.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ prompt Installing DBMSPLSQL Tables objects into &&ut3_owner schema
242242
@@install_component.sql 'expectations/matchers/ut_comparison_matcher.tps'
243243
@@install_component.sql 'expectations/matchers/ut_be_within_pct.tps'
244244
@@install_component.sql 'expectations/matchers/ut_be_within.tps'
245+
@@install_component.sql 'expectations/matchers/ut_be_within_helper.pks'
245246
@@install_component.sql 'expectations/ut_expectation.tps'
246247
@@install_component.sql 'expectations/matchers/ut_be_false.tps'
247248
@@install_component.sql 'expectations/matchers/ut_be_greater_or_equal.tps'
@@ -299,6 +300,7 @@ prompt Installing DBMSPLSQL Tables objects into &&ut3_owner schema
299300
@@install_component.sql 'expectations/matchers/ut_equal.tpb'
300301
@@install_component.sql 'expectations/matchers/ut_be_within_pct.tpb'
301302
@@install_component.sql 'expectations/matchers/ut_be_within.tpb'
303+
@@install_component.sql 'expectations/matchers/ut_be_within_helper.pkb'
302304
@@install_component.sql 'expectations/matchers/ut_contain.tpb'
303305
@@install_component.sql 'expectations/matchers/ut_have_count.tpb'
304306
@@install_component.sql 'expectations/matchers/ut_be_between.tpb'

source/uninstall_objects.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ drop type ut_be_within_pct force;
148148

149149
drop type ut_be_within force;
150150

151+
drop package ut_be_within_helper;
152+
151153
drop type ut_comparison_matcher force;
152154

153155
drop type ut_matcher force;

test/ut3_user/expectations/binary/test_to_be_within.pkb

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,19 @@ create or replace package body test_to_be_within is
7575
test_to_be_within_success('to_be_within','date', 'sysdate', 'sysdate+200','''1-0''','interval year to month');
7676
test_to_be_within_success('to_be_within','date', 'sysdate+200', 'sysdate','''1-0''','interval year to month');
7777

78-
test_to_be_within_success('to_be_within','timestamp_tz_unconstrained', q'[TIMESTAMP '2017-08-09 07:00:00 -7:00']', 'sysdate','''1 0:00:11.333''','interval day to second');
79-
test_to_be_within_success('to_be_within','timestamp_tz_unconstrained', q'[TIMESTAMP '2017-08-09 07:00:00 -7:00']', 'sysdate+1','''1 0:00:11.333''','interval day to second');
80-
test_to_be_within_success('to_be_within','timestamp_ltz_unconstrained', q'[TIMESTAMP '2017-08-09 07:00:00 -7:00']', 'sysdate+200','''1-0''','interval year to month');
81-
test_to_be_within_success('to_be_within','timestamp_ltz_unconstrained', 'sysdate+200', 'sysdate','''1-0''','interval year to month');
82-
78+
test_to_be_within_success('to_be_within','timestamp', q'[TIMESTAMP '2017-08-09 07:00:00']', q'[TIMESTAMP '2017-08-08 06:59:48.667']','''1 0:00:11.333''','interval day to second');
79+
test_to_be_within_success('to_be_within','timestamp', q'[TIMESTAMP '2017-08-08 06:59:48.667']', q'[TIMESTAMP '2017-08-09 07:00:00']','''1 0:00:11.333''','interval day to second');
80+
test_to_be_within_success('to_be_within','timestamp', q'[TIMESTAMP '2017-08-09 07:00:00']', q'[TIMESTAMP '2018-08-09 07:00:00']','''1-0''','interval year to month');
81+
test_to_be_within_success('to_be_within','timestamp', q'[TIMESTAMP '2018-08-09 07:00:00']', q'[TIMESTAMP '2017-08-09 07:00:00']','''1-0''','interval year to month');
82+
test_to_be_within_success('to_be_within','timestamp_tz_unconstrained', q'[TIMESTAMP '2017-08-09 07:00:00 -7:00']', q'[TIMESTAMP '2017-08-08 05:59:48.668 -8:00']','''1 0:00:11.333''','interval day to second');
83+
test_to_be_within_success('to_be_within','timestamp_tz_unconstrained', q'[TIMESTAMP '2017-08-08 05:59:48.668 -8:00']', q'[TIMESTAMP '2017-08-09 07:00:00 -7:00']','''1 0:00:11.333''','interval day to second');
84+
test_to_be_within_success('to_be_within','timestamp_tz_unconstrained', q'[TIMESTAMP '2017-08-09 07:00:00 -7:00']', q'[TIMESTAMP '2018-08-09 07:00:00 -7:00']','''1-0''','interval year to month');
85+
test_to_be_within_success('to_be_within','timestamp_tz_unconstrained', q'[TIMESTAMP '2018-08-09 07:00:00 -7:00']', q'[TIMESTAMP '2017-08-09 07:00:00 -7:00']','''1-0''','interval year to month');
86+
test_to_be_within_success('to_be_within','timestamp_ltz_unconstrained', q'[TIMESTAMP '2017-08-09 07:00:00 -7:00']', q'[TIMESTAMP '2017-08-08 05:59:48.668 -8:00']','''1 0:00:11.333''','interval day to second');
87+
test_to_be_within_success('to_be_within','timestamp_ltz_unconstrained', q'[TIMESTAMP '2017-08-08 05:59:48.668 -8:00']', q'[TIMESTAMP '2017-08-09 07:00:00 -7:00']','''1 0:00:11.333''','interval day to second');
88+
test_to_be_within_success('to_be_within','timestamp_ltz_unconstrained', q'[TIMESTAMP '2017-08-09 07:00:00 -7:00']', q'[TIMESTAMP '2018-08-09 07:00:00 -7:00']','''1-0''','interval year to month');
89+
test_to_be_within_success('to_be_within','timestamp_ltz_unconstrained', q'[TIMESTAMP '2018-08-09 07:00:00 -7:00']', q'[TIMESTAMP '2017-08-09 07:00:00 -7:00']','''1-0''','interval year to month');
90+
8391
test_to_be_within_success('to_( ut3_develop.be_within','number', '2', '4','2','number', ')');
8492
test_to_be_within_success('to_( ut3_develop.be_within','number', '4', '2','2','number', ')');
8593
test_to_be_within_success('to_( ut3_develop.be_within','date', 'sysdate+1', 'sysdate','''1 0:00:11.333''','interval day to second', ')');

0 commit comments

Comments
 (0)