Skip to content
This repository has been archived by the owner on Apr 22, 2020. It is now read-only.

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mneedham committed Feb 12, 2019
1 parent 4dcc706 commit 87df54f
Showing 1 changed file with 57 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<SimilarityResult> similarityPairs = Stream.empty();

int batches = exporter.export(similarityPairs, 1);
assertEquals(0, batches);

try (Transaction tx = api.beginTx()) {
List<SimilarityRelationship> allRelationships = getSimilarityRelationships(api);
assertThat(allRelationships, hasSize(0));
}
}

@Test
public void createOneRelationship() {
Expand All @@ -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<SimilarityRelationship> allRelationships = getSimilarityRelationships(api);
assertThat(allRelationships, hasSize(1));
assertThat(allRelationships, hasItems(new SimilarityRelationship(0, 1, 0.5)));
}
}

Expand All @@ -62,16 +77,46 @@ public void multipleBatches() {
assertEquals(2, batches);

try (Transaction tx = api.beginTx()) {
List<SimilarityRelationship> allRelationships = api.getAllRelationships().stream()
.map(relationship -> new SimilarityRelationship(relationship.getStartNodeId(), relationship.getEndNodeId(), (double)relationship.getProperty(PROPERTY_NAME)))
.collect(Collectors.toList());
List<SimilarityRelationship> allRelationships = getSimilarityRelationships(api);

assertThat(allRelationships, hasSize(2));
assertThat(allRelationships, hasItems(new SimilarityRelationship(0, 1, 0.5)));
assertThat(allRelationships, hasItems(new SimilarityRelationship(2, 3, 0.7)));
}
}

@Test
public void smallerThanBatchSize() {
GraphDatabaseAPI api = DB.getGraphDatabaseAPI();
createNodes(api, 5);

SequentialSimilarityExporter exporter = new SequentialSimilarityExporter(api, NullLog.getInstance(), RELATIONSHIP_TYPE, PROPERTY_NAME);

Stream<SimilarityResult> 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<SimilarityRelationship> 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<SimilarityRelationship> 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;
Expand Down

0 comments on commit 87df54f

Please sign in to comment.