implement property carousel for profile switching

This commit is contained in:
Jakob Ketterl
2021-03-01 00:26:56 +01:00
parent c7db144f7b
commit 6bd47cf914
3 changed files with 119 additions and 16 deletions

View File

@ -0,0 +1,78 @@
from unittest import TestCase
from unittest.mock import Mock
from owrx.property import PropertyCarousel, PropertyLayer, PropertyDeleted
class PropertyCarouselTest(TestCase):
def testInitiallyEmpty(self):
pc = PropertyCarousel()
with self.assertRaises(KeyError):
x = pc["testkey"]
def testPropertyAccess(self):
pc = PropertyCarousel()
pl = PropertyLayer(testkey="testvalue")
pc.addLayer("test", pl)
pc.switch("test")
self.assertEqual(pc["testkey"], "testvalue")
def testWriteAccess(self):
pc = PropertyCarousel()
pl = PropertyLayer(testkey="old_value")
pc.addLayer("test", pl)
pc.switch("test")
pc["testkey"] = "new_value"
self.assertEqual(pc["testkey"], "new_value")
self.assertEqual(pl["testkey"], "new_value")
def testForwardsEvents(self):
pc = PropertyCarousel()
pl = PropertyLayer(testkey="old_value")
pc.addLayer("test", pl)
pc.switch("test")
mock = Mock()
pc.wire(mock.method)
pc["testkey"] = "new_value"
mock.method.assert_called_once_with({"testkey": "new_value"})
def testStopsForwardingAfterSwitch(self):
pc = PropertyCarousel()
pl_x = PropertyLayer(testkey="old_value")
pc.addLayer("x", pl_x)
pl_y = PropertyLayer(testkey="new_value")
pc.addLayer("y", pl_y)
pc.switch("x")
pc.switch("y")
mock = Mock()
pc.wire(mock.method)
pl_x["testkey"] = "new_value"
mock.method.assert_not_called()
def testEventsOnSwitch(self):
pc = PropertyCarousel()
pl_x = PropertyLayer(old_key="old_value")
pc.addLayer("x", pl_x)
pl_y = PropertyLayer(new_key="new_value")
pc.addLayer("y", pl_y)
pc.switch("x")
mock = Mock()
pc.wire(mock.method)
pc.switch("y")
mock.method.assert_called_once_with({"old_key": PropertyDeleted, "new_key": "new_value"})
def testNoEventsIfKeysDontChange(self):
pc = PropertyCarousel()
pl_x = PropertyLayer(testkey="same_value")
pc.addLayer("x", pl_x)
pl_y = PropertyLayer(testkey="same_value")
pc.addLayer("y", pl_y)
pc.switch("x")
mock = Mock()
pc.wire(mock.method)
pc.switch("y")
mock.method.assert_not_called()
def testKeyErrorOnInvalidSwitch(self):
pc = PropertyCarousel()
with self.assertRaises(KeyError):
pc.switch("doesntmatter")