def test_parent_setting_precedence_over_environ_value(self): setting = appsettings.NestedDictSetting( settings=dict(a=appsettings.StringSetting(), b=appsettings.StringSetting(),), name="setting" ) setting.check() assert setting.value["a"] == "A" assert setting.value["b"] == "B"
def test_setting_raw_value(self): setting = appsettings.Setting(name="setting") setting.check() with pytest.raises(AttributeError): setting.raw_value with override_settings(SETTING="value"): setting.check() assert setting.raw_value == "value" setting.parent_setting = appsettings.NestedDictSetting( settings={}, name="parent_setting") with override_settings(PARENT_SETTING={}): with pytest.raises(KeyError): setting.raw_value with override_settings(PARENT_SETTING={"SETTING": "value"}): setting.check() assert setting.raw_value == "value" setting.parent_setting = appsettings.NestedListSetting( inner_setting=setting, name="parent_setting") setting.nested_list_index = 0 with override_settings(PARENT_SETTING=[]): with pytest.raises(IndexError): setting.raw_value with override_settings(PARENT_SETTING=["value"]): setting.check() assert setting.raw_value == "value"
def test_nested_setting(self): setting = appsettings.NestedDictSetting(settings=dict()) assert setting.value == {} setting.transform_default = True assert setting.value == {} setting = appsettings.NestedDictSetting( name="setting", default={}, settings=dict( bool1=appsettings.BooleanSetting(default=False), bool2=appsettings.BooleanSetting(name="bool3", default=True), ), ) assert setting.value == {} with override_settings(SETTING={"BOOL3": False}): assert setting.value == {"bool1": False, "bool2": False}
class AppConf(appsettings.AppSettings): setting = cast( Dict[str, str], appsettings.NestedDictSetting( settings=dict( one=appsettings.StringSetting(required=True), two=appsettings.StringSetting(default="Def_2"), ), required=True, ), )
class PainSettings(appsettings.AppSettings): """Specific settings for django-pain app.""" # Dictionary of names and dotted paths to processor classes setting. processors = NamedClassSetting( 'django_pain.processors.AbstractPaymentProcessor', required=True, key_type=str, value_type=str) # A card payment handler classes. card_payment_handlers = NamedClassSetting( 'django_pain.card_payment_handlers.AbstractCardPaymentHandler', key_type=str, value_type=str) # Location of process_payments command lock file. process_payments_lock_file = appsettings.StringSetting( default='/tmp/pain_process_payments.lock') # Whether variable symbol should be trimmed of leading zeros. trim_varsym = appsettings.BooleanSetting(default=False) # List of dotted paths to callables that takes BankPayment object as their argument and return (possibly) changed # BankPayment. # # These callables are called right before the payment is saved during the import. Especially, these callable can # raise ValidationError in order to avoid saving payment to the database. import_callbacks = CallableListSetting(item_type=str) downloaders = NamedDictSetting( dict(DOWNLOADER=appsettings.ObjectSetting( required=True, validators=[ClassPathValidator(BankStatementDownloader)]), PARSER=appsettings.ObjectSetting( required=True, validators=[ClassPathValidator(BankStatementParser)]), DOWNLOADER_PARAMS=appsettings.DictSetting(required=True, key_type=str))) # CSOB card settings csob_card = appsettings.NestedDictSetting(dict( api_url=appsettings.StringSetting( default='https://api.platebnibrana.csob.cz/api/v1.7/'), api_public_key=appsettings.FileSetting(required=True), merchant_id=appsettings.StringSetting(required=True), merchant_private_key=appsettings.FileSetting(required=True), account_name=appsettings.StringSetting(required=True), ), default=None) class Meta: """Meta class.""" setting_prefix = 'pain_'
def test_setting_required(self): setting = appsettings.Setting(name="setting", prefix="custom_", required=True, default=True) with pytest.raises(ImproperlyConfigured, match=self.message_required % setting.full_name): assert setting.value assert setting.default_value setting.parent_setting = appsettings.NestedDictSetting(settings={}, name="parent_setting") with override_settings(PARENT_SETTING={}): with pytest.raises( ImproperlyConfigured, match=self.message_missing_item % setting.parent_setting.full_name ): assert setting.value
def test_nested_dict_nested_dict(self): setting = appsettings.NestedDictSetting( settings=dict( sub=appsettings.NestedDictSetting(settings=dict(value=appsettings.Setting(name="value")), name="sub") ), name="setting", ) setting.check() assert setting.value == {} with override_settings(SETTING={}): setting.check() assert setting.value == {"sub": {}} with override_settings(SETTING={"SUB": {}}): setting.check() assert setting.value == {"sub": {"value": None}} with override_settings(SETTING={"SUB": {"VALUE": "Value"}}): setting.check() assert setting.value == {"sub": {"value": "Value"}}
class Settings(appsettings.AppSettings): DJANGO_LIVE_DASHBOARD = appsettings.NestedDictSetting( settings=dict( ENABLED=appsettings.BooleanSetting(default=False), WEBSOCKET_HOST=appsettings.StringSetting(default="localhost:8000"), TOTAL_TIME_CUTOFF=appsettings.FloatSetting(default=0.5), REDIS=appsettings.NestedDictSetting( settings=dict( HOST=appsettings.StringSetting(default=REDIS_DEFAULTS["HOST"]), PORT=appsettings.IntegerSetting(default=REDIS_DEFAULTS["PORT"]), DB=appsettings.IntegerSetting(default=REDIS_DEFAULTS["DB"]), PUBSUB_CHANNEL=appsettings.StringSetting( default=REDIS_DEFAULTS["PUBSUB_CHANNEL"] ), ), default=REDIS_DEFAULTS, ), CHART=appsettings.NestedDictSetting( settings=dict( REFRESH=appsettings.IntegerSetting( default=CHART_DEFAULTS["REFRESH"] ), DELAY=appsettings.IntegerSetting(default=CHART_DEFAULTS["DELAY"]), DURATION=appsettings.IntegerSetting( default=CHART_DEFAULTS["DURATION"] ), ), default=CHART_DEFAULTS, ), ), default={ "ENABLED": False, "WEBSOCKET_HOST": "localhost:8000", "TOTAL_TIME_CUTOFF": 0.5, "REDIS": REDIS_DEFAULTS, "CHART": CHART_DEFAULTS, }, )
def test_nested_dict_setting_required_both_inner_and_outer_setting(self): outer_setting = appsettings.NestedDictSetting( name="outer_setting", required=True, settings=dict(inner_setting=appsettings.StringSetting(required=True)) ) # Not passed anything with pytest.raises(ImproperlyConfigured): outer_setting.check() # Pass outer setting with override_settings(OUTER_SETTING={"INNER_FAKE_SETTING": "Fake setting value"}): with pytest.raises(ImproperlyConfigured): outer_setting.check() # Pass inner setting as well with override_settings(OUTER_SETTING={"INNER_SETTING": "Value"}): outer_setting.check() assert len(outer_setting.value.items()) == 1 assert outer_setting.value.get("inner_setting") == "Value"
def test_nested_list_in_nested_dict_setting(self): setting = appsettings.NestedDictSetting( name="setting", default={}, settings=dict(select=appsettings.NestedListSetting( name="pick", default=[1], inner_setting=appsettings.IntegerSetting())), ) setting.check() assert setting.value == {} with override_settings(SETTING={}): setting.check() assert setting.value == {"select": [1]} with override_settings(SETTING={"PICK": [2]}): setting.check() assert setting.value == {"select": (2, )} with override_settings(SETTING={"PICK": ["xyz"]}): with pytest.raises(ValueError): setting.check()
def test_nested_dict_nested_list(self): setting = appsettings.NestedDictSetting( settings=dict( sub=appsettings.NestedListSetting( inner_setting=appsettings.Setting(name="value", default=None), name="sub", default=None ) ), name="setting", default=None, ) setting.check() assert setting.value is None with override_settings(SETTING={}): setting.check() assert setting.value == {"sub": None} with override_settings(SETTING={"SUB": ["hello"]}): setting.check() assert setting.value == {"sub": ("hello",)}
def test_nested_dict_setting_not_required_anything(self): outer_setting = appsettings.NestedDictSetting( name="outer_setting", settings=dict(inner_setting=appsettings.StringSetting(default="Default")) ) # Not passed anything outer_setting.check() assert len(outer_setting.value.items()) == 0 assert outer_setting.value.get("inner_setting") is None # Pass outer setting with override_settings(OUTER_SETTING={"INNER_FAKE_SETTING": "Fake setting value"}): outer_setting.check() assert len(outer_setting.value.items()) == 1 assert outer_setting.value.get("inner_setting") == "Default" # Pass inner setting as well with override_settings(OUTER_SETTING={"INNER_SETTING": "Value"}): outer_setting.check() assert len(outer_setting.value.items()) == 1 assert outer_setting.value.get("inner_setting") == "Value"
def test_setting_checker(self): class Setting(appsettings.Setting): def checker(self, name, value): if isinstance(value, int): raise ValueError(name) setting = Setting(name="check_test") with override_settings(CHECK_TEST=0): with pytest.raises(ValueError): setting.check() with override_settings(CHECK_TEST="ok"): setting.check() def checker(name, value): if isinstance(value, str): raise ValueError(name) setting = Setting(name="check_test2", checker=checker) with override_settings(CHECK_TEST2="not ok"): with pytest.raises(ValueError): setting.check() with override_settings(CHECK_TEST2=1): setting.check() class SettingNoChecker(appsettings.Setting): pass setting = SettingNoChecker(name="check_test3") with override_settings(CHECK_TEST3=list(range(1, 10))): setting.check() setting = appsettings.NestedDictSetting( name="check_test3", settings=dict(inner=appsettings.Setting(checker=checker))) setting.check() with override_settings(CHECK_TEST3={"INNER": True}): setting.check() with override_settings(CHECK_TEST3={"INNER": "AAA"}): with pytest.raises(ValueError): setting.check()
def test_nested_dict_setting_from_environ_value(self): setting = appsettings.NestedDictSetting( settings=dict(a=appsettings.Setting(), b=appsettings.Setting(),), name="setting" ) setting.check() assert setting.value == {"a": "A", "b": "B"}