Search in sources :

Example 11 with InsertQuery

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));
}
Also used : InsertQuery(ai.grakn.graql.InsertQuery) Answer(ai.grakn.graql.admin.Answer) Entity(ai.grakn.concept.Entity) Relationship(ai.grakn.concept.Relationship) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 12 with InsertQuery

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"));
}
Also used : InsertQuery(ai.grakn.graql.InsertQuery) Answer(ai.grakn.graql.admin.Answer) VarPattern(ai.grakn.graql.VarPattern) Test(org.junit.Test)

Example 13 with InsertQuery

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());
}
Also used : InsertQuery(ai.grakn.graql.InsertQuery) Answer(ai.grakn.graql.admin.Answer) Test(org.junit.Test)

Example 14 with InsertQuery

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());
}
Also used : GraknTx(ai.grakn.GraknTx) InsertQuery(ai.grakn.graql.InsertQuery) Test(org.junit.Test)

Example 15 with InsertQuery

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());
}
Also used : InsertQuery(ai.grakn.graql.InsertQuery) Test(org.junit.Test)

Aggregations

InsertQuery (ai.grakn.graql.InsertQuery)31 Test (org.junit.Test)29 Answer (ai.grakn.graql.admin.Answer)6 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)4 QueryBuilder (ai.grakn.graql.QueryBuilder)3 GraknTx (ai.grakn.GraknTx)2 ConceptId (ai.grakn.concept.ConceptId)2 Entity (ai.grakn.concept.Entity)2 Label (ai.grakn.concept.Label)2 Relationship (ai.grakn.concept.Relationship)2 SchemaConcept (ai.grakn.concept.SchemaConcept)2 DeleteQuery (ai.grakn.graql.DeleteQuery)2 Graql (ai.grakn.graql.Graql)2 Graql.label (ai.grakn.graql.Graql.label)2 Graql.var (ai.grakn.graql.Graql.var)2 Match (ai.grakn.graql.Match)2 VarPattern (ai.grakn.graql.VarPattern)2 Conjunction (ai.grakn.graql.admin.Conjunction)2 PatternAdmin (ai.grakn.graql.admin.PatternAdmin)2 MovieKB (ai.grakn.test.kbs.MovieKB)2