def test_c(self):
        tt = construct({'a': C & int & float})
        self.assertEqual(tt({'a': 5}), {'a': 5.0})

        tt = construct(C | int | str)
        self.assertEqual(tt(5), 5)
        self.assertEqual(tt('a'), 'a')
Example #2
0
    def test_c(self):
        tt = construct({'a': C & int & float})
        self.assertEqual(tt({'a': 5}), {'a': 5.0})

        tt = construct(C | int | str)
        self.assertEqual(tt(5), 5)
        self.assertEqual(tt('a'), 'a')
Example #3
0
 def test_list(self):
     self.assertIsInstance(construct([int]), t.List)
     self.assertEqual(
         construct([int]).check([5]),
         [5]
     )
     self.assertEqual(
         construct([int]).check([5, 6]),
         [5, 6]
     )
    def test_call(self):
        a_three = lambda val: val if val == 3 else t.DataError('not a 3')
        tt = construct([a_three])
        self.assertEqual(tt([3, 3, 3]), [3, 3, 3])

        with self.assertRaises(t.DataError):
            tt([5])
Example #5
0
    def test_call(self):
        a_three = lambda val: val if val == 3 else t.DataError('not a 3')
        tt = construct([a_three])
        self.assertEqual(tt([3, 3, 3]), [3, 3, 3])

        with self.assertRaises(t.DataError):
            tt([5])
Example #6
0
    def test_call(self):
        a_three = lambda val: val if val == 3 else t.DataError('not a 3',
                                                               code='not_a_3')
        tt = construct([a_three])
        assert tt([3, 3, 3]) == [3, 3, 3]

        with pytest.raises(t.DataError):
            tt([5])
Example #7
0
 def test_dict(self):
     tt = construct({
         'a': int,
         'b': [str],
         'c': (str, str),
     })
     self.assertEqual(
         tt({'a': 5, 'b': ['a'], 'c': ['v', 'w']}),
         {'a': 5, 'b': ['a'], 'c': ('v', 'w')}
     )
 def test_dict(self):
     tt = construct({
         'a': int,
         'b': [str],
         'c': (str, str),
     })
     self.assertEqual(tt({
         'a': 5,
         'b': ['a'],
         'c': ['v', 'w']
     }), {
         'a': 5,
         'b': ['a'],
         'c': ('v', 'w')
     })
Example #9
0
def validate_json(validator):
    validate = construct(validator)

    def decorator(f):
        @wraps(f)
        async def wrapper(request, *args, **kwargs):
            try:
                request.json = validate(request.json)
            except DataError as e:
                return {'errors': e.as_dict()}, 400
            return await f(request, *args, **kwargs)

        return wrapper

    return decorator
Example #10
0
def validate_json(validator):
    validate = construct(validator)

    def decorator(f):
        if asyncio.iscoroutinefunction(f):

            async def wrapper(request, *args, **kwargs):
                return _get_json_error(validate, request) or await f(
                    request, *args, **kwargs)

        else:

            def wrapper(request, *args, **kwargs):
                return _get_json_error(validate, request) or f(
                    request, *args, **kwargs)

        return wraps(f)(wrapper)

    return decorator
Example #11
0
 def test_dict(self):
     tt = construct({
         'a': int,
         'b': [str],  # test List
         'c': (str, str, 'atom string'),  # test Tuple
         'f': float,  # test float
         't': Decimal,  # test Type
         t.Key('k'): int,  # test Key
     })
     assert tt({
         'a': 5,
         'b': [u'a'],
         'c': [u'v', u'w', 'atom string'],
         'f': 0.1,
         't': Decimal('100'),
         'k': 100,
     }) == {
         'a': 5,
         'b': ['a'],
         'c': (u'v', u'w', 'atom string'),
         'f': 0.1,
         't': Decimal('100'),
         'k': 100,
     }
Example #12
0
 def test_bad_key(self):
     # 123 can not be constructed to Key
     with pytest.raises(ValueError):
         construct({123: t.String})
Example #13
0
 def test_c(self):
     tt = construct({'a': C & int & float})
     assert tt({'a': 5}) == {'a': 5.0}
     tt = construct(C | int | str)
     assert tt(5) == 5
     assert tt('a') == 'a'
Example #14
0
 def test_optional_key(self):
     tt = construct({'a': int, 'b?': bool})
     assert tt({'a': '5'}) == {'a': 5}
     assert tt({'a': '5', 'b': True}) == {'a': 5, 'b': True}
Example #15
0
 def test_int(self):
     self.assertEqual(
         construct(int).check(5),
         5
     )
     self.assertIsInstance(construct(int), t.Int)
Example #16
0
 def test_tuple(self):
     tt = construct((int, ))
     assert isinstance(tt, t.Tuple)
     assert tt([5]) == (5, )
Example #17
0
 def test_str(self):
     assert construct(str).check(u'blabla') == u'blabla'
Example #18
0
import trafaret as t
from trafaret.constructor import construct


Item = construct(
    {'resource_key': str, 'resource_description': str,
                  ''
TestData = construct({})
CheckData = construct({})


EventData = construct({'object': (TestData | CheckData),
                   'event_name': str|})
EventData.allow_extra('cavity')

EventsData = construct([Event])
 def test_tuple(self):
     tt = construct((int, ))
     self.assertIsInstance(tt, t.Tuple)
     self.assertEqual(tt([5]), (5, ))
 def test_list(self):
     self.assertIsInstance(construct([int]), t.List)
     self.assertEqual(construct([int]).check([5]), [5])
     self.assertEqual(construct([int]).check([5, 6]), [5, 6])
 def test_int(self):
     self.assertEqual(construct(int).check(5), 5)
     self.assertIsInstance(construct(int), t.Int)
 def test_optional_key(self):
     tt = construct({'a': int, 'b?': bool})
     self.assertEqual(tt({'a': '5'}), {'a': 5})
     self.assertEqual(tt({'a': '5', 'b': True}), {'a': 5, 'b': True})
Example #23
0
 def test_tuple(self):
     tt = construct((int,))
     self.assertIsInstance(tt, t.Tuple)
     self.assertEqual(tt([5]), (5,))
 def test_str(self):
     self.assertEqual(construct(str).check('blabla'), 'blabla')
Example #25
0
 def test_unknown(self):
     assert construct(123) == 123
Example #26
0
 def test_int(self):
     assert construct(int).check(5) == 5
     assert isinstance(construct(int), t.Int)
Example #27
0
 def test_list(self):
     assert isinstance(construct([int]), t.List)
     assert construct([int]).check([5]) == [5]
     assert construct([int]).check([5, 6]) == [5, 6]
Example #28
0
 def test_optional_key(self):
     tt = construct({'a': int, 'b?': bool})
     self.assertEqual(tt({'a': '5'}), {'a': 5})
     self.assertEqual(tt({'a': '5', 'b': True}), {'a': 5, 'b': True})
Example #29
0
 def test_str(self):
     self.assertEqual(
         construct(str).check('blabla'),
         'blabla'
     )