create filtering that prevents overwriting the device name

This commit is contained in:
Jakob Ketterl
2021-02-24 00:09:57 +01:00
parent 4199a583f8
commit f69d78926e
8 changed files with 99 additions and 25 deletions

View File

View File

@ -0,0 +1,17 @@
from owrx.property.filter import ByLambda
from unittest import TestCase
from unittest.mock import Mock
class TestByLambda(TestCase):
def testPositive(self):
mock = Mock(return_value=True)
filter = ByLambda(mock)
self.assertTrue(filter.apply("test_key"))
mock.assert_called_with("test_key")
def testNegateive(self):
mock = Mock(return_value=False)
filter = ByLambda(mock)
self.assertFalse(filter.apply("test_key"))
mock.assert_called_with("test_key")

View File

@ -0,0 +1,12 @@
from owrx.property.filter import ByPropertyName
from unittest import TestCase
class ByPropertyNameTest(TestCase):
def testNameIsInList(self):
filter = ByPropertyName("test_key")
self.assertTrue(filter.apply("test_key"))
def testNameNotInList(self):
filter = ByPropertyName("test_key")
self.assertFalse(filter.apply("other_key"))

View File

@ -7,20 +7,26 @@ class PropertyFilterTest(TestCase):
def testPassesProperty(self):
pm = PropertyLayer()
pm["testkey"] = "testvalue"
pf = PropertyFilter(pm, "testkey")
mock = Mock()
mock.apply.return_value = True
pf = PropertyFilter(pm, mock)
self.assertEqual(pf["testkey"], "testvalue")
def testMissesProperty(self):
pm = PropertyLayer()
pm["testkey"] = "testvalue"
pf = PropertyFilter(pm, "other_key")
mock = Mock()
mock.apply.return_value = False
pf = PropertyFilter(pm, mock)
self.assertFalse("testkey" in pf)
with self.assertRaises(KeyError):
x = pf["testkey"]
def testForwardsEvent(self):
pm = PropertyLayer()
pf = PropertyFilter(pm, "testkey")
mock = Mock()
mock.apply.return_value = True
pf = PropertyFilter(pm, mock)
mock = Mock()
pf.wire(mock.method)
pm["testkey"] = "testvalue"
@ -28,7 +34,9 @@ class PropertyFilterTest(TestCase):
def testForwardsPropertyEvent(self):
pm = PropertyLayer()
pf = PropertyFilter(pm, "testkey")
mock = Mock()
mock.apply.return_value = True
pf = PropertyFilter(pm, mock)
mock = Mock()
pf.wireProperty("testkey", mock.method)
pm["testkey"] = "testvalue"
@ -36,7 +44,9 @@ class PropertyFilterTest(TestCase):
def testForwardsWrite(self):
pm = PropertyLayer()
pf = PropertyFilter(pm, "testkey")
mock = Mock()
mock.apply.return_value = True
pf = PropertyFilter(pm, mock)
pf["testkey"] = "testvalue"
self.assertTrue("testkey" in pm)
self.assertEqual(pm["testkey"], "testvalue")
@ -44,7 +54,9 @@ class PropertyFilterTest(TestCase):
def testOverwrite(self):
pm = PropertyLayer()
pm["testkey"] = "old value"
pf = PropertyFilter(pm, "testkey")
mock = Mock()
mock.apply.return_value = True
pf = PropertyFilter(pm, mock)
pf["testkey"] = "new value"
self.assertEqual(pm["testkey"], "new value")
self.assertEqual(pf["testkey"], "new value")
@ -52,7 +64,9 @@ class PropertyFilterTest(TestCase):
def testRejectsWrite(self):
pm = PropertyLayer()
pm["testkey"] = "old value"
pf = PropertyFilter(pm, "otherkey")
mock = Mock()
mock.apply.return_value = False
pf = PropertyFilter(pm, mock)
with self.assertRaises(KeyError):
pf["testkey"] = "new value"
self.assertEqual(pm["testkey"], "old value")