@Test public void addingACompositeIndexingRuleShouldSucceed() { // WHEN IndexDefinition index = createIndex( db, label, propertyKey, secondPropertyKey ); // THEN assertThat( getIndexes( db, label ), containsOnly( index ) ); }
@Test public void addingAnIndexingRuleShouldSucceed() { // WHEN IndexDefinition index = createIndex( db, label, propertyKey ); // THEN assertThat( getIndexes( db, label ), containsOnly( index ) ); }
@Test public void addingIndexWhenAlreadyIndexed() { // GIVEN createIndex( db, label, propertyKey ); // WHEN try { createIndex( db, label, propertyKey ); fail( "Expected exception to be thrown" ); } catch ( ConstraintViolationException e ) { assertEquals( "There already exists an index :MY_LABEL(my_property_key).", e.getMessage() ); } }
@Test public void droppingExistingIndexRuleShouldSucceed() { // GIVEN IndexDefinition index = createIndex( db, label, propertyKey ); // WHEN dropIndex( index ); // THEN assertThat( getIndexes( db, label ), isEmpty() ); }
@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 ) ); }
@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 awaitingAllIndexesComingOnlineWorks() { // GIVEN // WHEN IndexDefinition index = createIndex( db, label, propertyKey ); createIndex( db, label, "other_property" ); // PASS waitForIndex( db, index ); try ( Transaction tx = db.beginTx() ) { db.schema().awaitIndexesOnline( 1L, TimeUnit.MINUTES ); // THEN assertEquals( Schema.IndexState.ONLINE, db.schema().getIndexState( index ) ); } }
@Test public void addedUncommittedIndexesShouldBeVisibleWithinTheTransaction() { // GIVEN IndexDefinition indexA = createIndex( db, label, "a" ); createUniquenessConstraint( label, "b" ); // WHEN try ( Transaction tx = db.beginTx() ) { assertThat( count( db.schema().getIndexes( label ) ), is( 2L ) ); IndexDefinition indexC = db.schema().indexFor( label ).on( "c" ).create(); // THEN assertThat( count( db.schema().getIndexes( label ) ), is( 3L ) ); assertThat( db.schema().getIndexState( indexA ), is( Schema.IndexState.ONLINE ) ); assertThat( db.schema().getIndexState( indexC ), is( Schema.IndexState.POPULATING ) ); } }
@Test public void addingIndexWhenAlreadyConstrained() { // GIVEN createUniquenessConstraint( label, propertyKey ); // WHEN try { createIndex( db, label, propertyKey ); fail( "Expected exception to be thrown" ); } catch ( ConstraintViolationException e ) { assertEquals( "There is a uniqueness constraint on :MY_LABEL(my_property_key), so an index is already " + "created that matches this.", e.getMessage() ); } }
@Test public void addingConstraintWhenIndexAlreadyExistsGivesNiceError() { // GIVEN createIndex( db, label, propertyKey ); // WHEN try { createUniquenessConstraint( label, propertyKey ); fail( "Expected exception to be thrown" ); } catch ( ConstraintViolationException e ) { assertEquals( "There already exists an index :MY_LABEL(my_property_key). A constraint cannot be created " + "until the index has been dropped.", e.getMessage() ); } }
@Test public void searchingUsesIndexWhenItExists() { // Given GraphDatabaseService beansAPI = dbRule.getGraphDatabaseAPI(); Node myNode = createNode( beansAPI, map( "name", "Hawking" ), LABEL1 ); Neo4jMatchers.createIndex( beansAPI, LABEL1, "name" ); // When assertThat( findNodesByLabelAndProperty( LABEL1, "name", "Hawking", beansAPI ), containsOnly( myNode ) ); }
@Test public void awaitingIndexComingOnlineWorks() { // GIVEN // WHEN IndexDefinition index = createIndex( db, label, propertyKey ); // PASS try ( Transaction tx = db.beginTx() ) { db.schema().awaitIndexOnline( index, 1L, TimeUnit.MINUTES ); // THEN assertEquals( Schema.IndexState.ONLINE, db.schema().getIndexState( index ) ); } }
@Test public void createdNodeShouldShowUpWithinTransaction() { // GIVEN GraphDatabaseService beansAPI = dbRule.getGraphDatabaseAPI(); Neo4jMatchers.createIndex( beansAPI, LABEL1, "name" ); // WHEN Transaction tx = beansAPI.beginTx(); Node firstNode = createNode( beansAPI, map( "name", "Mattias" ), LABEL1 ); long sizeBeforeDelete = count( beansAPI.findNodes( LABEL1, "name", "Mattias" ) ); firstNode.delete(); long sizeAfterDelete = count( beansAPI.findNodes( LABEL1, "name", "Mattias" ) ); tx.close(); // THEN assertThat( sizeBeforeDelete, equalTo(1L) ); assertThat( sizeAfterDelete, equalTo(0L) ); }
@Test public void deletedNodeShouldShowUpWithinTransaction() { // GIVEN GraphDatabaseService beansAPI = dbRule.getGraphDatabaseAPI(); Neo4jMatchers.createIndex( beansAPI, LABEL1, "name" ); Node firstNode = createNode( beansAPI, map( "name", "Mattias" ), LABEL1 ); // WHEN Transaction tx = beansAPI.beginTx(); long sizeBeforeDelete = count( beansAPI.findNodes( LABEL1, "name", "Mattias" ) ); firstNode.delete(); long sizeAfterDelete = count( beansAPI.findNodes( LABEL1, "name", "Mattias" ) ); tx.close(); // THEN assertThat( sizeBeforeDelete, equalTo(1L) ); assertThat( sizeAfterDelete, equalTo(0L) ); }
@BeforeClass public static void init() { GraphDatabaseService db = new TestGraphDatabaseFactory().newImpermanentDatabase(); for ( TestValue value : TestValue.values() ) { createNode( db, PROPERTY_KEY, value.value ); } noIndexRun = runFindByLabelAndProperty( db ); createIndex( db, label( LABEL ), PROPERTY_KEY ); indexRun = runFindByLabelAndProperty( db ); db.shutdown(); }
@Test public void recoveryAfterCreateAndDropIndex() throws Exception { // GIVEN IndexDefinition indexDefinition = createIndex( db, label, propertyKey ); createSomeData( label, propertyKey ); doStuff( db, label, propertyKey ); dropIndex( indexDefinition ); doStuff( db, label, propertyKey ); // WHEN crashAndRestart(); // THEN assertThat( getIndexes( db, label ), isEmpty() ); }
@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 shouldIndexStringArrays() { String[] arrayPropertyValue = {"A, B", "C"}; 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 String[]{"A", "B, C"}, db ), isEmpty() ); assertThat( findNodesByLabelAndProperty( label, propertyKey, Arrays.toString( arrayPropertyValue ), db ), isEmpty() ); }
@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() ); }
@Test public void creatingIndexOnExistingDataBuildsIndexWhichWillBeOnlineNextStartup() { Node node1; Node node2; Node node3; try ( Transaction tx = db.beginTx() ) { node1 = createNode( label, "name", "One" ); node2 = createNode( label, "name", "Two" ); node3 = createNode( label, "name", "Three" ); tx.success(); } createIndex( db, label, propertyKey ); restart(); assertThat( findNodesByLabelAndProperty( label, "name", "One", db ), containsOnly( node1 ) ); assertThat( findNodesByLabelAndProperty( label, "name", "Two", db ), containsOnly( node2 ) ); assertThat( findNodesByLabelAndProperty( label, "name", "Three", db ), containsOnly( node3 ) ); }