From ff39f18177ebabe057163db80e4619879953d085 Mon Sep 17 00:00:00 2001 From: Arpan Mahanty Date: Fri, 19 Apr 2024 00:39:34 +0530 Subject: [PATCH 1/2] Added unit tests for invalid preference defination --- tests/test_settings.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/test_settings.py b/tests/test_settings.py index 8b5dd85..32c17c6 100644 --- a/tests/test_settings.py +++ b/tests/test_settings.py @@ -51,6 +51,9 @@ def preferences() -> AppPreferences: yield AppPreferences() +# Definition -------------------------------------- + + def test_prefs_are_registered() -> None: """Test if the preferences are registered properly or not""" @@ -58,6 +61,18 @@ def test_prefs_are_registered() -> None: assert len(PREF_DEF_REGISTRY) > 0, "No preference registered" +def test_invalid_pref_definition() -> None: + """Test that error is raised if preference definition is invalid""" + + with pytest.raises(TypeError): + # Declare a invalid preference definition + class InvalidPreference(Preference): + desc = "This is invalid definition and must throw exception" + + +# App Preferences ---------------------------------- + + def test_app_preference_getter_and_setters(preferences: AppPreferences): """Test if getting and setting preference is properly working or not.""" From 01be309c07c760a07eb486877d518cee212fae6d Mon Sep 17 00:00:00 2001 From: Arpan Mahanty Date: Fri, 19 Apr 2024 00:51:08 +0530 Subject: [PATCH 2/2] Added unit tests for preference with a callable as value --- tests/test_settings.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/tests/test_settings.py b/tests/test_settings.py index 32c17c6..27d7114 100644 --- a/tests/test_settings.py +++ b/tests/test_settings.py @@ -1,3 +1,5 @@ +import math + import pytest from fastboot.settings import PREF_DEF_REGISTRY, AppPreferences, Preference @@ -38,12 +40,21 @@ class DummyPreference(Preference): default = "default_value" validator = text_is_not_empty + class DummyPreferenceWithCallableValue(Preference): + name = "dummy_preference_with_callable_value" + default = math.sqrt + desc = """\ + This is a dummy preference with a method passed as value + """ + def teardown_module(module): """teardown any state that was previously setup with a setup_module method.""" # Remove dummy registered preferences - PREF_DEF_REGISTRY[:] = [pref for pref in PREF_DEF_REGISTRY if pref.__class__.__name__ not in ["DummyPreference"]] + PREF_DEF_REGISTRY[:] = [ + pref for pref in PREF_DEF_REGISTRY if not pref.__class__.__name__.startswith("DummyPreference") + ] @pytest.fixture