From 8aca436e49837a269267f267e30cb4323e91732d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20B=C3=BChler?= Date: Tue, 19 Jul 2016 08:41:17 +0200 Subject: [PATCH] Feature/update to new proc that (#4) * remove dependencies * change return type * refactor tests and loader --- ElasticLoader.spec.ts | 39 ++++++++++++++++++++++----------------- ElasticLoader.ts | 13 +++++++------ package.json | 17 +++++++++-------- typings.json | 17 +++++++++-------- 4 files changed, 47 insertions(+), 39 deletions(-) diff --git a/ElasticLoader.spec.ts b/ElasticLoader.spec.ts index 221bb20..220d719 100644 --- a/ElasticLoader.spec.ts +++ b/ElasticLoader.spec.ts @@ -2,6 +2,7 @@ import chai = require('chai'); import asPromised = require('chai-as-promised'); import sinon = require('sinon'); import sinonChai = require('sinon-chai'); +import {Observable} from 'rxjs'; import {ElasticLoader} from './ElasticLoader'; let should = chai.should(); @@ -10,30 +11,30 @@ chai.use(sinonChai); describe('ElasticLoader', () => { - let loader:ElasticLoader; - let client:any; - let stub:any; + let loader: ElasticLoader; + let client: any; + let stub: any; beforeEach(() => { client = { - index: o => Promise.resolve() + index: o => Observable.of(o) }; - stub = sinon.stub(client, 'index', o => Promise.resolve()); + stub = sinon.stub(client, 'index', o => Observable.of(o)); }); it('should resolve on correct usage', done => { loader = new ElasticLoader({}, 'testIndex', 'testType'); (loader as any).esClient = client; - loader.write({id: 1, text: 'test'}).then(done, done); + loader.write({id: 1, text: 'test'}).subscribe(null, done, done); }); it('should use correct index', done => { loader = new ElasticLoader({}, 'testIndex', 'testType'); (loader as any).esClient = client; - loader.write({id: 1, text: 'test'}).then(() => { + loader.write({id: 1, text: 'test'}).subscribe(null, done, () => { try { client.index.should.have.been.calledOnce; client.index.should.have.been.calledWithMatch({ @@ -50,7 +51,7 @@ describe('ElasticLoader', () => { loader = new ElasticLoader({}, 'testIndex', 'testType'); (loader as any).esClient = client; - loader.write({id: 1, text: 'test'}).then(() => { + loader.write({id: 1, text: 'test'}).subscribe(null, done, () => { try { client.index.should.have.been.calledOnce; client.index.should.have.been.calledWithMatch({ @@ -67,7 +68,7 @@ describe('ElasticLoader', () => { loader = new ElasticLoader({}, 'testIndex', 'testType'); (loader as any).esClient = client; - loader.write({id: 1, text: 'test'}).then(() => { + loader.write({id: 1, text: 'test'}).subscribe(null, done, () => { try { client.index.should.have.been.calledOnce; client.index.should.have.been.calledWithMatch({ @@ -84,7 +85,7 @@ describe('ElasticLoader', () => { loader = new ElasticLoader({}, 'testIndex', 'testType'); (loader as any).esClient = client; - loader.write({id: 1, text: 'test'}).then(() => { + loader.write({id: 1, text: 'test'}).subscribe(null, done, () => { try { client.index.should.have.been.calledOnce; client.index.should.have.been.calledWithMatch({ @@ -101,7 +102,7 @@ describe('ElasticLoader', () => { loader = new ElasticLoader({}, 'testIndex', 'testType', o => o.text === 'test'); (loader as any).esClient = client; - loader.write({id: 1, text: 'test'}).then(() => { + loader.write({id: 1, text: 'test'}).subscribe(null, done, () => { try { client.index.should.have.been.calledOnce; done(); @@ -115,7 +116,7 @@ describe('ElasticLoader', () => { loader = new ElasticLoader({}, 'testIndex', 'testType', o => o.text !== 'test'); (loader as any).esClient = client; - loader.write({id: 1, text: 'test'}).then(() => { + loader.write({id: 1, text: 'test'}).subscribe(null, done, () => { try { client.index.should.not.have.been.called; done(); @@ -129,7 +130,7 @@ describe('ElasticLoader', () => { loader = new ElasticLoader({}, 'testIndex', 'testType', o => true, o => o.myId); (loader as any).esClient = client; - loader.write({myId: 1, text: 'test'}).then(() => { + loader.write({myId: 1, text: 'test'}).subscribe(null, done, () => { try { client.index.should.have.been.calledOnce; client.index.should.have.been.calledWithMatch({ @@ -143,12 +144,16 @@ describe('ElasticLoader', () => { }); }); - it('should reject when no id is provided', () => { + it('should reject when no id is provided', done => { loader = new ElasticLoader({}, 'testIndex', 'testType'); (loader as any).esClient = client; - return loader.write({myId: 1, text: 'test'}) - .should.eventually.be.rejected; + loader.write({myId: 1, text: 'test'}).subscribe(null, () => { + done(); + }, () => { + done(new Error('did not throw.')); + }) }); -}); +}) +; diff --git a/ElasticLoader.ts b/ElasticLoader.ts index 219a34a..7ff8be7 100644 --- a/ElasticLoader.ts +++ b/ElasticLoader.ts @@ -1,4 +1,5 @@ -import {ILoad} from 'proc-that'; +import {Loader} from 'proc-that'; +import {Observable} from 'rxjs'; import {Buffer} from './helpers/Buffer'; let elasticsearch = require('elasticsearch'); @@ -9,7 +10,7 @@ class NoIdProvidedError extends Error { } } -export class ElasticLoader implements ILoad { +export class ElasticLoader implements Loader { private esClient:any; private buffer:Buffer = new Buffer(); @@ -27,15 +28,15 @@ export class ElasticLoader implements ILoad { } } - write(object:any):Promise { + write(object: any): Observable { if (!this.predicate(object)) { - return Promise.resolve(); + return Observable.empty(); } let id = this.idSelector(object); if (id === null || id === undefined) { - return Promise.reject(new NoIdProvidedError(object)); + return Observable.throw(new NoIdProvidedError(object)); } let promise = this.buffer @@ -49,6 +50,6 @@ export class ElasticLoader implements ILoad { this.buffer.write(object); - return promise; + return Observable.fromPromise(promise); } } \ No newline at end of file diff --git a/package.json b/package.json index aa2d890..3278721 100644 --- a/package.json +++ b/package.json @@ -34,17 +34,18 @@ "chai": "^3.5.0", "chai-as-promised": "^5.3.0", "del-cli": "^0.2.0", - "istanbul": "^0.4.3", - "mocha": "^2.4.5", + "istanbul": "^0.4.4", + "mocha": "^2.5.3", "mocha-lcov-reporter": "^1.2.0", - "rimraf": "^2.5.2", - "sinon": "^1.17.3", + "rimraf": "^2.5.3", + "sinon": "^1.17.4", "sinon-chai": "^2.8.0", - "typescript": "^1.8.9", - "typings": "^0.7.9" + "typescript": "^1.8.10", + "typings": "^1.3.1" }, "dependencies": { - "elasticsearch": "^10.1.3", - "proc-that": "^0.3.2" + "elasticsearch": "^11.0.1", + "proc-that": "^0.4.0", + "rxjs": "^5.0.0-beta.10" } } diff --git a/typings.json b/typings.json index 0aa3d79..5edf886 100644 --- a/typings.json +++ b/typings.json @@ -1,16 +1,17 @@ { + "name": "proc-that-elastic-loader", "dependencies": {}, - "ambientDependencies": { - "es6-shim": "registry:dt/es6-shim#0.31.2+20160317120654", - "node": "registry:dt/node#4.0.0+20160319033040" + "globalDependencies": { + "es6-shim": "registry:dt/es6-shim#0.31.2+20160602141504", + "node": "registry:dt/node#6.0.0+20160709114037" }, - "ambientDevDependencies": { - "mocha": "registry:dt/mocha#2.2.5+20160317120654" + "globalDevDependencies": { + "mocha": "registry:dt/mocha#2.2.5+20160619032855" }, "devDependencies": { - "chai": "registry:npm/chai#3.5.0+20160402210230", + "chai": "registry:npm/chai#3.5.0+20160415060238", "chai-as-promised": "registry:npm/chai-as-promised#5.1.0+20160310030142", - "sinon": "registry:npm/sinon#1.16.0+20160309002336", + "sinon": "registry:npm/sinon#1.16.0+20160427193336", "sinon-chai": "registry:npm/sinon-chai#2.8.0+20160310030142" } -} +} \ No newline at end of file