Search in sources :

Example 46 with ConstraintDefinition

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

the class SchemaWithPECAcceptanceTest method shouldListAddedConstraintsByRelationshipType.

@Test
public void shouldListAddedConstraintsByRelationshipType() throws Exception {
    // GIVEN
    ConstraintDefinition constraint1 = createRelationshipPropertyExistenceConstraint(Types.MY_TYPE, propertyKey);
    createRelationshipPropertyExistenceConstraint(Types.MY_OTHER_TYPE, propertyKey);
    // WHEN THEN
    assertThat(getConstraints(db, Types.MY_TYPE), containsOnly(constraint1));
}
Also used : UniquenessConstraintDefinition(org.neo4j.kernel.impl.coreapi.schema.UniquenessConstraintDefinition) RelationshipPropertyExistenceConstraintDefinition(org.neo4j.kernel.impl.coreapi.schema.RelationshipPropertyExistenceConstraintDefinition) ConstraintDefinition(org.neo4j.graphdb.schema.ConstraintDefinition) NodePropertyExistenceConstraintDefinition(org.neo4j.kernel.impl.coreapi.schema.NodePropertyExistenceConstraintDefinition) Test(org.junit.Test)

Example 47 with ConstraintDefinition

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

the class Neo4jMatchers method createConstraint.

public static ConstraintDefinition createConstraint(GraphDatabaseService db, Label label, String propertyKey) {
    try (Transaction tx = db.beginTx()) {
        ConstraintDefinition constraint = db.schema().constraintFor(label).assertPropertyIsUnique(propertyKey).create();
        tx.success();
        return constraint;
    }
}
Also used : Transaction(org.neo4j.graphdb.Transaction) ConstraintDefinition(org.neo4j.graphdb.schema.ConstraintDefinition)

Example 48 with ConstraintDefinition

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

the class BatchInsertTest method shouldCreateUniquenessConstraint.

@Test
public void shouldCreateUniquenessConstraint() throws Exception {
    // Given
    Label label = label("Person");
    String propertyKey = "name";
    String duplicatedValue = "Tom";
    BatchInserter inserter = newBatchInserter();
    // When
    inserter.createDeferredConstraint(label).assertPropertyIsUnique(propertyKey).create();
    // Then
    GraphDatabaseService db = switchToEmbeddedGraphDatabaseService(inserter);
    try {
        try (Transaction tx = db.beginTx()) {
            List<ConstraintDefinition> constraints = Iterables.asList(db.schema().getConstraints());
            assertEquals(1, constraints.size());
            ConstraintDefinition constraint = constraints.get(0);
            assertEquals(label.name(), constraint.getLabel().name());
            assertEquals(propertyKey, single(constraint.getPropertyKeys()));
            db.createNode(label).setProperty(propertyKey, duplicatedValue);
            tx.success();
        }
        try (Transaction tx = db.beginTx()) {
            db.createNode(label).setProperty(propertyKey, duplicatedValue);
            tx.success();
        }
        fail("Uniqueness property constraint was violated, exception expected");
    } catch (ConstraintViolationException e) {
        assertEquals(format("Node(0) already exists with label `%s` and property `%s` = '%s'", label.name(), propertyKey, duplicatedValue), e.getMessage());
    } finally {
        db.shutdown();
    }
}
Also used : BatchInserter(org.neo4j.unsafe.batchinsert.BatchInserter) GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Transaction(org.neo4j.graphdb.Transaction) Label(org.neo4j.graphdb.Label) ConstraintViolationException(org.neo4j.graphdb.ConstraintViolationException) ConstraintDefinition(org.neo4j.graphdb.schema.ConstraintDefinition) Test(org.junit.Test)

Example 49 with ConstraintDefinition

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

the class UniquenessRecoveryTest method shouldHaveUniquenessConstraintForNamePropertyOnPersonLabel.

// ASSERTIONS
private static void shouldHaveUniquenessConstraintForNamePropertyOnPersonLabel(GraphDatabaseService db) {
    try (Transaction tx = db.beginTx()) {
        ConstraintDefinition constraint = Iterables.single(db.schema().getConstraints());
        assertEquals(ConstraintType.UNIQUENESS, constraint.getConstraintType());
        assertEquals("Person", constraint.getLabel().name());
        assertEquals("name", Iterables.single(constraint.getPropertyKeys()));
        tx.success();
    }
}
Also used : Transaction(org.neo4j.graphdb.Transaction) ConstraintDefinition(org.neo4j.graphdb.schema.ConstraintDefinition)

Example 50 with ConstraintDefinition

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

the class DatabaseActions method dropRelationshipPropertyExistenceConstraint.

public boolean dropRelationshipPropertyExistenceConstraint(String typeName, Iterable<String> propertyKeys) {
    final Set<String> propertyKeysSet = Iterables.asSet(propertyKeys);
    ConstraintDefinition constraint = Iterables.singleOrNull(filteredRelationshipConstraints(typeName, relationshipPropertyExistenceFilter(propertyKeysSet)));
    if (constraint != null) {
        constraint.drop();
    }
    return constraint != null;
}
Also used : ConstraintDefinition(org.neo4j.graphdb.schema.ConstraintDefinition)

Aggregations

ConstraintDefinition (org.neo4j.graphdb.schema.ConstraintDefinition)70 Transaction (org.neo4j.graphdb.Transaction)26 Test (org.junit.Test)18 Test (org.junit.jupiter.api.Test)18 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)18 RepeatedTest (org.junit.jupiter.api.RepeatedTest)16 IndexDefinition (org.neo4j.graphdb.schema.IndexDefinition)14 ConstraintCreator (org.neo4j.graphdb.schema.ConstraintCreator)7 ArrayList (java.util.ArrayList)5 Schema (org.neo4j.graphdb.schema.Schema)5 NodePropertyExistenceConstraintDefinition (org.neo4j.kernel.impl.coreapi.schema.NodePropertyExistenceConstraintDefinition)5 RelationshipPropertyExistenceConstraintDefinition (org.neo4j.kernel.impl.coreapi.schema.RelationshipPropertyExistenceConstraintDefinition)5 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)4 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)4 UniquenessConstraintDefinition (org.neo4j.kernel.impl.coreapi.schema.UniquenessConstraintDefinition)4 Label (org.neo4j.graphdb.Label)3 NodeRepresentationTest (org.neo4j.server.rest.repr.NodeRepresentationTest)3 RelationshipRepresentationTest (org.neo4j.server.rest.repr.RelationshipRepresentationTest)3 AssertSchemaResult (apoc.result.AssertSchemaResult)2 MethodSource (org.junit.jupiter.params.provider.MethodSource)2