Skip to content

Commit 3f5b67f

Browse files
committed
Add tests for _validate_range method
1 parent 777afa6 commit 3f5b67f

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

Diff for: pykwalify/core.py

+2
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,8 @@ def _validate_range(self, max_, min_, max_ex, min_ex, value, path, prefix):
657657
"""
658658
Validate that value is within range values.
659659
"""
660+
if not isinstance(value, int) and not isinstance(value, float):
661+
raise CoreError("Value must be a integer type")
660662

661663
log.debug(
662664
u"Validate range : %s : %s : %s : %s : %s : %s",

Diff for: tests/test_core_methods.py

+43-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from datetime import datetime
33

44
from pykwalify.core import Core
5-
from pykwalify.errors import NotSequenceError
5+
from pykwalify.errors import NotSequenceError, CoreError
66

77

88
class Rule(object):
@@ -28,6 +28,48 @@ def ec():
2828
return Core(source_data={}, schema_data={})
2929

3030

31+
def test_validate_range():
32+
data_matrix = [
33+
(10, 5, 10, 5, 7, []),
34+
(None, None, None, None, 7, []),
35+
36+
(10, 5, None, None, 13, ["Type 'prefix' has size of '13', greater than max limit '10'. Path: '/'"]),
37+
(10, 5, None, None, 3, ["Type 'prefix' has size of '3', less than min limit '5'. Path: '/'"]),
38+
(10, 5, None, None, 13.5, ["Type 'prefix' has size of '13.5', greater than max limit '10'. Path: '/'"]),
39+
(10, 5, None, None, 3.5, ["Type 'prefix' has size of '3.5', less than min limit '5'. Path: '/'"]),
40+
(10, 5, None, None, 10, []),
41+
(10, 5, None, None, 5, []),
42+
(10, 5, None, None, 10.0, []),
43+
(10, 5, None, None, 5.0, []),
44+
45+
(None, None, 10, 5, 13, ["Type 'prefix' has size of '13', greater than or equals to max limit(exclusive) '10'. Path: '/'"]),
46+
(None, None, 10, 5, 3, ["Type 'prefix' has size of '3', less than or equals to min limit(exclusive) '5'. Path: '/'"]),
47+
(None, None, 10, 5, 13.5, ["Type 'prefix' has size of '13.5', greater than or equals to max limit(exclusive) '10'. Path: '/'"]),
48+
(None, None, 10, 5, 3.5, ["Type 'prefix' has size of '3.5', less than or equals to min limit(exclusive) '5'. Path: '/'"]),
49+
(None, None, 10, 5, 10, ["Type 'prefix' has size of '10', greater than or equals to max limit(exclusive) '10'. Path: '/'"]),
50+
(None, None, 10, 5, 5, ["Type 'prefix' has size of '5', less than or equals to min limit(exclusive) '5'. Path: '/'"]),
51+
(None, None, 10, 5, 8, []),
52+
(None, None, 10, 5, 7, []),
53+
(None, None, 10, 5, 8.5, []),
54+
(None, None, 10, 5, 7.5, []),
55+
]
56+
57+
for max_, min_, max_ex, min_ex, value, errors in data_matrix:
58+
print("Testing data: {} {} {} {} {}".format(max_, min_, max_ex, min_ex, value))
59+
c = ec()
60+
c._validate_range(max_, min_, max_ex, min_ex, value, '/', 'prefix')
61+
assert _remap_errors(c) == errors
62+
63+
# Test value type validation inside the method
64+
with pytest.raises(CoreError):
65+
c = ec()
66+
c._validate_range(5, 1, None, None, [1, 2, 3], '/', 'prefix')
67+
68+
with pytest.raises(CoreError):
69+
c = ec()
70+
c._validate_range(5, 1, None, None, {'a': 1, 'b': 2, 'c': 3}, '/', 'prefix')
71+
72+
3173
def test_validate_timestamp():
3274
data_matrix = [
3375
("", ["Timestamp value is empty. Path: ''"]),

0 commit comments

Comments
 (0)