Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(csi-634): added mock-knex lib to mock mysql in unit-tests #1113

Merged
merged 1 commit into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 1 addition & 2 deletions src/models/participant/externalParticipantCached.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -67,7 +67,6 @@ const getExternalParticipantsCached = async () => {
} else {
// unwrap participants list from catbox structure
cachedParticipants = cachedParticipants.item
hit = true
}
histTimer({ success: true, queryName, hit })

Expand Down
101 changes: 101 additions & 0 deletions test/unit/models/transfer/facade-withMockKnex.test.js
Original file line number Diff line number Diff line change
@@ -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 <email>.
* Gates Foundation
- Name Surname <[email protected]>

* Eugen Klymniuk <[email protected]>
--------------
**********/

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()
})
Loading