Skip to content

Commit

Permalink
Fix test result verification for properties
Browse files Browse the repository at this point in the history
  • Loading branch information
henriknyman committed Mar 6, 2018
1 parent dd28773 commit 8b618bf
Showing 1 changed file with 44 additions and 17 deletions.
Expand Up @@ -39,6 +39,7 @@
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.Consumer;


import org.neo4j.graphdb.GraphDatabaseService; import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Label; import org.neo4j.graphdb.Label;
Expand Down Expand Up @@ -99,6 +100,10 @@ private String nameOf( InputEntity node )
{ {
return (String) node.properties()[1]; return (String) node.properties()[1];
} }
private int indexOf( InputEntity node )
{
return Integer.parseInt(((String) node.properties()[1]).split( "\\s" )[1] ) ;
}


@Rule @Rule
public final TestDirectory directory = TestDirectory.testDirectory(); public final TestDirectory directory = TestDirectory.testDirectory();
Expand Down Expand Up @@ -172,7 +177,7 @@ private List<InputEntity> randomNodeData()
InputEntity node = new InputEntity(); InputEntity node = new InputEntity();
node.id( UUID.randomUUID().toString(), Group.GLOBAL ); node.id( UUID.randomUUID().toString(), Group.GLOBAL );
node.property( "name", "Node " + i ); node.property( "name", "Node " + i );
node.property( "point", "\" { x : 3, y : " + i + ", crs: WGS-84 } \""); node.property( "point", "\" { x : -4.2, y : " + i + ", crs: WGS-84 } \"");
node.labels( randomLabels( random ) ); node.labels( randomLabels( random ) );
nodes.add( node ); nodes.add( node );
} }
Expand Down Expand Up @@ -238,9 +243,6 @@ private File nodeDataAsFile( List<InputEntity> nodeData ) throws IOException
String csvLabels = csvLabels( node.labels() ); String csvLabels = csvLabels( node.labels() );
println( writer, node.id() + "," + node.properties()[1] + "," + node.properties()[3] + "," + println( writer, node.id() + "," + node.properties()[1] + "," + node.properties()[3] + "," +
(csvLabels != null && csvLabels.length() > 0 ? csvLabels : "") ); (csvLabels != null && csvLabels.length() > 0 ? csvLabels : "") );
// TODO: Remove me
//System.out.println( node.id() + "," + node.properties()[1] + "," + node.properties()[3] + "," +
// (csvLabels != null && csvLabels.length() > 0 ? csvLabels : "") );
} }
} }
return file; return file;
Expand Down Expand Up @@ -289,14 +291,14 @@ private void verifyImportedData( List<InputEntity> nodeData,
// Build up expected data for the verification below // Build up expected data for the verification below
Map<String/*id*/, InputEntity> expectedNodes = new HashMap<>(); Map<String/*id*/, InputEntity> expectedNodes = new HashMap<>();
Map<String,String[]> expectedNodeNames = new HashMap<>(); Map<String,String[]> expectedNodeNames = new HashMap<>();
Map<String, Map<String,Object>> expectedNodeProperties = new HashMap<>(); Map<String, Map<String,Consumer<Object>>> expectedNodePropertyVerifiers = new HashMap<>();
Map<String/*start node name*/, Map<String/*end node name*/, Map<String, AtomicInteger>>> expectedRelationships = Map<String/*start node name*/, Map<String/*end node name*/, Map<String, AtomicInteger>>> expectedRelationships =
new AutoCreatingHashMap<>( nested( String.class, nested( String.class, values( AtomicInteger.class ) ) ) ); new AutoCreatingHashMap<>( nested( String.class, nested( String.class, values( AtomicInteger.class ) ) ) );
Map<String, AtomicLong> expectedNodeCounts = new AutoCreatingHashMap<>( values( AtomicLong.class ) ); Map<String, AtomicLong> expectedNodeCounts = new AutoCreatingHashMap<>( values( AtomicLong.class ) );
Map<String, Map<String, Map<String, AtomicLong>>> expectedRelationshipCounts = Map<String, Map<String, Map<String, AtomicLong>>> expectedRelationshipCounts =
new AutoCreatingHashMap<>( nested( String.class, nested( String.class, values( AtomicLong.class ) ) ) ); new AutoCreatingHashMap<>( nested( String.class, nested( String.class, values( AtomicLong.class ) ) ) );
buildUpExpectedData( nodeData, relationshipData, expectedNodes, expectedNodeNames, expectedNodeProperties, expectedRelationships, buildUpExpectedData( nodeData, relationshipData, expectedNodes, expectedNodeNames, expectedNodePropertyVerifiers,
expectedNodeCounts, expectedRelationshipCounts ); expectedRelationships, expectedNodeCounts, expectedRelationshipCounts );


// Do the verification // Do the verification
GraphDatabaseService db = new TestGraphDatabaseFactory().newEmbeddedDatabase( directory.graphDbDir() ); GraphDatabaseService db = new TestGraphDatabaseFactory().newEmbeddedDatabase( directory.graphDbDir() );
Expand All @@ -308,13 +310,19 @@ private void verifyImportedData( List<InputEntity> nodeData,
String name = (String) node.getProperty( "name" ); String name = (String) node.getProperty( "name" );
String[] labels = expectedNodeNames.remove( name ); String[] labels = expectedNodeNames.remove( name );
assertEquals( asSet( labels ), names( node.getLabels() ) ); assertEquals( asSet( labels ), names( node.getLabels() ) );
Map<String,Object> expectedProperties = expectedNodeProperties.remove( name );
// Verify node properties
Map<String,Consumer<Object>> expectedPropertyVerifiers = expectedNodePropertyVerifiers.remove( name );
Map<String,Object> actualProperties = node.getAllProperties(); Map<String,Object> actualProperties = node.getAllProperties();
actualProperties.remove( "id" ); actualProperties.remove( "id" ); // The id does not exist in expected properties
// TODO: Fix the expectations for the points for ( Map.Entry actualProperty : actualProperties.entrySet() )
PointValue actualPoint = (PointValue) actualProperties.remove( "point" ); {
String expectedPoint = (String) expectedProperties.remove( "point" ); Consumer v = expectedPropertyVerifiers.get( actualProperty.getKey() );
assertEquals( expectedProperties, actualProperties ); if ( v != null )
{
v.accept( actualProperty.getValue() );
}
}
} }
assertEquals( 0, expectedNodeNames.size() ); assertEquals( 0, expectedNodeNames.size() );


