예제 #1
0
def test_coerce_in_keyschema():
    # https://github.com/nicolaiarocci/cerberus/issues/155
    schema = {'thing': {'type': 'dict',
                        'keyschema': {'coerce': int, 'type': 'integer'}}}
    document = {'thing': {'5': 'foo'}}
    expected = {'thing': {5: 'foo'}}
    assert_normalized(document, expected, schema)
예제 #2
0
def test_default_missing(default):
    bar_schema = {'type': 'string'}
    bar_schema.update(default)
    schema = {'foo': {'type': 'string'}, 'bar': bar_schema}
    document = {'foo': 'foo_value'}
    expected = {'foo': 'foo_value', 'bar': 'bar_value'}
    assert_normalized(document, expected, schema)
예제 #3
0
def test_rename_handler_in_allow_unknown():
    assert_normalized(
        schema={},
        document={'0': 'foo'},
        expected={0: 'foo'},
        validator=Validator(allow_unknown={'rename_handler': int}),
    )
예제 #4
0
def test_default_setters_with_document_reference():
    assert_normalized(
        schema={
            'a': {
                'type': 'integer'
            },
            'b': {
                'type': 'integer',
                'default_setter': lambda d: d['a'] + 1
            },
            'c': {
                'type': 'integer',
                'default_setter': lambda d: d['b'] * 2
            },
            'd': {
                'type': 'integer',
                'default_setter': lambda d: d['b'] + d['c']
            },
        },
        document={'a': 1},
        expected={
            'a': 1,
            'b': 2,
            'c': 4,
            'd': 6
        },
    )
예제 #5
0
def _test_default_existent(default):
    bar_schema = {'type': 'string'}
    bar_schema.update(default)
    schema = {'foo': {'type': 'string'},
              'bar': bar_schema}
    document = {'foo': 'foo_value', 'bar': 'non_default'}
    assert_normalized(document, document.copy(), schema)
예제 #6
0
def test_purge_unknown_in_subschema():
    schema = {'foo': {'type': 'dict',
                      'schema': {'foo': {'type': 'string'}},
                      'purge_unknown': True}}
    document = {'foo': {'bar': ''}}
    expected = {'foo': {}}
    assert_normalized(document, expected, schema)
예제 #7
0
def test_default_none_nonnullable(default):
    bar_schema = {'type': 'string', 'nullable': False}
    bar_schema.update(default)
    schema = {'foo': {'type': 'string'}, 'bar': bar_schema}
    document = {'foo': 'foo_value', 'bar': None}
    expected = {'foo': 'foo_value', 'bar': 'bar_value'}
    assert_normalized(document, expected, schema)
예제 #8
0
def _test_default_existent(default):
    bar_schema = {'type': 'string'}
    bar_schema.update(default)
    schema = {'foo': {'type': 'string'},
              'bar': bar_schema}
    document = {'foo': 'foo_value', 'bar': 'non_default'}
    assert_normalized(document, document.copy(), schema)
예제 #9
0
def test_defaults_in_allow_unknown_schema():
    schema = {'meta': {'type': 'dict'}, 'version': {'type': 'string'}}
    allow_unknown = {
        'type': 'dict',
        'schema': {
            'cfg_path': {
                'type': 'string',
                'default': 'cfg.yaml'
            },
            'package': {
                'type': 'string'
            },
        },
    }
    validator = Validator(schema=schema, allow_unknown=allow_unknown)

    document = {'version': '1.2.3', 'plugin_foo': {'package': 'foo'}}
    expected = {
        'version': '1.2.3',
        'plugin_foo': {
            'package': 'foo',
            'cfg_path': 'cfg.yaml'
        },
    }
    assert_normalized(document, expected, schema, validator)
예제 #10
0
def test_coerce_in_keyschema():
    # https://github.com/pyeve/cerberus/issues/155
    schema = {'thing': {'type': 'dict',
                        'keyschema': {'coerce': int, 'type': 'integer'}}}
    document = {'thing': {'5': 'foo'}}
    expected = {'thing': {5: 'foo'}}
    assert_normalized(document, expected, schema)
예제 #11
0
def test_purge_unknown_in_subschema():
    schema = {'foo': {'type': 'dict',
                      'schema': {'foo': {'type': 'string'}},
                      'purge_unknown': True}}
    document = {'foo': {'bar': ''}}
    expected = {'foo': {}}
    assert_normalized(document, expected, schema)
