Search in sources :

Example 76 with GraknTx

use of ai.grakn.GraknTx in project grakn by graknlabs.

the class RemoteGraknTxTest method whenExecutingAQuery_SendAnExecQueryMessageToGrpc.

@Test
public void whenExecutingAQuery_SendAnExecQueryMessageToGrpc() {
    Query<?> query = match(var("x").isa("person")).get();
    String queryString = query.toString();
    try (GraknTx tx = RemoteGraknTx.create(session, GrpcUtil.openRequest(KEYSPACE, GraknTxType.WRITE))) {
        // The open request
        verify(server.requests()).onNext(any());
        tx.graql().parse(queryString).execute();
    }
    verify(server.requests()).onNext(GrpcUtil.execQueryRequest(query));
}
Also used : GraknTx(ai.grakn.GraknTx) Test(org.junit.Test)

Example 77 with GraknTx

use of ai.grakn.GraknTx in project grakn by graknlabs.

the class RemoteGraknTxTest method whenExecutingAQueryWithASingleAnswer_GetAnAnswerBack.

@Test
public void whenExecutingAQueryWithASingleAnswer_GetAnAnswerBack() {
    Query<?> query = define(label("person").sub("entity"));
    String queryString = query.toString();
    GrpcConcept.Concept v123 = GrpcConcept.Concept.newBuilder().setId(V123).build();
    GrpcGrakn.Answer grpcAnswer = GrpcGrakn.Answer.newBuilder().putAnswer("x", v123).build();
    QueryResult queryResult = QueryResult.newBuilder().setAnswer(grpcAnswer).build();
    TxResponse response = TxResponse.newBuilder().setQueryResult(queryResult).build();
    server.setResponseSequence(GrpcUtil.execQueryRequest(query), response);
    Answer answer;
    try (GraknTx tx = RemoteGraknTx.create(session, GrpcUtil.openRequest(KEYSPACE, GraknTxType.WRITE))) {
        // The open request
        verify(server.requests()).onNext(any());
        answer = tx.graql().<DefineQuery>parse(queryString).execute();
    }
    assertEquals(answer.vars(), ImmutableSet.of(var("x")));
    assertEquals(ConceptId.of("V123"), answer.get(var("x")).getId());
}
Also used : Answer(ai.grakn.graql.admin.Answer) GraknTx(ai.grakn.GraknTx) QueryResult(ai.grakn.rpc.generated.GrpcGrakn.QueryResult) GrpcGrakn(ai.grakn.rpc.generated.GrpcGrakn) GrpcConcept(ai.grakn.rpc.generated.GrpcConcept) DefineQuery(ai.grakn.graql.DefineQuery) TxResponse(ai.grakn.rpc.generated.GrpcGrakn.TxResponse) Test(org.junit.Test)

Example 78 with GraknTx

use of ai.grakn.GraknTx in project grakn by graknlabs.

the class RemoteGraknTxTest method whenAnErrorOccurs_TheTxCloses.

@Test
public void whenAnErrorOccurs_TheTxCloses() {
    Query<?> query = match(var("x")).get();
    TxRequest execQueryRequest = GrpcUtil.execQueryRequest(query);
    throwOn(execQueryRequest, ErrorType.GRAQL_QUERY_EXCEPTION, "well something went wrong");
    try (GraknTx tx = RemoteGraknTx.create(session, GrpcUtil.openRequest(KEYSPACE, GraknTxType.WRITE))) {
        try {
            tx.graql().match(var("x")).get().execute();
        } catch (GraqlQueryException e) {
        // Ignore
        }
        assertTrue(tx.isClosed());
    }
}
Also used : GraknTx(ai.grakn.GraknTx) TxRequest(ai.grakn.rpc.generated.GrpcGrakn.TxRequest) GraqlQueryException(ai.grakn.exception.GraqlQueryException) Test(org.junit.Test)

Example 79 with GraknTx

use of ai.grakn.GraknTx in project grakn by graknlabs.

