From b5b88e2a7b400ca19fab3c0c82a3de828eaf3d4a Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Mon, 6 Jan 2025 19:29:58 -0600 Subject: [PATCH] fix: bugfix for bool simple settings with default value --- src/wuttaweb/views/master.py | 3 +++ tests/views/test_master.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/src/wuttaweb/views/master.py b/src/wuttaweb/views/master.py index 3701980..cedb1cf 100644 --- a/src/wuttaweb/views/master.py +++ b/src/wuttaweb/views/master.py @@ -1329,6 +1329,9 @@ class MasterView(View): if name in data: value = data[name] + elif simple.get('type') is bool: + # nb. bool false will be *missing* from data + value = False else: value = simple.get('default') diff --git a/tests/views/test_master.py b/tests/views/test_master.py index 56c51c2..d334258 100644 --- a/tests/views/test_master.py +++ b/tests/views/test_master.py @@ -1565,6 +1565,38 @@ class TestMasterView(WebTestCase): count = self.session.query(model.Setting).count() self.assertEqual(count, 0) + def test_configure_gather_settings(self): + view = self.make_view() + + simple_settings = [ + {'name': 'wutta.app_title'}, + {'name': 'wutta.foo'}, + {'name': 'wutta.flag', 'type': bool, 'default': True}, + {'name': 'wutta.number', 'type': int, 'default': 42}, + {'name': 'wutta.value1', 'save_if_empty': True}, + {'name': 'wutta.value2', 'save_if_empty': False}, + {'name': 'wutta.value3', 'save_if_empty': False, 'default': 'baz'}, + ] + + data = { + 'wutta.app_title': 'Poser', + 'wutta.foo': 'bar', + 'wutta.number': 44, + 'wutta.value1': None, + } + + with patch.object(view, 'configure_get_simple_settings', return_value=simple_settings): + settings = view.configure_gather_settings(data) + self.assertEqual(len(settings), 6) + self.assertEqual(settings, [ + {'name': 'wutta.app_title', 'value': 'Poser'}, + {'name': 'wutta.foo', 'value': 'bar'}, + {'name': 'wutta.flag', 'value': 'false'}, + {'name': 'wutta.number', 'value': '44'}, + {'name': 'wutta.value1', 'value': ''}, + {'name': 'wutta.value3', 'value': 'baz'}, + ]) + ############################## # row methods ##############################