def test_handles_relay_node(self): class A(ObjectType, interfaces=[relay.Node]): field = String() class B(ObjectType): a = Field(A) node = relay.Node.Field() class Root(ObjectType): stitched = Field(stitch(B)) def resolve_stitched(self, info): return stitch(B)({'node': {'field': 'value'}}) schema = Schema(query=Root) id_ = to_global_id('A', 'foo') result = schema.execute('''query Test($id: ID!) { stitched { node(id: $id) { __typename, ... on A { field } } } }''', variables={'id': id_}) assert_gql_data_equals( result, {'stitched': {'node': { '__typename': 'A', 'field': 'value' }}} )
def test_can_fetch_by_object_id(): with nengo.Network() as model: ens = nengo.Ensemble(10, 1, label="Ensemble") id_provider = IdProvider(model, {'model': model, 'ens': ens}, prefix='no') key = b64encode(b'NengoEnsemble:no:Ensemble:ens').decode() result = schema.execute( f'{{ node(id: "{key}") {{ ... on NengoEnsemble {{ label }} }} }}', context=Context(model, id_provider=id_provider)) assert_gql_data_equals(result, {'node': {'label': "Ensemble"}})
def test_can_query_objects_from_model(nengo_type): with nengo.Network() as model: create_dummy(nengo_type, label="label") query_name = nengo_type.__name__.lower() + 's' result = schema.execute(f'{{ model {{ {query_name} {{ label }} }} }}', context=Context(model)) assert_gql_data_equals(result, {'model': { query_name: [{ 'label': "label" }] }})
def test_can_query_syntax_errors(): context = Context(errors=[create_syntax_error()]) result = schema.execute(query_errors, context=context) assert_gql_data_equals( result, { 'errors': [{ 'message': MatchRegex(".*invalid syntax"), 'filename': '<string>', 'lineno': 1, 'offset': 7, 'text': 'import\n', 'traceback': None, }] })
async def test_can_subscribe_to_kernel(): context_mock = mock.MagicMock() context_mock.reloadable = rx.subjects.Subject() context_mock.reloadable.call = dummy_coro observer_mock = mock.MagicMock() obs = schema.execute('subscription Sub { kernel { model { label } } }', context=context_mock, allow_subscriptions=True) obs.subscribe(observer_mock) await complete_other_tasks() observer_mock.on_next.assert_called_once() observer_mock.on_next.reset_mock() context_mock.reloadable.on_next(2) await complete_other_tasks() observer_mock.on_next.assert_called_once() assert_gql_data_equals(observer_mock.on_next.call_args[0][0], {'kernel': { 'model': { 'label': 'foo' } }})
def test_can_query_general_non_execution_errors(): context = Context(errors=[create_general_non_execution_error()]) result = schema.execute(query_errors, context=context) assert_gql_data_equals( result, { 'errors': [{ 'message': "dummy exception", 'filename': None, 'lineno': None, 'offset': None, 'text': None, 'traceback': [{ 'filename': MatchAny(str), 'lineno': MatchAny(int), 'name': 'create_general_non_execution_error' }], }] })
def test_can_query_execution_errors(): context = Context(errors=[create_execution_error()]) result = schema.execute(query_errors, context=context) assert_gql_data_equals( result, { 'errors': [{ 'message': MatchRegex(".*not defined"), 'filename': '<string>', 'lineno': 1, 'offset': None, 'text': None, 'traceback': [{ 'filename': '<string>', 'lineno': 1, 'name': '<module>' }], }] })
def test_can_query_object_ids(nengo_type): with nengo.Network() as model: create_dummy(nengo_type, label="label") query_name = nengo_type.__name__.lower() + 's' result = schema.execute(f'{{ model {{ id, {query_name} {{ id }} }} }}', context=Context(model, id_provider=IdProvider( model, {'model': model}, prefix='no'))) assert_gql_data_equals( result, { 'model': { 'id': b64encode('NengoNetwork:no:Network:model'.encode()).decode(), query_name: [{ 'id': b64encode(( f'Nengo{nengo_type.__name__}:no:{nengo_type.__name__}:' f'model.{query_name}[0]').encode()).decode() }] } })