Example #1
0
def test_root_link_alias():
    data = {
        'xN': {'a': 1, 'b': 2},
    }

    @listify
    def x_fields(fields, ids):
        for i in ids:
            yield [data[i][f.name] for f in fields]

    graph = Graph([
        Node('X', [
            Field('a', None, x_fields),
            Field('b', None, x_fields),
        ]),
        Root([
            Link('x', TypeRef['X'], lambda: 'xN', requires=None),
        ]),
    ])
    result = execute(graph, q.Node([
        q.Link('x', q.Node([q.Field('a')]), alias='x1'),
        q.Link('x', q.Node([q.Field('b')]), alias='x2'),
    ]))
    check_result(result, {
        'x1': {'a': 1},
        'x2': {'b': 2},
    })
Example #2
0
def test_type_ref():
    assert_dumps(
        Graph([
            Node('xeric', [
                Field('derrida', String, _),
            ]),
            Node('amb', [
                Field('loor', String, _),
                Link('cressy', TypeRef['xeric'], _, requires=None),
            ]),
            Node('offeree', [
                Field('abila', String, _),
                Link('ferber', Sequence[TypeRef['xeric']], _, requires=None),
            ]),
        ]),
        """
        type xeric
          Record
            :derrida String

        type amb
          Record
            :loor String
            :cressy xeric

        type offeree
          Record
            :abila String
            :ferber
              List xeric
        """,
    )
Example #3
0
def test_links():
    fb = Mock(return_value=[1])
    fc = Mock(return_value=[2])
    fi = Mock(return_value=[3])
    fd = Mock(return_value=[['boners']])
    fe = Mock(return_value=[['julio']])

    graph = Graph([
        Node('a', [
            Field('d', None, fd),
            Field('e', None, fe),
        ]),
        Root([
            Field('i', None, fi),
            Link('b', Sequence[TypeRef['a']], fb, requires=None),
            Link('c', Sequence[TypeRef['a']], fc, requires='i'),
        ]),
    ])

    result = execute(graph, build([Q.b[Q.d], Q.c[Q.e]]))
    check_result(result, {'b': [{'d': 'boners'}],
                          'c': [{'e': 'julio'}]})

    fi.assert_called_once_with([q.Field('i')])
    fb.assert_called_once_with()
    fc.assert_called_once_with(3)
    fd.assert_called_once_with([q.Field('d')], [1])
    fe.assert_called_once_with([q.Field('e')], [2])
def test_mutation_type():
    data_types = {
        'Foo': Record[{
            'bar': Integer,
        }],
    }
    query_graph = Graph([Root([
        Field('getFoo', Integer, _noop, options=[
            Option('getterArg', TypeRef['Foo']),
        ]),
    ])], data_types)
    mutation_graph = Graph(query_graph.nodes + [Root([
        Field('setFoo', Integer, _noop, options=[
            Option('setterArg', TypeRef['Foo']),
        ]),
    ])], data_types=query_graph.data_types)
    assert introspect(query_graph, mutation_graph) == _schema([
        _type('Query', 'OBJECT', fields=[
            _field('getFoo', _non_null(_INT), args=[
                _ival('getterArg', _non_null(_iobj('IOFoo'))),
            ]),
        ]),
        _type('Mutation', 'OBJECT', fields=[
            _field('setFoo', _non_null(_INT), args=[
                _ival('setterArg', _non_null(_iobj('IOFoo'))),
            ]),
        ]),
        _type('Foo', 'OBJECT', fields=[
            _field('bar', _non_null(_INT)),
        ]),
        _type('IOFoo', 'INPUT_OBJECT', inputFields=[
            _ival('bar', _non_null(_INT)),
        ]),
    ], with_mutation=True)
