From 5c5934f6950fe53d6844f118271469abdf93ba4c Mon Sep 17 00:00:00 2001 From: Eli Sherer Date: Wed, 13 Nov 2024 13:44:59 +0200 Subject: [PATCH] unit tests work --- .../TransformerFunctionContains.test.ts | 38 ---------- .../functions/TransformerFunctionEval.test.ts | 54 -------------- javascript/json-transform/test/server.ts | 2 +- test/src/assertTransformation.ts | 2 +- test/tests/functions/eval.json | 73 +++++++++++++++++++ test/tests/transformations/jsonpath.json | 26 +++++++ .../transformations/transformations.json | 19 +---- 7 files changed, 102 insertions(+), 112 deletions(-) delete mode 100644 javascript/json-transform/src/__tests__/functions/TransformerFunctionContains.test.ts delete mode 100644 javascript/json-transform/src/__tests__/functions/TransformerFunctionEval.test.ts create mode 100644 test/tests/functions/eval.json diff --git a/javascript/json-transform/src/__tests__/functions/TransformerFunctionContains.test.ts b/javascript/json-transform/src/__tests__/functions/TransformerFunctionContains.test.ts deleted file mode 100644 index 0330529..0000000 --- a/javascript/json-transform/src/__tests__/functions/TransformerFunctionContains.test.ts +++ /dev/null @@ -1,38 +0,0 @@ -import { describe, test } from "vitest"; -import { assertTransformation } from "../BaseTransformationTest"; - -describe("TransformerFunctionContains", () => { - test("object", async () => { - await assertTransformation( - [0, [], "a"], - { - $$contains: "$", - that: "a", - }, - true, - ); - // with transformation - await assertTransformation( - "a", - { - $$contains: ["b", "$"], - that: "a", - }, - true, - ); - - await assertTransformation( - [0, [], "a"], - { - $$contains: "$", - that: "b", - }, - false, - ); - }); - - test("inline", async () => { - await assertTransformation([0, [], "a"], "$$contains(a):$", true); - await assertTransformation([0, [], "a"], "$$contains(b):$", false); - }); -}); diff --git a/javascript/json-transform/src/__tests__/functions/TransformerFunctionEval.test.ts b/javascript/json-transform/src/__tests__/functions/TransformerFunctionEval.test.ts deleted file mode 100644 index 29c4c52..0000000 --- a/javascript/json-transform/src/__tests__/functions/TransformerFunctionEval.test.ts +++ /dev/null @@ -1,54 +0,0 @@ -import { describe, test } from "vitest"; -import { assertTransformation } from "../BaseTransformationTest"; -import BigNumber from "bignumber.js"; -import { BigDecimal } from "../../functions/common/FunctionHelpers"; - -describe("TransformerFunctionEval", () => { - class Holder { - public value: BigNumber | null; - constructor(value: BigNumber | null) { - this.value = value; - } - } - - test("inline", async () => { - await assertTransformation( - { - a: 42, - b: "$.a", - }, - "$$eval:$.b", - 42, - ); - }); - - test("object", async () => { - const arr = [ - new Holder(new BigDecimal(4)), - new Holder(new BigDecimal(2)), - new Holder(new BigDecimal("13.45")), - new Holder(null), - ]; - await assertTransformation( - arr, - { - $$eval: { - $$join: ["\\$", "$avg:", "\\$", "..value"], - }, - }, - BigDecimal(4.8625), - ); - - await assertTransformation( - arr, - { - $$eval: { - $$jsonparse: { - $$join: ["{", '"$$avg"', ":", '"$"', ', "by":"##current.value"', "}"], - }, - }, - }, - BigDecimal(4.8625), - ); - }); -}); diff --git a/javascript/json-transform/test/server.ts b/javascript/json-transform/test/server.ts index ce5e462..d25b5a1 100644 --- a/javascript/json-transform/test/server.ts +++ b/javascript/json-transform/test/server.ts @@ -4,7 +4,7 @@ import { createServer, type IncomingMessage, type ServerResponse } from "http"; import JsonTransformer from "../src/JsonTransformer"; import { HTMLHeaders, JSONHeaders, parseBody, send, JSONBig } from "./serverUtils"; -const PORT = 10001; +const PORT = 10002; process.on("uncaughtException", function (err) { console.info("Some unhandled error occurred"); diff --git a/test/src/assertTransformation.ts b/test/src/assertTransformation.ts index 977ae54..9e819f8 100644 --- a/test/src/assertTransformation.ts +++ b/test/src/assertTransformation.ts @@ -28,7 +28,7 @@ export const JSONBig = { const hasOwn = Object.prototype.hasOwnProperty; const PODS = { - javascript: "http://localhost:10001/api/v1/transform", + javascript: "http://localhost:10002/api/v1/transform", java: "http://localhost:10000/api/v1/transform" } diff --git a/test/tests/functions/eval.json b/test/tests/functions/eval.json new file mode 100644 index 0000000..642061d --- /dev/null +++ b/test/tests/functions/eval.json @@ -0,0 +1,73 @@ +[ + { + "name": "inline", + "given": { + "input": { + "a": 42, + "b": "$.a" + }, + "definition": "$$eval:$.b" + }, + "expect": { + "equal": 42 + } + }, + { + "name": "object - 1", + "given": { + "input": [ + { + "value": 4 + }, + { + "value": 2 + }, + { + "value": 13.45 + }, + { + } + ], + "definition": { + "$$eval": { + "$$join": [ + "\\$", + "$avg:", + "\\$", + "..value" + ] + } + } + }, + "expect": { + "equal": 6.483333333333333 + } + }, + { + "name": "object - 2", + "given": { + "input": [ + { + "value": 4 + }, + { + "value": 2 + }, + { + "value": 13.45 + }, + { + "value": null + } + ], + "definition": { + "$$eval": { + "$$jsonparse": "{\"$$avg\":\"$\", \"by\":\"##current.value\"}" + } + } + }, + "expect": { + "equal": 4.8625 + } + } +] \ No newline at end of file diff --git a/test/tests/transformations/jsonpath.json b/test/tests/transformations/jsonpath.json index a2c59f9..24c6e54 100644 --- a/test/tests/transformations/jsonpath.json +++ b/test/tests/transformations/jsonpath.json @@ -47,6 +47,32 @@ ] } }, + { + "name": "Query indefinite path (descendent; 2)", + "given": { + "input": { + "x": [ + { + "a": 1 + }, + { + "a": 2 + }, + { + "a": null + } + ] + }, + "definition": "$..a" + }, + "expect": { + "equal": [ + 1, + 2, + null + ] + } + }, { "name": "Query indefinite path (wildcard)", "given": { diff --git a/test/tests/transformations/transformations.json b/test/tests/transformations/transformations.json index c847d9e..b281c6f 100644 --- a/test/tests/transformations/transformations.json +++ b/test/tests/transformations/transformations.json @@ -128,6 +128,7 @@ }, { "name": "Spread - remove by hash null", + "only": true, "given": { "input": { "a": "A", @@ -144,24 +145,6 @@ } } }, - { - "name": "Spread - remove by null", - "given": { - "input": { - "a": "A", - "b": "B" - }, - "definition": { - "*": "$", - "a": null - } - }, - "expect": { - "equal": { - "b": "B" - } - } - }, { "name": "Spread - as array", "given": {