Search in sources :

Example 11 with SchemaWriteOperations

use of org.neo4j.kernel.api.SchemaWriteOperations in project neo4j by neo4j.

the class AbstractConstraintCreationIT method shouldNotCreateConstraintThatAlreadyExists.

@Test
public void shouldNotCreateConstraintThatAlreadyExists() throws Exception {
    // given
    {
        SchemaWriteOperations statement = schemaWriteOperationsInNewTransaction();
        createConstraint(statement, descriptor);
        commit();
    }
    // when
    try {
        SchemaWriteOperations statement = schemaWriteOperationsInNewTransaction();
        createConstraint(statement, descriptor);
        fail("Should not have validated");
    }// then
     catch (AlreadyConstrainedException e) {
    // good
    }
}
Also used : SchemaWriteOperations(org.neo4j.kernel.api.SchemaWriteOperations) AlreadyConstrainedException(org.neo4j.kernel.api.exceptions.schema.AlreadyConstrainedException) Test(org.junit.Test)

Example 12 with SchemaWriteOperations

use of org.neo4j.kernel.api.SchemaWriteOperations in project neo4j by neo4j.

the class AbstractConstraintCreationIT method shouldNotPersistConstraintCreatedInAbortedTransaction.

@Test
public void shouldNotPersistConstraintCreatedInAbortedTransaction() throws Exception {
    // given
    SchemaWriteOperations schemaWriteOperations = schemaWriteOperationsInNewTransaction();
    createConstraint(schemaWriteOperations, descriptor);
    // when
    rollback();
    ReadOperations readOperations = readOperationsInNewTransaction();
    // then
    Iterator<?> constraints = readOperations.constraintsGetAll();
    assertFalse("should not have any constraints", constraints.hasNext());
}
Also used : ReadOperations(org.neo4j.kernel.api.ReadOperations) SchemaWriteOperations(org.neo4j.kernel.api.SchemaWriteOperations) Test(org.junit.Test)

Example 13 with SchemaWriteOperations

use of org.neo4j.kernel.api.SchemaWriteOperations in project neo4j by neo4j.

the class NodePropertyExistenceConstraintCreationIT method shouldNotDropPropertyExistenceConstraintThatDoesNotExistWhenThereIsAUniquePropertyConstraint.

@Test
public void shouldNotDropPropertyExistenceConstraintThatDoesNotExistWhenThereIsAUniquePropertyConstraint() throws Exception {
    // given
    UniquenessConstraintDescriptor constraint;
    {
        SchemaWriteOperations statement = schemaWriteOperationsInNewTransaction();
        constraint = statement.uniquePropertyConstraintCreate(descriptor);
        commit();
    }
    // when
    try {
        SchemaWriteOperations statement = schemaWriteOperationsInNewTransaction();
        statement.constraintDrop(ConstraintDescriptorFactory.existsForSchema(constraint.schema()));
        fail("expected exception");
    }// then
     catch (DropConstraintFailureException e) {
        assertThat(e.getCause(), instanceOf(NoSuchConstraintException.class));
    } finally {
        rollback();
    }
    // then
    {
        ReadOperations statement = readOperationsInNewTransaction();
        Iterator<ConstraintDescriptor> constraints = statement.constraintsGetForSchema(descriptor);
        assertEquals(constraint, single(constraints));
    }
}
Also used : ReadOperations(org.neo4j.kernel.api.ReadOperations) SchemaWriteOperations(org.neo4j.kernel.api.SchemaWriteOperations) ResourceIterator(org.neo4j.graphdb.ResourceIterator) Iterator(java.util.Iterator) UniquenessConstraintDescriptor(org.neo4j.kernel.api.schema_new.constaints.UniquenessConstraintDescriptor) DropConstraintFailureException(org.neo4j.kernel.api.exceptions.schema.DropConstraintFailureException) Test(org.junit.Test)

Example 14 with SchemaWriteOperations

use of org.neo4j.kernel.api.SchemaWriteOperations in project neo4j by neo4j.

the class UniquenessConstraintCreationIT method shouldAbortConstraintCreationWhenDuplicatesExist.

