@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 searchingByLabelAndPropertyReturnsEmptyWhenMissingLabelOrProperty() { // Given GraphDatabaseService beansAPI = dbRule.getGraphDatabaseAPI(); // When/Then assertThat( findNodesByLabelAndProperty( LABEL1, "name", "Hawking", beansAPI ), isEmpty() ); }
@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 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 shouldIndexArraysPostPopulation() { long[] arrayPropertyValue = {42, 23, 87}; Node node1; try ( Transaction tx = db.beginTx() ) { node1 = createNode( label, propertyKey, arrayPropertyValue ); tx.success(); } createIndex( db, label, propertyKey ); 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 ) ); }
@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 searchingForNodeByPropertyShouldWorkWithoutIndex() { // Given GraphDatabaseService beansAPI = dbRule.getGraphDatabaseAPI(); Node myNode = createNode( beansAPI, map( "name", "Hawking" ), LABEL1 ); // When assertThat( findNodesByLabelAndProperty( LABEL1, "name", "Hawking", beansAPI ), containsOnly( myNode ) ); }
@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 ) ); }
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 ) );
@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 shouldCorrectlyUpdateIndexesWhenChangingLabelsAndPropertyAtTheSameTime() { // Given GraphDatabaseService beansAPI = dbRule.getGraphDatabaseAPI(); Node myNode = createNode( beansAPI, map( "name", "Hawking" ), LABEL1, LABEL2 ); Neo4jMatchers.createIndex( beansAPI, LABEL1, "name" ); Neo4jMatchers.createIndex( beansAPI, LABEL2, "name" ); Neo4jMatchers.createIndex( beansAPI, LABEL3, "name" ); // When try ( Transaction tx = beansAPI.beginTx() ) { myNode.removeLabel( LABEL1 ); myNode.addLabel( LABEL3 ); myNode.setProperty( "name", "Einstein" ); tx.success(); } // Then assertThat( myNode, inTx( beansAPI, hasProperty("name").withValue( "Einstein" ) ) ); assertThat( labels( myNode ), containsOnly( LABEL2, LABEL3 ) ); assertThat( findNodesByLabelAndProperty( LABEL1, "name", "Hawking", beansAPI ), isEmpty() ); assertThat( findNodesByLabelAndProperty( LABEL1, "name", "Einstein", beansAPI ), isEmpty() ); assertThat( findNodesByLabelAndProperty( LABEL2, "name", "Hawking", beansAPI ), isEmpty() ); assertThat( findNodesByLabelAndProperty( LABEL2, "name", "Einstein", beansAPI ), containsOnly( myNode ) ); assertThat( findNodesByLabelAndProperty( LABEL3, "name", "Hawking", beansAPI ), isEmpty() ); assertThat( findNodesByLabelAndProperty( LABEL3, "name", "Einstein", beansAPI ), containsOnly( myNode ) ); }
assertThat( findNodesByLabelAndProperty( LABEL1, "key", bigValue, beansAPI ), containsOnly( myNode ) ); assertThat( findNodesByLabelAndProperty( LABEL1, "key", smallValue, beansAPI ), isEmpty() );
assertThat( findNodesByLabelAndProperty( LABEL1, "key2", LONG_STRING, beansAPI ), containsOnly( myNode ) );