예제 #12
0
def _test_default_missing(default):
    bar_schema = {'type': 'string'}
    bar_schema.update(default)
    schema = {'foo': {'type': 'string'}, 'bar': bar_schema}
    document = {'foo': 'foo_value'}
    expected = {'foo': 'foo_value', 'bar': 'bar_value'}
    assert_normalized(document, expected, schema)
예제 #13
0
def test_issue_250_no_type_fail_pass_on_other():
    # https://github.com/pyeve/cerberus/issues/250
    schema = {
        'list': {'schema': {'allow_unknown': True, 'schema': {'a': {'type': 'string'}}}}
    }
    document = {'list': 1}
    assert_normalized(document, document, schema)
예제 #14
0
def test_coerce():
    assert_normalized(
        schema={'amount': {
            'coerce': int
        }},
        document={'amount': '1'},
        expected={'amount': 1},
    )
예제 #15
0
def test_normalization_with_rules_set():
    # https://github.com/pyeve/cerberus/issues/283
    rules_set_registry.add('foo', {'default': 42})
    assert_normalized({}, {'bar': 42}, {'bar': 'foo'})
    rules_set_registry.add('foo', {'default_setter': lambda _: 42})
    assert_normalized({}, {'bar': 42}, {'bar': 'foo'})
    rules_set_registry.add('foo', {'type': 'integer', 'nullable': True})
    assert_success({'bar': None}, {'bar': 'foo'})
예제 #16
0
def test_default_none_default_value():
    schema = {
        'foo': {'type': 'string'},
        'bar': {'type': 'string', 'nullable': True, 'default': None},
    }
    document = {'foo': 'foo_value'}
    expected = {'foo': 'foo_value', 'bar': None}
    assert_normalized(document, expected, schema)
예제 #17
0
def _test_default_none_nullable(default):
    bar_schema = {'type': 'string',
                  'nullable': True}
    bar_schema.update(default)
    schema = {'foo': {'type': 'string'},
              'bar': bar_schema}
    document = {'foo': 'foo_value', 'bar': None}
    assert_normalized(document, document.copy(), schema)
예제 #18
0
def _test_default_none_nullable(default):
    bar_schema = {'type': 'string',
                  'nullable': True}
    bar_schema.update(default)
    schema = {'foo': {'type': 'string'},
              'bar': bar_schema}
    document = {'foo': 'foo_value', 'bar': None}
    assert_normalized(document, document.copy(), schema)
예제 #19
0
def test_coerce_in_valuesrules():
    # https://github.com/pyeve/cerberus/issues/155
    schema = {
        'thing': {'type': 'dict', 'valuesrules': {'coerce': int, 'type': 'integer'}}
    }
    document = {'thing': {'amount': '2'}}
    expected = {'thing': {'amount': 2}}
    assert_normalized(document, expected, schema)
예제 #20
0
def test_coercion_of_sequence_items(validator):
    # https://github.com/pyeve/cerberus/issues/161
    schema = {'a_list': {'type': 'list', 'schema': {'type': 'float', 'coerce': float}}}
    document = {'a_list': [3, 4, 5]}
    expected = {'a_list': [3.0, 4.0, 5.0]}
    assert_normalized(document, expected, schema, validator)
    for x in validator.document['a_list']:
        assert isinstance(x, float)
예제 #21
0
def test_normalization_with_rules_set():
    # https://github.com/pyeve/cerberus/issues/283
    rules_set_registry.add('foo', {'default': 42})
    assert_normalized({}, {'bar': 42}, {'bar': 'foo'})
    rules_set_registry.add('foo', {'default_setter': lambda _: 42})
    assert_normalized({}, {'bar': 42}, {'bar': 'foo'})
    rules_set_registry.add('foo', {'type': 'integer', 'nullable': True})
    assert_success({'bar': None}, {'bar': 'foo'})
예제 #22
0
def _test_default_missing_in_subschema(default):
    bar_schema = {'type': 'string'}
    bar_schema.update(default)
    schema = {'thing': {'type': 'dict',
                        'schema': {'foo': {'type': 'string'},
                                   'bar': bar_schema}}}
    document = {'thing': {'foo': 'foo_value'}}
    expected = {'thing': {'foo': 'foo_value',
                          'bar': 'bar_value'}}
    assert_normalized(document, expected, schema)
예제 #23
0
def test_depending_default_setters():
    schema = {
        'a': {'type': 'integer'},
        'b': {'type': 'integer', 'default_setter': lambda d: d['a'] + 1},
        'c': {'type': 'integer', 'default_setter': lambda d: d['b'] * 2},
        'd': {'type': 'integer', 'default_setter': lambda d: d['b'] + d['c']},
    }
    document = {'a': 1}
    expected = {'a': 1, 'b': 2, 'c': 4, 'd': 6}
    assert_normalized(document, expected, schema)
