Skip to content

Commit

Permalink
Make apoc.atomic.add and related procedures work with Integer propert…
Browse files Browse the repository at this point in the history
…ies (#2139) (#2163)

Fixes #1157

Co-authored-by: Giuseppe Villani <giuseppe.villani@larus-ba.it>

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Giuseppe Villani <giuseppe.villani@larus-ba.it>
  • Loading branch information
github-actions[bot] and vga91 committed Sep 28, 2021
1 parent efc9750 commit 44f2056
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
8 changes: 8 additions & 0 deletions core/src/main/java/apoc/atomic/util/AtomicUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ public class AtomicUtils {
public static Number sum(Number oldValue, Number number){
if(oldValue instanceof Long)
return oldValue.longValue() + number.longValue();
if(oldValue instanceof Integer)
return oldValue.intValue() + number.intValue();
if(oldValue instanceof Double)
return oldValue.doubleValue() + number.doubleValue();
if(oldValue instanceof Float)
return oldValue.floatValue() + number.floatValue();
if(oldValue instanceof Short)
return oldValue.shortValue() + number.shortValue();
if(oldValue instanceof Byte)
Expand All @@ -22,8 +26,12 @@ public static Number sum(Number oldValue, Number number){
public static Number sub(Number oldValue, Number number){
if(oldValue instanceof Long)
return oldValue.longValue() - number.longValue();
if(oldValue instanceof Integer)
return oldValue.intValue() - number.intValue();
if(oldValue instanceof Double)
return oldValue.doubleValue() - number.doubleValue();
if(oldValue instanceof Float)
return oldValue.floatValue() - number.floatValue();
if(oldValue instanceof Short)
return oldValue.shortValue() - number.shortValue();
if(oldValue instanceof Byte)
Expand Down
38 changes: 38 additions & 0 deletions core/src/test/java/apoc/atomic/AtomicTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,44 @@ public class AtomicTest {
TestUtil.registerProcedure(db, Atomic.class);
}

@Test
public void testAddAndSubInteger(){
db.executeTransactionally("CREATE (p:Person {name:'Tom'})");
try(Transaction tx = db.beginTx()) {
final Node node = tx.getAllNodes().stream().findFirst().orElse(null);
node.setProperty("age", 1);
tx.commit();
}
db.executeTransactionally("MATCH (n:Person {name:'Tom'}) CALL apoc.atomic.add(n,$property,$value) YIELD container RETURN count(*)",
map("property","age","value",10));
int age = TestUtil.singleResultFirstColumn(db, "MATCH (n:Person {name:'Tom'}) RETURN n.age as age");
assertEquals(11, age);
db.executeTransactionally("MATCH (n:Person {name:'Tom'}) CALL apoc.atomic.subtract(n,$property,$value) YIELD container RETURN count(*)",
map("property","age","value",5));
int ageSub = TestUtil.singleResultFirstColumn(db, "MATCH (n:Person {name:'Tom'}) RETURN n.age as age");
assertEquals(6, ageSub);
}

@Test
public void testAddAndSubFloat() {
db.executeTransactionally("CREATE (p:Person {name:'Tom'})");
try(Transaction tx = db.beginTx()) {
final Node node = tx.getAllNodes().stream().findFirst().orElse(null);
node.setProperty("age", 2.4F);
tx.commit();
}

db.executeTransactionally("MATCH (n:Person {name:'Tom'}) CALL apoc.atomic.add(n,$property,$value) YIELD container RETURN count(*)",
map("property","age","value",1.5F));
float age = TestUtil.singleResultFirstColumn(db, "MATCH (n:Person {name:'Tom'}) RETURN n.age as age");
assertEquals(3.9F, age, 0);

db.executeTransactionally("MATCH (n:Person {name:'Tom'}) CALL apoc.atomic.subtract(n,$property,$value) YIELD container RETURN count(*)",
map("property","age","value",0.5F));
float ageSub = TestUtil.singleResultFirstColumn(db, "MATCH (n:Person {name:'Tom'}) RETURN n.age as age");
assertEquals(3.4F, ageSub, 0);
}

@Test
public void testAddLong(){
db.executeTransactionally("CREATE (p:Person {name:'Tom',age: 40}) CREATE (c:Person {name:'John',age: 40}) CREATE (a:Person {name:'Anne',age: 22})");
Expand Down

0 comments on commit 44f2056

Please sign in to comment.