Skip to content

Commit

Permalink
lint fix
Browse files Browse the repository at this point in the history
  • Loading branch information
annyhe committed Dec 2, 2016
1 parent 0b2a1fb commit f8309bb
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 64 deletions.
30 changes: 16 additions & 14 deletions api/v1/helpers/verbs/findUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
const u = require('./utils');
const constants = require('../../constants');
const defaults = require('../../../../config').api.defaults;
const ZERO = 0;
const ONE = 1;

/**
* Escapes all percent literals so they're not treated as wildcards.
Expand All @@ -22,7 +24,7 @@ const defaults = require('../../../../config').api.defaults;
*/
function escapePercentLiterals(val) {
if (typeof val === 'string' || val instanceof String) {
if (val.indexOf(constants.SEQ_WILDCARD) >= 0) {
if (val.indexOf(constants.SEQ_WILDCARD) > -ONE) {
return val.replace(constants.ALL_PERCENTS_RE, constants.ESCAPED_PERCENT);
}
}
Expand Down Expand Up @@ -92,7 +94,7 @@ function toSequelizeWhere(filter, props) {
const where = {};
const keys = Object.keys(filter);

for (let i = 0; i < keys.length; i++) {
for (let i = ZERO; i < keys.length; i++) {
const key = keys[i];
if (filter[key] !== undefined) {
if (!Array.isArray(filter[key])) {
Expand All @@ -112,12 +114,12 @@ function toSequelizeWhere(filter, props) {
* }
*/
if (Array.isArray(props.fieldsWithEnum) &&
props.fieldsWithEnum.indexOf(key) > -1) {
props.fieldsWithEnum.indexOf(key) > -ONE) {
const enumArr = filter[key];
// to use $in instead of $contains in toWhereClause
props.isEnum = true;
values.push(toWhereClause(enumArr, props));
where[key] = values[0];
where[key] = values[ZERO];
}

/*
Expand All @@ -128,23 +130,23 @@ function toSequelizeWhere(filter, props) {
else if (props.tagFilterName && key === props.tagFilterName) {
const tagArr = filter[key];
values.push(toWhereClause(tagArr, props));
where[key] = values[0];
where[key] = values[ZERO];
} else {
for (let j = 0; j < filter[key].length; j++) {
for (let j = ZERO; j < filter[key].length; j++) {
const v = filter[key][j];
if (typeof v === 'boolean') {
values.push(v);
} else if (typeof v === 'string') {
const arr = v.split(constants.COMMA);
for (let k = 0; k < arr.length; k++) {
for (let k = ZERO; k < arr.length; k++) {
values.push(toWhereClause(arr[k]));
}
}
}

if (values.length === 1) {
where[key] = values[0];
} else if (values.length > 1) {
if (values.length === ONE) {
where[key] = values[ZERO];
} else if (values.length > ONE) {
where[key] = {};
where[key][constants.SEQ_OR] = values;
}
Expand All @@ -167,8 +169,8 @@ function toSequelizeOrder(sortOrder) {
const sortOrderArray = Array.isArray(sortOrder) ?
sortOrder : [sortOrder];
return sortOrderArray.map((s) => {
if (s.indexOf(constants.MINUS) === 0) {
return [`${s.substr(1)}`, constants.SEQ_DESC];
if (s.indexOf(constants.MINUS) === ZERO) {
return [`${s.substr(ONE)}`, constants.SEQ_DESC];
}

return `${s}`;
Expand Down Expand Up @@ -207,10 +209,10 @@ function options(params, props) {

const filter = {};
const keys = Object.keys(params);
for (let i = 0; i < keys.length; i++) {
for (let i = ZERO; i < keys.length; i++) {
const key = keys[i];

const isFilterField = constants.NOT_FILTER_FIELDS.indexOf(key) < 0;
const isFilterField = constants.NOT_FILTER_FIELDS.indexOf(key) < ZERO;

if (isFilterField && params[key].value !== undefined) {
filter[key] = params[key].value;
Expand Down
31 changes: 17 additions & 14 deletions tests/api/v1/aspects/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ const u = require('./utils');
const Aspect = tu.db.Aspect;
const path = '/v1/aspects';
const expect = require('chai').expect;
const ZERO = 0;
const ONE = 1;
const TWO = 2;

describe(`api: GET ${path}`, () => {
let token;
Expand Down Expand Up @@ -71,8 +74,8 @@ describe(`api: GET ${path}`, () => {
.set('Authorization', token)
.expect(constants.httpStatus.OK)
.expect((res) => {
expect(res.body.length).to.be.equal(1);
expect(res.body[0].valueType).to.be.equal('PERCENT');
expect(res.body.length).to.be.equal(ONE);
expect(res.body[ZERO].valueType).to.be.equal('PERCENT');
})
.end((err /* , res */) => done(err));
});
Expand All @@ -95,8 +98,8 @@ describe(`api: GET ${path}`, () => {
.set('Authorization', token)
.expect(constants.httpStatus.OK)
.expect((res) => {
if (!tu.gotArrayWithExpectedLength(res.body, 1) ||
res.body[0].name !== `${tu.namePrefix}a0`) {
if (!tu.gotArrayWithExpectedLength(res.body, ONE) ||
res.body[ZERO].name !== `${tu.namePrefix}a0`) {
throw new Error('expecting 1 aspect');
}
})
Expand All @@ -114,9 +117,9 @@ describe(`api: GET ${path}`, () => {
.set('Authorization', token)
.expect(constants.httpStatus.OK)
.expect((res) => {
expect(res.body.length).to.be.equal(2);
expect(res.body.length).to.be.equal(TWO);
res.body.map((aspect) => {
expect(aspect.name.slice(0, 3)).to.equal(tu.namePrefix);
expect(aspect.name.slice(ZERO, 3)).to.equal(tu.namePrefix);
});
})
.end((err /* , res */) => done(err));
Expand All @@ -127,8 +130,8 @@ describe(`api: GET ${path}`, () => {
.set('Authorization', token)
.expect(constants.httpStatus.OK)
.expect((res) => {
if (!tu.gotArrayWithExpectedLength(res.body, 1) ||
res.body[0].name !== `${tu.namePrefix}a1`) {
if (!tu.gotArrayWithExpectedLength(res.body, ONE) ||
res.body[ZERO].name !== `${tu.namePrefix}a1`) {
throw new Error('expecting 1 aspect');
}
})
Expand All @@ -146,7 +149,7 @@ describe(`api: GET ${path}`, () => {
.set('Authorization', token)
.expect(constants.httpStatus.OK)
.expect((res) => {
if (!tu.gotArrayWithExpectedLength(res.body, 2)) {
if (!tu.gotArrayWithExpectedLength(res.body, TWO)) {
throw new Error('expecting 2 aspects');
}
})
Expand All @@ -164,7 +167,7 @@ describe(`api: GET ${path}`, () => {
.set('Authorization', token)
.expect(constants.httpStatus.OK)
.expect((res) => {
if (!tu.gotArrayWithExpectedLength(res.body, 0)) {
if (!tu.gotArrayWithExpectedLength(res.body, ZERO)) {
throw new Error('expecting 0 aspects');
}
})
Expand All @@ -182,7 +185,7 @@ describe(`api: GET ${path}`, () => {
.set('Authorization', token)
.expect(constants.httpStatus.OK)
.expect((res) => {
if (!tu.gotArrayWithExpectedLength(res.body, 0)) {
if (!tu.gotArrayWithExpectedLength(res.body, ZERO)) {
throw new Error('expecting 0 aspects');
}
})
Expand All @@ -200,7 +203,7 @@ describe(`api: GET ${path}`, () => {
.set('Authorization', token)
.expect(constants.httpStatus.OK)
.expect((res) => {
if (!tu.gotArrayWithExpectedLength(res.body, 1)) {
if (!tu.gotArrayWithExpectedLength(res.body, ONE)) {
throw new Error('expecting 1 aspect');
}
})
Expand All @@ -220,7 +223,7 @@ describe(`api: GET ${path}`, () => {
.set('Authorization', token)
.expect(constants.httpStatus.OK)
.expect((res) => {
if (!tu.gotArrayWithExpectedLength(res.body, 2)) {
if (!tu.gotArrayWithExpectedLength(res.body, TWO)) {
throw new Error('expecting 2 aspects');
}
})
Expand All @@ -238,7 +241,7 @@ describe(`api: GET ${path}`, () => {
.set('Authorization', token)
.expect(constants.httpStatus.OK)
.expect((res) => {
expect(res.body.length).to.be.equal(2);
expect(res.body.length).to.be.equal(TWO);
res.body.map((aspect) => {
expect(aspect.name).to.contain('a');
});
Expand Down
69 changes: 33 additions & 36 deletions tests/api/v1/samples/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ const path = '/v1/samples';
const expect = require('chai').expect;

describe('sample api: FILTER' + path, () => {
let sampleId;
let token;
let SPECIAL_SAMPLE_ID;
const THREE = '3';
const ONE = '1';
const THREE = 3;
const TWO = 2;
const ZERO = 0;
const ONE = 1;
const MESSAGE_CODE_1 = '12345';

before((done) => {
Expand All @@ -50,7 +50,7 @@ describe('sample api: FILTER' + path, () => {
isPublished: true,
name: `${tu.namePrefix + aspectName}`,
timeout: '30s',
criticalRange: [3, 3],
criticalRange: [THREE, THREE],
valueType: 'NUMERIC',
};

Expand Down Expand Up @@ -82,18 +82,17 @@ describe('sample api: FILTER' + path, () => {
})
.then(() => doSetup('GELATO', 'COLUMBIA'))
.then((obj) => {
obj.value = THREE;
obj.value = String(THREE);
return Sample.create(obj);
})
.then(() => doSetup('SPECIAL', 'UNIQUE'))
.then((obj) => {
obj.value = THREE;
obj.value = String(THREE);
obj.messageCode = MESSAGE_CODE_1;
return Sample.create(obj);
})
.then((samp) => { // to test previousStatus
SPECIAL_SAMPLE_ID = samp.id;
return samp.update({ value: ONE });
return samp.update({ value: String(ONE) });
})
.then(() => { // sample updated
done();
Expand All @@ -109,9 +108,8 @@ describe('sample api: FILTER' + path, () => {
.set('Authorization', token)
.expect(constants.httpStatus.OK)
.expect((res) => {
res.body.map((sample) => {
console.log('*** value is', sample.value)
})
expect(res.body.length).to.equal(ONE);
expect(res.body[ZERO].value).to.equal(String(ONE));
})
.end((err /* , res */) => done(err));
});
Expand All @@ -123,8 +121,8 @@ describe('sample api: FILTER' + path, () => {
.set('Authorization', token)
.expect(constants.httpStatus.OK)
.expect((res) => {
expect(res.body.length).to.equal(1);
expect(res.body[0].name).to.equal(NAME);
expect(res.body.length).to.equal(ONE);
expect(res.body[ZERO].name).to.equal(NAME);
})
.end((err /* , res */) => done(err));
});
Expand All @@ -134,10 +132,10 @@ describe('sample api: FILTER' + path, () => {
.set('Authorization', token)
.expect(constants.httpStatus.OK)
.expect((res) => {
expect(res.body.length).to.equal(3);
expect(res.body.length).to.equal(THREE);
res.body.map((sample) => {
expect(sample.name.slice(0, 3)).to.equal(tu.namePrefix);
})
expect(sample.name.slice(ZERO, THREE)).to.equal(tu.namePrefix);
});
})
.end((err /* , res */) => done(err));
});
Expand All @@ -147,16 +145,15 @@ describe('sample api: FILTER' + path, () => {
.set('Authorization', token)
.expect(constants.httpStatus.OK)
.expect((res) => {
expect(res.body.length).to.equal(2);
expect(res.body.length).to.equal(TWO);
res.body.map((sample) => {
expect(sample.name.slice(-2)).to.equal('TO');
expect(sample.name.slice(-TWO)).to.equal('TO');
});
})
.end((err /* , res */) => done(err));
});

it('leading and trailing asterisks are treated as "contains"',
(done) => {
it('leading and trailing asterisks are treated as "contains"', (done) => {
api.get(path + '?name=*ATO*')
.set('Authorization', token)
.expect(constants.httpStatus.OK)
Expand All @@ -173,8 +170,8 @@ describe('sample api: FILTER' + path, () => {
.set('Authorization', token)
.expect(constants.httpStatus.OK)
.expect((res) => {
expect(res.body.length).to.equal(1);
expect(res.body[0].value).to.equal(ONE);
expect(res.body.length).to.equal(ONE);
expect(res.body[ZERO].value).to.equal(String(ONE));
})
.end((err /* , res */) => done(err));
});
Expand All @@ -184,8 +181,8 @@ describe('sample api: FILTER' + path, () => {
.set('Authorization', token)
.expect(constants.httpStatus.OK)
.expect((res) => {
expect(res.body.length).to.equal(1);
expect(res.body[0].messageCode).to.equal(MESSAGE_CODE_1);
expect(res.body.length).to.equal(ONE);
expect(res.body[ZERO].messageCode).to.equal(MESSAGE_CODE_1);
})
.end((err /* , res */) => done(err));
});
Expand All @@ -195,21 +192,21 @@ describe('sample api: FILTER' + path, () => {
.set('Authorization', token)
.expect(constants.httpStatus.OK)
.expect((res) => {
expect(res.body.length).to.equal(1);
expect(res.body[0].status).to.equal('Critical');
expect(res.body.length).to.equal(ONE);
expect(res.body[ZERO].status).to.equal('Critical');
})
.end((err /* , res */) => done(err));
});

it('filter by previousStatus', (done) => {
api.get(path + '?previousStatus=Critical')
.set('Authorization', token)
.expect(constants.httpStatus.OK)
.expect((res) => {
expect(res.body.length).to.equal(1);
expect(res.body[0].previousStatus).to.equal('Critical');
expect(res.body[0].status).to.equal('Invalid');
})
.end((err /* , res */) => done(err));
api.get(path + '?previousStatus=Critical')
.set('Authorization', token)
.expect(constants.httpStatus.OK)
.expect((res) => {
expect(res.body.length).to.equal(ONE);
expect(res.body[ZERO].previousStatus).to.equal('Critical');
expect(res.body[ZERO].status).to.equal('Invalid');
})
.end((err /* , res */) => done(err));
});
});

0 comments on commit f8309bb

Please sign in to comment.