Skip to content

Commit

Permalink
initial test commit
Browse files Browse the repository at this point in the history
  • Loading branch information
annyhe committed Dec 2, 2016
1 parent 824ca32 commit 0e8adf4
Show file tree
Hide file tree
Showing 8 changed files with 316 additions and 32 deletions.
1 change: 1 addition & 0 deletions api/v1/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ module.exports = {
SEQ_DESC: 'DESC',
SEQ_LIKE: '$iLike',
SEQ_CONTAINS: '$contains',
SEQ_IN: '$in',
SEQ_OR: '$or',
SEQ_WILDCARD: '%',
SLASH: '/',
Expand Down
2 changes: 2 additions & 0 deletions api/v1/helpers/nouns/aspects.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const m = 'aspect';

const fieldsWithJsonArrayType = ['relatedLinks'];
const fieldsWithArrayType = ['tags'];
const fieldsWithEnum = ['valueType'];

module.exports = {
apiLinks: {
Expand All @@ -34,4 +35,5 @@ module.exports = {
modelName: 'Aspect',
fieldsWithArrayType,
fieldsWithJsonArrayType,
fieldsWithEnum,
}; // exports
3 changes: 2 additions & 1 deletion api/v1/helpers/nouns/samples.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const config = require('../../../../config');
const m = 'sample';

const fieldsWithJsonArrayType = ['relatedLinks'];
const fieldsWithEnum = ['status', 'previousStatus'];
const loggingEnabled = (
config.auditSamples === 'API' || config.auditSamples === 'ALL'
) || false;
Expand All @@ -34,6 +35,6 @@ module.exports = {
model: Sample,
modelName: 'Sample',
fieldsWithJsonArrayType,
fieldsWithEnum,
loggingEnabled,

}; // exports
29 changes: 28 additions & 1 deletion api/v1/helpers/verbs/findUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ function toSequelizeWildcards(val) {
* case-insensitive string matching
*/
function toWhereClause(val, props) {
// given array, return { $in: array }
if (Array.isArray(val) && props.isEnum) {
const inClause = {};
inClause[constants.SEQ_IN] = val;
return inClause;
}

if (Array.isArray(val) && props.tagFilterName) {
const containsClause = {};
containsClause[constants.SEQ_CONTAINS] = val;
Expand Down Expand Up @@ -84,6 +91,7 @@ function toWhereClause(val, props) {
function toSequelizeWhere(filter, props) {
const where = {};
const keys = Object.keys(filter);

for (let i = 0; i < keys.length; i++) {
const key = keys[i];
if (filter[key] !== undefined) {
Expand All @@ -93,12 +101,31 @@ function toSequelizeWhere(filter, props) {

const values = [];

/*
* If enum filter is enabled and key is an enumerable field
* then create an "in"
* clause and add it to where clause, e.g.
* {
* where: {
valueType: { $in: ["PERCENT", "BOOLEAN"] },
},
* }
*/
if (Array.isArray(props.fieldsWithEnum) &&
props.fieldsWithEnum.indexOf(key) > -1) {
const enumArr = filter[key];
// to use $in instead of $contains in toWhereClause
props.isEnum = true;
values.push(toWhereClause(enumArr, props));
where[key] = values[0];
}

/*
* If tag filter is enabled and key is "tags", then create a "contains"
* clause and add it to where clause, e.g.
* { where : { '$contains': ['tag1', 'tag2'] } }
*/
if (props.tagFilterName && key === props.tagFilterName) {
else if (props.tagFilterName && key === props.tagFilterName) {
const tagArr = filter[key];
values.push(toWhereClause(tagArr, props));
where[key] = values[0];
Expand Down
55 changes: 45 additions & 10 deletions api/v1/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2447,6 +2447,41 @@ paths:
$ref: "#/parameters/limitParam"
-
$ref: "#/parameters/offsetParam"
-
name: name
in: query
description: >-
Filter by sample name; asterisk (*) wildcards ok.
required: false
type: string
-
name: messageCode
in: query
description: >-
Filter by sample messageCode; asterisk (*) wildcards ok.
required: false
type: string
-
name: status
in: query
description: >-
Filter by sample status (Critical|Invalid|Timeout|Warning|Info|OK).
required: false
type: string
-
name: previousStatus
in: query
description: >-
Filter by sample previousStatus (Critical|Invalid|Timeout|Warning|Info|OK).
required: false
type: string
-
name: value
in: query
description: >-
Filter by sample value (BOOLEAN|NUMERIC|PERCENT).
required: false
type: string
responses:
200:
description: >-
Expand Down Expand Up @@ -3624,11 +3659,11 @@ paths:
For example, ?aspect=FOO,BAR will only return subjects in the
hierarchy with samples for those two aspects (and all those
subjects' ancestors up to the specified root of the requested
hierarchy). Prefix each of the aspect name with a negative sign to
hierarchy). Prefix each of the aspect name with a negative sign to
indicate that a sample with that aspect should be excluded.
For example, ?aspect=-BAZ,-FOO will return only the subjects
(and its hierarchy) that have samples with aspect name not equal
to BAZ or FOO. Subjects without samples are not included in the
to BAZ or FOO. Subjects without samples are not included in the
result set
type: string
-
Expand All @@ -3639,7 +3674,7 @@ paths:
For example, ?status=OK,CRITICAL will only return subjects in the
hierarchy with samples that are in those statuses (and all those
subjects' ancestors up to the specified root of the requested
hierarchy). Prefix each of the status with a negative sign to
hierarchy). Prefix each of the status with a negative sign to
indicate that a sample with that status should be excluded.
For example, ?status=-OK,-CRITICAL will return only the subjects
(and its hierarchy) that have samples not in OK or CRITICAL status.
Expand All @@ -3653,11 +3688,11 @@ paths:
For example, ?aspectTags=TAG1,TAG2 will only return subjects in the
hierarchy with samples having aspect with tags matching TAG1 and TAG2
(and all those subjects' ancestors up to the specified root of the
requested hierarchy). Prefix each of the tag names with a negative
sign to indicate that a sample having aspect with those tag names
requested hierarchy). Prefix each of the tag names with a negative
sign to indicate that a sample having aspect with those tag names
will be excluded. For example, ?aspectTags=-TAG3,-TAG4 will
return the subject hierarchy without aspects having tags -
TAG3 and TAG4. Subjects without samples are not included in
return the subject hierarchy without aspects having tags -
TAG3 and TAG4. Subjects without samples are not included in
the result set
type: string
-
Expand All @@ -3668,9 +3703,9 @@ paths:
For example, ?subjectTags=TAG1,TAG2 will only return subjects in the
hierarchy with tags matching TAG1 and TAG2
(and all those subjects' ancestors up to the specified root of the
requested hierarchy). Prefix each of the tag names with a negative
sign to indicate that the subject having tags with those names will
be excluded. For example, ?subjectTags=-TAG3,-TAG4 will return the
requested hierarchy). Prefix each of the tag names with a negative
sign to indicate that the subject having tags with those names will
be excluded. For example, ?subjectTags=-TAG3,-TAG4 will return the
subject hierarchy without subjects having tags - TAG3 and TAG4.
type: string
-
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"view": "view/**/*/app.js"
},
"scripts": {
"cleanup": "npm run dropdb && npm run initdb && npm run checkdb",
"build": "NODE_ENV=development && webpack --config ./webpack.config.js --progress --profile",
"checkdb": "node db/createOrUpdateDb.js",
"dropdb": "node db/createOrDropDb.js --drop",
Expand Down
42 changes: 22 additions & 20 deletions tests/api/v1/aspects/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const tu = require('../../../testUtils');
const u = require('./utils');
const Aspect = tu.db.Aspect;
const path = '/v1/aspects';
const expect = require('chai').expect;

describe(`api: GET ${path}`, () => {
let token;
Expand Down Expand Up @@ -65,6 +66,17 @@ describe(`api: GET ${path}`, () => {


describe('Single Values: ', () => {
it('filter by BOOLEAN returns expected values', (done) => {
api.get(path + '?valueType=PERCENT') // BOOLEAN is default
.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');
})
.end((err /* , res */) => done(err));
});

it('key used twice in url', (done) => {
api.get(`${path}?name=${tu.namePrefix}a0&description=foo&name=xyz`)
.set('Authorization', token)
Expand Down Expand Up @@ -102,17 +114,12 @@ describe(`api: GET ${path}`, () => {
.set('Authorization', token)
.expect(constants.httpStatus.OK)
.expect((res) => {
if (!tu.gotArrayWithExpectedLength(res.body, 2)) {
throw new Error('expecting 2 aspects');
}
expect(res.body.length).to.be.equal(2);
res.body.map((aspect) => {
expect(aspect.name.slice(0, 3)).to.equal(tu.namePrefix);
});
})
.end((err /* , res */) => {
if (err) {
return done(err);
}

done();
});
.end((err /* , res */) => done(err));
});

it('leading asterisk is treated as "ends with"', (done) => {
Expand Down Expand Up @@ -231,17 +238,12 @@ describe(`api: GET ${path}`, () => {
.set('Authorization', token)
.expect(constants.httpStatus.OK)
.expect((res) => {
if (!tu.gotArrayWithExpectedLength(res.body, 2)) {
throw new Error('expecting 2 aspects');
}
expect(res.body.length).to.be.equal(2);
res.body.map((aspect) => {
expect(aspect.name).to.contain('a');
});
})
.end((err /* , res */) => {
if (err) {
return done(err);
}

done();
});
.end((err /* , res */) => done(err));
});
}); // Lists
});
Loading

0 comments on commit 0e8adf4

Please sign in to comment.