Skip to content

Commit

Permalink
Feature/update to new proc that (#4)
Browse files Browse the repository at this point in the history
* remove dependencies

* change return type

* refactor tests and loader
  • Loading branch information
buehler authored Jul 19, 2016
1 parent 8dd0c2c commit 8aca436
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 39 deletions.
39 changes: 22 additions & 17 deletions ElasticLoader.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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({
Expand All @@ -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({
Expand All @@ -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({
Expand All @@ -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({
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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({
Expand All @@ -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.'));
})
});

});
})
;
13 changes: 7 additions & 6 deletions ElasticLoader.ts
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -9,7 +10,7 @@ class NoIdProvidedError extends Error {
}
}

export class ElasticLoader implements ILoad {
export class ElasticLoader implements Loader {
private esClient:any;
private buffer:Buffer<any> = new Buffer();

Expand All @@ -27,15 +28,15 @@ export class ElasticLoader implements ILoad {
}
}

write(object:any):Promise<void> {
write(object: any): Observable<any> {
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
Expand All @@ -49,6 +50,6 @@ export class ElasticLoader implements ILoad {

this.buffer.write(object);

return promise;
return Observable.fromPromise(promise);
}
}
17 changes: 9 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
17 changes: 9 additions & 8 deletions typings.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}

0 comments on commit 8aca436

Please sign in to comment.