From 87df54fd025c41795b0f50bd3b1991073ee53abf Mon Sep 17 00:00:00 2001 From: Mark Needham Date: Tue, 12 Feb 2019 07:53:42 +0000 Subject: [PATCH] more tests --- ...rTest.java => SimilarityExporterTest.java} | 69 +++++++++++++++---- 1 file changed, 57 insertions(+), 12 deletions(-) rename tests/src/test/java/org/neo4j/graphalgo/similarity/{SequentialSimilarityExporterTest.java => SimilarityExporterTest.java} (55%) diff --git a/tests/src/test/java/org/neo4j/graphalgo/similarity/SequentialSimilarityExporterTest.java b/tests/src/test/java/org/neo4j/graphalgo/similarity/SimilarityExporterTest.java similarity index 55% rename from tests/src/test/java/org/neo4j/graphalgo/similarity/SequentialSimilarityExporterTest.java rename to tests/src/test/java/org/neo4j/graphalgo/similarity/SimilarityExporterTest.java index 6e09cb7cd..154b2be5a 100644 --- a/tests/src/test/java/org/neo4j/graphalgo/similarity/SequentialSimilarityExporterTest.java +++ b/tests/src/test/java/org/neo4j/graphalgo/similarity/SimilarityExporterTest.java @@ -2,9 +2,6 @@ import org.junit.Rule; import org.junit.Test; -import org.neo4j.graphdb.Direction; -import org.neo4j.graphdb.Relationship; -import org.neo4j.graphdb.RelationshipType; import org.neo4j.graphdb.Transaction; import org.neo4j.kernel.internal.GraphDatabaseAPI; import org.neo4j.logging.NullLog; @@ -20,12 +17,30 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; -public class SequentialSimilarityExporterTest { +public class SimilarityExporterTest { @Rule public final ImpermanentDatabaseRule DB = new ImpermanentDatabaseRule(); - public static final String RELATIONSHIP_TYPE = "SIMILAR"; - public static final String PROPERTY_NAME = "score"; + private static final String RELATIONSHIP_TYPE = "SIMILAR"; + private static final String PROPERTY_NAME = "score"; + + @Test + public void createNothing() { + GraphDatabaseAPI api = DB.getGraphDatabaseAPI(); + createNodes(api, 2); + + SequentialSimilarityExporter exporter = new SequentialSimilarityExporter(api, NullLog.getInstance(), RELATIONSHIP_TYPE, PROPERTY_NAME); + + Stream similarityPairs = Stream.empty(); + + int batches = exporter.export(similarityPairs, 1); + assertEquals(0, batches); + + try (Transaction tx = api.beginTx()) { + List allRelationships = getSimilarityRelationships(api); + assertThat(allRelationships, hasSize(0)); + } + } @Test public void createOneRelationship() { @@ -40,9 +55,9 @@ public void createOneRelationship() { assertEquals(1, batches); try (Transaction tx = api.beginTx()) { - Relationship relationship = api.getNodeById(0).getSingleRelationship(RelationshipType.withName(RELATIONSHIP_TYPE), Direction.OUTGOING); - assertEquals(1, relationship.getEndNodeId()); - assertEquals(0.5, (Double) relationship.getProperty(PROPERTY_NAME), 0.01); + List allRelationships = getSimilarityRelationships(api); + assertThat(allRelationships, hasSize(1)); + assertThat(allRelationships, hasItems(new SimilarityRelationship(0, 1, 0.5))); } } @@ -62,9 +77,7 @@ public void multipleBatches() { assertEquals(2, batches); try (Transaction tx = api.beginTx()) { - List allRelationships = api.getAllRelationships().stream() - .map(relationship -> new SimilarityRelationship(relationship.getStartNodeId(), relationship.getEndNodeId(), (double)relationship.getProperty(PROPERTY_NAME))) - .collect(Collectors.toList()); + List allRelationships = getSimilarityRelationships(api); assertThat(allRelationships, hasSize(2)); assertThat(allRelationships, hasItems(new SimilarityRelationship(0, 1, 0.5))); @@ -72,6 +85,38 @@ public void multipleBatches() { } } + @Test + public void smallerThanBatchSize() { + GraphDatabaseAPI api = DB.getGraphDatabaseAPI(); + createNodes(api, 5); + + SequentialSimilarityExporter exporter = new SequentialSimilarityExporter(api, NullLog.getInstance(), RELATIONSHIP_TYPE, PROPERTY_NAME); + + Stream similarityPairs = Stream.of( + new SimilarityResult(0, 1, -1, -1, -1, 0.5), + new SimilarityResult(2, 3, -1, -1, -1, 0.7), + new SimilarityResult(3, 4, -1, -1, -1, 0.7) + ); + + int batches = exporter.export(similarityPairs, 10); + assertEquals(1, batches); + + try (Transaction tx = api.beginTx()) { + List allRelationships = getSimilarityRelationships(api); + + assertThat(allRelationships, hasSize(3)); + assertThat(allRelationships, hasItems(new SimilarityRelationship(0, 1, 0.5))); + assertThat(allRelationships, hasItems(new SimilarityRelationship(2, 3, 0.7))); + assertThat(allRelationships, hasItems(new SimilarityRelationship(3, 4, 0.7))); + } + } + + private List getSimilarityRelationships(GraphDatabaseAPI api) { + return api.getAllRelationships().stream() + .map(relationship -> new SimilarityRelationship(relationship.getStartNodeId(), relationship.getEndNodeId(), (double)relationship.getProperty(PROPERTY_NAME))) + .collect(Collectors.toList()); + } + static class SimilarityRelationship { private final long startNodeId; private final long endNodeId;