]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
datamodel: types: SizeUnit: added method to return value in bytes
authorAleš <ales.mrazek@nic.cz>
Tue, 21 Sep 2021 18:58:34 +0000 (20:58 +0200)
committerAleš Mrázek <ales.mrazek@nic.cz>
Fri, 8 Apr 2022 14:17:53 +0000 (16:17 +0200)
manager/knot_resolver_manager/datamodel/types.py
manager/tests/datamodel/test_datamodel_types.py

index 440d7226e974a2f8abd3aa3ec15fab24d7ea1da5..e70bd89d3ad93e70dfcf9103473893545b8dd183 100644 (file)
@@ -77,6 +77,9 @@ class SizeUnit(Unit):
     _re = re.compile(r"^([0-9]+)\s{0,1}([BKMG]){0,1}$")
     _units = {"B": 1, "K": 1024, "M": 1024 ** 2, "G": 1024 ** 3}
 
+    def bytes(self) -> int:
+        return self._value
+
 
 class TimeUnit(Unit):
     _re = re.compile(r"^(\d+)\s{0,1}([smhd]s?){0,1}$")
@@ -136,10 +139,10 @@ class Listen(SchemaNode):
     _PREVIOUS_SCHEMA = Raw
 
     typ: ListenType
-    ip: Optional[Union[ipaddress.IPv4Address, ipaddress.IPv6Address]] = None
-    port: Optional[int] = None
-    unix_socket: Optional[AnyPath] = None
-    interface: Optional[str] = None
+    ip: Optional[Union[ipaddress.IPv4Address, ipaddress.IPv6Address]]
+    port: Optional[int]
+    unix_socket: Optional[AnyPath]
+    interface: Optional[str]
 
     def _typ(self, origin: Raw):
         present = {
index 340ebf8c2d5e57ae34005fc2332ec8f8c693f5f0..bd8904115fc54ff242f2f5d27b86d3cafdbf841d 100644 (file)
@@ -40,24 +40,15 @@ def test_time_unit():
 
 
 def test_parsing_units():
-    class TestClass(SchemaNode):
+    class TestSchema(SchemaNode):
         size: SizeUnit
         time: TimeUnit
 
-    class TestClassStrict(SchemaNode):
-        size: int
-        time: int
-
-        def _validate(self) -> None:
-            pass
-
-    obj = TestClass({"size": "3K", "time": "10m"})
-    assert obj.size == SizeUnit("3072B")
-    assert obj.time == TimeUnit("10m")
-
-    strict = TestClassStrict(obj)
-    assert strict.size == 3 * 1024
-    assert strict.time == 10 * 60 * 1000
+    o = TestSchema({"size": "3K", "time": "10m"})
+    assert o.size == SizeUnit("3072B")
+    assert o.time == TimeUnit("10m")
+    assert o.size.bytes() == 3072
+    assert o.time.seconds() == 10 * 60
 
 
 def test_anypath():