Exemple #1
0
    def it_emits_field_even_if_option_doesnt_exist_in_file(
            self, config_integer_config_schema,
            integer_option_doesnt_exist_file):
        data = errors = None
        if is_marshmallow3():
            data = config_integer_config_schema.load(
                integer_option_doesnt_exist_file)
        else:
            data, errors = config_integer_config_schema.load(
                integer_option_doesnt_exist_file)

        assert not errors
        assert 'INTEGER_WITH_DEFAULT' in data
        assert 'INTEGER_WITHOUT_DEFAULT' in data
Exemple #2
0
    def it_uses_field_default_if_option_is_blank_in_file(
            self, config_string_config_schema, string_option_is_blank_file):
        data = errors = None
        if is_marshmallow3():
            data = config_string_config_schema.load(
                string_option_is_blank_file)
        else:
            data, errors = config_string_config_schema.load(
                string_option_is_blank_file)

        assert not errors
        assert (data['STRING_WITH_DEFAULT'] == config_string_config_schema.
                declared_fields['STRING_WITH_DEFAULT'].default)
        assert data['STRING_WITHOUT_DEFAULT'] is ''
Exemple #3
0
    def it_emits_field_even_if_option_doesnt_exist_in_file(
            self, config_string_config_schema,
            string_option_doesnt_exist_file):
        data = errors = None
        if is_marshmallow3():
            data = config_string_config_schema.load(
                string_option_doesnt_exist_file)
        else:
            data, errors = config_string_config_schema.load(
                string_option_doesnt_exist_file)

        assert not errors
        assert 'STRING_WITH_DEFAULT' in data
        assert 'STRING_WITHOUT_DEFAULT' in data
Exemple #4
0
    def it_validates_config_files_readability(
        self, non_readable_config_files, config_schema
    ):
        errors = None
        obj = None
        if is_marshmallow3():
            try:
                obj = config_schema.load(non_readable_config_files)
            except ValidationError as e:
                errors = e.messages
        else:
            obj, errors = config_schema.load(non_readable_config_files)

        assert not obj
        assert 'config_files' in errors
        assert 'No config files loaded!' in errors['config_files']

        if not is_marshmallow3():
            config_schema.strict = True

            with pytest.raises(ValidationError) as e:
                obj, errors = config_schema.load(non_readable_config_files)
                assert 'config_files' in e.errors
                assert 'No config files loaded!' in e.errors['config_files']
Exemple #5
0
    def it_dumps_config_object_to_ini_file(self, config_files, config_schema):
        errors = None
        obj = None

        if is_marshmallow3():
            try:
                obj = config_schema.load(config_files)
                data = config_schema.dumps(obj)
            except ValidationError as e:
                errors = e.messages
        else:
            obj, errors = config_schema.load(config_files)
            data, errors = config_schema.dumps(obj)

        assert not errors
        assert data == '[Section1]\noption1 = mandatory string\noption2 = optional string\noption3 = 42\noption4 = 24\n[Section2]\noption1 = mandatory string\noption2 = optional string\noption3 = 42\noption4 = 24'
class ConfigIntegerConfigSchema(ConfigParserSchema):
    if is_marshmallow3():
        INTEGER_WITH_DEFAULT = ConfigInteger(
            section='Section1',
            default=42,
            data_key='integer_option_with_default')
        INTEGER_WITHOUT_DEFAULT = ConfigInteger(
            section='Section1', data_key='integer_option_without_default')
    else:
        INTEGER_WITH_DEFAULT = ConfigInteger(
            section='Section1',
            default=42,
            load_from='integer_option_with_default',
            dump_to='integer_option_with_default',
        )
        INTEGER_WITHOUT_DEFAULT = ConfigInteger(
            section='Section1',
            load_from='integer_option_without_default',
            dump_to='integer_option_without_default',
        )
class ConfigStringConfigSchema(ConfigParserSchema):
    if is_marshmallow3():
        STRING_WITH_DEFAULT = ConfigString(
            section='Section1',
            default='default value',
            data_key='string_option_with_default')
        STRING_WITHOUT_DEFAULT = ConfigString(
            section='Section1', data_key='string_option_without_default')
    else:
        STRING_WITH_DEFAULT = ConfigString(
            section='Section1',
            default='default value',
            load_from='string_option_with_default',
            dump_to='string_option_with_default',
        )
        STRING_WITHOUT_DEFAULT = ConfigString(
            section='Section1',
            load_from='string_option_without_default',
            dump_to='string_option_without_default',
        )
