Skip to content

Commit

Permalink
Add more tests for IndexEntryUpdate
Browse files Browse the repository at this point in the history
Also addressed some PR comments.
  • Loading branch information
ragadeeshu committed Feb 16, 2017
1 parent 8649d40 commit 9d4e39d
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 15 deletions.
Expand Up @@ -85,23 +85,44 @@ public boolean equals( Object o )
{ {
return true; return true;
} }
if ( o != null && o instanceof IndexEntryUpdate ) if ( o == null || getClass() != o.getClass() )
{ {
IndexEntryUpdate other = (IndexEntryUpdate) o; return false;
return entityId == other.entityId &&
updateMode == other.updateMode &&
descriptor.equals( other.descriptor ) &&
Arrays.equals( values, other.values );
} }
return false;
IndexEntryUpdate that = (IndexEntryUpdate) o;

if ( entityId != that.entityId )
{
return false;
}
if ( updateMode != that.updateMode )
{
return false;
}
// Probably incorrect - comparing Object[] arrays with Arrays.equals
if ( !Arrays.equals( before, that.before ) )
{
return false;
}
// Probably incorrect - comparing Object[] arrays with Arrays.equals
if ( !Arrays.equals( values, that.values ) )
{
return false;
}
return descriptor != null ? descriptor.equals( that.descriptor ) : that.descriptor == null;

} }


@Override @Override
public int hashCode() public int hashCode()
{ {
return ((((int)entityId * 31) + int result = (int) (entityId ^ (entityId >>> 32));
updateMode.ordinal()) * 31 + result = 31 * result + (updateMode != null ? updateMode.hashCode() : 0);
Arrays.hashCode( values )) * 31 + descriptor.hashCode(); result = 31 * result + Arrays.hashCode( before );
result = 31 * result + Arrays.hashCode( values );
result = 31 * result + (descriptor != null ? descriptor.hashCode() : 0);
return result;
} }


@Override @Override
Expand Down
Expand Up @@ -44,6 +44,9 @@ public class NodeUpdates
private final long[] labelsBefore; private final long[] labelsBefore;
private final long[] labelsAfter; private final long[] labelsAfter;
private final LabelChangeSummary labelChangeSummary; private final LabelChangeSummary labelChangeSummary;
private final Map<Integer,Object> propertiesBefore = new HashMap<>();
private final Map<Integer,Object> propertiesAfter = new HashMap<>();
private final Map<Integer,Object> propertiesUnchanged = new HashMap<>();


public static class Builder public static class Builder
{ {
Expand Down Expand Up @@ -130,10 +133,6 @@ public final long getNodeId()
return nodeId; return nodeId;
} }


private final Map<Integer,Object> propertiesBefore = new HashMap<>();
private final Map<Integer,Object> propertiesAfter = new HashMap<>();
private final Map<Integer,Object> propertiesUnchanged = new HashMap<>();