예제 #24
0
def test_coerce_in_listitems():
    schema = {'things': {'type': 'list', 'items': [{'coerce': int}, {'coerce': str}]}}
    document = {'things': ['1', 2]}
    expected = {'things': [1, '2']}
    assert_normalized(document, expected, schema)

    validator = Validator(schema)
    document['things'].append(3)
    assert not validator(document)
    assert validator.document['things'] == document['things']
예제 #25
0
def _test_default_missing_in_subschema(default):
    bar_schema = {'type': 'string'}
    bar_schema.update(default)
    schema = {'thing': {'type': 'dict',
                        'schema': {'foo': {'type': 'string'},
                                   'bar': bar_schema}}}
    document = {'thing': {'foo': 'foo_value'}}
    expected = {'thing': {'foo': 'foo_value',
                          'bar': 'bar_value'}}
    assert_normalized(document, expected, schema)
예제 #26
0
def test_purge_readonly():
    schema = {
        'description': {'type': 'string', 'maxlength': 500},
        'last_updated': {'readonly': True},
    }
    validator = Validator(schema=schema, purge_readonly=True)
    document = {'description': 'it is a thing'}
    expected = deepcopy(document)
    document['last_updated'] = 'future'
    assert_normalized(document, expected, validator=validator)
예제 #27
0
def test_nullables_fail_coerce_on_non_null_values(validator):
    def failing_coercion(value):
        raise Exception("expected to fail")

    schema = {'foo': {'coerce': failing_coercion, 'nullable': True, 'type': 'integer'}}
    document = {'foo': None}
    assert_normalized(document, document, schema)

    validator({'foo': 2}, schema)
    assert errors.COERCION_FAILED in validator._errors
예제 #28
0
def test_coerce_chain():
    drop_prefix = lambda x: x[2:]  # noqa: E731
    upper = lambda x: x.upper()  # noqa: E731
    assert_normalized(
        schema={'foo': {
            'coerce': [hex, drop_prefix, upper]
        }},
        document={'foo': 15},
        expected={'foo': 'F'},
    )
예제 #29
0
def test_rename():
    assert_normalized(
        schema={
            'foo': {
                'rename': 'bar'
            },
            'bar': {}
        },
        document={'foo': 0},
        expected={'bar': 0},
    )
예제 #30
0
def test_coerce_in_itemsrules_with_integer_values():
    assert_normalized(
        schema={'things': {
            'type': 'list',
            'itemsrules': {
                'coerce': int
            }
        }},
        document={'things': ['1', '2', '3']},
        expected={'things': [1, 2, 3]},
    )
예제 #31
0
def test_allow_unknown_with_purge_unknown_subdocument():
    validator = Validator(purge_unknown=True)
    schema = {
        'foo': {
            'type': 'dict',
            'schema': {'bar': {'type': 'string'}},
            'allow_unknown': True,
        }
    }
    document = {'foo': {'bar': 'baz', 'corge': False}, 'thud': 'xyzzy'}
    expected = {'foo': {'bar': 'baz', 'corge': False}}
    assert_normalized(document, expected, schema, validator)
예제 #32
0
def test_issue_250_no_type_pass_on_list():
    # https://github.com/pyeve/cerberus/issues/250
    schema = {
        'list': {
            'schema': {
                'allow_unknown': True,
                'type': 'dict',
                'schema': {'a': {'type': 'string'}},
            }
        }
    }
    document = {'list': [{'a': 'known', 'b': 'unknown'}]}
    assert_normalized(document, document, schema)
예제 #33
0
def test_coerce_in_allow_unknown():
    assert_normalized(
        schema={'foo': {
            'schema': {},
            'allow_unknown': {
                'coerce': int
            }
        }},
        document={'foo': {
            'bar': '0'
        }},
        expected={'foo': {
            'bar': 0
        }},
    )
예제 #34
0
def test_coercion_of_sequence_items_with_float_values(validator):
    # https://github.com/pyeve/cerberus/issues/161
    assert_normalized(
        schema={
            'a_list': {
                'type': 'list',
                'itemsrules': {
                    'type': 'float',
                    'coerce': float
                }
            }
        },
        document={'a_list': [3, 4, 5]},
        expected={'a_list': [3.0, 4.0, 5.0]},
        validator=validator,
    )