Example #5
0
def test_node_complex_fields():
    f1 = Mock(return_value=[1])
    f2 = Mock(return_value=[[{'f': 'marshes'}]])
    f3 = Mock(return_value=[[{'g': 'colline'}]])
    f4 = Mock(return_value=[[[{'h': 'magi'}]]])

    graph = Graph([
        Node('a', [
            Field('b', Optional[Record[{'f': Integer}]], f2),
            Field('c', Record[{'g': Integer}], f3),
            Field('d', Sequence[Record[{'h': Integer}]], f4),
        ]),
        Root([
            Link('e', Sequence[TypeRef['a']], f1, requires=None),
        ]),
    ])

    check_result(
        execute(graph, build([Q.e[Q.b[Q.f], Q.c[Q.g], Q.d[Q.h]]])),
        {'e': [{'b': {'f': 'marshes'},
                'c': {'g': 'colline'},
                'd': [{'h': 'magi'}]}]},
    )

    f1.assert_called_once_with()
    f2.assert_called_once_with(
        [q.Link('b', q.Node([q.Field('f')]))], [1],
    )
    f3.assert_called_once_with(
        [q.Link('c', q.Node([q.Field('g')]))], [1],
    )
    f4.assert_called_once_with(
        [q.Link('d', q.Node([q.Field('h')]))], [1],
    )
Example #6
0
def test_links():
    f1 = Mock(return_value=[1])
    f2 = Mock(return_value=[2])
    f3 = Mock(return_value=[['boners']])
    f4 = Mock(return_value=[['julio']])

    graph = Graph([
        Node('a', [
            Field('d', None, f3),
            Field('e', None, f4),
        ]),
        Root([
            Link('b', Sequence[TypeRef['a']], f1, requires=None),
            Link('c', Sequence[TypeRef['a']], f2, requires=None),
        ]),
    ])

    result = execute(graph, build([Q.b[Q.d], Q.c[Q.e]]))
    check_result(result, {'b': [{'d': 'boners'}], 'c': [{'e': 'julio'}]})
    assert result.index == {'a': {1: {'d': 'boners'}, 2: {'e': 'julio'}}}

    f1.assert_called_once_with()
    f2.assert_called_once_with()
    with reqs_eq_patcher():
        f3.assert_called_once_with([query.Field('d')], [1])
        f4.assert_called_once_with([query.Field('e')], [2])
Example #7
0
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'}}})
Example #8
0
def test_with_pass_context(graph_name, sample_count):
    def root_fields1(fields):
        return [1 for _ in fields]

    @pass_context
    def root_fields2(ctx, fields):
        return [2 for _ in fields]

    graph = Graph([
        Root([
            Field('a', None, root_fields1),
            Field('b', None, root_fields2),
        ]),
    ])

    graph = apply(graph, [GraphMetrics(graph_name)])

    assert sample_count('Root', 'a') is None
    assert sample_count('Root', 'b') is None

    result = Engine(SyncExecutor()).execute(
        graph, q.Node([
            q.Field('a'),
            q.Field('b'),
        ]))
    check_result(result, {
        'a': 1,
        'b': 2,
    })

    assert sample_count('Root', 'a') == 1.0
    assert sample_count('Root', 'b') == 1.0
Example #9
0
    def testTypeRef(self):
        self.assertDumps(
            Edge(None, [
                Edge('Foo', [
                    Field('a', StringType, noop),
                ]),
                Edge('Bar', [
                    Field('b', StringType, noop),
                    Link('c', None, 'Foo', noop, to_list=False),
                ]),
                Edge('Baz', [
                    Field('d', StringType, noop),
                    Link('e', None, 'Foo', noop, to_list=True),
                ]),
            ]),
            """
            type Foo
              Record
                :a String

            type Bar
              Record
                :b String
                :c Foo

            type Baz
              Record
                :d String
                :e
                  List Foo
            """,
        )
