From afa1828e30609dae9a53327802cc5a6b1371bb93 Mon Sep 17 00:00:00 2001 From: Eugen Klymniuk Date: Fri, 20 Sep 2024 12:22:16 +0100 Subject: [PATCH] feat(csi-634): added mock-knex lib to mock mysql in unit-tests (#1113) --- package-lock.json | 34 +++ package.json | 2 + .../participant/externalParticipantCached.js | 3 +- .../transfer/facade-withMockKnex.test.js | 101 ++++++++ test/unit/models/transfer/facade.test.js | 217 ------------------ 5 files changed, 138 insertions(+), 219 deletions(-) create mode 100644 test/unit/models/transfer/facade-withMockKnex.test.js diff --git a/package-lock.json b/package-lock.json index 696f8aab9..c019dc562 100644 --- a/package-lock.json +++ b/package-lock.json @@ -52,11 +52,13 @@ "require-glob": "^4.1.0" }, "devDependencies": { + "@types/mock-knex": "0.4.8", "async-retry": "1.3.3", "audit-ci": "^7.1.0", "get-port": "5.1.1", "jsdoc": "4.0.3", "jsonpath": "1.1.1", + "mock-knex": "0.4.13", "nodemon": "3.1.6", "npm-check-updates": "17.1.2", "nyc": "17.1.0", @@ -2138,6 +2140,15 @@ "integrity": "sha512-hov8bUuiLiyFPGyFPE1lwWhmzYbirOXQNNo40+y3zow8aFVTeyn3VWL0VFFfdNddA8S4Vf0Tc062rzyNr7Paag==", "dev": true }, + "node_modules/@types/mock-knex": { + "version": "0.4.8", + "resolved": "https://registry.npmjs.org/@types/mock-knex/-/mock-knex-0.4.8.tgz", + "integrity": "sha512-xRoaH9GmsgP5JBdMadzJSg/63HCifgJZsWmCJ5Z1rA36Fg3Y7Yb03dMzMIk5sHnBWcPkWqY/zyDO4nStI+Frbg==", + "dev": true, + "dependencies": { + "@types/node": "*" + } + }, "node_modules/@types/node": { "version": "20.12.7", "resolved": "https://registry.npmjs.org/@types/node/-/node-20.12.7.tgz", @@ -9154,6 +9165,29 @@ "lodash": "^4.17.21" } }, + "node_modules/mock-knex": { + "version": "0.4.13", + "resolved": "https://registry.npmjs.org/mock-knex/-/mock-knex-0.4.13.tgz", + "integrity": "sha512-UmZlxiJH7bBdzjSWcrLJ1tnLfPNL7GfJO1IWL4sHWfMzLqdA3VAVWhotq1YiyE5NwVcrQdoXj3TGGjhTkBeIcQ==", + "dev": true, + "dependencies": { + "bluebird": "^3.4.1", + "lodash": "^4.14.2", + "semver": "^5.3.0" + }, + "peerDependencies": { + "knex": "> 0.8" + } + }, + "node_modules/mock-knex/node_modules/semver": { + "version": "5.7.2", + "resolved": "https://registry.npmjs.org/semver/-/semver-5.7.2.tgz", + "integrity": "sha512-cBznnQ9KjJqU67B52RMC65CMarK2600WFnbkcaiwWq3xy/5haFJlshgnpjovMVJ+Hff49d8GEn0b87C5pDQ10g==", + "dev": true, + "bin": { + "semver": "bin/semver" + } + }, "node_modules/modify-values": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/modify-values/-/modify-values-1.0.1.tgz", diff --git a/package.json b/package.json index d0c3b408f..b9d5b9f2a 100644 --- a/package.json +++ b/package.json @@ -127,11 +127,13 @@ "mysql": "2.18.1" }, "devDependencies": { + "@types/mock-knex": "0.4.8", "async-retry": "1.3.3", "audit-ci": "^7.1.0", "get-port": "5.1.1", "jsdoc": "4.0.3", "jsonpath": "1.1.1", + "mock-knex": "0.4.13", "nodemon": "3.1.6", "npm-check-updates": "17.1.2", "nyc": "17.1.0", diff --git a/src/models/participant/externalParticipantCached.js b/src/models/participant/externalParticipantCached.js index a0bfb24db..9086d8acd 100644 --- a/src/models/participant/externalParticipantCached.js +++ b/src/models/participant/externalParticipantCached.js @@ -58,7 +58,7 @@ const getExternalParticipantsCached = async () => { ).startTimer() let cachedParticipants = cacheClient.get(epAllCacheKey) - let hit = false + const hit = !!cachedParticipants if (!cachedParticipants) { const allParticipants = await externalParticipantModel.getAll() @@ -67,7 +67,6 @@ const getExternalParticipantsCached = async () => { } else { // unwrap participants list from catbox structure cachedParticipants = cachedParticipants.item - hit = true } histTimer({ success: true, queryName, hit }) diff --git a/test/unit/models/transfer/facade-withMockKnex.test.js b/test/unit/models/transfer/facade-withMockKnex.test.js new file mode 100644 index 000000000..8c2e98f62 --- /dev/null +++ b/test/unit/models/transfer/facade-withMockKnex.test.js @@ -0,0 +1,101 @@ +/***** + License + -------------- + Copyright © 2017 Bill & Melinda Gates Foundation + The Mojaloop files are made available by the Bill & Melinda Gates Foundation under the Apache License, Version 2.0 (the "License") and you may not use these files except in compliance with the License. You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 + Unless required by applicable law or agreed to in writing, the Mojaloop files are distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. + + Contributors + -------------- + This is the official list of the Mojaloop project contributors for this file. + Names of the original copyright holders (individuals or organizations) + should be listed with a '*' in the first column. People who have + contributed from an organization can be listed under the organization + that actually holds the copyright for their contributions (see the + Gates Foundation organization for an example). Those individuals should have + their names indented and be marked with a '-'. Email address can be added + optionally within square brackets . + * Gates Foundation + - Name Surname + + * Eugen Klymniuk + -------------- + **********/ + +const Database = require('@mojaloop/database-lib/src/database') + +const Test = require('tapes')(require('tape')) +const knex = require('knex') +const mockKnex = require('mock-knex') +const Proxyquire = require('proxyquire') + +const config = require('#src/lib/config') +const { tryCatchEndTest } = require('#test/util/helpers') + +let transferFacade + +Test('Transfer facade Tests (with mockKnex) -->', async (transferFacadeTest) => { + const db = new Database() + db._knex = knex(config.DATABASE) + mockKnex.mock(db._knex) + + await db.connect(config.DATABASE) + + // we need to override the singleton Db (from ../lib/db), coz it was already modified by other unit-tests! + transferFacade = Proxyquire('#src/models/transfer/facade', { + '../../lib/db': db, + './transferExtension': Proxyquire('#src/models/transfer/transferExtension', { '../../lib/db': db }) + }) + + let tracker // allow to catch and respond to DB queries: https://www.npmjs.com/package/mock-knex#tracker + + await transferFacadeTest.beforeEach(async t => { + tracker = mockKnex.getTracker() + tracker.install() + t.end() + }) + + await transferFacadeTest.afterEach(t => { + tracker.uninstall() + t.end() + }) + + await transferFacadeTest.test('getById Method Tests -->', (getByIdTest) => { + getByIdTest.test('should find zero records', tryCatchEndTest(async (t) => { + const id = Date.now() + + tracker.on('query', (query) => { + if (query.bindings[0] === id && query.method === 'first') { + return query.response(null) + } + query.reject(new Error('Mock DB error')) + }) + const result = await transferFacade.getById(id) + t.equal(result, null, 'no transfers were found') + })) + + getByIdTest.test('should find transfer by id', tryCatchEndTest(async (t) => { + const id = Date.now() + const mockExtensionList = [id] + + tracker.on('query', (q) => { + if (q.step === 1 && q.method === 'first' && q.bindings[0] === id) { + return q.response({}) + } + if (q.step === 2 && q.method === 'select') { // TransferExtensionModel.getByTransferId() call + return q.response(mockExtensionList) + } + q.reject(new Error('Mock DB error')) + }) + + const result = await transferFacade.getById(id) + t.ok(result, 'transfers is found') + t.deepEqual(result.extensionList, mockExtensionList) + })) + + getByIdTest.end() + }) + + await transferFacadeTest.end() +}) diff --git a/test/unit/models/transfer/facade.test.js b/test/unit/models/transfer/facade.test.js index 5aa4b92da..7e4f036e4 100644 --- a/test/unit/models/transfer/facade.test.js +++ b/test/unit/models/transfer/facade.test.js @@ -146,223 +146,6 @@ Test('Transfer facade', async (transferFacadeTest) => { t.end() }) - // await transferFacadeTest.test('getById should return transfer by id', async (test) => { - // try { - // const transferId1 = 't1' - // const transferId2 = 't2' - // const extensions = cloneDeep(transferExtensions) - // const transfers = [ - // { transferId: transferId1, extensionList: extensions }, - // { transferId: transferId2, errorCode: 5105, transferStateEnumeration: Enum.Transfers.TransferState.ABORTED, extensionList: [{ key: 'key1', value: 'value1' }, { key: 'key2', value: 'value2' }, { key: 'cause', value: '5105: undefined' }], isTransferReadModel: true } - // ] - // - // const builderStub = sandbox.stub() - // const payerTransferStub = sandbox.stub() - // const payerRoleTypeStub = sandbox.stub() - // const payerCurrencyStub = sandbox.stub() - // const payerParticipantStub = sandbox.stub() - // const payeeTransferStub = sandbox.stub() - // const payeeRoleTypeStub = sandbox.stub() - // const payeeCurrencyStub = sandbox.stub() - // const payeeParticipantStub = sandbox.stub() - // const ilpPacketStub = sandbox.stub() - // const stateChangeStub = sandbox.stub() - // const stateStub = sandbox.stub() - // const transferFulfilmentStub = sandbox.stub() - // const transferErrorStub = sandbox.stub() - // - // const selectStub = sandbox.stub() - // const orderByStub = sandbox.stub() - // const firstStub = sandbox.stub() - // - // builderStub.where = sandbox.stub() - // - // Db.transfer.query.callsArgWith(0, builderStub) - // Db.transfer.query.returns(transfers[0]) - // - // builderStub.where.returns({ - // innerJoin: payerTransferStub.returns({ - // innerJoin: payerRoleTypeStub.returns({ - // innerJoin: payerParticipantStub.returns({ - // leftJoin: payerCurrencyStub.returns({ - // innerJoin: payeeTransferStub.returns({ - // innerJoin: payeeRoleTypeStub.returns({ - // innerJoin: payeeParticipantStub.returns({ - // leftJoin: payeeCurrencyStub.returns({ - // innerJoin: ilpPacketStub.returns({ - // leftJoin: stateChangeStub.returns({ - // leftJoin: stateStub.returns({ - // leftJoin: transferFulfilmentStub.returns({ - // leftJoin: transferErrorStub.returns({ - // select: selectStub.returns({ - // orderBy: orderByStub.returns({ - // first: firstStub.returns(transfers[0]) - // }) - // }) - // }) - // }) - // }) - // }) - // }) - // }) - // }) - // }) - // }) - // }) - // }) - // }) - // }) - // }) - // - // sandbox.stub(transferExtensionModel, 'getByTransferId') - // transferExtensionModel.getByTransferId.returns(extensions) - // - // const found = await TransferFacade.getById(transferId1) - // test.equal(found, transfers[0]) - // test.ok(builderStub.where.withArgs({ - // 'transfer.transferId': transferId1, - // 'tprt1.name': 'PAYER_DFSP', - // 'tprt2.name': 'PAYEE_DFSP' - // }).calledOnce) - // test.ok(payerTransferStub.withArgs('transferParticipant AS tp1', 'tp1.transferId', 'transfer.transferId').calledOnce) - // test.ok(payerRoleTypeStub.withArgs('transferParticipantRoleType AS tprt1', 'tprt1.transferParticipantRoleTypeId', 'tp1.transferParticipantRoleTypeId').calledOnce) - // test.ok(payerCurrencyStub.withArgs('participantCurrency AS pc1', 'pc1.participantCurrencyId', 'tp1.participantCurrencyId').calledOnce) - // test.ok(payerParticipantStub.withArgs('participant AS da', 'da.participantId', 'tp1.participantId').calledOnce) - // test.ok(payeeTransferStub.withArgs('transferParticipant AS tp2', 'tp2.transferId', 'transfer.transferId').calledOnce) - // test.ok(payeeRoleTypeStub.withArgs('transferParticipantRoleType AS tprt2', 'tprt2.transferParticipantRoleTypeId', 'tp2.transferParticipantRoleTypeId').calledOnce) - // test.ok(payeeCurrencyStub.withArgs('participantCurrency AS pc2', 'pc2.participantCurrencyId', 'tp2.participantCurrencyId').calledOnce) - // test.ok(payeeParticipantStub.withArgs('participant AS ca', 'ca.participantId', 'tp2.participantId').calledOnce) - // test.ok(ilpPacketStub.withArgs('ilpPacket AS ilpp', 'ilpp.transferId', 'transfer.transferId').calledOnce) - // test.ok(stateChangeStub.withArgs('transferStateChange AS tsc', 'tsc.transferId', 'transfer.transferId').calledOnce) - // test.ok(stateStub.withArgs('transferState AS ts', 'ts.transferStateId', 'tsc.transferStateId').calledOnce) - // test.ok(transferFulfilmentStub.withArgs('transferFulfilment AS tf', 'tf.transferId', 'transfer.transferId').calledOnce) - // test.ok(transferErrorStub.withArgs('transferError as te', 'te.transferId', 'transfer.transferId').calledOnce) - // test.ok(selectStub.withArgs( - // 'transfer.*', - // 'transfer.currencyId AS currency', - // 'pc1.participantCurrencyId AS payerParticipantCurrencyId', - // 'tp1.amount AS payerAmount', - // 'da.participantId AS payerParticipantId', - // 'da.name AS payerFsp', - // 'da.isProxy AS payerIsProxy', - // 'pc2.participantCurrencyId AS payeeParticipantCurrencyId', - // 'tp2.amount AS payeeAmount', - // 'ca.participantId AS payeeParticipantId', - // 'ca.name AS payeeFsp', - // 'ca.isProxy AS payeeIsProxy', - // 'tsc.transferStateChangeId', - // 'tsc.transferStateId AS transferState', - // 'tsc.reason AS reason', - // 'tsc.createdDate AS completedTimestamp', - // 'ts.enumeration as transferStateEnumeration', - // 'ts.description as transferStateDescription', - // 'ilpp.value AS ilpPacket', - // 'transfer.ilpCondition AS condition', - // 'tf.ilpFulfilment AS fulfilment' - // ).calledOnce) - // test.ok(orderByStub.withArgs('tsc.transferStateChangeId', 'desc').calledOnce) - // test.ok(firstStub.withArgs().calledOnce) - // - // Db.transfer.query.returns(transfers[1]) - // builderStub.where.returns({ - // innerJoin: payerTransferStub.returns({ - // innerJoin: payerRoleTypeStub.returns({ - // innerJoin: payerParticipantStub.returns({ - // leftJoin: payerCurrencyStub.returns({ - // innerJoin: payeeTransferStub.returns({ - // innerJoin: payeeRoleTypeStub.returns({ - // innerJoin: payeeParticipantStub.returns({ - // leftJoin: payeeCurrencyStub.returns({ - // innerJoin: ilpPacketStub.returns({ - // leftJoin: stateChangeStub.returns({ - // leftJoin: stateStub.returns({ - // leftJoin: transferFulfilmentStub.returns({ - // leftJoin: transferErrorStub.returns({ - // select: selectStub.returns({ - // orderBy: orderByStub.returns({ - // first: firstStub.returns(transfers[1]) - // }) - // }) - // }) - // }) - // }) - // }) - // }) - // }) - // }) - // }) - // }) - // }) - // }) - // }) - // }) - // }) - // - // const found2 = await TransferFacade.getById(transferId2) - // // TODO: extend testing for the current code branch - // test.deepEqual(found2, transfers[1]) - // - // transferExtensionModel.getByTransferId.returns(null) - // const found3 = await TransferFacade.getById(transferId2) - // // TODO: extend testing for the current code branch - // test.equal(found3, transfers[1]) - // test.end() - // } catch (err) { - // Logger.error(`getById failed with error - ${err}`) - // test.fail() - // test.end() - // } - // }) - - // await transferFacadeTest.test('getById should find zero records', async (test) => { - // try { - // const transferId1 = 't1' - // const builderStub = sandbox.stub() - // Db.transfer.query.callsArgWith(0, builderStub) - // builderStub.where = sandbox.stub() - // builderStub.where.returns({ - // innerJoin: sandbox.stub().returns({ - // innerJoin: sandbox.stub().returns({ - // innerJoin: sandbox.stub().returns({ - // leftJoin: sandbox.stub().returns({ - // innerJoin: sandbox.stub().returns({ - // innerJoin: sandbox.stub().returns({ - // innerJoin: sandbox.stub().returns({ - // leftJoin: sandbox.stub().returns({ - // innerJoin: sandbox.stub().returns({ - // leftJoin: sandbox.stub().returns({ - // leftJoin: sandbox.stub().returns({ - // leftJoin: sandbox.stub().returns({ - // leftJoin: sandbox.stub().returns({ - // select: sandbox.stub().returns({ - // orderBy: sandbox.stub().returns({ - // first: sandbox.stub().returns(null) - // }) - // }) - // }) - // }) - // }) - // }) - // }) - // }) - // }) - // }) - // }) - // }) - // }) - // }) - // }) - // }) - // const found = await TransferFacade.getById(transferId1) - // test.equal(found, null, 'no transfers were found') - // test.end() - // } catch (err) { - // Logger.error(`getById failed with error - ${err}`) - // test.fail('Error thrown') - // test.end() - // } - // }) - await transferFacadeTest.test('getById should throw an error', async (test) => { try { const transferId1 = 't1'