diff --git a/tests/settings/_files/config.yaml b/tests/settings/_files/config.yaml index c6d98294a..c660f9a12 100644 --- a/tests/settings/_files/config.yaml +++ b/tests/settings/_files/config.yaml @@ -18,6 +18,14 @@ plugins: a: 1 b: 2 c: 3 + baz: + a: 1 + b: 2 + c: 3 + d: + a: 1 + b: 2 + c: 3 server: port: 8080 devel: diff --git a/tests/settings/test_settings.py b/tests/settings/test_settings.py index 101000a88..2af22b7bd 100644 --- a/tests/settings/test_settings.py +++ b/tests/settings/test_settings.py @@ -768,6 +768,10 @@ def _key(*path): return octoprint.settings._CHAINMAP_SEP.join(path) +def _prefix(*path): + return _key(*path) + octoprint.settings._CHAINMAP_SEP + + @ddt.ddt class ChainmapTest(unittest.TestCase): def setUp(self): @@ -911,3 +915,66 @@ class ChainmapTest(unittest.TestCase): self.assertEqual( expected, octoprint.settings.HierarchicalChainMap._unflatten(value) ) + + def test_prefix_caching_has_populates(self): + # this should populate the prefix cache + self.chainmap.has_path(["plugins", "foo"]) + + # validate that + self.assertTrue(len(self.chainmap._prefixed_keys) == 1) + self.assertTrue(_prefix("plugins", "foo") in self.chainmap._prefixed_keys) + + def test_prefix_caching_get_populates(self): + # this should populate the prefix cache + self.chainmap.get_by_path(["plugins", "foo"]) + + # validate that + self.assertTrue(len(self.chainmap._prefixed_keys) == 1) + self.assertTrue(_prefix("plugins", "foo") in self.chainmap._prefixed_keys) + + def test_prefix_caching_scalars_ignored(self): + # this shouldn't populate the prefix cache + self.chainmap.has_path(["api", "key"]) + + # validate that + self.assertTrue(len(self.chainmap._prefixed_keys) == 0) + + def test_prefix_caching_set_invalidates(self): + # this should populate the prefix cache + self.chainmap.has_path(["plugins", "foo"]) + + # validate that + self.assertTrue(len(self.chainmap._prefixed_keys) == 1) + self.assertTrue(_prefix("plugins", "foo") in self.chainmap._prefixed_keys) + + # this should extend the prefix cache + self.chainmap.get_by_path(["plugins", "foo", "bar"]) + + # validate that + self.assertTrue(len(self.chainmap._prefixed_keys) == 2) + self.chainmap.has_path(["plugins", "foo", "bar"]) + + # this should remove all plugins.foo keys in the prefix cache + self.chainmap.set_by_path(["plugins", "foo"], {}) + + # validate that + self.assertTrue(len(self.chainmap._prefixed_keys) == 0) + + def test_prefix_caching_del_invalidates(self): + # this should populate the prefix cache + self.chainmap.has_path(["plugins", "baz", "d"]) + + # validate that + self.assertTrue(len(self.chainmap._prefixed_keys) == 1) + self.assertTrue(_prefix("plugins", "baz", "d") in self.chainmap._prefixed_keys) + + # this should remove all plugins.baz keys in the prefix cache + self.chainmap.del_by_path(["plugins", "baz"]) + + # validate that + keys = [ + key + for key in self.chainmap._prefixed_keys + if key.startswith(_prefix("plugins", "baz")) + ] + self.assertTrue(len(keys) == 0)