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_object_setting(self): setting = appsettings.ObjectSetting(name="object") setting.check() assert setting.value is None with override_settings( OBJECT="tests.test_appsettings.imported_object"): setting.check() assert setting.value is imported_object setting.default = imported_object setting.call_default = True assert setting.value == "tests.test_appsettings.SettingTestCase._imported_object2" setting.transform_default = True assert setting.value is self._imported_object2 with override_settings(OBJECT="this_package.does_not_exist"): with pytest.raises(ImportError): assert setting.value with override_settings( OBJECT= "tests.test_appsettings.SettingTestCase.this_function_does_not_exist" ): with pytest.raises(AttributeError): assert setting.value with override_settings(OBJECT=""): assert setting.value is None with override_settings(OBJECT=None): assert setting.value is None
def test_nested_list_setting(self): setting = appsettings.NestedListSetting( name="setting", default=[], inner_setting=appsettings.IntegerSetting()) setting.check() assert setting.value == [] with override_settings(SETTING=[0, 1, 2]): setting.check() assert setting.value == (0, 1, 2) with override_settings(SETTING=[0, "1", 2]): with pytest.raises(ValueError): setting.check() setting = appsettings.NestedListSetting( name="setting", default=[], inner_setting=appsettings.ObjectSetting()) setting.check() assert setting.value == [] with override_settings(SETTING=[ "tests.test_appsettings.imported_object", "tests.test_appsettings.SettingTestCase._imported_object2", ]): setting.check() assert setting.value == (imported_object, self._imported_object2)
def test_nested_list_setting(self): setting = appsettings.NestedListSetting(name="setting", default=[], inner_setting=appsettings.IntegerSetting()) setting.check() assert setting.value == [] with override_settings(SETTING=[0, 1, 2]): setting.check() assert setting.value == (0, 1, 2) with override_settings(SETTING=[0, "1", 2]): with pytest.raises(ImproperlyConfigured): setting.check() setting = appsettings.NestedListSetting( name="setting", default=["tests.test_appsettings.imported_object"], transform_default=True, inner_setting=appsettings.ObjectSetting(), ) setting.check() assert setting.value == (imported_object,) with override_settings( SETTING=[ "tests.test_appsettings.imported_object", "tests.test_appsettings.ImportedClass._imported_object2", ] ): setting.check() assert setting.value == (imported_object, ImportedClass._imported_object2)
def test_nested_nested_list_setting(self): setting = appsettings.NestedListSetting( name="setting", default=[], inner_setting=appsettings.NestedListSetting( name="inner", default=[], inner_setting=appsettings.IntegerSetting() ), ) setting.check() assert setting.value == [] assert setting.inner_setting.name == "inner" with override_settings(SETTING=([1, 2, 3], [4, 5])): setting.check() assert setting.value == ((1, 2, 3), (4, 5)) with override_settings(SETTING=[[1, 2, 3], ["x", 5]]): with pytest.raises(ImproperlyConfigured): setting.check() setting = appsettings.NestedListSetting( name="setting", inner_setting=appsettings.NestedListSetting( inner_setting=appsettings.NestedListSetting(inner_setting=appsettings.ObjectSetting()) ), ) assert setting.inner_setting.name == "setting" with override_settings( SETTING=[ ( ["tests.test_appsettings.imported_object"], ["tests.test_appsettings.ImportedClass._imported_object2"], ) ] ): setting.check() assert setting.value == (((imported_object,), (ImportedClass._imported_object2,)),) with override_settings( SETTING=[[["tests.test_appsettings.imported_object"], ["tests.test_appsettings.object_does_not_exist"]]] ): with pytest.raises(AttributeError): assert setting.value
def test_object_setting_from_environ_value(self): setting = appsettings.ObjectSetting(name="setting") setting.check() assert setting.value is imported_object