예제 #35
0
def test_defaults_in_allow_unknown_schema():
    schema = {'meta': {'type': 'dict'}, 'version': {'type': 'string'}}
    allow_unknown = {
        'type': 'dict',
        'schema': {
            'cfg_path': {'type': 'string', 'default': 'cfg.yaml'},
            'package': {'type': 'string'},
        },
    }
    validator = Validator(schema=schema, allow_unknown=allow_unknown)

    document = {'version': '1.2.3', 'plugin_foo': {'package': 'foo'}}
    expected = {
        'version': '1.2.3',
        'plugin_foo': {'package': 'foo', 'cfg_path': 'cfg.yaml'},
    }
    assert_normalized(document, expected, schema, validator)
예제 #36
0
def test_default_with_missing_value(default):
    assert_normalized(
        schema={
            'foo': {
                'type': 'string'
            },
            'bar': {
                'type': 'string',
                **default
            }
        },
        document={'foo': 'foo_value'},
        expected={
            'foo': 'foo_value',
            'bar': 'bar_value'
        },
    )
예제 #37
0
def test_default_with_none_as_value_on_nullable_field():
    assert_normalized(
        schema={
            'foo': {
                'type': 'string'
            },
            'bar': {
                'type': 'string',
                'nullable': True,
                'default': None
            },
        },
        document={'foo': 'foo_value'},
        expected={
            'foo': 'foo_value',
            'bar': None
        },
    )
예제 #38
0
def test_coerce_in_schema():
    assert_normalized(
        schema={
            'thing': {
                'type': 'dict',
                'schema': {
                    'amount': {
                        'coerce': int
                    }
                }
            }
        },
        document={'thing': {
            'amount': '2'
        }},
        expected={'thing': {
            'amount': 2
        }},
    )
예제 #39
0
def test_coerce_unknown():
    schema = {'foo': {'schema': {}, 'allow_unknown': {'coerce': int}}}
    document = {'foo': {'bar': '0'}}
    expected = {'foo': {'bar': 0}}
    assert_normalized(document, expected, schema)
예제 #40
0
def test_coerce_chain():
    drop_prefix = lambda x: x[2:]  # noqa: E731
    upper = lambda x: x.upper()  # noqa: E731
    schema = {'foo': {'coerce': [hex, drop_prefix, upper]}}
    assert_normalized({'foo': 15}, {'foo': 'F'}, schema)
예제 #41
0
def test_nullables_dont_fail_coerce():
    schema = {'foo': {'coerce': int, 'nullable': True, 'type': 'integer'}}
    document = {'foo': None}
    assert_normalized(document, document, schema)
예제 #42
0
def test_coerce():
    schema = {'amount': {'coerce': int}}
    document = {'amount': '1'}
    expected = {'amount': 1}
    assert_normalized(document, expected, schema)
예제 #43
0
def test_coerce_in_dictschema():
    schema = {'thing': {'type': 'dict', 'schema': {'amount': {'coerce': int}}}}
    document = {'thing': {'amount': '2'}}
    expected = {'thing': {'amount': 2}}
    assert_normalized(document, expected, schema)
예제 #44
0
def test_rename_handler():
    validator = Validator(allow_unknown={'rename_handler': int})
    schema = {}
    document = {'0': 'foo'}
    expected = {0: 'foo'}
    assert_normalized(document, expected, schema, validator)
예제 #45
0
def test_allow_unknown_with_purge_unknown():
    validator = Validator(purge_unknown=True)
    schema = {'foo': {'type': 'dict', 'allow_unknown': True}}
    document = {'foo': {'bar': True}, 'bar': 'foo'}
    expected = {'foo': {'bar': True}}
    assert_normalized(document, expected, schema, validator)
예제 #46
0
def test_purge_unknown():
    validator = Validator(purge_unknown=True)
    schema = {'foo': {'type': 'string'}}
    document = {'bar': 'foo'}
    expected = {}
    assert_normalized(document, expected, schema, validator)
예제 #47
0
def test_coerce_in_dictschema_in_listschema():
    item_schema = {'type': 'dict', 'schema': {'amount': {'coerce': int}}}
    schema = {'things': {'type': 'list', 'schema': item_schema}}
    document = {'things': [{'amount': '2'}]}
    expected = {'things': [{'amount': 2}]}
    assert_normalized(document, expected, schema)
예제 #48
0
def test_coerce_in_listschema():
    schema = {'things': {'type': 'list', 'schema': {'coerce': int}}}
    document = {'things': ['1', '2', '3']}
    expected = {'things': [1, 2, 3]}
    assert_normalized(document, expected, schema)