From 26441255cf2b28522d3f9ad8c416bfdee2bd8a7f Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Mon, 9 May 2022 13:28:01 +0100 Subject: [PATCH] Unit tests for the utility functions that have changed around app ID checks/generation./ --- packages/backend-core/src/db/conversions.js | 6 ++ .../backend-core/src/db/tests/utils.spec.js | 61 +++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 packages/backend-core/src/db/tests/utils.spec.js diff --git a/packages/backend-core/src/db/conversions.js b/packages/backend-core/src/db/conversions.js index b820a643e8..455cc712d8 100644 --- a/packages/backend-core/src/db/conversions.js +++ b/packages/backend-core/src/db/conversions.js @@ -27,6 +27,9 @@ exports.isDevApp = app => { * @returns {string} the dev app ID which can be used for dev database. */ exports.getDevelopmentAppID = appId => { + if (!appId || appId.startsWith(APP_DEV_PREFIX)) { + return appId + } // split to take off the app_ element, then join it together incase any other app_ exist const split = appId.split(APP_PREFIX) split.shift() @@ -38,6 +41,9 @@ exports.getDevelopmentAppID = appId => { * Convert a development app ID to a deployed app ID. */ exports.getProdAppID = appId => { + if (!appId || !appId.startsWith(APP_DEV_PREFIX)) { + return appId + } // split to take off the app_dev element, then join it together incase any other app_ exist const split = appId.split(APP_DEV_PREFIX) split.shift() diff --git a/packages/backend-core/src/db/tests/utils.spec.js b/packages/backend-core/src/db/tests/utils.spec.js new file mode 100644 index 0000000000..ebef670a81 --- /dev/null +++ b/packages/backend-core/src/db/tests/utils.spec.js @@ -0,0 +1,61 @@ +const { + generateAppID, + getDevelopmentAppID, + getProdAppID, + isDevAppID, + isProdAppID, +} = require("../utils") + +function getID() { + const appId = generateAppID() + const split = appId.split("_") + const uuid = split[split.length - 1] + const devAppId = `app_dev_${uuid}` + return { appId, devAppId, split, uuid } +} + +describe("app ID manipulation", () => { + it("should be able to generate a new app ID", () => { + expect(generateAppID().startsWith("app_")).toEqual(true) + }) + + it("should be able to convert a production app ID to development", () => { + const { appId, uuid } = getID() + expect(getDevelopmentAppID(appId)).toEqual(`app_dev_${uuid}`) + }) + + it("should be able to convert a development app ID to development", () => { + const { devAppId, uuid } = getID() + expect(getDevelopmentAppID(devAppId)).toEqual(`app_dev_${uuid}`) + }) + + it("should be able to convert a development ID to a production", () => { + const { devAppId, uuid } = getID() + expect(getProdAppID(devAppId)).toEqual(`app_${uuid}`) + }) + + it("should be able to convert a production ID to production", () => { + const { appId, uuid } = getID() + expect(getProdAppID(appId)).toEqual(`app_${uuid}`) + }) + + it("should be able to confirm dev app ID is development", () => { + const { devAppId } = getID() + expect(isDevAppID(devAppId)).toEqual(true) + }) + + it("should be able to confirm prod app ID is not development", () => { + const { appId } = getID() + expect(isDevAppID(appId)).toEqual(false) + }) + + it("should be able to confirm prod app ID is prod", () => { + const { appId } = getID() + expect(isProdAppID(appId)).toEqual(true) + }) + + it("should be able to confirm dev app ID is not prod", () => { + const { devAppId } = getID() + expect(isProdAppID(devAppId)).toEqual(false) + }) +}) \ No newline at end of file