From 3d20e3ed801fa2e29b205ef50db263b9a69cd858 Mon Sep 17 00:00:00 2001 From: Jakob Ketterl Date: Sat, 6 Mar 2021 22:20:47 +0100 Subject: [PATCH] simplify api by abstracting layer changes --- owrx/property/__init__.py | 10 ++++++---- owrx/source/__init__.py | 2 +- test/property/test_property_carousel.py | 18 ++++++++++++------ 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/owrx/property/__init__.py b/owrx/property/__init__.py index 9586cb7..80bd364 100644 --- a/owrx/property/__init__.py +++ b/owrx/property/__init__.py @@ -353,10 +353,12 @@ class PropertyCarousel(PropertyDelegator): return self.emptyLayer def addLayer(self, key, value): - self.layers[key] = value - - def hasLayer(self, key): - return key in self.layers + if key in self.layers and self.layers[key] is self.pm: + self.layers[key] = value + # switch after introducing the new value + self.switch(key) + else: + self.layers[key] = value def removeLayer(self, key): if key in self.layers and self.layers[key] is self.pm: diff --git a/owrx/source/__init__.py b/owrx/source/__init__.py index 1fc0650..8c210db 100644 --- a/owrx/source/__init__.py +++ b/owrx/source/__init__.py @@ -84,7 +84,7 @@ class SdrProfileCarousel(PropertyCarousel): for profile_id, profile in changes.items(): if profile is PropertyDeleted: self.removeLayer(profile_id) - elif not self.hasLayer(profile_id): + else: self.addLayer(profile_id, profile) def _getDefaultLayer(self): diff --git a/test/property/test_property_carousel.py b/test/property/test_property_carousel.py index 29f79ee..bc0c837 100644 --- a/test/property/test_property_carousel.py +++ b/test/property/test_property_carousel.py @@ -77,12 +77,6 @@ class PropertyCarouselTest(TestCase): with self.assertRaises(KeyError): pc.switch("doesntmatter") - def testHasLayer(self): - pc = PropertyCarousel() - pc.addLayer("testkey", PropertyLayer()) - self.assertTrue(pc.hasLayer("testkey")) - self.assertFalse(pc.hasLayer("otherkey")) - def testRemoveLayer(self): pc = PropertyCarousel() pl = PropertyLayer(testkey="testvalue") @@ -117,3 +111,15 @@ class PropertyCarouselTest(TestCase): pc = PropertyCarousel() with self.assertRaises(PropertyWriteError): pc["testkey"] = "testvalue" + + def testSendsChangesIfActiveLayerIsReplaced(self): + pc = PropertyCarousel() + pl = PropertyLayer(testkey="testvalue") + pc.addLayer("x", pl) + pc.switch("x") + self.assertEqual(pc["testkey"], "testvalue") + mock = Mock() + pc.wire(mock.method) + pl = PropertyLayer(testkey="othervalue") + pc.addLayer("x", pl) + mock.method.assert_called_once_with({"testkey": "othervalue"})