-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(csi-634): added mock-knex lib to mock mysql in unit-tests (#1113)
- Loading branch information
Showing
5 changed files
with
138 additions
and
219 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) |
Oops, something went wrong.