Example #10
0
def test_field_complex():
    assert_dumps(
        Graph([
            Root([
                Field('behave', Optional[Record[{
                    'burieth': Integer
                }]], _),
                Field('gemara', Record[{
                    'trevino': Integer
                }], _),
                Field('riffage', Sequence[Record[{
                    'shophar': Integer
                }]], _),
            ])
        ]),
        """
        type behave
          Option
            Record
              :burieth Integer

        type gemara
          Record
            :trevino Integer

        type riffage
          List
            Record
              :shophar Integer
        """,
    )
Example #11
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
Example #12
0
def test_process_ordered_node():
    ordering = []

    def f1(fields):
        names = tuple(f.name for f in fields)
        ordering.append(names)
        return names

    def f2(fields):
        return f1(fields)

    def f3():
        ordering.append('x1')
        return 'x1'

    @listify
    def f4(fields, ids):
        for i in ids:
            yield ['{}-e'.format(i) for _ in fields]

    graph = Graph([
        Node('X', [
            Field('e', None, f4),
        ]),
        Root([
            Field('a', None, f1),
            Field('b', None, f1),
            Field('c', None, f2),
            Field('d', None, f2),
            Link('x', TypeRef['X'], f3, requires=None),
        ]),
    ])
    query = q.Node([
        q.Field('d'),
        q.Field('b'),
        q.Field('a'),
        q.Link('x', q.Node([
            q.Field('e'),
        ])),
        q.Field('c'),
    ], ordered=True)

    engine = Engine(SyncExecutor())
    result = engine.execute(graph, query)
    check_result(result, {
        'a': 'a',
        'b': 'b',
        'c': 'c',
        'd': 'd',
        'x': {
            'e': 'x1-e',
        },
    })
    assert ordering == [('d',), ('b', 'a'), 'x1', ('c',)]
def test_introspection_query():
    graph = Graph([
        Node('flexed', [
            Field('yari',
                  Boolean,
                  _noop,
                  options=[
                      Option('membuka', Sequence[String], default=['frayed']),
                      Option('modist',
                             Optional[Integer],
                             default=None,
                             description='callow'),
                  ]),
        ]),
        Node('decian', [
            Field('dogme', Integer, _noop),
            Link('clarkia', Sequence[TypeRef['flexed']], _noop, requires=None),
        ]),
        Root([
            Field('_cowered', String, _noop),
            Field('entero', Float, _noop),
            Link('toma', Sequence[TypeRef['decian']], _noop, requires=None),
        ]),
    ])
    assert introspect(graph) == _schema([
        _type('flexed',
              'OBJECT',
              fields=[
                  _field('yari',
                         _non_null(_BOOL),
                         args=[
                             _ival('membuka',
                                   _seq_of(_STR),
                                   defaultValue='["frayed"]'),
                             _ival('modist',
                                   _INT,
                                   defaultValue='null',
                                   description='callow'),
                         ]),
              ]),
        _type('decian',
              'OBJECT',
              fields=[
                  _field('dogme', _non_null(_INT)),
                  _field('clarkia', _seq_of(_obj('flexed'))),
              ]),
        _type('Query',
              'OBJECT',
              fields=[
                  _field('entero', _non_null(_FLOAT)),
                  _field('toma', _seq_of(_obj('decian'))),
              ]),
    ])
def test_untyped_fields():
    graph = Graph([
        Root([
            Field('untyped', None, _noop),
            Field('any_typed', Any, _noop),
        ]),
    ])
    assert introspect(graph) == _schema([
        _type('Query', 'OBJECT', fields=[
            _field('untyped', _ANY),
            _field('any_typed', _ANY),
        ]),
    ])
def test_unsupported_option():
    result = introspect(
        Graph([
            Root([
                Field('huke',
                      Integer,
                      field_func,
                      options=[Option('orel', Optional[Any])]),
                Field('terapin', Integer, field_func),
            ]),
        ]))

    assert result['__schema']['types'][-1]['name'] == 'Root'
    assert [f['name'] for f in result['__schema']['types'][-1]['fields']] == \
           ['terapin']
