Skip to content

Commit

Permalink
🧪 Add unit tests for chainmap prefix caching
Browse files Browse the repository at this point in the history
  • Loading branch information
foosel committed Oct 4, 2023
1 parent 59aa6f1 commit 4773e9c
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
8 changes: 8 additions & 0 deletions tests/settings/_files/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
67 changes: 67 additions & 0 deletions tests/settings/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -911,3 +915,66 @@ def test_unflatten(self, value, expected):
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)

0 comments on commit 4773e9c

Please sign in to comment.