Expand Down Expand Up @@ -458,7 +466,7 @@ private void buildUpExpectedData(
List<InputEntity> relationshipData, List<InputEntity> relationshipData,
Map<String, InputEntity> expectedNodes, Map<String, InputEntity> expectedNodes,
Map<String, String[]> expectedNodeNames, Map<String, String[]> expectedNodeNames,
Map<String, Map<String,Object>> expectedNodeProperties, Map<String, Map<String,Consumer<Object>>> expectedNodePropertyVerifiers,
Map<String, Map<String, Map<String, AtomicInteger>>> expectedRelationships, Map<String, Map<String, Map<String, AtomicInteger>>> expectedRelationships,
Map<String, AtomicLong> nodeCounts, Map<String, AtomicLong> nodeCounts,
Map<String, Map<String, Map<String, AtomicLong>>> relationshipCounts ) Map<String, Map<String, Map<String, AtomicLong>>> relationshipCounts )
Expand All @@ -468,13 +476,32 @@ private void buildUpExpectedData(
expectedNodes.put( (String) node.id(), node ); expectedNodes.put( (String) node.id(), node );
expectedNodeNames.put( nameOf( node ), node.labels() ); expectedNodeNames.put( nameOf( node ), node.labels() );


// Build default verifiers for all the properties that compares the property value using equals
assert node.hasIntPropertyKeyIds == false; assert node.hasIntPropertyKeyIds == false;
Map<String,Object> properties = new TreeMap<>(); Map<String,Consumer<Object>> propertyVerifiers = new TreeMap<>();
for ( int i = 0; i < node.propertyCount(); i++ ) for ( int i = 0; i < node.propertyCount(); i++ )
{ {
properties.put( (String) node.propertyKey( i ), node.propertyValue( i ) ); final Object expectedValue = node.propertyValue( i );
Consumer verify = (actualValue) ->
{
assertEquals( expectedValue, actualValue );
};
propertyVerifiers.put( (String) node.propertyKey( i ), verify );
} }
expectedNodeProperties.put( nameOf( node ), properties );
// Special verifier for point property
Consumer verifyPoint = (actualValue) ->
{
// The y-coordinate should match the node number
PointValue v = (PointValue) actualValue;
double actualY = v.getCoordinates().get( 0 ).getCoordinate().get( 1 );
double expectedY = indexOf( node );
String message = actualValue.toString() + " does not have y=" + expectedY;
assertEquals( message, expectedY, actualY, 0.1 );
};
propertyVerifiers.put( "point", verifyPoint );

expectedNodePropertyVerifiers.put( nameOf( node ), propertyVerifiers );


countNodeLabels( nodeCounts, node.labels() ); countNodeLabels( nodeCounts, node.labels() );
} }
Expand Down

0 comments on commit 8b618bf

Please sign in to comment.