use of ai.grakn.graql.InsertQuery in project grakn by graknlabs.
the class InsertQueryTest method whenAddingAnAttributeRelationshipWithProvenance_TheAttributeAndProvenanceAreAdded.
@Test
public void whenAddingAnAttributeRelationshipWithProvenance_TheAttributeAndProvenanceAreAdded() {
InsertQuery query = qb.insert(y.has("provenance", z.val("Someone told me")), w.isa("movie").has(title, x.val("My Movie"), y));
Answer answer = Iterables.getOnlyElement(query.execute());
Entity movie = answer.get(w).asEntity();
Attribute<String> theTitle = answer.get(x).asAttribute();
Relationship hasTitle = answer.get(y).asRelationship();
Attribute<String> provenance = answer.get(z).asAttribute();
assertThat(hasTitle.rolePlayers().toArray(), arrayContainingInAnyOrder(movie, theTitle));
assertThat(hasTitle.attributes().toArray(), arrayContaining(provenance));
}
use of ai.grakn.graql.InsertQuery in project grakn by graknlabs.
the class InsertQueryTest method testIterateMatchInsertResults.
@Test
public void testIterateMatchInsertResults() {
VarPattern language1 = var().isa("language").has("name", "123");
VarPattern language2 = var().isa("language").has("name", "456");
qb.insert(language1, language2).execute();
assertExists(qb, language1);
assertExists(qb, language2);
InsertQuery query = qb.match(var("x").isa("language")).insert(var("x").has("name", "HELLO"));
Iterator<Answer> results = query.iterator();
assertNotExists(qb, var().isa("language").has("name", "123").has("name", "HELLO"));
assertNotExists(qb, var().isa("language").has("name", "456").has("name", "HELLO"));
Answer result1 = results.next();
assertEquals(ImmutableSet.of(var("x")), result1.vars());
boolean query123 = qb.match(var().isa("language").has("name", "123").has("name", "HELLO")).iterator().hasNext();
boolean query456 = qb.match(var().isa("language").has("name", "456").has("name", "HELLO")).iterator().hasNext();
// Check if one of the matches have had the insert executed correctly
boolean oneExists = query123 != query456;
assertTrue("A match insert was not executed correctly for only one match", oneExists);
// Check that both are inserted correctly
Answer result2 = results.next();
assertEquals(ImmutableSet.of(var("x")), result1.vars());
assertExists(qb, var().isa("language").has("name", "123").has("name", "HELLO"));
assertExists(qb, var().isa("language").has("name", "456").has("name", "HELLO"));
assertFalse(results.hasNext());
assertNotEquals(result1.get("x"), result2.get("x"));
}
use of ai.grakn.graql.InsertQuery in project grakn by graknlabs.
the class InsertQueryTest method whenInsertingWithAMatch_ProjectMatchResultsOnVariablesInTheInsert.
@Test
public void whenInsertingWithAMatch_ProjectMatchResultsOnVariablesInTheInsert() {
qb.define(label("maybe-friends").relates("friend").sub("relationship"), label("person").plays("friend")).execute();
InsertQuery query = qb.match(var().rel("actor", x).rel("production-with-cast", z), var().rel("actor", y).rel("production-with-cast", z)).insert(w.rel("friend", x).rel("friend", y).isa("maybe-friends"));
List<Answer> answers = query.execute();
for (Answer answer : answers) {
assertThat("Should contain only variables mentioned in the insert (so excludes `$z`)", answer.vars(), containsInAnyOrder(x, y, w));
}
assertEquals("Should contain only distinct results", answers.size(), Sets.newHashSet(answers).size());
}
use of ai.grakn.graql.InsertQuery in project grakn by graknlabs.
the class InsertQueryImplTest method insertQueriesWithTheSameVarsAndGraphAreEqual.
@Test
public void insertQueriesWithTheSameVarsAndGraphAreEqual() {
GraknTx graph = mock(GraknTx.class);
InsertQuery query1 = InsertQueryImpl.create(vars1, Optional.empty(), Optional.of(graph));
InsertQuery query2 = InsertQueryImpl.create(vars1, Optional.empty(), Optional.of(graph));
assertEquals(query1, query2);
assertEquals(query1.hashCode(), query2.hashCode());
}
use of ai.grakn.graql.InsertQuery in project grakn by graknlabs.
the class InsertQueryImplTest method insertQueriesWithTheSameVarsAndQueryAreEqual.
@Test
public void insertQueriesWithTheSameVarsAndQueryAreEqual() {
InsertQuery query1 = InsertQueryImpl.create(vars1, match1, Optional.empty());
InsertQuery query2 = InsertQueryImpl.create(vars1, match1, Optional.empty());
assertEquals(query1, query2);
assertEquals(query1.hashCode(), query2.hashCode());
}
Aggregations