distiguish the doctype from the jobtype

update tests and mock elasticsearch client
This commit is contained in:
2016-05-12 12:20:49 -07:00
parent 3375335d24
commit 609e81fdef
4 changed files with 32 additions and 13 deletions

View File

@@ -1,7 +1,7 @@
import { defaultSettings } from './constants'; import { defaultSettings } from './constants';
const schema = { const schema = {
type: { type: 'string', index: 'not_analyzed' }, jobtype: { type: 'string', index: 'not_analyzed' },
payload: { type: 'object', enabled: false }, payload: { type: 'object', enabled: false },
priority: { type: 'short' }, priority: { type: 'short' },
timeout: { type: 'long' }, timeout: { type: 'long' },

View File

@@ -18,7 +18,7 @@ export default class Job extends events.EventEmitter {
this.client = client; this.client = client;
this.id = puid.generate(); this.id = puid.generate();
this.index = index; this.index = index;
this.type = type; this.jobtype = type;
this.payload = payload; this.payload = payload;
this.timeout = options.timeout || 10000; this.timeout = options.timeout || 10000;
this.maxAttempts = options.max_attempts || 3; this.maxAttempts = options.max_attempts || 3;
@@ -31,9 +31,10 @@ export default class Job extends events.EventEmitter {
.then(() => { .then(() => {
return this.client.index({ return this.client.index({
index: this.index, index: this.index,
type: this.type, type: this.doctype,
id: this.id, id: this.id,
body: { body: {
jobtype: this.jobtype,
payload: this.payload, payload: this.payload,
priority: this.priority, priority: this.priority,
timeout: this.timeout, timeout: this.timeout,
@@ -66,7 +67,7 @@ export default class Job extends events.EventEmitter {
.then(() => { .then(() => {
return this.client.get({ return this.client.get({
index: this.index, index: this.index,
type: this.type, type: this.doctype,
id: this.id id: this.id
}); });
}) })
@@ -84,7 +85,8 @@ export default class Job extends events.EventEmitter {
return Object.assign({ return Object.assign({
id: this.id, id: this.id,
index: this.index, index: this.index,
type: this.type, type: this.doctype,
jobtype: this.jobtype,
payload: this.payload, payload: this.payload,
timeout: this.timeout, timeout: this.timeout,
max_attempts: this.maxAttempts, max_attempts: this.maxAttempts,

View File

@@ -1,5 +1,6 @@
import { uniqueId, times, random } from 'lodash'; import { uniqueId, times, random } from 'lodash';
import elasticsearch from 'elasticsearch'; import elasticsearch from 'elasticsearch';
import { DEFAULT_SETTING_DOCTYPE } from '../../lib/helpers/constants';
function Client() { function Client() {
this.indices = { this.indices = {
@@ -14,7 +15,7 @@ Client.prototype.index = function (params = {}) {
const shardCount = 2; const shardCount = 2;
return Promise.resolve({ return Promise.resolve({
_index: params.index || 'index', _index: params.index || 'index',
_type: params.type || 'type', _type: params.type || DEFAULT_SETTING_DOCTYPE,
_id: params.id || uniqueId('testDoc'), _id: params.id || uniqueId('testDoc'),
_version: 1, _version: 1,
_shards: { total: shardCount, successful: shardCount, failed: 0 }, _shards: { total: shardCount, successful: shardCount, failed: 0 },
@@ -30,6 +31,7 @@ Client.prototype.get = function (params = {}, source = {}) {
if (params === elasticsearch.errors.NotFound) return elasticsearch.errors.NotFound; if (params === elasticsearch.errors.NotFound) return elasticsearch.errors.NotFound;
const _source = Object.assign({ const _source = Object.assign({
jobtype: 'jobtype',
payload: { payload: {
id: 'sample-job-1', id: 'sample-job-1',
now: 'Mon Apr 25 2016 14:13:04 GMT-0700 (MST)' now: 'Mon Apr 25 2016 14:13:04 GMT-0700 (MST)'
@@ -44,7 +46,7 @@ Client.prototype.get = function (params = {}, source = {}) {
return { return {
_index: params.index || 'index', _index: params.index || 'index',
_type: params.type || 'type', _type: params.type || DEFAULT_SETTING_DOCTYPE,
_id: params.id || 'AVRPRLnlp7Ur1SZXfT-T', _id: params.id || 'AVRPRLnlp7Ur1SZXfT-T',
_version: params.version || 1, _version: params.version || 1,
found: true, found: true,
@@ -56,7 +58,7 @@ Client.prototype.search = function (params = {}, count = 5, source = {}) {
const hits = times(count, () => { const hits = times(count, () => {
return { return {
_index: params.index || 'index', _index: params.index || 'index',
_type: params.type || 'type', _type: params.type || DEFAULT_SETTING_DOCTYPE,
_id: uniqueId('documentId'), _id: uniqueId('documentId'),
_version: random(1, 5), _version: random(1, 5),
_score: null, _score: null,
@@ -86,7 +88,7 @@ Client.prototype.update = function (params = {}) {
const shardCount = 2; const shardCount = 2;
return Promise.resolve({ return Promise.resolve({
_index: params.index || 'index', _index: params.index || 'index',
_type: params.type || 'type', _type: params.type || DEFAULT_SETTING_DOCTYPE,
_id: params.id || uniqueId('testDoc'), _id: params.id || uniqueId('testDoc'),
_version: params.version + 1 || 2, _version: params.version + 1 || 2,
_shards: { total: shardCount, successful: shardCount, failed: 0 }, _shards: { total: shardCount, successful: shardCount, failed: 0 },

View File

@@ -3,7 +3,7 @@ import expect from 'expect.js';
import sinon from 'sinon'; import sinon from 'sinon';
import proxyquire from 'proxyquire'; import proxyquire from 'proxyquire';
import elasticsearchMock from '../fixtures/elasticsearch'; import elasticsearchMock from '../fixtures/elasticsearch';
import { JOB_STATUS_PENDING } from '../../lib/helpers/constants'; import { JOB_STATUS_PENDING, DEFAULT_SETTING_DOCTYPE } from '../../lib/helpers/constants';
const createIndexMock = sinon.stub().returns(Promise.resolve('mock')); const createIndexMock = sinon.stub().returns(Promise.resolve('mock'));
const module = proxyquire.noPreserveCache()('../../lib/job', { const module = proxyquire.noPreserveCache()('../../lib/job', {
@@ -73,12 +73,23 @@ describe('Job Class', function () {
return job.ready.then(() => { return job.ready.then(() => {
const newDoc = validateDoc(client.index); const newDoc = validateDoc(client.index);
expect(newDoc).to.have.property('index', index); expect(newDoc).to.have.property('index', index);
expect(newDoc).to.have.property('type', type); expect(newDoc).to.have.property('type', DEFAULT_SETTING_DOCTYPE);
expect(newDoc).to.have.property('body'); expect(newDoc).to.have.property('body');
expect(newDoc.body).to.have.property('payload', payload); expect(newDoc.body).to.have.property('payload', payload);
}); });
}); });
it('should index the job type', function () {
const job = new Job(client, index, type, payload);
return job.ready.then(() => {
const newDoc = validateDoc(client.index);
expect(newDoc).to.have.property('index', index);
expect(newDoc).to.have.property('type', DEFAULT_SETTING_DOCTYPE);
expect(newDoc).to.have.property('body');
expect(newDoc.body).to.have.property('jobtype', type);
});
});
it('should index timeout value from options', function () { it('should index timeout value from options', function () {
const job = new Job(client, index, type, payload, options); const job = new Job(client, index, type, payload, options);
return job.ready.then(() => { return job.ready.then(() => {
@@ -162,14 +173,17 @@ describe('Job Class', function () {
it('should return the job document', function () { it('should return the job document', function () {
const job = new Job(client, index, type, payload); const job = new Job(client, index, type, payload);
return job.get() return job.get()
.then((doc) => { .then((doc) => {
const jobDoc = job.document; // document should be resolved const jobDoc = job.document; // document should be resolved
expect(doc).to.have.property('index', index); expect(doc).to.have.property('index', index);
expect(doc).to.have.property('type', type); expect(doc).to.have.property('type', jobDoc.type);
expect(doc).to.have.property('id', jobDoc.id); expect(doc).to.have.property('id', jobDoc.id);
expect(doc).to.have.property('version', jobDoc.version); expect(doc).to.have.property('version', jobDoc.version);
expect(doc).to.have.property('payload'); expect(doc).to.have.property('payload');
expect(doc).to.have.property('jobtype');
expect(doc).to.have.property('priority'); expect(doc).to.have.property('priority');
expect(doc).to.have.property('timeout'); expect(doc).to.have.property('timeout');
}); });
@@ -195,7 +209,8 @@ describe('Job Class', function () {
const doc = job.toJSON(); const doc = job.toJSON();
expect(doc).to.have.property('index', index); expect(doc).to.have.property('index', index);
expect(doc).to.have.property('type', type); expect(doc).to.have.property('type', DEFAULT_SETTING_DOCTYPE);
expect(doc).to.have.property('jobtype', type);
expect(doc).to.have.property('timeout', options.timeout); expect(doc).to.have.property('timeout', options.timeout);
expect(doc).to.have.property('max_attempts', options.max_attempts); expect(doc).to.have.property('max_attempts', options.max_attempts);
expect(doc).to.have.property('priority', options.priority); expect(doc).to.have.property('priority', options.priority);