From 09ba2b62979eeca5a51ef7999960b76ffe559e50 Mon Sep 17 00:00:00 2001 From: godu Date: Mon, 26 Sep 2016 13:00:14 +0200 Subject: [PATCH 1/2] Rework api.set --- src/api.js | 22 +++------------------- src/index.js | 2 +- src/test/api.js | 33 +++++++++++++++++++++++++++------ src/test/fixtures/node-etcd.js | 14 -------------- 4 files changed, 31 insertions(+), 40 deletions(-) delete mode 100644 src/test/fixtures/node-etcd.js diff --git a/src/api.js b/src/api.js index 874ef9c..8b8f744 100644 --- a/src/api.js +++ b/src/api.js @@ -6,11 +6,12 @@ import { startsWith, curry } from 'lodash/fp'; +import {set$} from './util/etcd'; import createDebug from 'debug'; const debug = createDebug('squirrel'); -const createAPI = (store, client, options = {cwd: '/'}) => { +const createAPI = (store, client) => { const getBy = (index, key) => { debug(`getBy: ${index} => ${key}`); return store('indexes').then(getOr(null, [index, key, 'value'])); @@ -29,9 +30,7 @@ const createAPI = (store, client, options = {cwd: '/'}) => { }; const set = (_path, value) => { - _path = path.join(options.cwd, _path); - debug(`set => ${_path}`); - return _set(_path, value); + return set$(client, _path, value).toPromise(); }; const _get = curry((_path, node) => { @@ -43,21 +42,6 @@ const createAPI = (store, client, options = {cwd: '/'}) => { }, node.nodes)); }); - const _set = (_path, value) => { - return new Promise(function(resolve, reject) { - if (!value) { - return resolve(value); - } - client.set(_path, value, (err, res) => { - if (err) { - return reject(err); - } - - return resolve(res); - }); - }); - }; - return { getBy, getAll, diff --git a/src/index.js b/src/index.js index bff63e6..197b96f 100644 --- a/src/index.js +++ b/src/index.js @@ -46,7 +46,7 @@ const createSquirrel = options => { save(node$), indexBuilder ); - const api = createAPI(store, client, options); + const api = createAPI(store, client); return { ...api, diff --git a/src/test/api.js b/src/test/api.js index 44fdab1..ef37b71 100644 --- a/src/test/api.js +++ b/src/test/api.js @@ -1,7 +1,7 @@ import test from 'ava'; +import createEtcdMock from '../util/test/helpers/etcd'; import createAPI from '../api'; -import Etcd from './fixtures/node-etcd'; const node = { key: '/', dir: true, @@ -85,10 +85,7 @@ const store = { }; const getStore = key => Promise.resolve(store[key]); -const client = new Etcd(); -const api = createAPI(getStore, client); -const clientErrored = new Etcd({err: new Error('boom')}); -const apiErrored = createAPI(getStore, clientErrored); +const api = createAPI(getStore); test('should create API', t => { t.truthy(api); @@ -180,6 +177,16 @@ test('should get null if any node matches this path', async t => { }); test('should set nothing if value is null', async t => { + const client = createEtcdMock({ + set: [{ + assert: (key, value) => { + t.deepEqual(key, '/nope'); + t.deepEqual(value, null); + }, + values: [null, null, null] + }] + }); + const api = createAPI(getStore, client); t.deepEqual( await api.set('/nope', null), null @@ -187,6 +194,16 @@ test('should set nothing if value is null', async t => { }); test('should set value if value setted', async t => { + const client = createEtcdMock({ + set: [{ + assert: (key, value) => { + t.deepEqual(key, '/foo'); + t.deepEqual(value, {foo: 'baz'}); + }, + values: [null, {foo: 'baz'}, null] + }] + }); + const api = createAPI(getStore, client); t.deepEqual( await api.set('/foo', {foo: 'baz'}), {foo: 'baz'} @@ -194,8 +211,12 @@ test('should set value if value setted', async t => { }); test('should failed if error occured', async t => { + const client = createEtcdMock({ + set: [[new Error('boom'), null, null]] + }); + const api = createAPI(getStore, client); t.throws( - apiErrored.set('/foo', {foo: 'baz'}), + api.set('/foo', {foo: 'baz'}), /boom/ ); }); diff --git a/src/test/fixtures/node-etcd.js b/src/test/fixtures/node-etcd.js deleted file mode 100644 index 43c3a14..0000000 --- a/src/test/fixtures/node-etcd.js +++ /dev/null @@ -1,14 +0,0 @@ -'use strict'; - -function Etcd(options = {}) { - this.options = options; -} - -Etcd.prototype.set = function set(path, value, callback) { - if (this.options.err) { - return callback(this.options.err); - } - callback(null, value); -}; - -module.exports = Etcd; From e7194eab6fc0d6209a88f462813c17069302d5cb Mon Sep 17 00:00:00 2001 From: godu Date: Mon, 26 Sep 2016 13:02:15 +0200 Subject: [PATCH 2/2] Removes 'should set nothing if value is null' test --- src/test/api.js | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/src/test/api.js b/src/test/api.js index ef37b71..e2927c9 100644 --- a/src/test/api.js +++ b/src/test/api.js @@ -176,23 +176,6 @@ test('should get null if any node matches this path', async t => { ); }); -test('should set nothing if value is null', async t => { - const client = createEtcdMock({ - set: [{ - assert: (key, value) => { - t.deepEqual(key, '/nope'); - t.deepEqual(value, null); - }, - values: [null, null, null] - }] - }); - const api = createAPI(getStore, client); - t.deepEqual( - await api.set('/nope', null), - null - ); -}); - test('should set value if value setted', async t => { const client = createEtcdMock({ set: [{