def test_unsupported_field():
    result = introspect(
        Graph([
            Root([
                Field('fall', Optional[Any], field_func),
                Field('bayman', Optional[Record[{
                    'foo': Optional[Integer]
                }]], field_func),
                Field('huss', Integer, field_func),
            ]),
        ]))

    assert result['__schema']['types'][-1]['name'] == 'Root'
    assert [f['name'] for f in result['__schema']['types'][-1]['fields']] == \
           ['huss']
Example #17
0
def test_conflicting_fields():
    x_data = {'xN': {'a': 42}}

    @listify
    def x_fields(fields, ids):
        for i in ids:
            yield ['{}-{}'.format(x_data[i][f.name], f.options['k'])
                   for f in fields]

    graph = Graph([
        Node('X', [
            Field('a', None, x_fields, options=[Option('k', Integer)]),
        ]),
        Root([
            Link('x1', TypeRef['X'], lambda: 'xN', requires=None),
            Link('x2', TypeRef['X'], lambda: 'xN', requires=None),
        ]),
    ])

    result = execute(graph, q.Node([
        q.Link('x1', q.Node([q.Field('a', options={'k': 1})])),
        q.Link('x2', q.Node([q.Field('a', options={'k': 2})])),
    ]))
    check_result(result, {
        'x1': {'a': '42-1'},
        'x2': {'a': '42-2'},
    })
def test_unsupported_field_type():
    graph = Graph([
        Root([
            Field('fall', Optional[Any], _noop),
            Field('bayman', Optional[Record[{'foo': Optional[Integer]}]],
                  _noop),
            Field('huss', Integer, _noop),
        ]),
    ])
    assert introspect(graph) == _schema([
        _type('Query', 'OBJECT', fields=[
            _field('fall', _ANY),
            _field('bayman', _ANY),
            _field('huss', _non_null(_INT)),
        ]),
    ])
Example #19
0
def test_pass_context_link():
    f1 = pass_context(Mock(return_value=[1]))
    f2 = Mock(return_value=[['boners']])

    graph = Graph([
        Node('a', [
            Field('b', None, f2),
        ]),
        Root([
            Link('c', Sequence[TypeRef['a']], f1, requires=None),
        ]),
    ])

    result = execute(graph, build([Q.c[Q.b]]), {'fibs': 'dossil'})
    check_result(result, {'c': [{'b': 'boners'}]})
    assert result.index == {'a': {1: {'b': 'boners'}}}

    f1.assert_called_once_with(ANY)
    with reqs_eq_patcher():
        f2.assert_called_once_with([query.Field('b')], [1])

    ctx = f1.call_args[0][0]
    assert isinstance(ctx, Context)
    assert ctx['fibs'] == 'dossil'
    with pytest.raises(KeyError) as err:
        _ = ctx['invalid']  # noqa
    err.match('is not specified in the query context')
def test_unsupported_option_type():
    graph = Graph([
        Root([
            Field('huke', Integer, _noop,
                  options=[Option('orel', Optional[Any])]),
            Field('terapin', Integer, _noop),
        ]),
    ])
    assert introspect(graph) == _schema([
        _type('Query', 'OBJECT', fields=[
            _field('huke', _non_null(_INT), args=[
                _ival('orel', _ANY),
            ]),
            _field('terapin', _non_null(_INT)),
        ]),
    ])
