Example #1
0
    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'),
            ])
        )
Example #2
0
 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')]
     )
Example #3
0
 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'),
         ]
     )
Example #4
0
 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, '') == []
Example #5
0
 def test_required(self):
     assert (
         BoolT(required=True).validate(None, '') ==
         [Result(0, 0, '', 'value required')]
     )
     assert BoolT(required=True).validate(True, '') == []
     assert BoolT(required=True).validate(False, '') == []
Example #6
0
 def test_required(self):
     assert (
         TextT(required=True).validate(None, '') ==
         [Result(0, 0, '', 'value required')]
     )
     assert TextT(required=True).validate('', '') == []
     assert TextT(required=True).validate('foo', '') == []
Example #7
0
 def test_required_with_none(self):
     assert (
         T(required=True).validate(None, '') ==
         [Result(0, 0, '', 'value required')]
     )
Example #8
0
 def test_non_text(self):
     assert (
         TextT().validate(0, '') ==
         [Result(0, 0, '', 'value is not a valid text value: 0')]
     )
Example #9
0
 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')]
     )
Example #10
0
 def test_unknown_keys(self):
     assert (
         DictOfT({'foo': IntT()}).validate({'bar': 5}, '') ==
         [Result(0, 0, '', 'unknown key: %r' % 'bar')]
     )
Example #11
0
 def test_bad_subtype(self):
     assert (
         DictOfT({'foo': IntT()}).validate({'foo': 'bar'}, '') ==
         [Result(0, 0, ':foo', 'value is not a valid int: %r' % 'bar')]
     )
Example #12
0
 def test_bad_value(self):
     assert (
         DictOfT({'foo': IntT()}).validate(5, '') ==
         [Result(0, 0, '', 'value is not a dict: %r' % 5)]
     )
Example #13
0
 def test_non_int(self, test_input):
     assert (
         IntT().validate(test_input, '') ==
         [Result(0, 0, '', 'value is not a valid int: %r' % test_input)]
     )
Example #14
0
 def test_depth(self):
     assert (
         ListOfT(IntT()).validate([10, 'foo'], 'TOP') ==
         [Result(0, 0, 'TOP[1]', 'value is not a valid int: %r' % 'foo')]
     )
Example #15
0
 def test_non_slug(self, test_input):
     assert (
         TextT(slug=True).validate(test_input, '') ==
         [Result(0, 0, '', 'value is not a valid slug: %r' % test_input)]
     )
Example #16
0
 def test_required(self):
     assert (
         ListOfT(IntT(), required=True).validate(None, '') ==
         [Result(0, 0, '', 'value required')]
     )
     assert ListOfT(IntT(), required=True).validate([], '') == []
Example #17
0
 def test_non_date(self, test_input):
     assert (
         DateT(required=True).validate(test_input, '') ==
         [Result(0, 0, '', 'value is not date in YYYY-MM-DD format: %r' % test_input)]
     )
Example #18
0
 def test_required(self):
     assert (
         DateT(required=True).validate(None, '') ==
         [Result(0, 0, '', 'value required')]
     )
     assert DateT(required=True).validate('2016-02-14', '') == []
Example #19
0
 def test_required(self):
     assert (
         DictOfT({'foo': IntT()}, required=True).validate(None, '') ==
         [Result(0, 0, '', 'value required')]
     )
     assert DictOfT({'foo': IntT()}, required=True).validate({}, '') == []
Example #20
0
 def test_non_bool(self, test_input):
     assert (
         BoolT().validate(test_input, '') ==
         [Result(0, 0, '', 'value is not a valid bool: %r' % test_input)]
     )