public Optional<IndexEntryUpdate> forIndex( NewIndexDescriptor index ) public Optional<IndexEntryUpdate> forIndex( NewIndexDescriptor index )
{ {
if ( index.schema().getPropertyIds().length > 1 ) if ( index.schema().getPropertyIds().length > 1 )
Expand Down
Expand Up @@ -19,15 +19,24 @@
*/ */
package org.neo4j.kernel.api.index; package org.neo4j.kernel.api.index;


import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException;


import org.neo4j.kernel.api.schema_new.index.NewIndexDescriptorFactory; import org.neo4j.kernel.api.schema_new.index.NewIndexDescriptorFactory;


import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;


public class IndexEntryUpdateTest public class IndexEntryUpdateTest
{ {
private final Object[] multiValue = new Object[]{"value", "value2"};
private final String singleValue = "value";

@Rule
public ExpectedException thrown = ExpectedException.none();

@Test @Test
public void indexEntryUpdatesShouldBeEqual() public void indexEntryUpdatesShouldBeEqual()
{ {
Expand All @@ -36,4 +45,76 @@ public void indexEntryUpdatesShouldBeEqual()
assertThat( a, equalTo( b ) ); assertThat( a, equalTo( b ) );
assertThat( a.hashCode(), equalTo( b.hashCode() ) ); assertThat( a.hashCode(), equalTo( b.hashCode() ) );
} }

@Test
public void addShouldRetainValues()
{
IndexEntryUpdate single = IndexEntryUpdate.add( 0, NewIndexDescriptorFactory.forLabel( 3, 4 ), singleValue );
IndexEntryUpdate multi = IndexEntryUpdate.add( 0, NewIndexDescriptorFactory.forLabel( 3, 4, 5 ), multiValue );
assertThat( single, not( equalTo( multi ) ) );
assertThat( single.values(), equalTo( new Object[]{singleValue} ) );
assertThat( multi.values(), equalTo( multiValue ) );
}

@Test
public void removeShouldRetainValues()
{
IndexEntryUpdate single = IndexEntryUpdate.remove( 0, NewIndexDescriptorFactory.forLabel( 3, 4 ), singleValue );
IndexEntryUpdate multi = IndexEntryUpdate
.remove( 0, NewIndexDescriptorFactory.forLabel( 3, 4, 5 ), multiValue );
assertThat( single, not( equalTo( multi ) ) );
assertThat( single.values(), equalTo( new Object[]{singleValue} ) );
assertThat( multi.values(), equalTo( multiValue ) );
}

@Test
public void addShouldThrowIfAskedForChanged() throws Exception
{
IndexEntryUpdate single = IndexEntryUpdate.add( 0, NewIndexDescriptorFactory.forLabel( 3, 4 ), singleValue );
thrown.expect( UnsupportedOperationException.class );
single.beforeValues();
}

@Test
public void removeShouldThrowIfAskedForChanged() throws Exception
{
IndexEntryUpdate single = IndexEntryUpdate.remove( 0, NewIndexDescriptorFactory.forLabel( 3, 4 ), singleValue );
thrown.expect( UnsupportedOperationException.class );
single.beforeValues();
}

@Test
public void updatesShouldEqualRegardlessOfCreationMethod()
{
IndexEntryUpdate singleAdd = IndexEntryUpdate.add( 0, NewIndexDescriptorFactory.forLabel( 3, 4 ), singleValue );
Object[] singleAsArray = {singleValue};
IndexEntryUpdate multiAdd = IndexEntryUpdate
.add( 0, NewIndexDescriptorFactory.forLabel( 3, 4 ), singleAsArray );
IndexEntryUpdate singleRemove = IndexEntryUpdate
.remove( 0, NewIndexDescriptorFactory.forLabel( 3, 4 ), singleValue );
IndexEntryUpdate multiRemove = IndexEntryUpdate
.remove( 0, NewIndexDescriptorFactory.forLabel( 3, 4 ), singleAsArray );
IndexEntryUpdate singleChange = IndexEntryUpdate
.change( 0, NewIndexDescriptorFactory.forLabel( 3, 4 ), singleValue, singleValue );
IndexEntryUpdate multiChange = IndexEntryUpdate
.change( 0, NewIndexDescriptorFactory.forLabel( 3, 4 ), singleAsArray, singleAsArray );
assertThat( singleAdd, equalTo( multiAdd ) );
assertThat( singleRemove, equalTo( multiRemove ) );
assertThat( singleChange, equalTo( multiChange ) );
}

@Test
public void changedShouldRetainValues() throws Exception
{
String singleAfter = "Hello";
IndexEntryUpdate singleChange = IndexEntryUpdate
.change( 0, NewIndexDescriptorFactory.forLabel( 3, 4 ), singleValue, singleAfter );
Object[] multiAfter = {"Hello", "Hi"};
IndexEntryUpdate multiChange = IndexEntryUpdate
.change( 0, NewIndexDescriptorFactory.forLabel( 3, 4 ), multiValue, multiAfter );
assertThat( new Object[]{singleValue}, equalTo( singleChange.beforeValues() ) );
assertThat( new Object[]{singleAfter}, equalTo( singleChange.values() ) );
assertThat( multiValue, equalTo( multiChange.beforeValues() ) );
assertThat( multiAfter, equalTo( multiChange.values() ) );
}
} }
Expand Up @@ -939,7 +939,8 @@ public void shouldRunIndexPopulationJobAtShutdown() throws Throwable
verify( provider ).getPopulator( anyLong(), any( IndexDescriptor.class ), any( IndexConfiguration.class ), verify( provider ).getPopulator( anyLong(), any( IndexDescriptor.class ), any( IndexConfiguration.class ),
any( IndexSamplingConfig.class ) ); any( IndexSamplingConfig.class ) );
verify( populator ).create(); verify( populator ).create();
verify( populator ).add( singletonList( IndexEntryUpdate.add( nodeId, any(), "Jakewins" ) ) ); verify( populator ).add( singletonList( IndexEntryUpdate.add( nodeId, any( NewIndexDescriptor.class)
, "Jakewins" ) ) );
verify( populator ).verifyDeferredConstraints( any( PropertyAccessor.class ) ); verify( populator ).verifyDeferredConstraints( any( PropertyAccessor.class ) );
verify( populator ).close( true ); verify( populator ).close( true );
verify( provider ).stop(); verify( provider ).stop();
Expand Down

0 comments on commit 9d4e39d

Please sign in to comment.