From: Aleš Date: Thu, 10 Feb 2022 14:01:06 +0000 (+0100) Subject: datamodel: types: improved IntRangeBase X-Git-Tag: v6.0.0a1~42^2~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=cde938aea95fc7ca6690f25b27fa6ff263503dab;p=thirdparty%2Fknot-resolver.git datamodel: types: improved IntRangeBase - the value can also be limited from one side (_min or _max) --- diff --git a/manager/knot_resolver_manager/datamodel/types/base_types.py b/manager/knot_resolver_manager/datamodel/types/base_types.py index 8615d4d29..b168fbc44 100644 --- a/manager/knot_resolver_manager/datamodel/types/base_types.py +++ b/manager/knot_resolver_manager/datamodel/types/base_types.py @@ -64,9 +64,8 @@ class IntRangeBase(IntBase): Base class to work with integer value in range. Just inherit the class and set the values for '_min' and '_max'. - class CustomIntRange(IntRangeBase): + class IntNonNegative(IntRangeBase): _min: int = 0 - _max: int = 10_000 """ _min: int @@ -74,10 +73,14 @@ class IntRangeBase(IntBase): def __init__(self, source_value: Any, object_path: str = "/") -> None: super().__init__(source_value) - if isinstance(source_value, int): - if not self._min <= source_value <= self._max: + if isinstance(source_value, int) and not isinstance(source_value, bool): + if hasattr(self, "_min") and (source_value < self._min): raise SchemaException( - f"Integer value {source_value} out of range <{self._min}, {self._max}>", object_path + f"The value {source_value} is lower than the allowed minimum {self._min}.", object_path + ) + if hasattr(self, "_max") and (source_value > self._max): + raise SchemaException( + f"The value {source_value} is higher than the allowed maximum {self._max}", object_path ) self._value = source_value else: @@ -89,7 +92,12 @@ class IntRangeBase(IntBase): @classmethod def json_schema(cls: Type["IntRangeBase"]) -> Dict[Any, Any]: - return {"type": "integer", "minimum": 0, "maximum": 65_535} + typ: Dict[str, Any] = {"type": "integer"} + if hasattr(cls, "_min"): + typ["minimum"] = cls._min + if hasattr(cls, "_max"): + typ["maximum"] = cls._max + return typ class PatternBase(StrBase): diff --git a/manager/tests/unit/datamodel/types/test_base_types.py b/manager/tests/unit/datamodel/types/test_base_types.py new file mode 100644 index 000000000..e7a55179e --- /dev/null +++ b/manager/tests/unit/datamodel/types/test_base_types.py @@ -0,0 +1,39 @@ +import ipaddress + +from pytest import raises + +from knot_resolver_manager.datamodel.types.base_types import IntRangeBase +from knot_resolver_manager.exceptions import KresManagerException + + +def test_int_range_base(): + class MinTest(IntRangeBase): + _min = 10 + + assert int(MinTest(10)) == 10 + assert int(MinTest(20)) == 20 + + with raises(KresManagerException): + MinTest(9) + + class MaxTest(IntRangeBase): + _max = 25 + + assert int(MaxTest(20)) == 20 + assert int(MaxTest(25)) == 25 + + with raises(KresManagerException): + MaxTest(26) + + class MinMaxTest(IntRangeBase): + _min = 10 + _max = 25 + + assert int(MinMaxTest(10)) == 10 + assert int(MinMaxTest(20)) == 20 + assert int(MinMaxTest(25)) == 25 + + with raises(KresManagerException): + MinMaxTest(9) + with raises(KresManagerException): + MinMaxTest(26)