Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[eBlQUOcs] Bug in apoc.create.cloneToVirtual #375

Merged
merged 1 commit into from
Apr 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions common/src/main/java/apoc/result/VirtualPath.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package apoc.result;

import org.apache.commons.collections4.CollectionUtils;
import org.neo4j.graphdb.Entity;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Path;
Expand All @@ -8,6 +9,7 @@

import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -127,17 +129,19 @@ public String toString() {
}

private void requireConnected(Relationship relationship) {
Node previousEndNode;
final List<Node> previousNodes = getPreviousNodes();
boolean isRelConnectedToPrevious = CollectionUtils.containsAny( previousNodes, relationship.getNodes() );
if (!isRelConnectedToPrevious) {
throw new IllegalArgumentException("Relationship is not part of current path.");
}
}

private List<Node> getPreviousNodes() {
Relationship previousRelationship = lastRelationship();
if (previousRelationship != null) {
previousEndNode = previousRelationship.getEndNode();
} else {
previousEndNode = endNode();
}
if (!relationship.getStartNode().equals(previousEndNode)
&& !relationship.getEndNode().equals(previousEndNode)) {
throw new IllegalArgumentException("Relationship is not part of current path.");
return Arrays.asList(previousRelationship.getNodes());
}
return List.of(endNode());
}

public static final class Builder {
Expand Down
28 changes: 28 additions & 0 deletions core/src/test/java/apoc/create/CreateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,34 @@ public void testClonePathShouldNotEditOriginalOne() {
});
}

@Test
public void testClonePathWithMixedDirectionRelationships() {
// rel `:b` is to the left, rel `d` is to the right
db.executeTransactionally("CREATE (:a {id: 1})<-[:b {id: 10}]-(:c {id: 2})-[:d {id: 20}]->(:e {id: 3})");

testCall(db, """
MATCH p = (:a)<-[:b]-(:c)-[:d]->(:e)
CALL apoc.create.clonePathToVirtual(p) YIELD path RETURN path""", r -> {
Path path = (Path) r.get("path");
Iterator<Node> nodes = path.nodes().iterator();
Node node = nodes.next();
assertEquals(Map.of("id", 1L), node.getAllProperties());
node = nodes.next();
assertEquals(Map.of("id", 2L), node.getAllProperties());
node = nodes.next();
assertEquals(Map.of("id", 3L), node.getAllProperties());
assertFalse(nodes.hasNext());

Iterator<Relationship> rels = path.relationships().iterator();
Relationship rel = rels.next();
assertEquals(Map.of("id", 10L), rel.getAllProperties());
rel = rels.next();
assertEquals(Map.of("id", 20L), rel.getAllProperties());
assertFalse(rels.hasNext());

});
}

@Test
public void testClonePathShouldNotDuplicateRelsWithMultipaths() {
//create path with single rels
Expand Down