Search in sources :

Example 36 with ConstraintDefinition

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

the class SchemaAcceptanceTest method constraintNamesInTransactionStateMustBeUniqueEvenWhenGenerated.

@Test
void constraintNamesInTransactionStateMustBeUniqueEvenWhenGenerated() {
    try (Transaction tx = db.beginTx()) {
        ConstraintDefinition constraint = tx.schema().constraintFor(label).assertPropertyIsUnique(propertyKey).create();
        ConstraintCreator creator = tx.schema().constraintFor(otherLabel).assertPropertyIsUnique(secondPropertyKey).withName(constraint.getName());
        ConstraintViolationException exception = assertThrows(ConstraintViolationException.class, creator::create);
        assertThat(exception).hasMessageContaining(thereAlreadyExistsConstraintMessage(constraint.getName()));
        tx.commit();
    }
}
Also used : ConstraintCreator(org.neo4j.graphdb.schema.ConstraintCreator) ConstraintDefinition(org.neo4j.graphdb.schema.ConstraintDefinition) RepeatedTest(org.junit.jupiter.api.RepeatedTest) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 37 with ConstraintDefinition

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

the class SchemaAcceptanceTest method constraintNamesCanContainBackTicks.

@Test
void constraintNamesCanContainBackTicks() {
    try (Transaction tx = db.beginTx()) {
        ConstraintCreator creator = tx.schema().constraintFor(label).withName("`a`b``").assertPropertyIsUnique(propertyKey);
        creator.create();
        tx.commit();
    }
    try (Transaction tx = db.beginTx()) {
        final Iterable<IndexDefinition> indexes = tx.schema().getIndexes();
        assertThat(count(indexes)).isEqualTo(1L);
        assertEquals("`a`b``", indexes.iterator().next().getName());
        final Iterable<ConstraintDefinition> constraints = tx.schema().getConstraints();
        assertThat(count(constraints)).isEqualTo(1L);
        assertEquals("`a`b``", constraints.iterator().next().getName());
        tx.commit();
    }
}
Also used : IndexDefinition(org.neo4j.graphdb.schema.IndexDefinition) ConstraintCreator(org.neo4j.graphdb.schema.ConstraintCreator) ConstraintDefinition(org.neo4j.graphdb.schema.ConstraintDefinition) RepeatedTest(org.junit.jupiter.api.RepeatedTest) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 38 with ConstraintDefinition

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

the class SchemaAcceptanceTest method shouldDropUniquenessConstraint.

@Test
void shouldDropUniquenessConstraint() {
    // GIVEN
    ConstraintDefinition constraint = createUniquenessConstraint(label, propertyKey);
    // WHEN
    dropConstraint(db, constraint);
    // THEN
    try (Transaction tx = db.beginTx()) {
        assertThat(getConstraints(tx, label)).isEmpty();
    }
}
Also used : ConstraintDefinition(org.neo4j.graphdb.schema.ConstraintDefinition) RepeatedTest(org.junit.jupiter.api.RepeatedTest) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 39 with ConstraintDefinition

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

the class SchemaAcceptanceTest method shouldCreateNamedUniquenessConstraint.

@Test
void shouldCreateNamedUniquenessConstraint() {
    // When
    ConstraintDefinition constraint = createUniquenessConstraint("MyConstraint", label, propertyKey);
    // Then
    try (Transaction tx = db.beginTx()) {
        constraint = tx.schema().getConstraintByName(constraint.getName());
        assertEquals(ConstraintType.UNIQUENESS, constraint.getConstraintType());
        assertEquals(label.name(), constraint.getLabel().name());
        assertEquals(asSet(propertyKey), Iterables.asSet(constraint.getPropertyKeys()));
        assertEquals("MyConstraint", constraint.getName());
        tx.commit();
    }
}
Also used : ConstraintDefinition(org.neo4j.graphdb.schema.ConstraintDefinition) RepeatedTest(org.junit.jupiter.api.RepeatedTest) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 40 with ConstraintDefinition

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

the class SchemaAcceptanceTest method constraintNamesInTransactionStateMustBeUniqueEvenWhenGenerated2.

@Test
void constraintNamesInTransactionStateMustBeUniqueEvenWhenGenerated2() {
    try (Transaction tx = db.beginTx()) {
        ConstraintDefinition constraint = tx.schema().constraintFor(otherLabel).assertPropertyIsUnique(secondPropertyKey).withName("constraint_c8a3b28f").create();
        ConstraintCreator creator = tx.schema().constraintFor(label).assertPropertyIsUnique(propertyKey);
        ConstraintViolationException exception = assertThrows(ConstraintViolationException.class, creator::create);
        assertThat(exception).hasMessageContaining(thereAlreadyExistsConstraintMessage(constraint.getName()));
        tx.commit();
    }
}
Also used : ConstraintCreator(org.neo4j.graphdb.schema.ConstraintCreator) ConstraintDefinition(org.neo4j.graphdb.schema.ConstraintDefinition) RepeatedTest(org.junit.jupiter.api.RepeatedTest) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

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