This commit is contained in:
Josh Harvey 2017-03-20 16:43:40 +02:00
parent a35e9d2d44
commit 340ad2b518
8 changed files with 129 additions and 38 deletions

View file

@ -6,27 +6,68 @@ const argv = require('minimist')(process.argv.slice(2))
const pify = require('pify')
const db = require('./db')
const schema = require('../lamassu-schema.json')
const mapWithKey = _.map.convert({cap: false})
let settingsCache
function expandFixture (fixture) {
const deviceId = argv.machine
function findField (code) {
const field = _.find(_.matchesProperty('code', code), schema.fields)
const group = _.find(r => _.includes(code, r.fields), schema.groups)
if (!field || !group) {
throw new Error('No such field from fixture: ' + code)
}
return _.merge({cryptoScope: group.cryptoScope, machineScope: group.machineScope}, field)
}
function expand (value, code) {
const field = findField(code)
const machine = field.machineScope === 'specific'
? deviceId
: 'global'
const crypto = field.cryptoScope === 'specific'
? 'BTC'
: 'global'
return {
fieldLocator: {
fieldScope: {crypto, machine},
code,
fieldType: field.fieldType,
fieldClass: field.fieldClass
},
fieldValue: {
fieldType: field.fieldType,
value
}
}
}
return mapWithKey(expand, fixture)
}
function loadFixture () {
const fixture = argv.fixture
const machine = argv.machine
const deviceId = argv.machine
if (fixture && !machine) throw new Error('Missing --machine parameter for --fixture')
if (fixture && !deviceId) throw new Error('Missing --machine parameter for --fixture')
const fixturePath = fixture => path.resolve(__dirname, '..', 'test', 'fixtures', fixture + '.json')
const promise = fixture
? pify(fs.readFile)(fixturePath(fixture)).then(JSON.parse)
: Promise.resolve([])
: Promise.resolve({})
return promise
.then(values => _.map(v => {
return (v.fieldLocator.fieldScope.machine === 'machine')
? _.set('fieldLocator.fieldScope.machine', machine, v)
: v
}, values))
.then(expandFixture)
}
function isEquivalentField (a, b) {
@ -60,15 +101,14 @@ function loadLatest () {
}
function loadConfig (versionId) {
if (argv.fixture) return loadFixture()
const sql = `select data
from user_config
where id=$1 and type=$2`
return Promise.all([db.oneOrNone(sql, [versionId, 'config']), loadFixture()])
.then(([row, fixture]) => {
const config = row ? row.data.config : []
return mergeValues(config, fixture)
})
return db.oneOrNone(sql, [versionId, 'config'])
.then(row => row ? row.data.config : [])
}
function loadLatestConfig () {
@ -78,11 +118,8 @@ function loadLatestConfig () {
order by id desc
limit 1`
return Promise.all([db.oneOrNone(sql, ['config']), loadFixture()])
.then(([row, fixture]) => {
const config = row ? row.data.config : []
return mergeValues(config, fixture)
})
return db.oneOrNone(sql, ['config'])
.then(row => row ? row.data.config : [])
}
function loadAccounts () {