Search in sources :

Example 66 with ConstraintDefinition

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

the class SchemaAcceptanceTest method shouldListAddedConstraintsByLabel.

@Test
void shouldListAddedConstraintsByLabel() {
    // GIVEN
    ConstraintDefinition constraint1 = createUniquenessConstraint(label, propertyKey);
    createUniquenessConstraint(otherLabel, propertyKey);
    // WHEN THEN
    try (Transaction tx = db.beginTx()) {
        assertThat(getConstraints(tx, label)).containsOnly(constraint1);
    }
}
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 67 with ConstraintDefinition

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

the class SchemaAcceptanceTest method mustBeAbleToCreateUniquenessConstraintWithBtreeIndexType.

@Test
void mustBeAbleToCreateUniquenessConstraintWithBtreeIndexType() {
    String name;
    try (Transaction tx = db.beginTx()) {
        ConstraintDefinition constraint = tx.schema().constraintFor(label).assertPropertyIsUnique(propertyKey).withIndexType(BTREE).create();
        name = constraint.getName();
        IndexDefinition index = getIndex(tx, name);
        assertThat(index.getIndexType()).isEqualTo(BTREE);
        tx.commit();
    }
    try (Transaction tx = db.beginTx()) {
        IndexDefinition index = getIndex(tx, name);
        assertThat(index.getIndexType()).isEqualTo(BTREE);
    }
}
Also used : IndexDefinition(org.neo4j.graphdb.schema.IndexDefinition) 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 68 with ConstraintDefinition

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

the class SchemaAcceptanceTest method mustBeAbleToSpecifyIndexConfigurationForUniquenessConstraint.

@Test
void mustBeAbleToSpecifyIndexConfigurationForUniquenessConstraint() {
    try (Transaction tx = db.beginTx()) {
        ConstraintDefinition constraint = tx.schema().constraintFor(label).withName("my constraint").assertPropertyIsUnique(propertyKey).withIndexConfiguration(Map.of(IndexSettingImpl.SPATIAL_CARTESIAN_MAX, new double[] { 200.0, 200.0 }, IndexSettingImpl.SPATIAL_WGS84_MIN, new double[] { -90.0, -90.0 })).create();
        IndexDefinition index = getIndex(tx, constraint.getName());
        Map<IndexSetting, Object> config = index.getIndexConfiguration();
        assertArrayEquals(new double[] { 200.0, 200.0 }, (double[]) config.get(IndexSettingImpl.SPATIAL_CARTESIAN_MAX));
        assertArrayEquals(new double[] { -90.0, -90.0 }, (double[]) config.get(IndexSettingImpl.SPATIAL_WGS84_MIN));
        tx.commit();
    }
    try (Transaction tx = db.beginTx()) {
        IndexDefinition index = getIndex(tx, "my constraint");
        Map<IndexSetting, Object> config = index.getIndexConfiguration();
        assertArrayEquals(new double[] { 200.0, 200.0 }, (double[]) config.get(IndexSettingImpl.SPATIAL_CARTESIAN_MAX));
        assertArrayEquals(new double[] { -90.0, -90.0 }, (double[]) config.get(IndexSettingImpl.SPATIAL_WGS84_MIN));
        tx.commit();
    }
}
Also used : IndexDefinition(org.neo4j.graphdb.schema.IndexDefinition) ConstraintDefinition(org.neo4j.graphdb.schema.ConstraintDefinition) IndexSetting(org.neo4j.graphdb.schema.IndexSetting) RepeatedTest(org.junit.jupiter.api.RepeatedTest) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 69 with ConstraintDefinition

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

the class SchemaAcceptanceTest method createAndDropConstraintInSameTx.

@Test
void createAndDropConstraintInSameTx() {
    // When
    try (Transaction tx = db.beginTx()) {
        ConstraintDefinition constraint = tx.schema().constraintFor(label).assertPropertyIsUnique(propertyKey).withName(nameA).create();
        constraint.drop();
        tx.commit();
    }
    // Then
    try (Transaction tx = db.beginTx()) {
        assertThat(count(tx.schema().getConstraints())).isEqualTo(0);
        assertThat(count(tx.schema().getIndexes())).isEqualTo(0);
        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 70 with ConstraintDefinition

use of org.neo4j.graphdb.schema.ConstraintDefinition in project neo4j-nlp by graphaware.

the class SchemaProcedureTest method schemaAssert.

private void schemaAssert(Label label, String s, String type) {
    boolean exist = false;
    try (Transaction tx = getDatabase().beginTx()) {
        if (type.equals(UNIQUE)) {
            for (ConstraintDefinition constraintDefinition : getDatabase().schema().getConstraints(label)) {
                for (String p : constraintDefinition.getPropertyKeys()) {
                    if (p.equals(s)) {
                        exist = true;
                    }
                }
            }
        } else {
            for (IndexDefinition indexDefinition : getDatabase().schema().getIndexes()) {
                for (String p : indexDefinition.getPropertyKeys()) {
                    if (p.equals(s)) {
                        exist = true;
                    }
                }
            }
        }
        tx.success();
    }
    assertTrue(exist);
}
Also used : Transaction(org.neo4j.graphdb.Transaction) IndexDefinition(org.neo4j.graphdb.schema.IndexDefinition) 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