Exemplo n.º 1
0
def test_optional_field():
    expr = check_expr(if_some([S.x, S.peen], S.x.copies, 0))
    if_some_bind = expr.values[1]
    assert isinstance(if_some_bind, List)
    x = if_some_bind.values[0]
    assert isinstance(x, Symbol)
    peen_ref = NamedRef(None, 'peen', Optional[Record[{'copies': Integer}]])
    check_ref(x, Ref(peen_ref, Record[{'copies': Integer}]))
Exemplo n.º 2
0
def test_optional_link():
    expr = check_expr(if_some([S.x, S.rakyats], S.x.pinout, ''))
    if_some_bind = expr.values[1]
    assert isinstance(if_some_bind, List)
    x = if_some_bind.values[0]
    assert isinstance(x, Symbol)
    rakyats_ref = NamedRef(None, 'rakyats', Optional[TypeRef['thalweg']])
    check_ref(x, Ref(rakyats_ref, TypeRef['thalweg']))
Exemplo n.º 3
0
def test_optional_link():
    expr = check_expr(if_some([S.x, S.rakyats], S.x.pinout, ''))
    if_some_bind = expr.values[1]
    assert isinstance(if_some_bind, List)
    x = if_some_bind.values[0]
    assert isinstance(x, Symbol)
    rakyats_ref = NamedRef(None, 'rakyats', Optional[TypeRef['thalweg']])
    check_ref(x, Ref(rakyats_ref, TypeRef['thalweg']))
Exemplo n.º 4
0
def test_optional_field():
    expr = check_expr(if_some([S.x, S.peen], S.x.copies, 0))
    if_some_bind = expr.values[1]
    assert isinstance(if_some_bind, List)
    x = if_some_bind.values[0]
    assert isinstance(x, Symbol)
    peen_ref = NamedRef(None, 'peen', Optional[Record[{'copies': Integer}]])
    check_ref(x, Ref(peen_ref, Record[{'copies': Integer}]))
Exemplo n.º 5
0
def test_if_some_optional():
    types = {
        'Bar': Record[{
            'baz': Integer
        }],
        'Foo': Record[{
            'bar': Sequence['Bar']
        }],
        '__root__': Record[{
            'foo': Optional['Foo']
        }],
    }
    ast = check_expr(types,
                     if_some([S.x, S.foo], each(S.y, S.x.bar, S.y.baz), None))
    _, _, each_ast, _ = ast.values
    _, _, _, y_baz = each_ast.values
    check_ref(y_baz, [
        ('baz', Integer),
        (None, TypeRef['Bar']),
        ('bar', Sequence[TypeRef['Bar']]),
        (None, TypeRef['Foo']),
        ('foo', Optional[TypeRef['Foo']]),
    ])
Exemplo n.º 6
0
@define(Record[{'id': Integer, 'name': String}])
def image_url(image):
    return 'http://example.com/{id}-{name}'.format(id=image['id'],
                                                   name=image['name'])


character_sg = SubGraph(_GRAPH, 'Character')

GRAPH = Graph([
    Node('Character', [
        Field('id', None, character_sg),
        Field('name', None, character_sg),
        Field(
            'image-url', None,
            character_sg.c(
                if_some([S.img, S.this.image], image_url(S.img),
                        'http://example.com/no-photo.jpg'), )),
    ]),
    Root([
        Link('characters',
             Sequence[TypeRef['Character']],
             to_characters_query,
             requires=None),
    ]),
])

# test high-level graph


def test_high_level():
    result = execute(GRAPH, '[{:characters [:name :image-url]}]')
    assert result == {
Exemplo n.º 7
0
def test_query_if_some():
    check_query(
        if_some([S.x, S.civics], S.x.clacks, 'false'),
        Node([Link('civics', Node([Field('clacks')]))]),
    )
Exemplo n.º 8
0
 def testIfSomeExpr(self):
     self.assertCompiles(
         if_some([S.x, S.nitrox], S.x, 'nothing'),
         "next(((x if (x is not None) else 'nothing') for x in "
         "(ctx['nitrox'],)))"
     )
Exemplo n.º 9
0
def test_query_optional():
    check_query(
        if_some([S.x, S.civics], S.x.clacks, 'false'),
        Node([Link('civics', Node([Field('clacks')]))]),
    )
Exemplo n.º 10
0
def test_if_some_expr():
    check_compiles(
        if_some([S.x, S.nitrox], S.x, 'nothing'),
        "next(x if x is not None else 'nothing' for x in (ctx['nitrox'],)) ")