From 1183950dadc3c83f25faff02fbbd1d07beb5a80e Mon Sep 17 00:00:00 2001 From: Josh Harvey Date: Mon, 9 Jan 2017 01:04:27 +0200 Subject: [PATCH] WIP --- lib/app.js | 2 + lib/pp.js | 1 + lib/settings-loader.js | 37 +++-- test/fixtures/two-way-btc.json | 246 +++++++++++++++++++++++++++++++++ 4 files changed, 277 insertions(+), 9 deletions(-) create mode 100644 lib/pp.js create mode 100644 test/fixtures/two-way-btc.json diff --git a/lib/app.js b/lib/app.js index 484d5c85..636705cd 100644 --- a/lib/app.js +++ b/lib/app.js @@ -32,10 +32,12 @@ function run () { return runner() } +const pp = require('./pp') function runOnce () { return verifySchema.valid() .then(() => settingsLoader.loadLatest()) .then(settings => { + // pp(settings) poller.start(settings) const httpsServerOptions = { diff --git a/lib/pp.js b/lib/pp.js new file mode 100644 index 00000000..8aa7b76c --- /dev/null +++ b/lib/pp.js @@ -0,0 +1 @@ +module.exports = o => console.log(require('util').inspect(o, {depth: null, colors: true})) diff --git a/lib/settings-loader.js b/lib/settings-loader.js index 7a17f089..b1c2a242 100644 --- a/lib/settings-loader.js +++ b/lib/settings-loader.js @@ -11,11 +11,23 @@ let settingsCache function loadFixture () { const fixture = argv.fixture + const machine = argv.machine + + console.log('DEBUG22: %j', [fixture, machine]) + if (fixture && !machine) throw new Error('Missing --machine parameter for --fixture') + const fixturePath = fixture => path.resolve(__dirname, '..', 'test', 'fixtures', fixture + '.json') - return fixture + 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)) } function isEquivalentField (a, b) { @@ -33,9 +45,9 @@ function mergeValues (a, b) { function load (versionId) { if (!versionId) throw new Error('versionId is required') - return Promise.all([loadConfig(versionId), loadAccounts(), loadFixture()]) - .then(([config, accounts, fixture]) => ({ - config: mergeValues(config, fixture), + return Promise.all([loadConfig(versionId), loadAccounts()]) + .then(([config, accounts]) => ({ + config, accounts })) } @@ -53,8 +65,11 @@ function loadConfig (versionId) { from user_config where id=$1 and type=$2` - return db.oneOrNone(sql, [versionId, 'config']) - .then(row => row ? row.data.config : []) + return Promise.all([db.oneOrNone(sql, [versionId, 'config']), loadFixture()]) + .then(([row, fixture]) => { + const config = row ? row.data.config : [] + return mergeValues(config, fixture) + }) } function loadLatestConfig () { @@ -64,8 +79,12 @@ function loadLatestConfig () { order by id desc limit 1` - return db.oneOrNone(sql, ['config']) - .then(row => row ? row.data.config : []) + return Promise.all([db.oneOrNone(sql, ['config']), loadFixture()]) + .then(([row, fixture]) => { + const config = row ? row.data.config : [] + require('./pp')(fixture) + return mergeValues(config, fixture) + }) } function loadAccounts () { diff --git a/test/fixtures/two-way-btc.json b/test/fixtures/two-way-btc.json new file mode 100644 index 00000000..a624443f --- /dev/null +++ b/test/fixtures/two-way-btc.json @@ -0,0 +1,246 @@ +[ + { + "fieldLocator": { + "fieldScope": { + "crypto": "BTC", + "machine": "machine" + }, + "code": "cashInCommission", + "fieldType": "percentage", + "fieldClass": null + }, + "fieldValue": { + "fieldType": "percentage", + "value": 5 + } + }, + { + "fieldLocator": { + "fieldScope": { + "crypto": "BTC", + "machine": "machine" + }, + "code": "lowBalanceMargin", + "fieldType": "percentage", + "fieldClass": null + }, + "fieldValue": { + "fieldType": "percentage", + "value": 5 + } + }, + { + "fieldLocator": { + "fieldScope": { + "crypto": "global", + "machine": "machine" + }, + "code": "machineLanguages", + "fieldType": "language", + "fieldClass": null + }, + "fieldValue": { + "fieldType": "language", + "value": [ + "en-US" + ] + } + }, + { + "fieldLocator": { + "fieldScope": { + "crypto": "global", + "machine": "machine" + }, + "code": "cryptoCurrencies", + "fieldType": "cryptoCurrency", + "fieldClass": null + }, + "fieldValue": { + "fieldType": "cryptoCurrency", + "value": [ + "BTC" + ] + } + }, + { + "fieldLocator": { + "fieldScope": { + "crypto": "global", + "machine": "machine" + }, + "code": "emptyBillMargin", + "fieldType": "integer", + "fieldClass": null + }, + "fieldValue": { + "fieldType": "integer", + "value": 0 + } + }, + { + "fieldLocator": { + "fieldScope": { + "crypto": "global", + "machine": "global" + }, + "code": "smsVerificationEnabled", + "fieldType": "onOff", + "fieldClass": null + }, + "fieldValue": { + "fieldType": "onOff", + "value": false + } + }, + { + "fieldLocator": { + "fieldScope": { + "crypto": "global", + "machine": "machine" + }, + "code": "idVerificationEnabled", + "fieldType": "onOff", + "fieldClass": null + }, + "fieldValue": { + "fieldType": "onOff", + "value": false + } + }, + { + "fieldLocator": { + "fieldScope": { + "crypto": "global", + "machine": "global" + }, + "code": "notificationsEnabled", + "fieldType": "onOff", + "fieldClass": null + }, + "fieldValue": { + "fieldType": "onOff", + "value": false + } + }, + { + "fieldLocator": { + "fieldScope": { + "crypto": "global", + "machine": "global" + }, + "code": "notificationsEmailEnabled", + "fieldType": "onOff", + "fieldClass": null + }, + "fieldValue": { + "fieldType": "onOff", + "value": false + } + }, + { + "fieldLocator": { + "fieldScope": { + "crypto": "global", + "machine": "global" + }, + "code": "notificationsSMSEnabled", + "fieldType": "onOff", + "fieldClass": null + }, + "fieldValue": { + "fieldType": "onOff", + "value": false + } + }, + { + "fieldLocator": { + "fieldScope": { + "crypto": "global", + "machine": "machine" + }, + "code": "machineName", + "fieldType": "string", + "fieldClass": null + }, + "fieldValue": { + "fieldType": "string", + "value": "test1" + } + }, + { + "fieldLocator": { + "fieldScope": { + "crypto": "global", + "machine": "machine" + }, + "code": "cashOutEnabled", + "fieldType": "onOff", + "fieldClass": null + }, + "fieldValue": { + "fieldType": "onOff", + "value": true + } + }, + { + "fieldLocator": { + "fieldScope": { + "crypto": "global", + "machine": "global" + }, + "code": "fiatCurrency", + "fieldType": "fiatCurrency", + "fieldClass": null + }, + "fieldValue": { + "fieldType": "fiatCurrency", + "value": "USD" + } + }, + { + "fieldLocator": { + "fieldScope": { + "crypto": "BTC", + "machine": "global" + }, + "code": "ticker", + "fieldType": "account", + "fieldClass": "ticker" + }, + "fieldValue": { + "fieldType": "account", + "value": "mock-ticker" + } + }, + { + "fieldLocator": { + "fieldScope": { + "crypto": "BTC", + "machine": "global" + }, + "code": "wallet", + "fieldType": "account", + "fieldClass": "wallet" + }, + "fieldValue": { + "fieldType": "account", + "value": "mock-wallet" + } + }, + { + "fieldLocator": { + "fieldScope": { + "crypto": "global", + "machine": "machine" + }, + "code": "cashInTransactionLimit", + "fieldType": "integer", + "fieldClass": null + }, + "fieldValue": { + "fieldType": "integer", + "value": 100 + } + } +]