start adding test framework

This commit is contained in:
Josh Harvey 2017-01-08 23:18:27 +02:00
parent d9b2e021e7
commit 94dc861b87
4 changed files with 2152 additions and 34 deletions

View file

@ -1,15 +1,41 @@
const path = require('path')
const fs = require('fs')
const _ = require('lodash/fp')
const argv = require('minimist')(process.argv.slice(2))
const pify = require('pify')
const db = require('./db')
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) {
if (!versionId) throw new Error('versionId is required')
return Promise.all([loadConfig(versionId), loadAccounts()])
.then(([config, accounts]) => ({
config,
return Promise.all([loadConfig(versionId), loadAccounts(), loadFixture()])
.then(([config, accounts, fixture]) => ({
config: mergeValues(config, fixture),
accounts
}))
}
@ -67,5 +93,7 @@ module.exports = {
loadConfig,
load,
loadLatest,
save
save,
loadFixture,
mergeValues
}

View file

@ -57,6 +57,10 @@
"hkdf": "./bin/hkdf",
"lamassu-backup-pg": "./bin/lamassu-backup-pg"
},
"scripts": {},
"devDependencies": {}
"scripts": {
"test": "ava"
},
"devDependencies": {
"ava": "^0.17.0"
}
}

View 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)
})

2092
yarn.lock

File diff suppressed because it is too large Load diff