@Test
public void shouldAbortConstraintCreationWhenDuplicatesExist() throws Exception {
    // given
    Statement statement = statementInNewTransaction(AnonymousContext.writeToken());
    // name is not unique for Foo in the existing data
    int foo = statement.tokenWriteOperations().labelGetOrCreateForName("Foo");
    int name = statement.tokenWriteOperations().propertyKeyGetOrCreateForName("name");
    long node1 = statement.dataWriteOperations().nodeCreate();
    statement.dataWriteOperations().nodeAddLabel(node1, foo);
    statement.dataWriteOperations().nodeSetProperty(node1, Property.stringProperty(name, "foo"));
    long node2 = statement.dataWriteOperations().nodeCreate();
    statement.dataWriteOperations().nodeAddLabel(node2, foo);
    statement.dataWriteOperations().nodeSetProperty(node2, Property.stringProperty(name, "foo"));
    commit();
    // when
    LabelSchemaDescriptor descriptor = SchemaDescriptorFactory.forLabel(foo, name);
    try {
        SchemaWriteOperations schemaWriteOperations = schemaWriteOperationsInNewTransaction();
        schemaWriteOperations.uniquePropertyConstraintCreate(descriptor);
        fail("expected exception");
    }// then
     catch (CreateConstraintFailureException ex) {
        assertEquals(ConstraintDescriptorFactory.uniqueForSchema(descriptor), ex.constraint());
        Throwable cause = ex.getCause();
        assertThat(cause, instanceOf(ConstraintValidationException.class));
        String expectedMessage = String.format("Both Node(%d) and Node(%d) have the label `Foo` and property `name` = 'foo'", node1, node2);
        String actualMessage = userMessage((ConstraintValidationException) cause);
        assertEquals(expectedMessage, actualMessage);
    }
}
Also used : SchemaWriteOperations(org.neo4j.kernel.api.SchemaWriteOperations) Statement(org.neo4j.kernel.api.Statement) ConstraintValidationException(org.neo4j.kernel.api.exceptions.schema.ConstraintValidationException) LabelSchemaDescriptor(org.neo4j.kernel.api.schema_new.LabelSchemaDescriptor) CreateConstraintFailureException(org.neo4j.kernel.api.exceptions.schema.CreateConstraintFailureException) Test(org.junit.Test)

Example 15 with SchemaWriteOperations

use of org.neo4j.kernel.api.SchemaWriteOperations in project neo4j by neo4j.

the class UniquenessConstraintCreationIT method shouldNotDropUniquePropertyConstraintThatDoesNotExistWhenThereIsAPropertyExistenceConstraint.

@Test
public void shouldNotDropUniquePropertyConstraintThatDoesNotExistWhenThereIsAPropertyExistenceConstraint() throws Exception {
    // given
    SchemaWriteOperations schemaWriteOperations = schemaWriteOperationsInNewTransaction();
    schemaWriteOperations.nodePropertyExistenceConstraintCreate(descriptor);
    commit();
    // when
    try {
        SchemaWriteOperations statement = schemaWriteOperationsInNewTransaction();
        statement.constraintDrop(ConstraintDescriptorFactory.uniqueForSchema(descriptor));
        fail("expected exception");
    }// then
     catch (DropConstraintFailureException e) {
        assertThat(e.getCause(), instanceOf(NoSuchConstraintException.class));
    } finally {
        rollback();
    }
    // then
    {
        ReadOperations statement = readOperationsInNewTransaction();
        Iterator<ConstraintDescriptor> constraints = statement.constraintsGetForSchema(descriptor);
        assertEquals(ConstraintDescriptorFactory.existsForSchema(descriptor), single(constraints));
    }
}
Also used : ReadOperations(org.neo4j.kernel.api.ReadOperations) SchemaWriteOperations(org.neo4j.kernel.api.SchemaWriteOperations) ResourceIterator(org.neo4j.graphdb.ResourceIterator) Iterator(java.util.Iterator) DropConstraintFailureException(org.neo4j.kernel.api.exceptions.schema.DropConstraintFailureException) Test(org.junit.Test)

Aggregations

SchemaWriteOperations (org.neo4j.kernel.api.SchemaWriteOperations)20 Test (org.junit.Test)19 ReadOperations (org.neo4j.kernel.api.ReadOperations)9 KernelIntegrationTest (org.neo4j.kernel.impl.api.integrationtest.KernelIntegrationTest)8 NewIndexDescriptor (org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor)5 Statement (org.neo4j.kernel.api.Statement)4 Iterator (java.util.Iterator)2 ResourceIterator (org.neo4j.graphdb.ResourceIterator)2 DropConstraintFailureException (org.neo4j.kernel.api.exceptions.schema.DropConstraintFailureException)2 SchemaKernelException (org.neo4j.kernel.api.exceptions.schema.SchemaKernelException)2 UniquenessConstraintDescriptor (org.neo4j.kernel.api.schema_new.constaints.UniquenessConstraintDescriptor)2 ArrayList (java.util.ArrayList)1 Node (org.neo4j.graphdb.Node)1 QueryExecutionException (org.neo4j.graphdb.QueryExecutionException)1 Transaction (org.neo4j.graphdb.Transaction)1 TokenWriteOperations (org.neo4j.kernel.api.TokenWriteOperations)1 ProcedureException (org.neo4j.kernel.api.exceptions.ProcedureException)1 AlreadyConstrainedException (org.neo4j.kernel.api.exceptions.schema.AlreadyConstrainedException)1 ConstraintValidationException (org.neo4j.kernel.api.exceptions.schema.ConstraintValidationException)1 CreateConstraintFailureException (org.neo4j.kernel.api.exceptions.schema.CreateConstraintFailureException)1