Example #21
0
def subquery_fields(sub_root, sub_edge_name, exprs):
    re_env = {}
    exprs = {name: to_expr(obj, re_env) for name, obj in exprs.items()}
    ec_env = {func.__fn_name__ for func in re_env.values()}
    fn_env = {func.__fn_name__: func.fn for func in re_env.values()}

    re_env['this'] = sub_root.fields[sub_edge_name]
    re_env.update(sub_root.fields)

    ec = ExpressionCompiler(ec_env)
    reqs_map = {}
    procs_map = {}
    for name, expr in exprs.items():
        reqs_map[name] = RequirementsExtractor.analyze(re_env, expr)
        procs_map[name] = eval(
            compile(ec.compile_lambda_expr(expr), '<expr>', 'eval'))

    def query_func(queue, task_set, edge, fields, ids):
        this_link = Link(THIS_LINK_NAME, None, sub_edge_name, None, True)

        reqs = merge(reqs_map[f.name] for f in fields)
        # FIXME: implement proper way to handle "this" value
        # and query other possible data from sub_root
        pattern = reqs.fields['this'].edge
        procs = [procs_map[f.name] for f in fields]

        query = Query(queue, task_set, sub_root, None)
        query._process_link(sub_root, this_link, pattern, None, ids)

        return _create_result_proc(query, fn_env, edge, fields, procs, ids)

    query_func.__subquery__ = True

    return [Field(name, query_func) for name in exprs.keys()]
Example #22
0
def test_root_fields():
    f1 = Mock(return_value=['boiardo'])
    f2 = Mock(return_value=['isolde'])

    graph = Graph([
        Root([
            Field('a', None, f1),
            Field('b', None, f2),
        ]),
    ])

    result = execute(graph, build([Q.a, Q.b]))
    check_result(result, {'a': 'boiardo', 'b': 'isolde'})

    f1.assert_called_once_with([q.Field('a')])
    f2.assert_called_once_with([q.Field('b')])
Example #23
0
def test_link_optional(value, expected):
    graph = Graph([
        Node('Bar', [
            Field('baz', Integer, _),
        ]),
        Node('Foo', [
            Link('bar', Optional[TypeRef['Bar']], _, requires=None),
        ]),
        Root([
            Link('foo', Optional[TypeRef['Foo']], _, requires=None),
        ]),
    ])
    query = build([
        Q.foo[Q.bar[Q.baz, ], ],
    ])
    index = Index()
    index[ROOT.node][ROOT.ident].update({
        'foo': Reference('Foo', 1),
    })
    index['Foo'][1].update({
        'bar': value,
    })
    index['Bar'][2].update({'baz': 42})
    result = Proxy(index, ROOT, query)
    assert Denormalize(graph, result).process(query) == {
        'foo': {
            'bar': expected,
        },
    }
Example #24
0
def testDocs():
    assert_dumps(
        Graph([
            Node('switzer', [
                Field('beatch', String, _, description="attribute beatch"),
            ],
                 description="switzer description"),
            Node('trine', [
                Field('propels',
                      Optional[String],
                      _,
                      description="attribute propels"),
                Link('cardura',
                     TypeRef['switzer'],
                     _,
                     requires=None,
                     description="link cardura to switzer"),
            ],
                 description="trine description"),
            Node('packrat', [
                Field('pikes', String, _, description="attribute pikes"),
                Link('albus',
                     Sequence[TypeRef['switzer']],
                     _,
                     requires=None,
                     description="link albus to switzer"),
            ],
                 description="packrat description"),
        ]),
        """
        type switzer  ; switzer description
          Record
            :beatch String  ; attribute beatch

        type trine  ; trine description
          Record
            :propels  ; attribute propels
              Option String
            :cardura switzer  ; link cardura to switzer

        type packrat  ; packrat description
          Record
            :pikes String  ; attribute pikes
            :albus  ; link albus to switzer
              List switzer
        """,
    )
