From c8a51b416f2affb1e6ef256fc254fd78d74d362f Mon Sep 17 00:00:00 2001 From: Tobi-wan Kenobi Date: Fri, 9 Dec 2016 07:41:07 +0100 Subject: [PATCH] [core] Add "Store" interface Add an interface that allows arbitrary objects to store/retrieve arbitrary key/value pairs. This will be used for different purposes in the future: * Config class(es) can store user-defined parameters for modules * Widgets can store state * ??? see #23 --- bumblebee/store.py | 18 ++++++++++++++++++ tests/test_store.py | 24 ++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 bumblebee/store.py create mode 100644 tests/test_store.py diff --git a/bumblebee/store.py b/bumblebee/store.py new file mode 100644 index 0000000..9492c1d --- /dev/null +++ b/bumblebee/store.py @@ -0,0 +1,18 @@ +"""Store interface + +Allows arbitrary classes to offer a simple get/set +store interface by deriving from the Store class in +this module +""" + +class Store(object): + def __init__(self): + self._data = {} + + def set(self, key, value): + self._data[key] = value + + def get(self, key, default=None): + return self._data.get(key, default) + +# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/tests/test_store.py b/tests/test_store.py new file mode 100644 index 0000000..0b712f0 --- /dev/null +++ b/tests/test_store.py @@ -0,0 +1,24 @@ +# pylint: disable=C0103,C0111,W0703 + +import unittest + +from bumblebee.store import Store + +class TestStore(unittest.TestCase): + def setUp(self): + self.store = Store() + self.anyKey = "some-key" + self.anyValue = "some-value" + self.unsetKey = "invalid-key" + + def test_set_value(self): + self.store.set(self.anyKey, self.anyValue) + self.assertEquals(self.store.get(self.anyKey), self.anyValue) + + def test_get_invalid_value(self): + result = self.store.get(self.unsetKey) + self.assertEquals(result, None) + + def test_get_invalid_with_default_value(self): + result = self.store.get(self.unsetKey, self.anyValue) + self.assertEquals(result, self.anyValue)