start implementing a validation layer, refs #215

This commit is contained in:
Jakob Ketterl 2021-01-24 20:53:51 +01:00
parent 8b52988dcd
commit 40e531c0da
8 changed files with 131 additions and 0 deletions

View File

@ -1,4 +1,5 @@
from abc import ABC, abstractmethod
from owrx.property.validators import Validator
import logging
logger = logging.getLogger(__name__)
@ -23,6 +24,7 @@ class Subscription(object):
class PropertyManager(ABC):
def __init__(self):
self.subscribers = []
self.validators = {}
@abstractmethod
def __getitem__(self, item):
@ -81,6 +83,9 @@ class PropertyManager(ABC):
except Exception as e:
logger.exception(e)
def setValidator(self, name, validator):
self.validators[name] = Validator.of(validator)
class PropertyLayer(PropertyManager):
def __init__(self):

View File

@ -0,0 +1,42 @@
from abc import ABC, abstractmethod
class ValidatorException(Exception):
pass
class Validator(ABC):
@staticmethod
def of(x):
if isinstance(x, Validator):
return x
if callable(x):
return LambdaValidator(x)
raise ValidatorException("Cannot create validator")
@abstractmethod
def isValid(self, value):
pass
class LambdaValidator(Validator):
def __init__(self, c):
self.callable = c
def isValid(self, value):
return self.callable(value)
class NumberValidator(Validator):
def isValid(self, value):
return isinstance(value, int) or isinstance(value, float)
class IntegerValidator(Validator):
def isValid(self, value):
return isinstance(value, int)
class StringValidator(Validator):
def isValid(self, value):
return isinstance(value, str)

View File

View File

@ -0,0 +1,15 @@
from unittest import TestCase
from owrx.property.validators import IntegerValidator
class IntegerValidatorTest(TestCase):
def testPassesIntegers(self):
validator = IntegerValidator()
self.assertTrue(validator.isValid(123))
self.assertTrue(validator.isValid(-2))
def testDoesntPassOthers(self):
validator = IntegerValidator()
self.assertFalse(validator.isValid(.5))
self.assertFalse(validator.isValid("text"))
self.assertFalse(validator.isValid(object()))

View File

@ -0,0 +1,21 @@
from unittest import TestCase
from unittest.mock import Mock
from owrx.property.validators import LambdaValidator
class LambdaValidatorTest(TestCase):
def testPassesValue(self):
mock = Mock()
validator = LambdaValidator(mock.method)
validator.isValid("test")
mock.method.assert_called_once_with("test")
def testReturnsTrue(self):
validator = LambdaValidator(lambda x: True)
self.assertTrue(validator.isValid("any value"))
self.assertTrue(validator.isValid(3.1415926))
def testReturnsFalse(self):
validator = LambdaValidator(lambda x: False)
self.assertFalse(validator.isValid("any value"))
self.assertFalse(validator.isValid(42))

View File

@ -0,0 +1,18 @@
from unittest import TestCase
from owrx.property.validators import NumberValidator
class NumberValidatorTest(TestCase):
def testPassesNumbers(self):
validator = NumberValidator()
self.assertTrue(validator.isValid(123))
self.assertTrue(validator.isValid(-2))
self.assertTrue(validator.isValid(.5))
def testDoesntPassOthers(self):
validator = NumberValidator()
# bool is a subclass of int, so it passes this test.
# not sure if we need to be more specific or if this is alright.
# self.assertFalse(validator.isValid(True))
self.assertFalse(validator.isValid("text"))
self.assertFalse(validator.isValid(object()))

View File

@ -0,0 +1,14 @@
from unittest import TestCase
from owrx.property.validators import StringValidator
class StringValidatorTest(TestCase):
def testPassesStrings(self):
validator = StringValidator()
self.assertTrue(validator.isValid("text"))
def testDoesntPassOthers(self):
validator = StringValidator()
self.assertFalse(validator.isValid(123))
self.assertFalse(validator.isValid(-2))
self.assertFalse(validator.isValid(.5))
self.assertFalse(validator.isValid(object()))

View File

@ -0,0 +1,16 @@
from unittest import TestCase
from owrx.property.validators import Validator, NumberValidator, LambdaValidator
class ValidatorTest(TestCase):
def testReturnsValidator(self):
validator = NumberValidator()
self.assertIs(validator, Validator.of(validator))
def testTransformsLambda(self):
def my_callable(v):
return True
validator = Validator.of(my_callable)
self.assertIsInstance(validator, LambdaValidator)
self.assertTrue(validator.isValid("test"))