use of org.neo4j.graphdb.Entity in project neo4j by neo4j.
the class DijkstraMultiplePathsTest method test7.
@Test
void test7() {
try (Transaction transaction = graphDb.beginTx()) {
Relationship edgeAB = graph.makeEdge(transaction, "a", "b");
Relationship edgeBC = graph.makeEdge(transaction, "b", "c");
Relationship edgeCD = graph.makeEdge(transaction, "c", "d");
Relationship edgeDE = graph.makeEdge(transaction, "d", "e");
Relationship edgeAB2 = graph.makeEdge(transaction, "a", "b2");
Relationship edgeB2C = graph.makeEdge(transaction, "b2", "c");
Relationship edgeCD2 = graph.makeEdge(transaction, "c", "d2");
Relationship edgeD2E = graph.makeEdge(transaction, "d2", "e");
Dijkstra<Double> dijkstra = new Dijkstra<>(0.0, graph.getNode(transaction, "a"), graph.getNode(transaction, "e"), (relationship, direction) -> 1.0, new DoubleAdder(), Double::compareTo, Direction.OUTGOING, MyRelTypes.R1);
// path discovery flags
boolean pathBD = false;
boolean pathB2D = false;
boolean pathBD2 = false;
boolean pathB2D2 = false;
List<List<Entity>> paths = dijkstra.getPaths();
assertEquals(4, paths.size());
for (List<Entity> path : paths) {
assertEquals(9, path.size());
assertEquals(path.get(0), graph.getNode(transaction, "a"));
assertEquals(path.get(4), graph.getNode(transaction, "c"));
assertEquals(path.get(8), graph.getNode(transaction, "e"));
// first choice
if (path.get(2).equals(graph.getNode(transaction, "b"))) {
assertEquals(path.get(1), edgeAB);
assertEquals(path.get(3), edgeBC);
} else {
assertEquals(path.get(1), edgeAB2);
assertEquals(path.get(2), graph.getNode(transaction, "b2"));
assertEquals(path.get(3), edgeB2C);
}
// second choice
if (path.get(6).equals(graph.getNode(transaction, "d"))) {
assertEquals(path.get(5), edgeCD);
assertEquals(path.get(7), edgeDE);
} else {
assertEquals(path.get(5), edgeCD2);
assertEquals(path.get(6), graph.getNode(transaction, "d2"));
assertEquals(path.get(7), edgeD2E);
}
// combinations
if (path.get(2).equals(graph.getNode(transaction, "b"))) {
if (path.get(6).equals(graph.getNode(transaction, "d"))) {
pathBD = true;
} else if (path.get(6).equals(graph.getNode(transaction, "d2"))) {
pathBD2 = true;
}
} else {
if (path.get(6).equals(graph.getNode(transaction, "d"))) {
pathB2D = true;
} else if (path.get(6).equals(graph.getNode(transaction, "d2"))) {
pathB2D2 = true;
}
}
}
assertTrue(pathBD);
assertTrue(pathB2D);
assertTrue(pathBD2);
assertTrue(pathB2D2);
transaction.commit();
}
}
use of org.neo4j.graphdb.Entity in project neo4j by neo4j.
the class FulltextProceduresTestSupport method assertQueryFindsIds.
static void assertQueryFindsIds(GraphDatabaseService db, boolean queryNodes, String index, String query, LongHashSet ids) {
// Create a defensive copy, because we're going to modify this instance.
ids = new LongHashSet(ids);
String queryCall = queryNodes ? QUERY_NODES : QUERY_RELS;
long[] expectedIds = ids.toArray();
MutableLongSet actualIds = new LongHashSet();
try (Transaction tx = db.beginTx()) {
LongFunction<Entity> getEntity = queryNodes ? tx::getNodeById : tx::getRelationshipById;
Result result = tx.execute(format(queryCall, index, query));
Double score = Double.MAX_VALUE;
while (result.hasNext()) {
Map<String, Object> entry = result.next();
long nextId = ((Entity) entry.get(queryNodes ? NODE : RELATIONSHIP)).getId();
Double nextScore = (Double) entry.get(SCORE);
assertThat(nextScore).isLessThanOrEqualTo(score);
score = nextScore;
actualIds.add(nextId);
if (!ids.remove(nextId)) {
String msg = "This id was not expected: " + nextId;
failQuery(getEntity, index, query, ids, expectedIds, actualIds, msg);
}
}
if (!ids.isEmpty()) {
String msg = "Not all expected ids were found: " + ids;
failQuery(getEntity, index, query, ids, expectedIds, actualIds, msg);
}
tx.commit();
}
}
use of org.neo4j.graphdb.Entity in project neo4j by neo4j.
the class FulltextIndexTransactionStateTest method queryResultsMustIncludeOldPropertyValuesWhenRemovalsAreUndone.
@MethodSource("entityTypeProvider")
@ParameterizedTest
void queryResultsMustIncludeOldPropertyValuesWhenRemovalsAreUndone(EntityUtil entityUtil) {
createIndexAndWait(entityUtil);
long entityId;
try (Transaction tx = db.beginTx()) {
entityId = entityUtil.createEntityWithProperty(tx, "primo");
tx.commit();
}
try (Transaction tx = db.beginTx()) {
entityUtil.assertQueryFindsIdsInOrder(tx, "primo", entityId);
Entity entity = entityUtil.getEntity(tx, entityId);
entity.removeProperty(PROP);
entityUtil.assertQueryFindsIdsInOrder(tx, "primo");
entity.setProperty(PROP, "primo");
entityUtil.assertQueryFindsIdsInOrder(tx, "primo", entityId);
tx.commit();
}
}
use of org.neo4j.graphdb.Entity in project neo4j by neo4j.
the class Neo4jJsonCodecTest method testIteratorWriting.
@Test
void testIteratorWriting() throws IOException {
// Given
Entity entity = mock(Entity.class);
when(entity.getAllProperties()).thenThrow(RuntimeException.class);
// When
assertThrows(Exception.class, () -> jsonCodec.writeValue(jsonGenerator, Collections.singletonList(entity)));
// Then
verify(jsonGenerator).writeEndArray();
}
use of org.neo4j.graphdb.Entity in project neo4j by neo4j.
the class Neo4jJsonCodecTest method testPathWriting.
@Test
void testPathWriting() throws IOException {
// Given
Path path = mock(Path.class);
Entity entity = mock(Entity.class);
when(entity.getAllProperties()).thenThrow(RuntimeException.class);
when(path.iterator()).thenReturn(Arrays.asList(entity).listIterator());
// When
assertThrows(Exception.class, () -> jsonCodec.writeValue(jsonGenerator, path));
// Then
verify(jsonGenerator).writeEndArray();
}
Aggregations