コード例 #1
0
ファイル: test_source_graph.py プロジェクト: kindermax/hiku
def test_complex_field():
    engine = Engine(SyncExecutor())

    def get_a(fields, ids):
        return [[{'s': 'bar'} for _ in fields] for _ in ids]

    ll_graph = Graph([
        Node('Foo', [
            Field('a', Record[{
                's': String
            }], get_a),
        ]),
    ])
    foo_sg = SubGraph(ll_graph, 'Foo')
    hl_graph = Graph([
        Node('Foo', [
            Field('a', Record[{
                's': String
            }], foo_sg),
        ]),
        Root([
            Link('foo', TypeRef['Foo'], lambda: 1, requires=None),
        ]),
    ])
    result = engine.execute(hl_graph, build([Q.foo[Q.a[Q.s]]]))
    check_result(result, {'foo': {'a': {'s': 'bar'}}})
コード例 #2
0
async def test_simple_async(graph_name, sample_count):
    async def x_fields(fields, ids):
        return [[42 for _ in fields] for _ in ids]

    async def root_fields(fields):
        return [1 for _ in fields]

    async def x_link():
        return 2

    ll_graph = Graph([
        Node('X', [
            Field('id', None, x_fields),
        ]),
    ])

    x_sg = SubGraph(ll_graph, 'X')

    hl_graph = Graph([
        Node('X', [
            Field('id', None, x_sg),
        ]),
        Root([
            Field('a', None, root_fields),
            Link('x', TypeRef['X'], x_link, requires=None),
        ]),
    ])

    hl_graph = apply(hl_graph, [AsyncGraphMetrics(graph_name)])

    query = q.Node([
        q.Field('a'),
        q.Link('x', q.Node([
            q.Field('id'),
        ])),
    ])

    engine = Engine(AsyncIOExecutor())

    assert sample_count('Root', 'a') is None
    assert sample_count('Root', 'x') is None
    assert sample_count('X', 'id') is None

    result = await engine.execute(hl_graph, query)
    check_result(result, {
        'a': 1,
        'x': {
            'id': 42,
        },
    })

    assert sample_count('Root', 'a') == 1.0
    assert sample_count('Root', 'x') == 1.0
    assert sample_count('X', 'id') == 1.0
コード例 #3
0
ファイル: test_source_graph.py プロジェクト: anxolerd/hiku
def bar(x):
    return '{x[b]} {x[y][d]}'.format(x=x).upper()


@define(Record[{'d': Any, 'xs': Sequence[Record[{'b': Any}]]}])
def baz(y):
    xs = ', '.join('{x[b]}'.format(x=x) for x in y['xs'])
    return '{y[d]} [{xs}]'.format(y=y, xs=xs).upper()


@define(Record[{'a': Any}], Any)
def buz(x, size):
    return '{x[a]} - {size}'.format(x=x, size=size)


sg_x = SubGraph(_GRAPH, 'x')

sg_y = SubGraph(_GRAPH, 'y')

# TODO: refactor
GRAPH = Graph([
    Node('x1', [
        Field('id', None, sg_x),
        Field('a', None, sg_x),
        Field('f', None, sg_x.c(S.f1)),
        Field('foo', None, sg_x.c(foo(S.this, S.this.y))),
        Field('bar', None, sg_x.c(bar(S.this))),
        Field('baz', None, sg_x.c(baz(S.this.y))),
        Field('buz',
              None,
              sg_x.c(buz(S.this, S.size)),
コード例 #4
0
ファイル: test_subgraph.py プロジェクト: anxolerd/hiku

# define high-level graph

from hiku.types import Record, Integer, String
from hiku.expr.core import S, define, if_some
from hiku.sources.graph import SubGraph


@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,
コード例 #5
0
def field_func():
    raise NotImplementedError


def link_func():
    raise NotImplementedError


_GRAPH = Graph([
    Node('flexed', [
        Field('yari', Boolean, field_func),
    ]),
])

flexed_sg = SubGraph(_GRAPH, 'flexed')

GRAPH = Graph([
    Node('flexed', [
        Field('yari',
              Boolean,
              flexed_sg,
              options=[
                  Option('membuka', Sequence[String], default=['frayed']),
                  Option('modist',
                         Optional[Integer],
                         default=None,
                         description='callow'),
              ]),
    ]),
    Node('decian', [