def test_missing_object(): index = Index() index.finish() ref = Reference('SomeNode', 'unknown') proxy = Proxy(index, ref, hiku_query.Node([hiku_query.Field('foo')])) with pytest.raises(AssertionError) as err: proxy.foo err.match(r"Object SomeNode\[u?'unknown'\] is missing in the index")
def test_missing_field(): index = Index() index['SomeNode'][42].update({}) index.finish() ref = Reference('SomeNode', 42) proxy = Proxy(index, ref, hiku_query.Node([hiku_query.Field('foo')])) with pytest.raises(AssertionError) as err: proxy.foo err.match(r"Field SomeNode\[42\]\.foo is missing in the index")
def test_non_requested_field_attr(): index = Index() index['SomeNode'][42]['foo'] = 'bar' index.finish() ref = Reference('SomeNode', 42) proxy = Proxy(index, ref, hiku_query.Node([hiku_query.Field('foo')])) assert proxy.foo == 'bar' with pytest.raises(AttributeError) as err: proxy.unknown err.match(r"Field u?'unknown' wasn't requested in the query")
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, }, }
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 }, }
def test_denormalize_data_type(): index = Index() index.root.update({'foo': {'a': 42}}) index.finish() graph = Graph([ Root([ Field('foo', TypeRef['Foo'], None), ]), ], data_types={ 'Foo': Record[{ 'a': Integer }], }) query = hiku_query.Node([ hiku_query.Link('foo', hiku_query.Node([ hiku_query.Field('a'), ])), ]) assert denormalize(graph, Proxy(index, ROOT, query)) == {'foo': {'a': 42}}
def test_typename(): graph = Graph([ Node('Bar', [ Field('baz', Integer, _), ]), Node('Foo', [ Link('bar', Sequence[TypeRef['Bar']], _, requires=None), ]), Root([ Link('foo', TypeRef['Foo'], _, requires=None), ]), ]) query = read(""" query { __typename foo { __typename bar { __typename baz } } } """) index = Index() index[ROOT.node][ROOT.ident].update({ 'foo': Reference('Foo', 1), }) index['Foo'][1].update({ 'bar': [Reference('Bar', 2), Reference('Bar', 3)], }) index['Bar'][2].update({ 'baz': 42 }) index['Bar'][3].update({ 'baz': 43 }) result = Proxy(index, ROOT, query) assert DenormalizeGraphQL(graph, result, 'Query').process(query) == { '__typename': 'Query', 'foo': { '__typename': 'Foo', 'bar': [ { '__typename': 'Bar', 'baz': 42, }, { '__typename': 'Bar', 'baz': 43, }, ], }, }
def test_denormalize_non_merged_query(): 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), ]), ]) non_merged_query = hiku_query.Node([ hiku_query.Link('x', hiku_query.Node([ hiku_query.Field('a'), ])), hiku_query.Link('x', hiku_query.Node([ hiku_query.Field('b'), ])), ]) with pytest.raises(KeyError) as err: denormalize(graph, Proxy(index, ROOT, non_merged_query)) err.match("Field u?'a' wasn't requested in the query") merged_query = merge([non_merged_query]) assert denormalize(graph, Proxy(index, ROOT, merged_query)) == { 'x': { 'a': 1, 'b': 2 }, }
def test_link_sequence(): graph = Graph([ Node('Bar', [ Field('baz', Integer, _), ]), Node('Foo', [ Link('bar', Sequence[TypeRef['Bar']], _, requires=None), ]), Root([ Link('foo', Sequence[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': [Reference('Bar', 2), Reference('Bar', 3)], }) index['Bar'][2].update({'baz': 42}) index['Bar'][3].update({'baz': 43}) result = Proxy(index, ROOT, query) assert Denormalize(graph, result).process(query) == { 'foo': [ { 'bar': [ { 'baz': 42 }, { 'baz': 43 }, ], }, ], }
Field( 'elemental', Record[{ 'air': Sequence[CharacterRecord], 'water': Sequence[CharacterRecord], }], _), Field('barbary', Sequence[Record[{ 'betty': String }]], _), Link('flossy', TypeRef['Flossy'], _, requires=None), Link('zareeba', TypeRef['cosies'], _, requires=None), Link('crowdie', Sequence[TypeRef['cosies']], _, requires=None), Link('moujik', TypeRef['saunas'], _, requires=None), ]), ]) INDEX = Index() INDEX.root.update({ 'slotted': 'quoy_ushered', 'tatler': { 'orudis': 'fhp_musterd' }, 'coom': { 'yappers': 'idaho_golok' }, 'lovecraft': { 'characters': { 'cthulhu': cthulhu_data, 'dagon': None, } }, 'rlyeh': {
def execute_service(self, graph): idx = Index() idx[ROOT.node] = Index() idx[ROOT.node][ROOT.ident] = {'sdl': print_sdl(graph)} return Proxy(idx, ROOT, Node(fields=[Field('sdl')]))