From 9f495c7791ab440d80b69faae9b2ad0f725cf338 Mon Sep 17 00:00:00 2001 From: Joe Fleming Date: Fri, 15 Apr 2016 16:51:56 -0700 Subject: [PATCH] add job object, and tests --- src/index.js | 6 +++- src/job.js | 19 +++++++++++ test/src/job.js | 84 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 src/job.js create mode 100644 test/src/job.js diff --git a/src/index.js b/src/index.js index 231348b..aa843ab 100644 --- a/src/index.js +++ b/src/index.js @@ -1,5 +1,6 @@ import events from 'events'; import createClient from './helpers/create_client'; +import Job from './job.js'; import { omit } from 'lodash'; export default class Elastique extends events.EventEmitter { @@ -9,6 +10,7 @@ export default class Elastique extends events.EventEmitter { super(); this.ready = true; + this.jobs = []; this.index = index; this.settings = Object.assign({ interval: '1w', @@ -22,6 +24,8 @@ export default class Elastique extends events.EventEmitter { timeout: this.settings.timeout }, opts); + var job = new Job(this, type, payload, options); + this.jobs.push(job.id); + return job; } } - diff --git a/src/job.js b/src/job.js new file mode 100644 index 0000000..139bf4c --- /dev/null +++ b/src/job.js @@ -0,0 +1,19 @@ +import events from 'events'; +import { isPlainObject } from 'lodash'; + +export default class Job extends events.EventEmitter { + constructor(queue, type, payload, options = {}) { + if (typeof type !== 'string') throw new Error('Type must be a string'); + if (!isPlainObject(payload)) throw new Error('Payload must be a plain object'); + + super(); + + queue.client.index({ + index: queue.index, + type: type, + body: Object.assign({}, options, { + payload: payload + }) + }); + } +} \ No newline at end of file diff --git a/test/src/job.js b/test/src/job.js new file mode 100644 index 0000000..ffc7703 --- /dev/null +++ b/test/src/job.js @@ -0,0 +1,84 @@ +import expect from 'expect.js'; +import sinon from 'sinon'; +import Job from '../../lib/job'; +import * as elasticsearchMock from '../fixtures/elasticsearch'; + +describe('Jobs', function () { + let mockQueue; + beforeEach(function () { + mockQueue = { + index: 'test', + client: new elasticsearchMock.Client(), + }; + }); + + describe('invlaid construction', function () { + it('should throw with a missing type', function () { + const init = () => new Job(mockQueue); + expect(init).to.throwException(/type.+string/i); + }); + + it('should throw with an invalid type', function () { + const init = () => new Job(mockQueue, { 'not a string': true }); + expect(init).to.throwException(/type.+string/i); + }); + + it('should throw with an invalid payload', function () { + const init = () => new Job(mockQueue, 'type1', [1, 2, 3]); + expect(init).to.throwException(/plain.+object/i); + }); + }); + + describe('construction', function () { + let type; + let payload; + let options; + + beforeEach(function () { + type = 'type1'; + payload = { id: '123' }; + options = { timeout: 1234 }; + sinon.stub(mockQueue.client, 'index'); + }); + + it('should index the payload', function () { + new Job(mockQueue, type, payload); + + sinon.assert.calledOnce(mockQueue.client.index); + sinon.assert.calledWith(mockQueue.client.index, { + index: mockQueue.index, + type: type, + body: { + payload: payload + } + }); + }); + + it('should index any optional params', function () { + new Job(mockQueue, type, payload, options); + + sinon.assert.calledOnce(mockQueue.client.index); + sinon.assert.calledWith(mockQueue.client.index, { + index: mockQueue.index, + type: type, + body: Object.assign(options, { + payload: payload + }) + }); + }); + + it('should not allow options to clobber payload', function () { + options = { payload: 1234 }; + new Job(mockQueue, type, payload, options); + + sinon.assert.calledOnce(mockQueue.client.index); + sinon.assert.calledWith(mockQueue.client.index, { + index: mockQueue.index, + type: type, + body: { + payload: payload + } + }); + }); + }); +});