2021-02-26 00:12:03 +00:00
|
|
|
from owrx.property import PropertyLayer, PropertyDeleted
|
2020-03-23 22:56:05 +00:00
|
|
|
from unittest import TestCase
|
|
|
|
from unittest.mock import Mock
|
|
|
|
|
|
|
|
|
|
|
|
class PropertyLayerTest(TestCase):
|
2021-02-11 18:31:44 +00:00
|
|
|
def testCreationWithKwArgs(self):
|
|
|
|
pm = PropertyLayer(testkey="value")
|
|
|
|
self.assertEqual(pm["testkey"], "value")
|
|
|
|
|
|
|
|
# this should be synonymous, so this is rather for illustration purposes
|
|
|
|
contents = {"testkey": "value"}
|
|
|
|
pm = PropertyLayer(**contents)
|
|
|
|
self.assertEqual(pm["testkey"], "value")
|
|
|
|
|
2020-03-23 22:56:05 +00:00
|
|
|
def testKeyIsset(self):
|
|
|
|
pm = PropertyLayer()
|
|
|
|
self.assertFalse("some_key" in pm)
|
|
|
|
|
|
|
|
def testKeyError(self):
|
|
|
|
pm = PropertyLayer()
|
|
|
|
with self.assertRaises(KeyError):
|
|
|
|
x = pm["some_key"]
|
|
|
|
|
|
|
|
def testSubscription(self):
|
|
|
|
pm = PropertyLayer()
|
|
|
|
pm["testkey"] = "before"
|
|
|
|
mock = Mock()
|
|
|
|
pm.wire(mock.method)
|
|
|
|
pm["testkey"] = "after"
|
2020-12-30 16:14:06 +00:00
|
|
|
mock.method.assert_called_once_with({"testkey": "after"})
|
2020-03-23 22:56:05 +00:00
|
|
|
|
|
|
|
def testUnsubscribe(self):
|
|
|
|
pm = PropertyLayer()
|
|
|
|
pm["testkey"] = "before"
|
|
|
|
mock = Mock()
|
|
|
|
sub = pm.wire(mock.method)
|
|
|
|
pm["testkey"] = "between"
|
2020-12-30 16:14:06 +00:00
|
|
|
mock.method.assert_called_once_with({"testkey": "between"})
|
2020-03-23 22:56:05 +00:00
|
|
|
|
|
|
|
mock.reset_mock()
|
|
|
|
pm.unwire(sub)
|
|
|
|
pm["testkey"] = "after"
|
|
|
|
mock.method.assert_not_called()
|
|
|
|
|
|
|
|
def testContains(self):
|
|
|
|
pm = PropertyLayer()
|
|
|
|
pm["testkey"] = "value"
|
|
|
|
self.assertTrue("testkey" in pm)
|
|
|
|
|
|
|
|
def testDoesNotContain(self):
|
|
|
|
pm = PropertyLayer()
|
|
|
|
self.assertFalse("testkey" in pm)
|
|
|
|
|
|
|
|
def testSubscribeBeforeSet(self):
|
|
|
|
pm = PropertyLayer()
|
|
|
|
mock = Mock()
|
|
|
|
pm.wireProperty("testkey", mock.method)
|
|
|
|
mock.method.assert_not_called()
|
|
|
|
pm["testkey"] = "newvalue"
|
|
|
|
mock.method.assert_called_once_with("newvalue")
|
2020-03-23 23:18:10 +00:00
|
|
|
|
|
|
|
def testEventPreventedWhenValueUnchanged(self):
|
|
|
|
pm = PropertyLayer()
|
|
|
|
pm["testkey"] = "testvalue"
|
|
|
|
mock = Mock()
|
|
|
|
pm.wire(mock.method)
|
|
|
|
pm["testkey"] = "testvalue"
|
|
|
|
mock.method.assert_not_called()
|
2021-02-26 00:12:03 +00:00
|
|
|
|
|
|
|
def testDeletionIsSent(self):
|
|
|
|
pm = PropertyLayer(testkey="somevalue")
|
|
|
|
mock = Mock()
|
|
|
|
pm.wireProperty("testkey", mock.method)
|
|
|
|
mock.method.reset_mock()
|
|
|
|
del pm["testkey"]
|
|
|
|
mock.method.assert_called_once_with(PropertyDeleted)
|
|
|
|
|
|
|
|
def testDeletionInGeneralWiring(self):
|
|
|
|
pm = PropertyLayer(testkey="somevalue")
|
|
|
|
mock = Mock()
|
|
|
|
pm.wire(mock.method)
|
|
|
|
del pm["testkey"]
|
|
|
|
mock.method.assert_called_once_with({"testkey": PropertyDeleted})
|
|
|
|
|
|
|
|
def testNoDeletionEventWhenPropertyDoesntExist(self):
|
|
|
|
pm = PropertyLayer(otherkey="somevalue")
|
|
|
|
mock = Mock()
|
|
|
|
pm.wireProperty("testkey", mock.method)
|
|
|
|
mock.method.reset_mock()
|
|
|
|
with self.assertRaises(KeyError):
|
|
|
|
del pm["testkey"]
|
|
|
|
mock.method.assert_not_called()
|