Пример #1
0
def test_sorting():
    """ Expect alphabetic sorting """
    foo = Required('foo')
    bar = Required('bar')
    items = [foo, bar]
    expected = [bar, foo]
    result = sorted(items)
    assert result == expected
Пример #2
0
def test_schema_infer_dict():
    schema = Schema.infer({'a': {'b': {'c': 'foo'}}})

    assert_equal(
        schema, Schema({Required('a'): {
                            Required('b'): {
                                Required('c'): str
                            }
                        }}))
Пример #3
0
def test_schema_infer():
    schema = Schema.infer({
        'str': 'foo',
        'bool': True,
        'int': 42,
        'float': 3.14
    })
    assert_equal(
        schema,
        Schema({
            Required('str'): str,
            Required('bool'): bool,
            Required('int'): int,
            Required('float'): float
        }))
Пример #4
0
def test_marker_hashable():
    """Verify that you can get schema keys, even if markers were used"""
    definition = {
        Required('x'): int,
        Optional('y'): float,
        Remove('j'): int,
        Remove(int): str,
        int: int
    }
    assert_equal(definition.get('x'), int)
    assert_equal(definition.get('y'), float)
    assert_true(Required('x') == Required('x'))
    assert_true(Required('x') != Required('y'))
    # Remove markers are not hashable
    assert_equal(definition.get('j'), None)
Пример #5
0
def test_extra_with_required():
    """Verify that Required does not break Extra."""
    schema = Schema({Required('toaster'): str, Extra: object})
    r = schema({'toaster': 'blue', 'another_valid_key': 'another_valid_value'})
    assert_equal(r, {
        'toaster': 'blue',
        'another_valid_key': 'another_valid_value'
    })
Пример #6
0
def test_schema_extend_key_swap():
    """Verify that Schema.extend can replace keys, even when different markers are used"""

    base = Schema({Optional('a'): int})
    extension = {Required('a'): int}
    extended = base.extend(extension)

    assert_equal(len(base.schema), 1)
    assert_true(isinstance(list(base.schema)[0], Optional))
    assert_equal(len(extended.schema), 1)
    assert_true((list(extended.schema)[0], Required))
Пример #7
0
def test_required():
    """Verify that Required works."""
    schema = Schema({Required('q'): 1})
    # Can't use nose's raises (because we need to access the raised
    # exception, nor assert_raises which fails with Python 2.6.9.
    try:
        schema({})
    except Invalid as e:
        assert_equal(str(e), "required key not provided @ data['q']")
    else:
        assert False, "Did not raise Invalid"
Пример #8
0
def test_description():
    marker = Marker(Schema(str), description='Hello')
    assert marker.description == 'Hello'

    optional = Optional('key', description='Hello')
    assert optional.description == 'Hello'

    exclusive = Exclusive('alpha', 'angles', description='Hello')
    assert exclusive.description == 'Hello'

    required = Required('key', description='Hello')
    assert required.description == 'Hello'
Пример #9
0
def test_copy_dict_undefined():
    """ test with a copied dictionary """
    fields = {Required("foo"): int}
    copied_fields = copy.deepcopy(fields)

    schema = Schema(copied_fields)

    # This used to raise a `TypeError` because the instance of `Undefined`
    # was a copy, so object comparison would not work correctly.
    try:
        schema({"foo": "bar"})
    except Exception as e:
        assert isinstance(e, MultipleInvalid)
Пример #10
0
def test_schema_infer_list():
    schema = Schema.infer({'list': ['foo', True, 42, 3.14]})

    assert_equal(schema, Schema({Required('list'): [str, bool, int, float]}))