Skip to content

Commit

Permalink
category and number settings now checks when getting wrong stored value
Browse files Browse the repository at this point in the history
  • Loading branch information
tadeubas committed Aug 27, 2024
1 parent 04e7cca commit 00071cf
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/krux/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,15 @@ def __init__(self, attr, default_value, categories):
super().__init__(attr, default_value)
self.categories = categories

def __get__(self, obj, objtype=None):
if obj is None:
return self

stored_val = store.get(obj.namespace, self.attr, self.default_value)
if stored_val not in self.categories:
return self.default_value
return stored_val


class NumberSetting(Setting):
"""Setting that can be a number within a defined range"""
Expand All @@ -104,6 +113,19 @@ def __init__(self, numtype, attr, default_value, value_range):
self.numtype = numtype
self.value_range = value_range

def __get__(self, obj, objtype=None):
if obj is None:
return self

stored_val = store.get(obj.namespace, self.attr, self.default_value)
if (
(self.numtype is int and isinstance(stored_val, self.numtype))
or (self.numtype is float and isinstance(stored_val, (self.numtype, int)))
) and self.value_range[0] <= stored_val <= self.value_range[1]:
return stored_val

return self.default_value


class Store:
"""Acts as a simple JSON file store for settings, falling back to an in-memory dict if no SD"""
Expand Down
116 changes: 116 additions & 0 deletions tests/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,122 @@ def test_init(mocker, m5stickv):
sn.label("test")


def test_stored_i18n_settings(mocker, m5stickv):
# mock store singleton creation before import
stored_settings = """{"settings": {"i18n": {"locale": "pl-PL"}}}"""
mocker.patch("builtins.open", mocker.mock_open(read_data=stored_settings))

# import will create store singleton with mocked values
from krux.krux_settings import I18nSettings

i18n = I18nSettings()

assert i18n.locale == "pl-PL"


def test_wrong_stored_i18n_settings(mocker, m5stickv):
# mock store singleton creation before import
stored_settings = """{"settings": {"i18n": {"locale": "aa-AA"}}}"""
mocker.patch("builtins.open", mocker.mock_open(read_data=stored_settings))

# import will create store singleton with mocked values
from krux.krux_settings import I18nSettings

i18n = I18nSettings()

assert i18n.locale == "en-US"


def test_stored_adafruit_printer_settings(mocker, m5stickv):
print("")

# mock store singleton creation before import
stored_settings = (
"""{"settings": {"printer": {"thermal": {"adafruit": {"line_delay": 35}}}}}"""
)
mocker.patch("builtins.open", mocker.mock_open(read_data=stored_settings))

# import will create store singleton with mocked values
from krux.krux_settings import AdafruitPrinterSettings

ada = AdafruitPrinterSettings()
assert ada.line_delay == 35


def test_wrong_stored_adafruit_printer_settings(mocker, m5stickv):
print("")

# mock store singleton creation before import
stored_settings = (
"""{"settings": {"printer": {"thermal": {"adafruit": {"line_delay": 511}}}}}"""
)
mocker.patch("builtins.open", mocker.mock_open(read_data=stored_settings))

# import will create store singleton with mocked values
from krux.krux_settings import AdafruitPrinterSettings

ada = AdafruitPrinterSettings()
assert ada.line_delay == 20


def test_float_stored_adafruit_printer_settings(mocker, m5stickv):
print("")

# mock store singleton creation before import
stored_settings = (
"""{"settings": {"printer": {"thermal": {"adafruit": {"line_delay": 21.2}}}}}"""
)
mocker.patch("builtins.open", mocker.mock_open(read_data=stored_settings))

# import will create store singleton with mocked values
from krux.krux_settings import AdafruitPrinterSettings

ada = AdafruitPrinterSettings()
assert ada.line_delay == 20


def test_string_stored_adafruit_printer_settings(mocker, m5stickv):
print("")

# mock store singleton creation before import
stored_settings = """{"settings": {"printer": {"thermal": {"adafruit": {"line_delay": 1,abc}}}}}"""
mocker.patch("builtins.open", mocker.mock_open(read_data=stored_settings))

# import will create store singleton with mocked values
from krux.krux_settings import AdafruitPrinterSettings

ada = AdafruitPrinterSettings()
assert ada.line_delay == 20


def test_stored_cnc_settings(mocker, m5stickv):
print("")

# mock store singleton creation before import
stored_settings = """{"settings": {"printer": {"cnc": {"border_padding": 127}}}}"""
mocker.patch("builtins.open", mocker.mock_open(read_data=stored_settings))

# import will create store singleton with mocked values
from krux.krux_settings import CNCSettings

cnc = CNCSettings()
assert cnc.border_padding == 127


def test_wrong_stored_cnc_settings(mocker, m5stickv):
print("")

# mock store singleton creation before import
stored_settings = """{"settings": {"printer": {"thermal": {"adafruit": {"line_delay": 1.abc}}}}}"""
mocker.patch("builtins.open", mocker.mock_open(read_data=stored_settings))

# import will create store singleton with mocked values
from krux.krux_settings import CNCSettings

cnc = CNCSettings()
assert cnc.border_padding == 0.0625


def test_store_init(mocker, m5stickv):
from krux.settings import Store, SETTINGS_FILENAME, SD_PATH

Expand Down

0 comments on commit 00071cf

Please sign in to comment.