From 1ebdc369cdffc7845553bbe5a3cea38341be03ae Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Wed, 5 Jan 2022 15:01:28 +0000 Subject: [PATCH] Adding test case to check that query dynamic variables are cached correctly and error scenario is handled. --- packages/server/__mocks__/node-fetch.ts | 12 +++ .../server/src/api/routes/tests/query.spec.js | 79 +++++++++++-------- 2 files changed, 59 insertions(+), 32 deletions(-) diff --git a/packages/server/__mocks__/node-fetch.ts b/packages/server/__mocks__/node-fetch.ts index 6bd55186b5..5f5e53657d 100644 --- a/packages/server/__mocks__/node-fetch.ts +++ b/packages/server/__mocks__/node-fetch.ts @@ -1,5 +1,6 @@ module FetchMock { const fetch = jest.requireActual("node-fetch") + let failCount = 0 module.exports = async (url: any, opts: any) => { function json(body: any, status = 200) { @@ -63,6 +64,17 @@ module FetchMock { opts, value: "", }) + } else if (url.includes("failonce.com")) { + failCount++ + if (failCount === 1) { + return json({ message: "error" }, 500) + } else { + return json({ + fails: failCount - 1, + url, + opts, + }) + } } return fetch(url, opts) } diff --git a/packages/server/src/api/routes/tests/query.spec.js b/packages/server/src/api/routes/tests/query.spec.js index 8589f4ded6..c8f842e584 100644 --- a/packages/server/src/api/routes/tests/query.spec.js +++ b/packages/server/src/api/routes/tests/query.spec.js @@ -11,6 +11,7 @@ authDb.isProdAppID = mockIsProdAppID const setup = require("./utilities") const { checkBuilderEndpoint } = require("./utilities/TestFunctions") +const { checkCacheForDynamicVariable } = require("../../../threads/utils") const { basicQuery, basicDatasource } = setup.structures describe("/queries", () => { @@ -239,33 +240,7 @@ describe("/queries", () => { }) } - it("should work with static variables", async () => { - const datasource = await restDatasource({ - staticVariables: { - variable: "google", - variable2: "1", - }, - }) - const res = await request - .post(`/api/queries/preview`) - .send({ - datasourceId: datasource._id, - parameters: {}, - fields: { - path: "www.{{ variable }}.com", - queryString: "test={{ variable2 }}", - }, - queryVerb: "read", - }) - .set(config.defaultHeaders()) - .expect("Content-Type", /json/) - .expect(200) - // these responses come from the mock - expect(res.body.schemaFields).toEqual(["url", "opts", "value"]) - expect(res.body.rows[0].url).toEqual("http://www.google.com?test=1") - }) - - it("should work with dynamic variables", async () => { + async function dynamicVariableDatasource() { const datasource = await restDatasource() const basedOnQuery = await config.createQuery({ ...basicQuery(datasource._id), @@ -281,22 +256,62 @@ describe("/queries", () => { ] } }) - const res = await request + return { datasource, query: basedOnQuery } + } + + async function preview(datasource, fields) { + return await request .post(`/api/queries/preview`) .send({ datasourceId: datasource._id, parameters: {}, - fields: { - path: "www.google.com", - queryString: "test={{ variable3 }}", - }, + fields, queryVerb: "read", }) .set(config.defaultHeaders()) .expect("Content-Type", /json/) .expect(200) + } + + it("should work with static variables", async () => { + const datasource = await restDatasource({ + staticVariables: { + variable: "google", + variable2: "1", + }, + }) + const res = await preview(datasource, { + path: "www.{{ variable }}.com", + queryString: "test={{ variable2 }}", + }) + // these responses come from the mock + expect(res.body.schemaFields).toEqual(["url", "opts", "value"]) + expect(res.body.rows[0].url).toEqual("http://www.google.com?test=1") + }) + + it("should work with dynamic variables", async () => { + const { datasource } = await dynamicVariableDatasource() + const res = await preview(datasource, { + path: "www.google.com", + queryString: "test={{ variable3 }}", + }) expect(res.body.schemaFields).toEqual(["url", "opts", "value"]) expect(res.body.rows[0].url).toContain("doctype html") }) + + it("check that it automatically retries on fail with cached dynamics", async () => { + const { datasource, query: base } = await dynamicVariableDatasource() + // preview once to cache + await preview(datasource, { path: "www.google.com", queryString: "test={{ variable3 }}" }) + // check its in cache + const contents = await checkCacheForDynamicVariable(base._id, "variable3") + expect(contents.rows.length).toEqual(1) + const res = await preview(datasource, { + path: "www.failonce.com", + queryString: "test={{ variable3 }}", + }) + expect(res.body.schemaFields).toEqual(["fails", "url", "opts"]) + expect(res.body.rows[0].fails).toEqual(1) + }) }) })