def _mock_label_response(cid: str, label: str) -> MockResponse: run_concept_method = RunConceptMethod( id=ConceptId(value=cid), conceptMethod=ConceptMethod(getLabel=Unit())) concept_response = ConceptResponse(label=Label(value=label)) request = TxRequest(runConceptMethod=run_concept_method) return MockResponse(eq(request), TxResponse(conceptResponse=concept_response))
def test_sends_open_request_with_keyspace(self) -> None: with engine_responding_with_nothing() as engine, client().open(): pass expected_request = TxRequest( open=Open(keyspace=Keyspace(value=keyspace), txType=Write)) engine.verify(expected_request)
def test_sends_commit_request(self) -> None: with engine_responding_to_streaming_query() as engine, client().open( ) as tx: tx.execute(query) tx.commit() expected = TxRequest(commit=Commit()) engine.verify(expected)
def test_sends_execute_query_request_with_parameters(self) -> None: with engine_responding_to_streaming_query() as engine, client().open( ) as tx: tx.execute(query) expected = TxRequest( execQuery=ExecQuery(query=Query(value=query), infer=None)) engine.verify(expected)
def _get_label(self, cid: grpc_concept.ConceptId) -> str: concept_method = grpc_concept.ConceptMethod( getLabel=grpc_concept.Unit()) request = TxRequest(runConceptMethod=grpc_grakn.RunConceptMethod( id=cid, conceptMethod=concept_method)) self._requests.add(request) response = self._next_response() return response.conceptResponse.label.value
def _get_value(self, cid: grpc_concept.ConceptId) -> Any: concept_method = grpc_concept.ConceptMethod( getValue=grpc_concept.Unit()) request = TxRequest(runConceptMethod=grpc_grakn.RunConceptMethod( id=cid, conceptMethod=concept_method)) self._requests.add(request) response = self._next_response() return self._convert_value(response.conceptResponse.attributeValue)
def _mock_value_response(cid: str, value: concept_pb2.AttributeValue) -> MockResponse: run_concept_method = RunConceptMethod( id=ConceptId(value=cid), conceptMethod=ConceptMethod(getValue=Unit())) concept_response = ConceptResponse(attributeValue=value) request = TxRequest(runConceptMethod=run_concept_method) return MockResponse(eq(request), TxResponse(conceptResponse=concept_response))
def _collect_results(self, iterator_id: IteratorId) -> List[Any]: query_results = [] while True: next_request = TxRequest(next=Next(iteratorId=iterator_id)) self._requests.add(next_request) response = self._next_response() if response.HasField('done'): break else: query_results.append(response.queryResult) return [ self._parse_result(query_result) for query_result in query_results ]
def __init__(self, keyspace: str, stub: grakn_pb2_grpc.GraknStub, timeout) -> None: self._requests: BlockingIter = BlockingIter() try: self._responses: Iterator[TxResponse] = stub.Tx(self._requests, timeout=timeout) except grpc.RpcError as e: _raise_grpc_error(e) request = TxRequest(open=grpc_grakn.Open(keyspace=grpc_grakn.Keyspace( value=keyspace), txType=grpc_grakn.Write)) self._requests.add(request) # wait for response from "open" _next_response(self._responses) self._tx = GraknTx(self._requests, self._responses)
def execute(self, query: str, *, infer: Optional[bool] = None) -> Any: """Execute a Graql query against the knowledge base :param query: the Graql query string to execute against the knowledge base :param infer: enable inference :return: a list of query results :raises: GraknError, GraknConnectionError """ grpc_infer = grpc_grakn.Infer( value=infer) if infer is not None else None request = TxRequest(execQuery=grpc_grakn.ExecQuery( query=grpc_grakn.Query(value=query), infer=grpc_infer)) self._requests.add(request) response = self._next_response() if response.HasField('done'): return elif response.HasField('queryResult'): return self._parse_result(response.queryResult) elif response.HasField('iteratorId'): return self._collect_results(response.iteratorId)
def test_sends_open_request_with_keyspace(self) -> None: with engine_responding_to_streaming_query() as engine: client().execute(query) expected_request = TxRequest( open=Open(keyspace=Keyspace(value=keyspace), txType=Write)) engine.verify(expected_request)
def commit(self) -> None: """Commit the transaction.""" self._requests.add(TxRequest(commit=grpc_grakn.Commit())) self._next_response()
import concept_pb2 import grakn_pb2_grpc from concept_pb2 import Concept, ConceptId, ConceptMethod, Unit, ConceptResponse, Label from grakn_pb2 import TxResponse, Done, TxRequest, Answer, QueryResult, RunConceptMethod from grakn_pb2_grpc import GraknServicer from iterator_pb2 import IteratorId, Next DONE = TxResponse(done=Done()) error_type = 'GRAQL_SYNTAX_EXCEPTION' ITERATOR_ID = IteratorId(id=5) ITERATOR_RESPONSE = TxResponse(iteratorId=ITERATOR_ID) NEXT = TxRequest(next=Next(iteratorId=ITERATOR_ID)) query: str = 'match $x sub concept; limit 3;' grpc_answers = [ Answer(answer={ 'x': Concept(id=ConceptId(value='a'), baseType=concept_pb2.MetaType) }), Answer(answer={ 'x': Concept(id=ConceptId(value='b'), baseType=concept_pb2.Attribute) }), Answer(answer={ 'x': Concept(id=ConceptId(value='c'), baseType=concept_pb2.AttributeType) })