Search in sources :

Example 16 with ConstraintViolationException

use of org.neo4j.graphdb.ConstraintViolationException in project neo4j by neo4j.

the class BatchInsertTest method shouldNotAllowDuplicatedIndexes.

@Test
public void shouldNotAllowDuplicatedIndexes() throws Exception {
    // Given
    Label label = label("Person3-" + denseNodeThreshold);
    String property = "name";
    BatchInserter inserter = globalInserter;
    // When
    inserter.createDeferredSchemaIndex(label).on(property).create();
    try {
        inserter.createDeferredSchemaIndex(label).on(property).create();
        fail("Exception expected");
    } catch (ConstraintViolationException e) {
        // Then
        assertEquals("Index for given {label;property} already exists", e.getMessage());
    }
}
Also used : BatchInserter(org.neo4j.unsafe.batchinsert.BatchInserter) Label(org.neo4j.graphdb.Label) ConstraintViolationException(org.neo4j.graphdb.ConstraintViolationException) Test(org.junit.Test)

Example 17 with ConstraintViolationException

use of org.neo4j.graphdb.ConstraintViolationException in project neo4j by neo4j.

the class BatchInsertTest method shouldNotAllowCreationOfDuplicateConstraint.

@Test
public void shouldNotAllowCreationOfDuplicateConstraint() throws Exception {
    // GIVEN
    BatchInserter inserter = globalInserter;
    String labelName = "Hacker2-" + denseNodeThreshold;
    // WHEN
    inserter.createDeferredConstraint(label(labelName)).assertPropertyIsUnique("handle").create();
    try {
        inserter.createDeferredConstraint(label(labelName)).assertPropertyIsUnique("handle").create();
        fail("Should have thrown exception.");
    } catch (ConstraintViolationException e) {
    // THEN Good
    }
}
Also used : BatchInserter(org.neo4j.unsafe.batchinsert.BatchInserter) ConstraintViolationException(org.neo4j.graphdb.ConstraintViolationException) Test(org.junit.Test)

Example 18 with ConstraintViolationException

use of org.neo4j.graphdb.ConstraintViolationException 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 19 with ConstraintViolationException

use of org.neo4j.graphdb.ConstraintViolationException in project neo4j by neo4j.

the class BatchInsertTest method shouldNotAllowCreationOfUniquenessConstraintAndIndexOnSameLabelAndProperty.

@Test
public void shouldNotAllowCreationOfUniquenessConstraintAndIndexOnSameLabelAndProperty() throws Exception {
    // Given
    Label label = label("Person1-" + denseNodeThreshold);
    String property = "name";
    BatchInserter inserter = globalInserter;
    // When
    inserter.createDeferredConstraint(label).assertPropertyIsUnique(property).create();
    try {
        inserter.createDeferredSchemaIndex(label).on(property).create();
        fail("Exception expected");
    } catch (ConstraintViolationException e) {
        // Then
        assertEquals("Index for given {label;property} already exists", e.getMessage());
    }
}
Also used : BatchInserter(org.neo4j.unsafe.batchinsert.BatchInserter) Label(org.neo4j.graphdb.Label) ConstraintViolationException(org.neo4j.graphdb.ConstraintViolationException) Test(org.junit.Test)

Example 20 with ConstraintViolationException

use of org.neo4j.graphdb.ConstraintViolationException in project neo4j by neo4j.

the class BatchInsertTest method shouldNotAllowCreationOfDeferredSchemaConstraintAfterIndexOnSameKeys.

@Test
public void shouldNotAllowCreationOfDeferredSchemaConstraintAfterIndexOnSameKeys() throws Exception {
    // GIVEN
    BatchInserter inserter = globalInserter;
    String labelName = "Hacker3-" + denseNodeThreshold;
    // WHEN
    inserter.createDeferredSchemaIndex(label(labelName)).on("handle").create();
    try {
        inserter.createDeferredConstraint(label(labelName)).assertPropertyIsUnique("handle").create();
        fail("Should have thrown exception.");
    } catch (ConstraintViolationException e) {
    // THEN Good
    }
}
Also used : BatchInserter(org.neo4j.unsafe.batchinsert.BatchInserter) ConstraintViolationException(org.neo4j.graphdb.ConstraintViolationException) Test(org.junit.Test)

Aggregations

ConstraintViolationException (org.neo4j.graphdb.ConstraintViolationException)21 Test (org.junit.Test)14 BatchInserter (org.neo4j.unsafe.batchinsert.BatchInserter)8 Transaction (org.neo4j.graphdb.Transaction)6 Label (org.neo4j.graphdb.Label)4 File (java.io.File)3 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)3 Node (org.neo4j.graphdb.Node)3 NotFoundException (org.neo4j.graphdb.NotFoundException)3 Statement (org.neo4j.kernel.api.Statement)3 EntityNotFoundException (org.neo4j.kernel.api.exceptions.EntityNotFoundException)3 InvalidTransactionTypeKernelException (org.neo4j.kernel.api.exceptions.InvalidTransactionTypeKernelException)3 PropertyNotFoundException (org.neo4j.kernel.api.exceptions.PropertyNotFoundException)2 IllegalTokenNameException (org.neo4j.kernel.api.exceptions.schema.IllegalTokenNameException)2 HighlyAvailableGraphDatabase (org.neo4j.kernel.ha.HighlyAvailableGraphDatabase)2 IOException (java.io.IOException)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 ComException (org.neo4j.com.ComException)1 TransactionFailureException (org.neo4j.graphdb.TransactionFailureException)1 TransientTransactionFailureException (org.neo4j.graphdb.TransientTransactionFailureException)1