Example #25
0
def test_conflicting_links():
    data = {
        'yA': {'a': 1, 'b': 2},
        'yB': {'a': 3, 'b': 4},
        'yC': {'a': 5, 'b': 6},
    }
    x2y = {'xN': ['yA', 'yB', 'yC']}

    @listify
    def y_fields(fields, ids):
        for i in ids:
            yield [data[i][f.name] for f in fields]

    @listify
    def x_to_y_link(ids, options):
        for i in ids:
            yield [y for y in x2y[i] if y not in options['exclude']]

    graph = Graph([
        Node('Y', [
            Field('a', None, y_fields),
            Field('b', None, y_fields),
        ]),
        Node('X', [
            Field('id', None, id_field),
            Link('y', Sequence[TypeRef['Y']], x_to_y_link, requires='id',
                 options=[Option('exclude', None)]),
        ]),
        Root([
            Link('x1', TypeRef['X'], lambda: 'xN', requires=None),
            Link('x2', TypeRef['X'], lambda: 'xN', requires=None),
        ]),
    ])
    result = execute(graph, q.Node([
        q.Link('x1', q.Node([
            q.Link('y', q.Node([q.Field('a')]),
                   options={'exclude': ['yA']}),
        ])),
        q.Link('x2', q.Node([
            q.Link('y', q.Node([q.Field('b')]),
                   options={'exclude': ['yC']}),
        ])),
    ]))
    check_result(result, {
        'x1': {'y': [{'a': 3}, {'a': 5}]},
        'x2': {'y': [{'b': 2}, {'b': 4}]},
    })
Example #26
0
def test_node_contain_duplicate_fields():
    check_errors(
        [
            Root([
                Field('b', None, _fields_func),
            ]),
            Node('foo', [
                Field('a', None, _fields_func),
                Field('a', None, _fields_func),
            ]),
            Root([
                Field('b', None, _fields_func),
            ]),
        ],
        ['Duplicated names found in the "root" node: "b"',
         'Duplicated names found in the "foo" node: "a"'],
    )
Example #27
0
    def testDocs(self):
        self.assertDumps(
            Edge(None, [
                Edge('Foo', [
                    Field('a', StringType, noop, doc="Attribute a"),
                ],
                     doc="Some Foo explanation"),
                Edge('Bar', [
                    Field('b', StringType, noop, doc="Attribute b"),
                    Link('c',
                         None,
                         'Foo',
                         noop,
                         to_list=False,
                         doc="Link c to Foo"),
                ],
                     doc="Some Bar explanation"),
                Edge('Baz', [
                    Field('d', StringType, noop, doc="Attribute d"),
                    Link('e',
                         None,
                         'Foo',
                         noop,
                         to_list=True,
                         doc="Link e to Foo"),
                ],
                     doc="Some Baz explanation"),
            ]),
            """
            type Foo  ; Some Foo explanation
              Record
                :a String  ; Attribute a

            type Bar  ; Some Bar explanation
              Record
                :b String  ; Attribute b
                :c Foo  ; Link c to Foo

            type Baz  ; Some Baz explanation
              Record
                :d String  ; Attribute d
                :e  ; Link e to Foo
                  List Foo
            """,
        )
Example #28
0
def test_field():
    assert_dumps(
        Graph([Root([
            Field('leones', String, _),
        ])]),
        """
        type leones String
        """,
    )
Example #29
0
 def testField(self):
     self.assertDumps(
         Edge(None, [
             Field('A', StringType, noop),
         ]),
         """
         type A String
         """,
     )
Example #30
0
def test_denormalize_with_alias():
    index = Index()
    index.root.update({
        'x': Reference('X', 'xN'),
    })
    index['X']['xN'].update({
        'a': 1,
        'b': 2,
    })
    index.finish()

    graph = Graph([
        Node('X', [
            Field('a', None, None),
            Field('b', None, None),
        ]),
        Root([
            Link('x', TypeRef['X'], lambda: 'xN', requires=None),
        ]),
    ])

    query = hiku_query.Node([
        hiku_query.Link('x',
                        hiku_query.Node([
                            hiku_query.Field('a', alias='a1'),
                        ]),
                        alias='x1'),
        hiku_query.Link('x',
                        hiku_query.Node([
                            hiku_query.Field('b', alias='b1'),
                        ]),
                        alias='x2'),
    ])

    result = Proxy(index, ROOT, query)

    assert denormalize(graph, result) == {
        'x1': {
            'a1': 1
        },
        'x2': {
            'b1': 2
        },
    }