Search in sources :

Example 21 with ConstraintDefinition

use of org.neo4j.graphdb.schema.ConstraintDefinition in project neo4j by neo4j.

the class BatchInsertTest method shouldCreateDeferredUniquenessConstraintInEmptyDatabase.

@Test
public void shouldCreateDeferredUniquenessConstraintInEmptyDatabase() throws Exception {
    // GIVEN
    BatchInserter inserter = newBatchInserter();
    // WHEN
    ConstraintDefinition definition = inserter.createDeferredConstraint(label("Hacker")).assertPropertyIsUnique("handle").create();
    // THEN
    assertEquals("Hacker", definition.getLabel().name());
    assertEquals(ConstraintType.UNIQUENESS, definition.getConstraintType());
    assertEquals(asSet("handle"), Iterables.asSet(definition.getPropertyKeys()));
    inserter.shutdown();
}
Also used : BatchInserter(org.neo4j.unsafe.batchinsert.BatchInserter) ConstraintDefinition(org.neo4j.graphdb.schema.ConstraintDefinition) Test(org.junit.Test)

Example 22 with ConstraintDefinition

use of org.neo4j.graphdb.schema.ConstraintDefinition in project neo4j by neo4j.

the class DatabaseActionsTest method shouldCreatePropertyUniquenessConstraint.

@Test
public void shouldCreatePropertyUniquenessConstraint() throws Exception {
    // GIVEN
    String labelName = "person", propertyKey = "name";
    // WHEN
    actions.createPropertyUniquenessConstraint(labelName, asList(propertyKey));
    // THEN
    try (Transaction tx = graph.beginTx()) {
        Iterable<ConstraintDefinition> defs = graphdbHelper.getPropertyUniquenessConstraints(labelName, propertyKey);
        assertEquals(asSet(propertyKey), Iterables.asSet(single(defs).getPropertyKeys()));
        tx.success();
    }
}
Also used : Transaction(org.neo4j.graphdb.Transaction) ConstraintDefinition(org.neo4j.graphdb.schema.ConstraintDefinition) NodeRepresentationTest(org.neo4j.server.rest.repr.NodeRepresentationTest) RelationshipRepresentationTest(org.neo4j.server.rest.repr.RelationshipRepresentationTest) Test(org.junit.Test)

Example 23 with ConstraintDefinition

use of org.neo4j.graphdb.schema.ConstraintDefinition in project neo4j by neo4j.

the class DatabaseActionsTest method shouldDropPropertyUniquenessConstraint.

@Test
public void shouldDropPropertyUniquenessConstraint() throws Exception {
    // GIVEN
    String labelName = "user", propertyKey = "login";
    ConstraintDefinition index = graphdbHelper.createPropertyUniquenessConstraint(labelName, asList(propertyKey));
    // WHEN
    actions.dropPropertyUniquenessConstraint(labelName, asList(propertyKey));
    // THEN
    assertFalse("Constraint should have been dropped", Iterables.asSet(graphdbHelper.getPropertyUniquenessConstraints(labelName, propertyKey)).contains(index));
}
Also used : ConstraintDefinition(org.neo4j.graphdb.schema.ConstraintDefinition) NodeRepresentationTest(org.neo4j.server.rest.repr.NodeRepresentationTest) RelationshipRepresentationTest(org.neo4j.server.rest.repr.RelationshipRepresentationTest) Test(org.junit.Test)

Example 24 with ConstraintDefinition

use of org.neo4j.graphdb.schema.ConstraintDefinition in project neo4j by neo4j.

the class DatabaseActionsTest method dropNonExistentConstraint.

@Test
public void dropNonExistentConstraint() throws Exception {
    // GIVEN
    String labelName = "user", propertyKey = "login";
    ConstraintDefinition constraint = graphdbHelper.createPropertyUniquenessConstraint(labelName, asList(propertyKey));
    // EXPECT
    expectedException.expect(ConstraintViolationException.class);
    // WHEN
    try (Transaction tx = graph.beginTx()) {
        constraint.drop();
        constraint.drop();
    }
}
Also used : Transaction(org.neo4j.graphdb.Transaction) ConstraintDefinition(org.neo4j.graphdb.schema.ConstraintDefinition) NodeRepresentationTest(org.neo4j.server.rest.repr.NodeRepresentationTest) RelationshipRepresentationTest(org.neo4j.server.rest.repr.RelationshipRepresentationTest) Test(org.junit.Test)

Example 25 with ConstraintDefinition

use of org.neo4j.graphdb.schema.ConstraintDefinition in project neo4j by neo4j.

the class CypherResultSubGraph method from.

public static SubGraph from(Result result, GraphDatabaseService gds, boolean addBetween) {
    final CypherResultSubGraph graph = new CypherResultSubGraph();
    final List<String> columns = result.columns();
    for (Map<String, Object> row : loop(result)) {
        for (String column : columns) {
            final Object value = row.get(column);
            graph.addToGraph(value);
        }
    }
    for (IndexDefinition def : gds.schema().getIndexes()) {
        if (graph.getLabels().contains(def.getLabel())) {
            graph.addIndex(def);
        }
    }
    for (ConstraintDefinition def : gds.schema().getConstraints()) {
        if (graph.getLabels().contains(def.getLabel())) {
            graph.addConstraint(def);
        }
    }
    if (addBetween) {
        graph.addRelationshipsBetweenNodes();
    }
    return graph;
}
Also used : IndexDefinition(org.neo4j.graphdb.schema.IndexDefinition) ConstraintDefinition(org.neo4j.graphdb.schema.ConstraintDefinition)

Aggregations

ConstraintDefinition (org.neo4j.graphdb.schema.ConstraintDefinition)34 Test (org.junit.Test)16 Transaction (org.neo4j.graphdb.Transaction)12 NodePropertyExistenceConstraintDefinition (org.neo4j.kernel.impl.coreapi.schema.NodePropertyExistenceConstraintDefinition)5 RelationshipPropertyExistenceConstraintDefinition (org.neo4j.kernel.impl.coreapi.schema.RelationshipPropertyExistenceConstraintDefinition)5 UniquenessConstraintDefinition (org.neo4j.kernel.impl.coreapi.schema.UniquenessConstraintDefinition)5 NodeRepresentationTest (org.neo4j.server.rest.repr.NodeRepresentationTest)3 RelationshipRepresentationTest (org.neo4j.server.rest.repr.RelationshipRepresentationTest)3 ArrayList (java.util.ArrayList)2 ConstraintCreator (org.neo4j.graphdb.schema.ConstraintCreator)2 IndexDefinition (org.neo4j.graphdb.schema.IndexDefinition)2 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)2 BatchInserter (org.neo4j.unsafe.batchinsert.BatchInserter)2 ConstraintViolationException (org.neo4j.graphdb.ConstraintViolationException)1 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)1 Label (org.neo4j.graphdb.Label)1 RelationshipType (org.neo4j.graphdb.RelationshipType)1 Schema (org.neo4j.graphdb.schema.Schema)1 ConstraintDescriptor (org.neo4j.kernel.api.schema_new.constaints.ConstraintDescriptor)1 Documented (org.neo4j.kernel.impl.annotations.Documented)1