def test_recursion(self): assert DictOfT({'foo': DictOfT({'bar': IntT()})}).validate({'foo': {'bar': 5}}, 'TOP') == [] assert ( DictOfT({'foo': DictOfT({'bar': IntT()})}).validate({'foo': {'bar': 'baz'}}, 'TOP') == [Result(0, 0, 'TOP:foo:bar', 'value is not a valid int: %r' % 'baz')] ) req = DictOfT({ 'foo': IntT(), 'bar': IntT(), 'baz': DictOfT({ 'key': IntT(), }) }) data = { 'foo': 5, 'bar': 'foo1', 'baz': { 'key': 'foo2' } } assert ( sort_results(req.validate(data, 'TOP')) == sort_results([ Result(0, 0, 'TOP:bar', 'value is not a valid int: %r' % 'foo1'), Result(0, 0, 'TOP:baz:key', 'value is not a valid int: %r' % 'foo2'), ]) )
def test_required(self): assert ( IntT(required=True).validate(None, '') == [Result(0, 0, '', 'value required')] ) assert IntT(required=True).validate(0, '') == [] assert IntT(required=True).validate(1, '') == []
def test_bad_subtype(self): assert ( ListOfT(IntT()).validate(['foo'], '') == [Result(0, 0, '[0]', 'value is not a valid int: %r' % 'foo')] ) assert ( ListOfT(IntT()).validate([10, 'foo'], '') == [Result(0, 0, '[1]', 'value is not a valid int: %r' % 'foo')] )
def test_recursion(self): assert ListOfT(ListOfT(IntT())).validate([[10, 2], [1, 2, 3]], 'TOP') == [] assert ( ListOfT(ListOfT(IntT())).validate([[10, 2], [1, 'foo', 3]], 'TOP') == [Result(0, 0, 'TOP[1][1]', 'value is not a valid int: %r' % 'foo')] ) assert ( ListOfT(ListOfT(IntT())).validate([[10, 2], [1, 'foo', 'bar', 3]], 'TOP') == [ Result(0, 0, 'TOP[1][1]', 'value is not a valid int: %r' % 'foo'), Result(0, 0, 'TOP[1][2]', 'value is not a valid int: %r' % 'bar'), ] )
def test_non_int(self, test_input): assert ( IntT().validate(test_input, '') == [Result(0, 0, '', 'value is not a valid int: %r' % test_input)] )
def test_int(self): assert IntT().validate(1, '') == [] assert IntT().validate(-1, '') == []
def test_depth(self): assert ( DictOfT({'foo': IntT()}).validate({'foo': 'bar'}, 'TOP') == [Result(0, 0, 'TOP:foo', 'value is not a valid int: %r' % 'bar')] )
def test_unknown_keys(self): assert ( DictOfT({'foo': IntT()}).validate({'bar': 5}, '') == [Result(0, 0, '', 'unknown key: %r' % 'bar')] )
def test_bad_subtype(self): assert ( DictOfT({'foo': IntT()}).validate({'foo': 'bar'}, '') == [Result(0, 0, ':foo', 'value is not a valid int: %r' % 'bar')] )
def test_subtype(self): assert DictOfT({'foo': IntT()}).validate({'foo': 5}, '') == [] assert DictOfT({'foo': TextT()}).validate({'foo': 'bar'}, '') == [] assert DictOfT({'foo': TextT(), 'bar': IntT()}).validate({'foo': 'val1', 'bar': 5}, '') == []
def test_bad_value(self): assert ( DictOfT({'foo': IntT()}).validate(5, '') == [Result(0, 0, '', 'value is not a dict: %r' % 5)] )
def test_required(self): assert ( DictOfT({'foo': IntT()}, required=True).validate(None, '') == [Result(0, 0, '', 'value required')] ) assert DictOfT({'foo': IntT()}, required=True).validate({}, '') == []
def test_depth(self): assert ( ListOfT(IntT()).validate([10, 'foo'], 'TOP') == [Result(0, 0, 'TOP[1]', 'value is not a valid int: %r' % 'foo')] )
def test_subtype(self): assert ListOfT(IntT()).validate([], '') == [] assert ListOfT(IntT()).validate([1], '') == [] assert ListOfT(TextT()).validate(['foo', 'bar'], '') == []
def test_required(self): assert ( ListOfT(IntT(), required=True).validate(None, '') == [Result(0, 0, '', 'value required')] ) assert ListOfT(IntT(), required=True).validate([], '') == []