use of ai.grakn.graql.DefineQuery 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());
}
use of ai.grakn.graql.DefineQuery in project grakn by graknlabs.
the class DefineQueryTest method whenExecutingADefineQuery_ResultContainsAllInsertedVars.
@Test
public void whenExecutingADefineQuery_ResultContainsAllInsertedVars() {
Var type = var("type");
Var type2 = var("type2");
// Note that two variables refer to the same type. They should both be in the result
DefineQuery query = qb.define(type.label("my-type").sub("entity"), type2.label("my-type"));
Answer result = query.execute();
assertThat(result.vars(), containsInAnyOrder(type, type2));
assertEquals(result.get(type), result.get(type2));
}
use of ai.grakn.graql.DefineQuery in project grakn by graknlabs.
the class QueryParserTest method whenParsingDefineQuery_ResultIsSameAsJavaGraql.
@Test
public void whenParsingDefineQuery_ResultIsSameAsJavaGraql() {
DefineQuery expected = define(label("pokemon").sub(Schema.MetaSchema.ENTITY.getLabel().getValue()), label("evolution").sub(Schema.MetaSchema.RELATIONSHIP.getLabel().getValue()), label("evolves-from").sub(Schema.MetaSchema.ROLE.getLabel().getValue()), label("evolves-to").sub(Schema.MetaSchema.ROLE.getLabel().getValue()), label("evolution").relates("evolves-from").relates("evolves-to"), label("pokemon").plays("evolves-from").plays("evolves-to").has("name"));
DefineQuery parsed = parse("define " + "'pokemon' sub entity;" + "evolution sub " + Schema.MetaSchema.RELATIONSHIP.getLabel() + ";" + "evolves-from sub role;" + "label \"evolves-to\" sub role;" + "evolution relates evolves-from, relates evolves-to;" + "pokemon plays evolves-from plays evolves-to has name;");
assertEquals(expected, parsed);
}
use of ai.grakn.graql.DefineQuery in project grakn by graknlabs.
the class QueryParserTest method whenParsingAsInDefine_ResultIsSameAsSub.
@Test
public void whenParsingAsInDefine_ResultIsSameAsSub() {
DefineQuery expected = define(label("parent").sub("role"), label("child").sub("role"), label("parenthood").sub("relationship").relates(var().label("parent")).relates(var().label("child")), label("fatherhood").sub("parenthood").relates(label("father"), label("parent")).relates(label("son"), label("child")));
DefineQuery parsed = parse("define " + "parent sub role;\n" + "child sub role;\n" + "parenthood sub relationship, relates parent, relates child;\n" + "fatherhood sub parenthood, relates father as parent, relates son as child;");
assertEquals(expected, parsed);
assertEquals(expected, parse(expected.toString()));
}
use of ai.grakn.graql.DefineQuery in project grakn by graknlabs.
the class DefineQueryTest method whenCallingToStringOfDefineQuery_ReturnCorrectRepresentation.
@Test
public void whenCallingToStringOfDefineQuery_ReturnCorrectRepresentation() {
String queryString = "define label my-entity sub entity;";
DefineQuery defineQuery = parse(queryString);
assertEquals(queryString, defineQuery.toString());
}
Aggregations