@Test public void shouldCreateNodePropertyExistenceConstraint() throws Exception { ConstraintDescriptor constraint; try ( Transaction transaction = beginTransaction() ) { constraint = transaction.schemaWrite().nodePropertyExistenceConstraintCreate( labelDescriptor( label, prop1 ) ); transaction.success(); } try ( Transaction transaction = beginTransaction() ) { SchemaRead schemaRead = transaction.schemaRead(); assertTrue( schemaRead.constraintExists( constraint ) ); Iterator<ConstraintDescriptor> constraints = schemaRead.constraintsGetForLabel( label ); assertThat( asList( constraints ), equalTo( singletonList( constraint ) ) ); } }
int labelId = tokenWrite.labelGetOrCreateForName( label.name() ); int[] propertyKeyIds = getOrCreatePropertyKeyIds( tokenWrite, propertyKeys ); transaction.schemaWrite().nodePropertyExistenceConstraintCreate( forLabel( labelId, propertyKeyIds ) ); return new NodePropertyExistenceConstraintDefinition( this, label, propertyKeys );
@Test public void shouldNotSeeDroppedNodePropertyExistenceConstraintFromTransaction() throws Exception { ConstraintDescriptor existing; try ( Transaction transaction = beginTransaction() ) { existing = transaction.schemaWrite().nodePropertyExistenceConstraintCreate( labelDescriptor( label, prop1 ) ); transaction.success(); } try ( Transaction transaction = beginTransaction() ) { transaction.schemaWrite().constraintDrop( existing ); SchemaRead schemaRead = transaction.schemaRead(); assertFalse( schemaRead.constraintExists( existing ) ); assertThat( schemaRead.index( label, prop2 ), equalTo( NO_INDEX ) ); assertThat( asList( schemaRead.constraintsGetForLabel( label ) ), empty() ); } }
@Test public void shouldSeeNodePropertyExistenceConstraintFromTransaction() throws Exception { ConstraintDescriptor existing; try ( Transaction transaction = beginTransaction() ) { existing = transaction.schemaWrite().nodePropertyExistenceConstraintCreate( labelDescriptor( label, prop1 ) ); transaction.success(); } try ( Transaction transaction = beginTransaction() ) { ConstraintDescriptor newConstraint = transaction.schemaWrite().nodePropertyExistenceConstraintCreate( labelDescriptor( label, prop2 ) ); SchemaRead schemaRead = transaction.schemaRead(); assertTrue( schemaRead.constraintExists( existing ) ); assertTrue( schemaRead.constraintExists( newConstraint ) ); assertThat( asList( schemaRead.constraintsGetForLabel( label ) ), containsInAnyOrder( existing, newConstraint ) ); } }
@Test public void shouldDropNodePropertyExistenceConstraint() throws Exception { ConstraintDescriptor constraint; try ( Transaction transaction = beginTransaction() ) { constraint = transaction.schemaWrite().nodePropertyExistenceConstraintCreate( labelDescriptor( label, prop1 ) ); transaction.success(); } try ( Transaction transaction = beginTransaction() ) { transaction.schemaWrite().constraintDrop( constraint ); transaction.success(); } try ( Transaction transaction = beginTransaction() ) { SchemaRead schemaRead = transaction.schemaRead(); assertFalse( schemaRead.constraintExists( constraint ) ); assertThat( asList( schemaRead.constraintsGetForLabel( label ) ), empty() ); } }
@Test public void shouldListConstraintsByLabel() throws Exception { int wrongLabel; ConstraintDescriptor inStore; ConstraintDescriptor droppedInTx; ConstraintDescriptor createdInTx; try ( Transaction tx = beginTransaction() ) { wrongLabel = tx.tokenWrite().labelGetOrCreateForName( "wrongLabel" ); tx.schemaWrite().uniquePropertyConstraintCreate( labelDescriptor( wrongLabel, prop1 ) ); inStore = tx.schemaWrite().uniquePropertyConstraintCreate( labelDescriptor( label, prop1 ) ); droppedInTx = tx.schemaWrite().uniquePropertyConstraintCreate( labelDescriptor( label, prop2 ) ); tx.success(); } try ( Transaction tx = beginTransaction() ) { createdInTx = tx.schemaWrite().nodePropertyExistenceConstraintCreate( labelDescriptor( label, prop1 ) ); tx.schemaWrite().nodePropertyExistenceConstraintCreate( labelDescriptor( wrongLabel, prop1 ) ); tx.schemaWrite().constraintDrop( droppedInTx ); Iterable<ConstraintDescriptor> allConstraints = () -> tx.schemaRead().constraintsGetForLabel( label ); assertThat( allConstraints, containsInAnyOrder( inStore, createdInTx ) ); tx.success(); } }
@Test public void shouldFailToCreatePropertyExistenceConstraintIfConstraintNotSatisfied() throws Exception { //Given try ( Transaction transaction = beginTransaction() ) { Write write = transaction.dataWrite(); long node = write.nodeCreate(); write.nodeAddLabel( node, label ); transaction.success(); } //Expect exception.expect( SchemaKernelException.class ); //When try ( Transaction transaction = beginTransaction() ) { transaction.schemaWrite().nodePropertyExistenceConstraintCreate( labelDescriptor( label, prop1 ) ); } }
@Test public void shouldListAllConstraints() throws Exception { ConstraintDescriptor toRetain; ConstraintDescriptor toRetain2; ConstraintDescriptor toDrop; ConstraintDescriptor created; try ( Transaction tx = beginTransaction() ) { toRetain = tx.schemaWrite().uniquePropertyConstraintCreate( labelDescriptor( label, prop1 ) ); toRetain2 = tx.schemaWrite().uniquePropertyConstraintCreate( labelDescriptor( label2, prop1 ) ); toDrop = tx.schemaWrite().uniquePropertyConstraintCreate( labelDescriptor( label, prop2 ) ); tx.success(); } try ( Transaction tx = beginTransaction() ) { created = tx.schemaWrite().nodePropertyExistenceConstraintCreate( labelDescriptor( label, prop1 ) ); tx.schemaWrite().constraintDrop( toDrop ); Iterable<ConstraintDescriptor> allConstraints = () -> tx.schemaRead().constraintsGetAll(); assertThat( allConstraints, containsInAnyOrder( toRetain, toRetain2, created ) ); tx.success(); } }
int labelId = tokenWrite.labelGetOrCreateForName( label.name() ); int[] propertyKeyIds = getOrCreatePropertyKeyIds( tokenWrite, propertyKeys ); transaction.schemaWrite().nodePropertyExistenceConstraintCreate( forLabel( labelId, propertyKeyIds ) ); return new NodePropertyExistenceConstraintDefinition( this, label, propertyKeys );