Exemple #8
0
    def it_loads_and_validates_config_file(self, config_files, config_schema):
        errors = None
        obj = None

        if is_marshmallow3():
            try:
                obj = config_schema.load(config_files)
            except ValidationError as e:
                errors = e.messages
        else:
            obj, errors = config_schema.load(config_files)

        assert not errors
        assert obj.MANDATORY_STRING1 == 'mandatory string'
        assert obj.OPTIONAL_STRING1 == 'optional string'
        assert obj.MANDATORY_INTEGER1 == 42
        assert obj.OPTIONAL_INTEGER1 == 24
        assert obj.MANDATORY_STRING2 == 'mandatory string'
        assert obj.OPTIONAL_STRING2 == 'optional string'
        assert obj.MANDATORY_INTEGER2 == 42
        assert obj.OPTIONAL_INTEGER2 == 24
def config_integer_config_schema():
    if is_marshmallow3():
        return ConfigIntegerConfigSchema(unknown='EXCLUDE')
    else:
        return ConfigIntegerConfigSchema()
def config_string_config_schema():
    if is_marshmallow3():
        return ConfigStringConfigSchema(unknown='EXCLUDE')
    else:
        return ConfigStringConfigSchema()
class ConfigSchema(ConfigParserSchema):
    class Meta:
        model = ConfigObject

    if is_marshmallow3():
        MANDATORY_STRING1 = String(section='Section1',
                                   data_key='option1',
                                   allow_null=False,
                                   required=True,
                                   default='',
                                   missing='',
                                   validate=[IsNotBlank()])
        OPTIONAL_STRING1 = String(section='Section1',
                                  data_key='option2',
                                  allow_null=False,
                                  required=False,
                                  default='',
                                  missing='')
        MANDATORY_INTEGER1 = Integer(section='Section1',
                                     data_key='option3',
                                     allow_null=False,
                                     required=True,
                                     default=0,
                                     missing=0,
                                     validate=[Range(min=24, max=42)])
        OPTIONAL_INTEGER1 = Integer(section='Section1',
                                    data_key='option4',
                                    allow_null=False,
                                    required=False,
                                    default=0,
                                    missing=0)

        MANDATORY_STRING2 = String(section='Section2',
                                   data_key='option1',
                                   allow_null=False,
                                   required=True,
                                   default='',
                                   missing='',
                                   validate=[IsNotBlank()])
        OPTIONAL_STRING2 = String(section='Section2',
                                  data_key='option2',
                                  allow_null=False,
                                  required=False,
                                  default='',
                                  missing='')
        MANDATORY_INTEGER2 = Integer(section='Section2',
                                     data_key='option3',
                                     allow_null=False,
                                     required=True,
                                     default=0,
                                     missing=0,
                                     validate=[Range(min=24, max=42)])
        OPTIONAL_INTEGER2 = Integer(section='Section2',
                                    data_key='option4',
                                    allow_null=False,
                                    required=False,
                                    default=0,
                                    missing=0)
    else:
        MANDATORY_STRING1 = String(section='Section1',
                                   load_from='option1',
                                   dump_to='option1',
                                   allow_null=False,
                                   required=True,
                                   default='',
                                   missing='',
                                   validate=[IsNotBlank()])
        OPTIONAL_STRING1 = String(section='Section1',
                                  load_from='option2',
                                  dump_to='option2',
                                  allow_null=False,
                                  required=False,
                                  default='',
                                  missing='')
        MANDATORY_INTEGER1 = Integer(section='Section1',
                                     load_from='option3',
                                     dump_to='option3',
                                     allow_null=False,
                                     required=True,
                                     default=0,
                                     missing=0,
                                     validate=[Range(min=24, max=42)])
        OPTIONAL_INTEGER1 = Integer(section='Section1',
                                    load_from='option4',
                                    dump_to='option4',
                                    allow_null=False,
                                    required=False,
                                    default=0,
                                    missing=0)

        MANDATORY_STRING2 = String(section='Section2',
                                   load_from='option1',
                                   dump_to='option1',
                                   allow_null=False,
                                   required=True,
                                   default='',
                                   missing='',
                                   validate=[IsNotBlank()])
        OPTIONAL_STRING2 = String(section='Section2',
                                  load_from='option2',
                                  dump_to='option2',
                                  allow_null=False,
                                  required=False,
                                  default='',
                                  missing='')
        MANDATORY_INTEGER2 = Integer(section='Section2',
                                     load_from='option3',
                                     dump_to='option3',
                                     allow_null=False,
                                     required=True,
                                     default=0,
                                     missing=0,
                                     validate=[Range(min=24, max=42)])
        OPTIONAL_INTEGER2 = Integer(section='Section2',
                                    load_from='option4',
                                    dump_to='option4',
                                    allow_null=False,
                                    required=False,
                                    default=0,
                                    missing=0)