@Test public void shouldIndexArrays() { long[] arrayPropertyValue = {42, 23, 87}; createIndex( db, label, propertyKey ); Node node1; try ( Transaction tx = db.beginTx() ) { node1 = createNode( label, propertyKey, arrayPropertyValue ); tx.success(); } restart(); assertThat( getIndexes( db, label ), inTx( db, haveState( db, IndexState.ONLINE ) )); assertThat( findNodesByLabelAndProperty( label, propertyKey, arrayPropertyValue, db ), containsOnly( node1 ) ); assertThat( findNodesByLabelAndProperty( label, propertyKey, new long[]{42, 23}, db ), isEmpty() ); assertThat( findNodesByLabelAndProperty( label, propertyKey, Arrays.toString( arrayPropertyValue ), db ), isEmpty() ); }
Neo4jMatchers.createIndex( beansAPI, LABEL1, "name" ); Neo4jMatchers.createIndex( beansAPI, LABEL2, "name" ); Neo4jMatchers.createIndex( beansAPI, LABEL3, "name" ); assertThat( myNode, inTx( beansAPI, hasProperty("name").withValue( "Feynman" ) ) ); assertThat( labels( myNode ), containsOnly( LABEL2, LABEL3 ) ); assertThat( findNodesByLabelAndProperty( LABEL1, "name", "Hawking", beansAPI ), isEmpty() ); assertThat( findNodesByLabelAndProperty( LABEL1, "name", "Einstein", beansAPI ), isEmpty() ); assertThat( findNodesByLabelAndProperty( LABEL1, "name", "Feynman", beansAPI ), isEmpty() ); assertThat( findNodesByLabelAndProperty( LABEL2, "name", "Hawking", beansAPI ), isEmpty() ); assertThat( findNodesByLabelAndProperty( LABEL2, "name", "Einstein", beansAPI ), isEmpty() ); assertThat( findNodesByLabelAndProperty( LABEL2, "name", "Feynman", beansAPI ), containsOnly( myNode ) ); assertThat( findNodesByLabelAndProperty( LABEL3, "name", "Hawking", beansAPI ), isEmpty() ); assertThat( findNodesByLabelAndProperty( LABEL3, "name", "Einstein", beansAPI ), isEmpty() ); assertThat( findNodesByLabelAndProperty( LABEL3, "name", "Feynman", beansAPI ), containsOnly( myNode ) );
public static TypeSafeDiagnosingMatcher<Node> hasNoLabels() { return hasLabels( emptySet() ); }
public static IndexDefinition createIndex( GraphDatabaseService beansAPI, Label label, String... properties ) { IndexDefinition indexDef = createIndexNoWait( beansAPI, label, properties ); waitForIndex( beansAPI, indexDef ); return indexDef; }
@Test public void addingAnIndexingRuleShouldSucceed() { // WHEN IndexDefinition index = createIndex( db, label, propertyKey ); // THEN assertThat( getIndexes( db, label ), containsOnly( index ) ); }
@Test public void shouldPopulateIndex() { // GIVEN Node node = createNode( db, propertyKey, "Neo", label ); // create an index IndexDefinition index = createIndex( db, label, propertyKey ); waitForIndex( db, index ); // THEN assertThat( findNodesByLabelAndProperty( label, propertyKey, "Neo", db ), containsOnly( node ) ); }
String value3 = "hello"; worker1.setProperty( key1, value1 ).get(); assertThat( properties, inTx( db, not( hasProperty( key1 ) ) ) ); assertFalse( worker2.hasProperty( key1 ) ); Future<Void> blockedSetProperty = worker2.setProperty( key2, value2 ); assertThat( properties, inTx( db, not( hasProperty( key1 ) ) ) ); assertThat( properties, inTx( db, not( hasProperty( key2 ) ) ) ); worker1.setProperty( key3, value3 ).get(); assertFalse( blockedSetProperty.isDone() ); assertThat( properties, inTx( db, not( hasProperty( key1 ) ) ) ); assertThat( properties, inTx( db, not( hasProperty( key2 ) ) ) ); assertThat( properties, inTx( db, not( hasProperty( key3 ) ) ) ); worker1.commitTx(); assertThat( properties, inTx( db, hasProperty( key1 ) ) ); assertThat( properties, inTx( db, not( hasProperty( key2 ) ) ) ); assertThat( properties, inTx( db, hasProperty( key3 ) ) ); blockedSetProperty.get(); assertTrue( blockedSetProperty.isDone() ); worker2.commitTx(); assertThat( properties, inTx( db, hasProperty( key1 ).withValue( value1 ) ) ); assertThat( properties, inTx( db, hasProperty( key2 ).withValue( value2 ) ) ); assertThat( properties, inTx( db, hasProperty( key3 ).withValue( value3 ) ) );
@Test public void shouldKeepFailedIndexesAsFailedAfterRestart() throws Exception { // Given IndexPopulator indexPopulator = mock( IndexPopulator.class ); when( mockedIndexProvider.getPopulator( any( StoreIndexDescriptor.class ), any( IndexSamplingConfig.class ) ) ) .thenReturn( indexPopulator ); IndexAccessor indexAccessor = mock( IndexAccessor.class ); when( mockedIndexProvider.getOnlineAccessor( any( StoreIndexDescriptor.class ), any( IndexSamplingConfig.class ) ) ) .thenReturn( indexAccessor ); startDb(); createIndex( myLabel ); rotateLogsAndCheckPoint(); // And Given killDb(); when( mockedIndexProvider.getInitialState( any( StoreIndexDescriptor.class ) ) ) .thenReturn( InternalIndexState.FAILED ); // When startDb(); // Then assertThat( getIndexes( db, myLabel ), inTx( db, hasSize( 1 ) ) ); assertThat( getIndexes( db, myLabel ), inTx( db, haveState( db, Schema.IndexState.FAILED ) ) ); verify( mockedIndexProvider, times( 2 ) ) .getPopulator( any( StoreIndexDescriptor.class ), any( IndexSamplingConfig.class ) ); }
@Test public void removingCommittedLabel() { // Given GraphDatabaseService graphDatabase = dbRule.getGraphDatabaseAPI(); Label label = Labels.MY_LABEL; Node myNode = createNode( graphDatabase, label ); // When try ( Transaction tx = graphDatabase.beginTx() ) { myNode.removeLabel( label ); tx.success(); } // Then assertThat( myNode, not( inTx( graphDatabase, hasLabel( label ) ) ) ); }
@Test public void shouldRecreateDroppedIndex() { // GIVEN Node node = createNode( db, propertyKey, "Neo", label ); // create an index IndexDefinition index = createIndex( db, label, propertyKey ); waitForIndex( db, index ); // delete the index right away dropIndex( index ); // WHEN recreating that index createIndex( db, label, propertyKey ); waitForIndex( db, index ); // THEN it should exist and be usable assertThat( getIndexes( db, label ), contains( index ) ); assertThat( findNodesByLabelAndProperty( label, propertyKey, "Neo", db ), containsOnly( node ) ); }
@Test public void createNodeWithLabels() { // GIVEN GraphDatabaseService db = dbRule.getGraphDatabaseAPI(); // WHEN Node node = null; try ( Transaction tx = db.beginTx() ) { node = db.createNode( Labels.values() ); tx.success(); } // THEN Set<String> names = Stream.of( Labels.values() ).map( Labels::name ).collect( toSet() ); assertThat( node, inTx( db, hasLabels( names ) ) ); }
@Test public void shouldHandleRestartIndexThatHasNotComeOnlineYet() { // Given startDb(); createIndex(); // And Given stopDb(); provider.setInitialIndexState( POPULATING ); // When startDb(); assertThat( getIndexes( db, myLabel ), inTx( db, not( haveState( db, Schema.IndexState.FAILED ) ) ) ); assertEquals( 2, provider.populatorCallCount.get() ); }
assertThat( graphProperties, inTx( db, not( hasProperty( "test" ) ) ) ); tx.success(); tx.close(); assertThat( graphProperties, inTx( db, hasProperty( "test" ).withValue( "yo" ) ) ); tx = db.beginTx(); assertNull( graphProperties.removeProperty( "something non existent" ) ); tx.success(); tx.close(); assertThat( graphProperties, inTx( db, not( hasProperty( "test" ) ) ) ); assertThat( graphProperties, inTx( db, hasProperty( "other" ).withValue( 10 ) ) ); assertThat( getPropertyKeys( db, graphProperties ), containsOnly( "other", "new" ) ); assertEquals( true, graphProperties.getProperty( "rollback" ) ); tx.close(); assertThat( graphProperties, inTx( db, not( hasProperty( "rollback" ) ) ) ); db.shutdown();
@Test public void shouldSeeIndexUpdatesWhenQueryingOutsideTransaction() { // GIVEN GraphDatabaseService beansAPI = dbRule.getGraphDatabaseAPI(); Neo4jMatchers.createIndex( beansAPI, LABEL1, "name" ); Node firstNode = createNode( beansAPI, map( "name", "Mattias" ), LABEL1 ); // WHEN THEN assertThat( findNodesByLabelAndProperty( LABEL1, "name", "Mattias", beansAPI ), containsOnly( firstNode ) ); Node secondNode = createNode( beansAPI, map( "name", "Taylor" ), LABEL1 ); assertThat( findNodesByLabelAndProperty( LABEL1, "name", "Taylor", beansAPI ), containsOnly( secondNode ) ); }
@Test public void droppingExistingIndexRuleShouldSucceed() { // GIVEN IndexDefinition index = createIndex( db, label, propertyKey ); // WHEN dropIndex( index ); // THEN assertThat( getIndexes( db, label ), isEmpty() ); }
@Test public void shouldBeAbleToDropIndexWhileItIsPopulating() { // GIVEN startDb(); DoubleLatch populationCompletionLatch = provider.installPopulationJobCompletionLatch(); IndexDefinition index = createIndex(); populationCompletionLatch.waitForAllToStart(); // await population job to start // WHEN dropIndex( index, populationCompletionLatch ); // THEN assertThat( getIndexes( db, myLabel ), inTx( db, hasSize( 0 ) ) ); try { getIndexState( db, index ); fail( "This index should have been deleted" ); } catch ( NotFoundException e ) { assertThat( e.getMessage(), CoreMatchers.containsString( myLabel.name() ) ); } }
Neo4jMatchers.createIndex( beansAPI, LABEL1, "key" ); assertThat( findNodesByLabelAndProperty( LABEL1, "key", bigValue, beansAPI ), containsOnly( myNode ) ); assertThat( findNodesByLabelAndProperty( LABEL1, "key", smallValue, beansAPI ), isEmpty() );
@Test public void addingAnIndexingRuleInNestedTxShouldSucceed() { IndexDefinition index; // WHEN IndexDefinition indexDef; try ( Transaction tx = db.beginTx() ) { try ( Transaction nestedTransaction = db.beginTx() ) { indexDef = db.schema().indexFor( label ).on( propertyKey ).create(); nestedTransaction.success(); } index = indexDef; tx.success(); } waitForIndex( db, indexDef ); // THEN assertThat( getIndexes( db, label ), containsOnly( index ) ); }
@Documented( "Drop uniqueness constraint.\n" + "Drop uniqueness constraint for a label and a property." ) @Test @GraphDescription.Graph( nodes = {} ) public void drop_constraint() { data.get(); String labelName = labels.newInstance(); String propertyKey = properties.newInstance(); ConstraintDefinition constraintDefinition = createLabelUniquenessPropertyConstraint( labelName, propertyKey ); assertThat( getConstraints( graphdb(), label( labelName ) ), containsOnly( constraintDefinition ) ); gen.get().expectedStatus( 204 ).delete( getSchemaConstraintLabelUniquenessPropertyUri( labelName, propertyKey ) ).entity(); assertThat( getConstraints( graphdb(), label( labelName ) ), isEmpty() ); }
@Test public void droppingAnUnexistingIndexShouldGiveHelpfulExceptionInSeparateTransactions() { // GIVEN IndexDefinition index = createIndex( db, label, propertyKey ); dropIndex( index ); // WHEN try { dropIndex( index ); fail( "Should not be able to drop index twice" ); } catch ( ConstraintViolationException e ) { assertThat( e.getMessage(), containsString( "No such INDEX ON :MY_LABEL(my_property_key)." ) ); } // THEN assertThat( "Index should have been deleted", getIndexes( db, label ), not( contains( index ) ) ); }