the class RemoteGraknTxTest method whenStreamingAQueryWithInfiniteAnswers_Terminate.

@Test(timeout = 5_000)
public void whenStreamingAQueryWithInfiniteAnswers_Terminate() {
    Query<?> query = match(var("x").sub("thing")).get();
    String queryString = query.toString();
    GrpcConcept.Concept v123 = GrpcConcept.Concept.newBuilder().setId(V123).build();
    GrpcGrakn.Answer grpcAnswer = GrpcGrakn.Answer.newBuilder().putAnswer("x", v123).build();
    QueryResult queryResult = QueryResult.newBuilder().setAnswer(grpcAnswer).build();
    TxResponse response = TxResponse.newBuilder().setQueryResult(queryResult).build();
    server.setResponse(GrpcUtil.execQueryRequest(query), GrpcUtil.iteratorResponse(ITERATOR));
    server.setResponse(GrpcUtil.nextRequest(ITERATOR), response);
    List<Answer> answers;
    int numAnswers = 10;
    try (GraknTx tx = RemoteGraknTx.create(session, GrpcUtil.openRequest(KEYSPACE, GraknTxType.WRITE))) {
        // The open request
        verify(server.requests()).onNext(any());
        answers = tx.graql().<GetQuery>parse(queryString).stream().limit(numAnswers).collect(toList());
    }
    assertEquals(10, answers.size());
    for (Answer answer : answers) {
        assertEquals(answer.vars(), ImmutableSet.of(var("x")));
        assertEquals(ConceptId.of("V123"), answer.get(var("x")).getId());
    }
}
Also used : GrpcConcept(ai.grakn.rpc.generated.GrpcConcept) Answer(ai.grakn.graql.admin.Answer) GraknTx(ai.grakn.GraknTx) QueryResult(ai.grakn.rpc.generated.GrpcGrakn.QueryResult) GrpcGrakn(ai.grakn.rpc.generated.GrpcGrakn) GetQuery(ai.grakn.graql.GetQuery) TxResponse(ai.grakn.rpc.generated.GrpcGrakn.TxResponse) Test(org.junit.Test)

Example 80 with GraknTx

use of ai.grakn.GraknTx in project grakn by graknlabs.

the class RemoteGraknTxTest method whenCommittingATxFails_Throw.

@Test
public void whenCommittingATxFails_Throw() {
    throwOn(GrpcUtil.commitRequest(), ErrorType.INVALID_KB_EXCEPTION, "do it better next time");
    try (GraknTx tx = RemoteGraknTx.create(session, GrpcUtil.openRequest(KEYSPACE, GraknTxType.WRITE))) {
        exception.expect(InvalidKBException.class);
        exception.expectMessage("do it better next time");
        tx.commit();
    }
}
Also used : GraknTx(ai.grakn.GraknTx) Test(org.junit.Test)

Aggregations

GraknTx (ai.grakn.GraknTx)243 Test (org.junit.Test)189 EntityType (ai.grakn.concept.EntityType)54 GetQuery (ai.grakn.graql.GetQuery)52 Entity (ai.grakn.concept.Entity)51 QueryBuilder (ai.grakn.graql.QueryBuilder)49 Role (ai.grakn.concept.Role)48 RelationshipType (ai.grakn.concept.RelationshipType)46 Answer (ai.grakn.graql.admin.Answer)44 Set (java.util.Set)44 EmbeddedGraknTx (ai.grakn.kb.internal.EmbeddedGraknTx)33 Label (ai.grakn.concept.Label)28 Attribute (ai.grakn.concept.Attribute)27 Concept (ai.grakn.concept.Concept)27 HashSet (java.util.HashSet)26 GraknSession (ai.grakn.GraknSession)22 AttributeType (ai.grakn.concept.AttributeType)22 ConceptId (ai.grakn.concept.ConceptId)22 List (java.util.List)19 GraknTxType (ai.grakn.GraknTxType)17