Skip to content

Commit

Permalink
Mathematical operators grouping
Browse files Browse the repository at this point in the history
  • Loading branch information
angrykoala committed Jun 5, 2023
1 parent 2994b21 commit 1c1bd0c
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 30 deletions.
5 changes: 5 additions & 0 deletions .changeset/tasty-drinks-pay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@neo4j/cypher-builder": patch
---

Groups mathematical operators with parenthesis
20 changes: 10 additions & 10 deletions src/clauses/With.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ describe("CypherBuilder With", () => {
const queryResult = withQuery.build();
expect(queryResult.cypher).toMatchInlineSnapshot(`"WITH this0, var1, $param0"`);
expect(queryResult.params).toMatchInlineSnapshot(`
{
"param0": "Matrix",
}
`);
{
"param0": "Matrix",
}
`);
});

describe("With alias", () => {
Expand All @@ -76,13 +76,13 @@ describe("CypherBuilder With", () => {
const withQuery = new Cypher.With([expr, alias]);

const queryResult = withQuery.build();
expect(queryResult.cypher).toMatchInlineSnapshot(`"WITH $param0 + $param1 AS var0"`);
expect(queryResult.cypher).toMatchInlineSnapshot(`"WITH ($param0 + $param1) AS var0"`);
expect(queryResult.params).toMatchInlineSnapshot(`
{
"param0": "The ",
"param1": "Matrix",
}
`);
{
"param0": "The ",
"param1": "Matrix",
}
`);
});

test("With alias and delete", () => {
Expand Down
14 changes: 7 additions & 7 deletions src/expressions/functions/ListFunctions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ describe("List Functions", () => {

const queryResult = new TestClause(reduceFn).build();
expect(queryResult.cypher).toMatchInlineSnapshot(
`"reduce(var0 = $param0, var1 IN [ $param1, $param2 ] | var0 + var1)"`
`"reduce(var0 = $param0, var1 IN [ $param1, $param2 ] | (var0 + var1))"`
);
expect(queryResult.params).toMatchInlineSnapshot(`
{
"param0": 0,
"param1": 2,
"param2": 3,
}
`);
{
"param0": 0,
"param1": 2,
"param2": 3,
}
`);
});
});
24 changes: 12 additions & 12 deletions src/expressions/operations/math.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,51 +33,51 @@ describe("math operators", () => {
const queryResult = matchQuery.build();
expect(queryResult.cypher).toMatchInlineSnapshot(`
"MATCH (this0:\`Movie\`)
WHERE this0.released = 10 + $param0
WHERE this0.released = (10 + $param0)
RETURN this0"
`);

expect(queryResult.params).toMatchInlineSnapshot(`
{
"param0": 2000,
}
`);
{
"param0": 2000,
}
`);
});

test("plus", () => {
const add = Cypher.plus(new Cypher.Literal(10), new Cypher.Literal(3));
const { cypher } = new TestClause(add).build();
expect(cypher).toMatchInlineSnapshot(`"10 + 3"`);
expect(cypher).toMatchInlineSnapshot(`"(10 + 3)"`);
});

test("minus", () => {
const subtract = Cypher.minus(new Cypher.Literal(10), new Cypher.Literal(3));
const { cypher } = new TestClause(subtract).build();
expect(cypher).toMatchInlineSnapshot(`"10 - 3"`);
expect(cypher).toMatchInlineSnapshot(`"(10 - 3)"`);
});

test("divide", () => {
const divide = Cypher.divide(new Cypher.Literal(10), new Cypher.Literal(3));
const { cypher } = new TestClause(divide).build();
expect(cypher).toMatchInlineSnapshot(`"10 / 3"`);
expect(cypher).toMatchInlineSnapshot(`"(10 / 3)"`);
});

test("multiply", () => {
const multiply = Cypher.multiply(new Cypher.Literal(10), new Cypher.Literal(3));
const { cypher } = new TestClause(multiply).build();
expect(cypher).toMatchInlineSnapshot(`"10 * 3"`);
expect(cypher).toMatchInlineSnapshot(`"(10 * 3)"`);
});

test("mod", () => {
const mod = Cypher.mod(new Cypher.Literal(10), new Cypher.Literal(3));
const { cypher } = new TestClause(mod).build();
expect(cypher).toMatchInlineSnapshot(`"10 % 3"`);
expect(cypher).toMatchInlineSnapshot(`"(10 % 3)"`);
});

test("pow", () => {
const pow = Cypher.pow(new Cypher.Literal(10), new Cypher.Literal(3));
const { cypher } = new TestClause(pow).build();
expect(cypher).toMatchInlineSnapshot(`"10 ^ 3"`);
expect(cypher).toMatchInlineSnapshot(`"(10 ^ 3)"`);
});

test("complex expression", () => {
Expand All @@ -87,6 +87,6 @@ describe("math operators", () => {
const multiply = Cypher.multiply(divide, new Cypher.Literal(3));
const pow = Cypher.pow(multiply, new Cypher.Literal(3));
const { cypher } = new TestClause(pow).build();
expect(cypher).toMatchInlineSnapshot(`"10 + 3 + 5 - 3 / 2 * 3 ^ 3"`);
expect(cypher).toMatchInlineSnapshot(`"(((((10 + 3 + 5) - 3) / 2) * 3) ^ 3)"`);
});
});
2 changes: 1 addition & 1 deletion src/expressions/operations/math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class MathOp extends CypherASTNode {
*/
public getCypher(env: CypherEnvironment): string {
const exprs = this.exprs.map((e) => e.getCypher(env));
return exprs.join(` ${this.operator} `);
return `(${exprs.join(` ${this.operator} `)})`;
}
}

Expand Down

0 comments on commit 1c1bd0c

Please sign in to comment.