@Test( expected = SchemaKernelException.class ) public void shouldFailUniquenessConstraintCreateForRepeatedProperties() throws Exception { try ( Transaction tx = beginTransaction() ) { tx.schemaWrite().uniquePropertyConstraintCreate( labelDescriptor( label, prop1, prop1 ) ); } }
private ConstraintDescriptor createConstraint() throws KernelException { try ( Transaction transaction = kernel.beginTransaction( implicit, AUTH_DISABLED ) ) { ConstraintDescriptor descriptor = transaction.schemaWrite().uniquePropertyConstraintCreate( SchemaDescriptorFactory.forLabel( 1, 1 ) ); transaction.success(); return descriptor; } }
@Test public void shouldListAll() throws Exception { // given SchemaWrite schemaWrite = schemaWriteInNewTransaction(); IndexReference index1 = schemaWrite.indexCreate( descriptor ); IndexReference index2 = ((IndexBackedConstraintDescriptor) schemaWrite.uniquePropertyConstraintCreate( descriptor2 )) .ownedIndexDescriptor(); commit(); // then/when SchemaRead schemaRead = newTransaction().schemaRead(); List<IndexReference> indexes = Iterators.asList( schemaRead.indexesGetAll() ); assertThat( indexes, containsInAnyOrder( index1, index2 ) ); commit(); }
@Test public void shouldCreateUniquePropertyConstraint() throws Exception { ConstraintDescriptor constraint; try ( Transaction transaction = beginTransaction() ) { constraint = transaction.schemaWrite().uniquePropertyConstraintCreate( 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 ) ) ); } }
@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(); } }
@Test public void shouldSeeUniqueConstraintFromTransaction() throws Exception { ConstraintDescriptor existing; try ( Transaction transaction = beginTransaction() ) { existing = transaction.schemaWrite().uniquePropertyConstraintCreate( labelDescriptor( label, prop1 ) ); transaction.success(); } try ( Transaction transaction = beginTransaction() ) { ConstraintDescriptor newConstraint = transaction.schemaWrite().uniquePropertyConstraintCreate( labelDescriptor( label, prop2 ) ); SchemaRead schemaRead = transaction.schemaRead(); assertTrue( schemaRead.constraintExists( existing ) ); assertTrue( schemaRead.constraintExists( newConstraint ) ); assertThat( asList( schemaRead.constraintsGetForLabel( label ) ), containsInAnyOrder( existing, newConstraint ) ); } }
private void createConstraint( String label, String propertyKey ) throws KernelException { int labelId; int propertyKeyId; TokenWrite tokenWrite = tokenWriteInNewTransaction(); labelId = tokenWrite.labelGetOrCreateForName( label ); propertyKeyId = tokenWrite.propertyKeyGetOrCreateForName( propertyKey ); commit(); SchemaWrite schemaWrite = schemaWriteInNewTransaction(); schemaWrite.uniquePropertyConstraintCreate( forLabel( labelId, propertyKeyId ) ); commit(); } }
private IndexReference createUniquenessConstraint( int labelId, int... propertyIds ) throws Exception { Transaction transaction = newTransaction( LoginContext.AUTH_DISABLED ); LabelSchemaDescriptor descriptor = SchemaDescriptorFactory.forLabel( labelId, propertyIds ); transaction.schemaWrite().uniquePropertyConstraintCreate( descriptor ); IndexReference result = transaction.schemaRead().index( descriptor.getLabelId(), descriptor.getPropertyIds() ); commit(); return result; } }
@Test public void shouldNotSeeDroppedUniqueConstraintFromTransaction() throws Exception { ConstraintDescriptor existing; try ( Transaction transaction = beginTransaction() ) { existing = transaction.schemaWrite().uniquePropertyConstraintCreate( labelDescriptor( label, prop1 ) ); transaction.success(); } try ( Transaction transaction = beginTransaction() ) { transaction.schemaWrite().constraintDrop( existing ); SchemaRead schemaRead = transaction.schemaRead(); assertFalse( schemaRead.constraintExists( existing ) ); 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(); } }
@Before public void setup() throws Exception { graphDatabaseAPI = dbRule.getGraphDatabaseAPI(); kernel = graphDatabaseAPI.getDependencyResolver().resolveDependency( Kernel.class ); newTransaction(); transaction.schemaWrite().uniquePropertyConstraintCreate( forLabel( label, propertyIds() ) ); commit(); }
@Test public void shouldDropUniquePropertyConstraint() throws Exception { ConstraintDescriptor constraint; try ( Transaction transaction = beginTransaction() ) { constraint = transaction.schemaWrite().uniquePropertyConstraintCreate( 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 shouldNotAllowCreationOfConstraintsWhenInHA() throws Exception { //noinspection deprecation GraphDatabaseAPI db = new FakeHaDatabase(); ThreadToStatementContextBridge stmtBridge = db.getDependencyResolver().resolveDependency( ThreadToStatementContextBridge.class ); try ( Transaction ignored = db.beginTx() ) { KernelTransaction ktx = stmtBridge.getKernelTransactionBoundToThisThread( true ); try { ktx.schemaWrite().uniquePropertyConstraintCreate( forLabel( 1, 1 ) ); fail( "expected exception here" ); } catch ( InvalidTransactionTypeKernelException e ) { assertThat( e.getMessage(), containsString( "HA" ) ); } } db.shutdown(); }
@Test public void shouldFailToCreateUniqueConstraintIfExistingIndex() throws Exception { //Given try ( Transaction transaction = beginTransaction() ) { transaction.schemaWrite().indexCreate( labelDescriptor( label, prop1 ) ); transaction.success(); } //Expect exception.expect( SchemaKernelException.class ); //When try ( Transaction transaction = beginTransaction() ) { transaction.schemaWrite().uniquePropertyConstraintCreate( labelDescriptor( label, prop1 ) ); transaction.success(); } }
@Test public void shouldFailToCreateIndexIfExistingUniqueConstraint() throws Exception { //Given try ( Transaction transaction = beginTransaction() ) { transaction.schemaWrite().uniquePropertyConstraintCreate( labelDescriptor( label, prop1 ) ); transaction.success(); } //Expect exception.expect( SchemaKernelException.class ); //When try ( Transaction transaction = beginTransaction() ) { transaction.schemaWrite().indexCreate( labelDescriptor( label, prop1 ) ); transaction.success(); } }
@Test public void shouldFailToCreateIndexWhereAConstraintAlreadyExists() throws Exception { // given { SchemaWrite statement = schemaWriteInNewTransaction(); statement.uniquePropertyConstraintCreate( descriptor ); commit(); } // when try { SchemaWrite statement = schemaWriteInNewTransaction(); statement.indexCreate( descriptor ); commit(); fail( "expected exception" ); } // then catch ( SchemaKernelException e ) { assertEquals( "There is a uniqueness constraint on :" + LABEL + "(" + PROPERTY_KEY + "), so an index is " + "already created that matches this.", e.getMessage() ); } commit(); }
@Before public void setup() throws Exception { graphDatabaseAPI = dbRule.getGraphDatabaseAPI(); try ( Transaction tx = graphDatabaseAPI.beginTx() ) { KernelTransaction ktx = ktx(); if ( index.type() == UNIQUE ) { ktx.schemaWrite().uniquePropertyConstraintCreate( index.schema() ); } else { ktx.schemaWrite().indexCreate( index.schema() ); } tx.success(); } try ( Transaction ignore = graphDatabaseAPI.beginTx() ) { KernelTransaction ktx = ktx(); while ( ktx.schemaRead().indexGetState( index ) != InternalIndexState.ONLINE ) { Thread.sleep( 10 ); } // Will break loop on test timeout } }
@Test public void shouldFailToDropIndexIfExistingUniqueConstraint() throws Exception { //Given try ( Transaction transaction = beginTransaction() ) { transaction.schemaWrite().uniquePropertyConstraintCreate( labelDescriptor( label, prop1 ) ); transaction.success(); } //Expect exception.expect( SchemaKernelException.class ); //When try ( Transaction transaction = beginTransaction() ) { IndexReference index = transaction.schemaRead().index( label, prop1 ); transaction.schemaWrite().indexDrop( index ); transaction.success(); } }
@Test public void shouldListIndexesByLabel() throws Exception { int wrongLabel; IndexReference inStore; IndexReference droppedInTx; IndexReference createdInTx; try ( Transaction tx = beginTransaction() ) { wrongLabel = tx.tokenWrite().labelGetOrCreateForName( "wrongLabel" ); tx.schemaWrite().uniquePropertyConstraintCreate( labelDescriptor( wrongLabel, prop1 ) ); inStore = tx.schemaWrite().indexCreate( labelDescriptor( label, prop1 ) ); droppedInTx = tx.schemaWrite().indexCreate( labelDescriptor( label, prop2 ) ); tx.success(); } try ( Transaction tx = beginTransaction() ) { createdInTx = tx.schemaWrite().indexCreate( labelDescriptor( label, prop3 ) ); tx.schemaWrite().indexCreate( labelDescriptor( wrongLabel, prop2 ) ); tx.schemaWrite().indexDrop( droppedInTx ); Iterable<IndexReference> indexes = () -> tx.schemaRead().indexesGetForLabel( label ); assertThat( indexes, containsInAnyOrder( inStore, createdInTx ) ); tx.success(); } }
@Test public void shouldFailToCreateUniqueConstraintIfConstraintNotSatisfied() throws Exception { //Given try ( Transaction transaction = beginTransaction() ) { Write write = transaction.dataWrite(); long node1 = write.nodeCreate(); write.nodeAddLabel( node1, label ); write.nodeSetProperty( node1, prop1, Values.intValue( 42 ) ); long node2 = write.nodeCreate(); write.nodeAddLabel( node2, label ); write.nodeSetProperty( node2, prop1, Values.intValue( 42 ) ); transaction.success(); } //Expect exception.expect( SchemaKernelException.class ); //When try ( Transaction transaction = beginTransaction() ) { transaction.schemaWrite().uniquePropertyConstraintCreate( labelDescriptor( label, prop1 ) ); } }