def test_forward(self): node = t.Forward() node << t.Dict(name=t.String, children=t.List[node]) self.assertEqual( repr(node), '<Forward(<Dict(children=<List(<recur>)>, name=<String>)>)>') res = node.check({ "name": "foo", "children": [] }) == { 'children': [], 'name': 'foo' } self.assertEqual(res, True) res = extract_error(node, {"name": "foo", "children": [1]}) self.assertEqual(res, {'children': {0: 'value is not a dict'}}) res = node.check({ "name": "foo", "children": [{ "name": "bar", "children": [] }] }) self.assertEqual(res, { 'children': [{ 'children': [], 'name': 'bar' }], 'name': 'foo' }) empty_node = t.Forward() self.assertEqual(repr(empty_node), '<Forward(None)>') res = extract_error(empty_node, 'something') self.assertEqual(res, 'trafaret not set yet')
def test_forward(self): node = t.Forward() res = extract_error(node, 'something') assert res == 'trafaret not set yet' node << t.Dict(name=t.String, children=t.List[node]) assert node.check({ "name": u"foo", "children": [] }) == { 'children': [], 'name': u'foo' } res = extract_error(node, {"name": u"foo", "children": [1]}) assert res == {'children': {0: 'value is not a dict'}} res = node.check({ "name": u"foo", "children": [{ "name": u"bar", "children": [] }] }) assert res == { 'children': [{ 'children': [], 'name': u'bar' }], 'name': u'foo' } with pytest.raises(RuntimeError): # __rshift__ is not overridden node << t.Int()
def test_repr(self): node = t.Forward() assert repr(node) == '<Forward(None)>' node << t.Dict(name=t.String, children=t.List[node]) assert repr( node ) == '<Forward(<Dict(<Key "children" <List(<recur>)>>, <Key "name" <String>>)>)>'
class TestForward(unittest.TestCase): FWD = T.Forward() TRAFARET = T.Dict({ T.Key("value", optional=True): FWD, }) FWD << T.Int() def test_int(self): self.assertEqual(get_err(self.TRAFARET, u""" value: "hello" """), dedent(u"""\ config.yaml:2: value: value can't be converted to int """))
def guard(params=None, **kwargs): specs = t.Forward() if params is None: specs << t.Dict(**kwargs) else: specs << params def wrapped(fn): guarder = t.guard(specs) wrapper = wraps(fn)(guarder(fn)) wrapper.__doc__ = fn.__doc__ return wrapper return wrapped
def test_forward(self): trafaret = t.Forward() trafaret << t.List(CONTEXT_TRAFARET) assert trafaret([123], context=123) == [123]
def test_forward(self): trafaret = t.Forward() trafaret << t.List(t.Int & check_int) res = self.loop.run_until_complete(trafaret.async_check(['5'])) self.assertEqual(res, [5])
def test_forward(self): trafaret = t.Forward() trafaret << t.List(CONTEXT_TRAFARET) self.assertEqual(trafaret([123], context=123), [123])
async def test_not_set_forward(): trafaret = t.Forward() with pytest.raises(t.DataError) as res: await (trafaret.async_check(None)) assert res.value.as_dict() == "trafaret not set yet"
async def test_forward(): trafaret = t.Forward() trafaret << t.List(t.ToInt & check_int) res = await (trafaret.async_check(['5'])) assert res == [5]
return checked_mapping return inner def ref_field(reference, context=None): register = context register.reg_reference(reference) def inner(value, context=None): schema = register.get_schema(reference) return schema(value, context=context) return inner json_schema = t.Forward() noop = just(t.Any()) metadata = ( t.Key('$id', optional=True, trafaret=t.URL & noop), t.Key('$schema', optional=True, trafaret=t.URL & noop), t.Key('$ref', optional=True, trafaret=t.String & ref_field), t.Key('title', optional=True, trafaret=t.String & noop), t.Key('description', optional=True, trafaret=t.String & noop), t.Key('definitions', optional=True, trafaret=deep_schema_mapping('definitions', t.String()) & noop), t.Key('examples', optional=True, trafaret=t.List(t.Any) & noop), t.Key('default', optional=True, trafaret=t.Any & noop), ) schema_keywords = (