diff --git a/core/src/test/java/apoc/merge/MergeTest.java b/core/src/test/java/apoc/merge/MergeTest.java index ee65641b6..681eb9e51 100644 --- a/core/src/test/java/apoc/merge/MergeTest.java +++ b/core/src/test/java/apoc/merge/MergeTest.java @@ -61,6 +61,7 @@ public void testMergeNodeWithStats() { testMergeNodeCommon(true); } + // MERGE NODES private void testMergeNodeCommon(boolean isWithStats) { String procName = isWithStats ? "nodeWithStats" : "node"; @@ -111,33 +112,6 @@ public void testMergeWithNoLabel() { ); } - @Test - public void testMergeRelationships() { - db.executeTransactionally("create (:Person{name:'Foo'}), (:Person{name:'Bar'})"); - - testCall(db, "MERGE (s:Person{name:'Foo'}) MERGE (e:Person{name:'Bar'}) WITH s,e CALL apoc.merge.relationship(s, 'KNOWS', {rid:123}, {since:'Thu'}, e) YIELD rel RETURN rel", - (row) -> { - Relationship rel = (Relationship) row.get("rel"); - assertEquals("KNOWS", rel.getType().name()); - assertEquals(123L, rel.getProperty("rid")); - assertEquals("Thu", rel.getProperty("since")); - }); - - testCall(db, "MERGE (s:Person{name:'Foo'}) MERGE (e:Person{name:'Bar'}) WITH s,e CALL apoc.merge.relationship(s, 'KNOWS', {rid:123}, {since:'Fri'}, e) YIELD rel RETURN rel", - (row) -> { - Relationship rel = (Relationship) row.get("rel"); - assertEquals("KNOWS", rel.getType().name()); - assertEquals(123L, rel.getProperty("rid")); - assertEquals("Thu", rel.getProperty("since")); - }); - testCall(db, "MERGE (s:Person{name:'Foo'}) MERGE (e:Person{name:'Bar'}) WITH s,e CALL apoc.merge.relationship(s, 'OTHER', null, null, e) YIELD rel RETURN rel", - (row) -> { - Relationship rel = (Relationship) row.get("rel"); - assertEquals("OTHER", rel.getType().name()); - assertTrue(rel.getAllProperties().isEmpty()); - }); - } - @Test public void testMergeWithEmptyIdentityPropertiesShouldFail() { for (String idProps: new String[]{"null", "{}"}) { @@ -173,44 +147,22 @@ public void testMergeNodeWithEmptyLabelListShouldFail() { } } - @Test - public void testMergeRelWithNullRelTypeShouldFail() { - try { - testCall(db, "MERGE (s:Person{name:'Foo'}) MERGE (e:Person{name:'Bar'}) WITH s,e CALL apoc.merge.relationship(s, null, null, null, e) YIELD rel RETURN rel", - row -> assertTrue(row.get("rel") instanceof Relationship)); - fail(); - } catch (QueryExecutionException e) { - assertTrue(e.getMessage().contains("It is not possible to merge a relationship without a relationship type.")); - } - } - - @Test - public void testMergeWithEmptyRelTypeShouldFail() { - try { - testCall(db, "MERGE (s:Person{name:'Foo'}) MERGE (e:Person{name:'Bar'}) WITH s,e CALL apoc.merge.relationship(s, '', null, null, e) YIELD rel RETURN rel", - row -> assertTrue(row.get("rel") instanceof Relationship)); - fail(); - } catch (QueryExecutionException e) { - assertTrue(e.getMessage().contains("It is not possible to merge a relationship without a relationship type.")); - } - } - @Test public void testEscapeIdentityPropertiesWithSpecialCharactersShouldWork() { for (String key: new String[]{"normal", "i:d", "i-d", "i d"}) { Map identProps = MapUtil.map(key, "value"); Map params = MapUtil.map("identProps", identProps); - + testCall(db, "CALL apoc.merge.node(['Person'], $identProps) YIELD node RETURN node", params, - (row) -> { - Node node = (Node) row.get("node"); - assertNotNull(node); - assertTrue(node.hasProperty(key)); - assertEquals("value", node.getProperty(key)); - }); + (row) -> { + Node node = (Node) row.get("node"); + assertNotNull(node); + assertTrue(node.hasProperty(key)); + assertEquals("value", node.getProperty(key)); + }); } } - + @Test public void testLabelsWithSpecialCharactersShouldWork() { for (String label: new String[]{"Label with spaces", ":LabelWithColon", "label-with-dash", "LabelWithUmlautsÄÖÜ"}) { @@ -220,6 +172,34 @@ public void testLabelsWithSpecialCharactersShouldWork() { } } + // MERGE RELATIONSHIPS + @Test + public void testMergeRelationships() { + db.executeTransactionally("create (:Person{name:'Foo'}), (:Person{name:'Bar'})"); + + testCall(db, "MERGE (s:Person{name:'Foo'}) MERGE (e:Person{name:'Bar'}) WITH s,e CALL apoc.merge.relationship(s, 'KNOWS', {rid:123}, {since:'Thu'}, e) YIELD rel RETURN rel", + (row) -> { + Relationship rel = (Relationship) row.get("rel"); + assertEquals("KNOWS", rel.getType().name()); + assertEquals(123L, rel.getProperty("rid")); + assertEquals("Thu", rel.getProperty("since")); + }); + + testCall(db, "MERGE (s:Person{name:'Foo'}) MERGE (e:Person{name:'Bar'}) WITH s,e CALL apoc.merge.relationship(s, 'KNOWS', {rid:123}, {since:'Fri'}, e) YIELD rel RETURN rel", + (row) -> { + Relationship rel = (Relationship) row.get("rel"); + assertEquals("KNOWS", rel.getType().name()); + assertEquals(123L, rel.getProperty("rid")); + assertEquals("Thu", rel.getProperty("since")); + }); + testCall(db, "MERGE (s:Person{name:'Foo'}) MERGE (e:Person{name:'Bar'}) WITH s,e CALL apoc.merge.relationship(s, 'OTHER', null, null, e) YIELD rel RETURN rel", + (row) -> { + Relationship rel = (Relationship) row.get("rel"); + assertEquals("OTHER", rel.getType().name()); + assertTrue(rel.getAllProperties().isEmpty()); + }); + } + @Test public void testRelationshipTypesWithSpecialCharactersShouldWork() { for (String relType: new String[]{"Reltype with space", ":ReltypeWithCOlon", "rel-type-with-dash"}) { @@ -229,10 +209,29 @@ public void testRelationshipTypesWithSpecialCharactersShouldWork() { } } + @Test + public void testMergeRelWithNullRelTypeShouldFail() { + try { + testCall(db, "MERGE (s:Person{name:'Foo'}) MERGE (e:Person{name:'Bar'}) WITH s,e CALL apoc.merge.relationship(s, null, null, null, e) YIELD rel RETURN rel", + row -> assertTrue(row.get("rel") instanceof Relationship)); + fail(); + } catch (QueryExecutionException e) { + assertTrue(e.getMessage().contains("It is not possible to merge a relationship without a relationship type.")); + } + } - // MERGE EAGER TESTS - + @Test + public void testMergeWithEmptyRelTypeShouldFail() { + try { + testCall(db, "MERGE (s:Person{name:'Foo'}) MERGE (e:Person{name:'Bar'}) WITH s,e CALL apoc.merge.relationship(s, '', null, null, e) YIELD rel RETURN rel", + row -> assertTrue(row.get("rel") instanceof Relationship)); + fail(); + } catch (QueryExecutionException e) { + assertTrue(e.getMessage().contains("It is not possible to merge a relationship without a relationship type.")); + } + } + // MERGE EAGER TESTS @Test public void testMergeEagerNode() { testMergeEagerCommon(false);