]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
datamodel: types: improved IntRangeBase
authorAleš <ales.mrazek@nic.cz>
Thu, 10 Feb 2022 14:01:06 +0000 (15:01 +0100)
committerAleš Mrázek <ales.mrazek@nic.cz>
Fri, 8 Apr 2022 14:17:54 +0000 (16:17 +0200)
- the value can also be limited from one side (_min or _max)

manager/knot_resolver_manager/datamodel/types/base_types.py
manager/tests/unit/datamodel/types/test_base_types.py [new file with mode: 0644]

index 8615d4d292bdda0a55307193a487213784ad0121..b168fbc44feef4a197a243397203ae6e56a6f9b2 100644 (file)
@@ -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 (file)
index 0000000..e7a5517
--- /dev/null
@@ -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)