start adding test framework
This commit is contained in:
parent
d9b2e021e7
commit
94dc861b87
4 changed files with 2152 additions and 34 deletions
|
|
@ -1,15 +1,41 @@
|
||||||
|
const path = require('path')
|
||||||
|
const fs = require('fs')
|
||||||
|
|
||||||
const _ = require('lodash/fp')
|
const _ = require('lodash/fp')
|
||||||
|
const argv = require('minimist')(process.argv.slice(2))
|
||||||
|
const pify = require('pify')
|
||||||
|
|
||||||
const db = require('./db')
|
const db = require('./db')
|
||||||
|
|
||||||
let settingsCache
|
let settingsCache
|
||||||
|
|
||||||
|
function loadFixture () {
|
||||||
|
const fixture = argv.fixture
|
||||||
|
const fixturePath = fixture => path.resolve(__dirname, '..', 'test', 'fixtures', fixture + '.json')
|
||||||
|
|
||||||
|
return fixture
|
||||||
|
? pify(fs.readFile)(fixturePath(fixture)).then(JSON.parse)
|
||||||
|
: Promise.resolve({})
|
||||||
|
}
|
||||||
|
|
||||||
|
function isEquivalentField (a, b) {
|
||||||
|
return _.isEqual(
|
||||||
|
[a.fieldLocator.code, a.fieldLocator.fieldScope],
|
||||||
|
[b.fieldLocator.code, b.fieldLocator.fieldScope]
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// b overrides a
|
||||||
|
function mergeValues (a, b) {
|
||||||
|
return _.unionWith(isEquivalentField, b, a)
|
||||||
|
}
|
||||||
|
|
||||||
function load (versionId) {
|
function load (versionId) {
|
||||||
if (!versionId) throw new Error('versionId is required')
|
if (!versionId) throw new Error('versionId is required')
|
||||||
|
|
||||||
return Promise.all([loadConfig(versionId), loadAccounts()])
|
return Promise.all([loadConfig(versionId), loadAccounts(), loadFixture()])
|
||||||
.then(([config, accounts]) => ({
|
.then(([config, accounts, fixture]) => ({
|
||||||
config,
|
config: mergeValues(config, fixture),
|
||||||
accounts
|
accounts
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
@ -67,5 +93,7 @@ module.exports = {
|
||||||
loadConfig,
|
loadConfig,
|
||||||
load,
|
load,
|
||||||
loadLatest,
|
loadLatest,
|
||||||
save
|
save,
|
||||||
|
loadFixture,
|
||||||
|
mergeValues
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -57,6 +57,10 @@
|
||||||
"hkdf": "./bin/hkdf",
|
"hkdf": "./bin/hkdf",
|
||||||
"lamassu-backup-pg": "./bin/lamassu-backup-pg"
|
"lamassu-backup-pg": "./bin/lamassu-backup-pg"
|
||||||
},
|
},
|
||||||
"scripts": {},
|
"scripts": {
|
||||||
"devDependencies": {}
|
"test": "ava"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"ava": "^0.17.0"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
50
test/unit/settings-loader-load-fixture.js
Normal file
50
test/unit/settings-loader-load-fixture.js
Normal file
|
|
@ -0,0 +1,50 @@
|
||||||
|
import test from 'ava'
|
||||||
|
|
||||||
|
import settingsLoader from '../../lib/settings-loader'
|
||||||
|
|
||||||
|
test('simple merge', t => {
|
||||||
|
const fieldCode = 'testCode'
|
||||||
|
const fieldLocator = {fieldScope: {crypto: 'global', machine: 'global'}, code: fieldCode}
|
||||||
|
const fieldValueA = {fieldType: 'percentage', value: 5}
|
||||||
|
const fieldValueB = {fieldType: 'percentage', value: 6}
|
||||||
|
const merged = settingsLoader.mergeValues(
|
||||||
|
[{fieldLocator, fieldValue: fieldValueA}],
|
||||||
|
[{fieldLocator, fieldValue: fieldValueB}]
|
||||||
|
)
|
||||||
|
t.deepEqual(merged, [{fieldLocator, fieldValue: fieldValueB}])
|
||||||
|
})
|
||||||
|
|
||||||
|
test('bigger merge', t => {
|
||||||
|
const fieldCode = 'testCode'
|
||||||
|
const fieldLocator1 = {fieldScope: {crypto: 'BTC', machine: 'xx'}, code: fieldCode}
|
||||||
|
const fieldLocator2 = {fieldScope: {crypto: 'global', machine: 'global'}, code: fieldCode}
|
||||||
|
const fieldLocator3 = {fieldScope: {crypto: 'BTC', machine: 'xx'}, code: 'testCode2'}
|
||||||
|
const fieldLocator4 = {fieldScope: {crypto: 'BTC', machine: 'xx'}, code: 'testCode3'}
|
||||||
|
|
||||||
|
const fieldValue1 = {fieldType: 'percentage', value: 1}
|
||||||
|
const fieldValue2 = {fieldType: 'percentage', value: 2}
|
||||||
|
const fieldValue3 = {fieldType: 'percentage', value: 3}
|
||||||
|
const fieldValue4 = {fieldType: 'percentage', value: 4}
|
||||||
|
const fieldValue5 = {fieldType: 'percentage', value: 5}
|
||||||
|
|
||||||
|
const merged = settingsLoader.mergeValues(
|
||||||
|
[
|
||||||
|
{fieldLocator: fieldLocator1, fieldValue: fieldValue1},
|
||||||
|
{fieldLocator: fieldLocator2, fieldValue: fieldValue2},
|
||||||
|
{fieldLocator: fieldLocator3, fieldValue: fieldValue3}
|
||||||
|
],
|
||||||
|
[
|
||||||
|
{fieldLocator: fieldLocator1, fieldValue: fieldValue4},
|
||||||
|
{fieldLocator: fieldLocator4, fieldValue: fieldValue5}
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
const expected = [
|
||||||
|
{fieldLocator: fieldLocator1, fieldValue: fieldValue4},
|
||||||
|
{fieldLocator: fieldLocator4, fieldValue: fieldValue5},
|
||||||
|
{fieldLocator: fieldLocator2, fieldValue: fieldValue2},
|
||||||
|
{fieldLocator: fieldLocator3, fieldValue: fieldValue3}
|
||||||
|
]
|
||||||
|
|
||||||
|
t.deepEqual(merged, expected)
|
||||||
|
})
|
||||||
Loading…
Add table
Add a link
Reference in a new issue