Skip to content

Commit

Permalink
tests for element IDs with Neo4J
Browse files Browse the repository at this point in the history
  • Loading branch information
danthe1st committed Nov 27, 2024
1 parent 5b80a77 commit 291c4d3
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 1 deletion.
2 changes: 2 additions & 0 deletions arebac-neo4j/Benchmarks.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ The following command can be used for checking whether all benchmarks run succes
java -cp target/classes:target/test-classes:target/all-dependencies/*:target/arebac-neo4j-0.0.1-SNAPSHOT.jar:../arebac-core/target/classes/ org.openjdk.jmh.Main -f 1 -i 1 -wi 0 -r 100ms -foe true
```

To store the output in a `jmh-result.csv` file, add `-rf csv` to the command.

## profiling

In order to profile a specific benchmark using [`async-profiler`](https://github.com/async-profiler/async-profiler), first download and extract `async-profiler` and then use the following command (replace `/PATH/TO/async-profiler` with the path you extracted `async-profiler` to):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.neo4j.graphdb.Direction;
import org.neo4j.graphdb.Label;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.NotFoundException;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.RelationshipType;
import org.neo4j.graphdb.ResourceIterable;
Expand Down Expand Up @@ -41,7 +42,12 @@ public Neo4jDB(Transaction tx) {

@Override
public Neo4jNode findNodeById(String id) {
return new Neo4jNode(tx.getNodeByElementId(id));
try{
Node dbNode = tx.getNodeByElementId(id);
return new Neo4jNode(dbNode);
}catch(NotFoundException | IllegalArgumentException e){
return null;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package io.github.danthe1st.arebac.neo4j.tests.airbnb;

import static io.github.danthe1st.arebac.data.commongraph.attributed.AttributeValue.attribute;
import static io.github.danthe1st.arebac.data.graph_pattern.constraints.AttributeRequirement.ID_KEY;
import static io.github.danthe1st.arebac.data.graph_pattern.constraints.AttributeRequirementOperator.EQUAL;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;

import io.github.danthe1st.arebac.data.graph_pattern.GPGraph;
import io.github.danthe1st.arebac.data.graph_pattern.GPNode;
import io.github.danthe1st.arebac.data.graph_pattern.GraphPattern;
import io.github.danthe1st.arebac.data.graph_pattern.constraints.AttributeRequirement;
import io.github.danthe1st.arebac.gpeval.GPEval;
import io.github.danthe1st.arebac.neo4j.graph.Neo4jDB;
import io.github.danthe1st.arebac.neo4j.graph.Neo4jNode;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.neo4j.dbms.archive.IncorrectFormat;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Label;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.ResourceIterator;
import org.neo4j.graphdb.Transaction;

class ElementIdTest {

private GraphDatabaseService database;

@BeforeEach
void setUp() throws IOException, IncorrectFormat {
database = AirbnbSetup.getDatabase();
}

@Test
void testLookupWithElementId() {
try(Transaction tx = database.beginTx()){
Node someNode = findSomeNode(tx);
Neo4jNode retrieved = new Neo4jDB(tx).findNodeById(someNode.getElementId());
assertNotNull(retrieved);
assertEquals(someNode.getElementId(), retrieved.getDBNode().getElementId());
}
}

@Test
void testLookupWithNonexistentElementId() {
try(Transaction tx = database.beginTx()){
Neo4jNode retrieved = new Neo4jDB(tx).findNodeById("thisdoesnotexist");
assertNull(retrieved);
}
}

@Test
void testLookupWithNonexistentElementIdUsingGPEval() {
try(Transaction tx = database.beginTx()){
GraphPattern pattern = createGraphPatternExpectingElementId("thisdoesnotexist");
Set<List<Neo4jNode>> result = GPEval.evaluate(new Neo4jDB(tx), pattern);
assertEquals(Set.of(), result);
}
}

@Test
void testLookupWithElementIdUsingGPEval() {
try(Transaction tx = database.beginTx()){
Node someNode = findSomeNode(tx);
String elementId = someNode.getElementId();
GraphPattern pattern = createGraphPatternExpectingElementId(elementId);
Set<List<Neo4jNode>> result = GPEval.evaluate(new Neo4jDB(tx), pattern);
assertEquals(1, result.size());
assertEquals(result, Set.of(List.of(new Neo4jNode(someNode))));
}
}

private GraphPattern createGraphPatternExpectingElementId(String elementId) {
GPNode patternNode = new GPNode("node", AirbnbSetup.HOST);
return new GraphPattern(
new GPGraph(List.of(patternNode), List.of()),
List.of(),
Map.of(patternNode, List.of(new AttributeRequirement(ID_KEY, EQUAL, attribute(elementId)))),
Map.of(),
List.of(patternNode),
Map.of()
);
}

private Node findSomeNode(Transaction tx) {
try(ResourceIterator<Node> nodes = tx.findNodes(Label.label(AirbnbSetup.HOST))){
return nodes.next();
}
}
}

0 comments on commit 291c4d3

Please sign in to comment.