From a8e8b28c1147091133b7af3b0663cac239cf9c44 Mon Sep 17 00:00:00 2001 From: VLE2FE Date: Thu, 23 Jul 2020 15:25:45 +0200 Subject: [PATCH] added first_id for template response --- api/schemas.yaml | 4 +++ src/routes/template.spec.ts | 52 ++++++++++++++++++--------------- src/routes/validate/template.ts | 1 + 3 files changed, 34 insertions(+), 23 deletions(-) diff --git a/api/schemas.yaml b/api/schemas.yaml index 1c844bb..2ac14ce 100644 --- a/api/schemas.yaml +++ b/api/schemas.yaml @@ -158,6 +158,10 @@ Template: type: number readOnly: true example: 1 + first_id: + readOnly: true + type: string + example: 5ea0450ed851c30a90e70894 parameters: type: array items: diff --git a/src/routes/template.spec.ts b/src/routes/template.spec.ts index bf932cf..db924b3 100644 --- a/src/routes/template.spec.ts +++ b/src/routes/template.spec.ts @@ -25,10 +25,11 @@ describe('/template', () => { const json = require('../test/db.json'); should(res.body).have.lengthOf(json.collections.condition_templates.length); should(res.body).matchEach(condition => { - should(condition).have.only.keys('_id', 'name', 'version', 'parameters'); + should(condition).have.only.keys('_id', 'name', 'version', 'first_id', 'parameters'); should(condition).have.property('_id').be.type('string'); should(condition).have.property('name').be.type('string'); should(condition).have.property('version').be.type('number'); + should(condition).have.property('first_id').be.type('string'); should(condition.parameters).matchEach(number => { should(number).have.only.keys('name', 'range'); should(number).have.property('name').be.type('string'); @@ -62,7 +63,7 @@ describe('/template', () => { url: '/template/condition/200000000000000000000001', auth: {basic: 'janedoe'}, httpStatus: 200, - res: {_id: '200000000000000000000001', name: 'heat treatment', version: 1, parameters: [{name: 'material', range: {values: ['copper', 'hot air']}}, {name: 'weeks', range: {min: 1, max: 10}}]} + res: {_id: '200000000000000000000001', name: 'heat treatment', version: 1, first_id: '200000000000000000000001', parameters: [{name: 'material', range: {values: ['copper', 'hot air']}}, {name: 'weeks', range: {min: 1, max: 10}}]} }); }); it('rejects an API key', done => { @@ -98,7 +99,7 @@ describe('/template', () => { auth: {basic: 'admin'}, httpStatus: 200, req: {}, - res: {_id: '200000000000000000000001', name: 'heat treatment', version: 1, parameters: [{name: 'material', range: {values: ['copper', 'hot air']}}, {name: 'weeks', range: {min: 1, max: 10}}]} + res: {_id: '200000000000000000000001', name: 'heat treatment', version: 1, first_id: '200000000000000000000001', parameters: [{name: 'material', range: {values: ['copper', 'hot air']}}, {name: 'weeks', range: {min: 1, max: 10}}]} }); }); it('keeps unchanged properties', done => { @@ -108,7 +109,7 @@ describe('/template', () => { auth: {basic: 'admin'}, httpStatus: 200, req: {name: 'heat treatment', parameters: [{name: 'material', range: {values: ['copper', 'hot air']}}, {name: 'weeks', range: {min: 1, max: 10}}]}, - res: {_id: '200000000000000000000001', name: 'heat treatment', version: 1, parameters: [{name: 'material', range: {values: ['copper', 'hot air']}}, {name: 'weeks', range: {min: 1, max: 10}}]} + res: {_id: '200000000000000000000001', name: 'heat treatment', version: 1, first_id: '200000000000000000000001', parameters: [{name: 'material', range: {values: ['copper', 'hot air']}}, {name: 'weeks', range: {min: 1, max: 10}}]} }); }); it('keeps only one unchanged property', done => { @@ -118,7 +119,7 @@ describe('/template', () => { auth: {basic: 'admin'}, httpStatus: 200, req: {name: 'heat treatment'}, - res: {_id: '200000000000000000000001', name: 'heat treatment', version: 1, parameters: [{name: 'material', range: {values: ['copper', 'hot air']}}, {name: 'weeks', range: {min: 1, max: 10}}]} + res: {_id: '200000000000000000000001', name: 'heat treatment', version: 1, first_id: '200000000000000000000001', parameters: [{name: 'material', range: {values: ['copper', 'hot air']}}, {name: 'weeks', range: {min: 1, max: 10}}]} }); }); it('changes the given properties', done => { @@ -136,6 +137,7 @@ describe('/template', () => { should(data.first_id.toString()).be.eql('200000000000000000000001'); should(data).have.property('name', 'heat aging'); should(data).have.property('version', 2); + should(data.first_id.toString()).be.eql('200000000000000000000001'); should(data).have.property('parameters').have.lengthOf(1); should(data.parameters[0]).have.property('name', 'time'); should(data.parameters[0]).have.property('range'); @@ -191,7 +193,7 @@ describe('/template', () => { req: {parameters: [{name: 'time', range: {values: [1, 2, 5]}}]} }).end((err, res) => { if (err) return done(err); - should(_.omit(res.body, '_id')).be.eql({name: 'heat treatment', version: 2, parameters: [{name: 'time', range: {values: [1, 2, 5]}}]}); + should(_.omit(res.body, '_id')).be.eql({name: 'heat treatment', version: 2, first_id: '200000000000000000000001', parameters: [{name: 'time', range: {values: [1, 2, 5]}}]}); done(); }); }); @@ -204,7 +206,7 @@ describe('/template', () => { req: {parameters: [{name: 'time', range: {min: 1, max: 11}}]} }).end((err, res) => { if (err) return done(err); - should(_.omit(res.body, '_id')).be.eql({name: 'heat treatment', version: 2, parameters: [{name: 'time', range: {min: 1, max: 11}}]}); + should(_.omit(res.body, '_id')).be.eql({name: 'heat treatment', version: 2, first_id: '200000000000000000000001', parameters: [{name: 'time', range: {min: 1, max: 11}}]}); done(); }); }); @@ -217,7 +219,7 @@ describe('/template', () => { req: {parameters: [{name: 'time', range: {type: 'array'}}]} }).end((err, res) => { if (err) return done(err); - should(_.omit(res.body, '_id')).be.eql({name: 'heat treatment', version: 2, parameters: [{name: 'time', range: {type: 'array'}}]}); + should(_.omit(res.body, '_id')).be.eql({name: 'heat treatment', version: 2, first_id: '200000000000000000000001', parameters: [{name: 'time', range: {type: 'array'}}]}); done(); }); }); @@ -230,7 +232,7 @@ describe('/template', () => { req: {parameters: [{name: 'time', range: {}}]} }).end((err, res) => { if (err) return done(err); - should(_.omit(res.body, '_id')).be.eql({name: 'heat treatment', version: 2, parameters: [{name: 'time', range: {}}]}); + should(_.omit(res.body, '_id')).be.eql({name: 'heat treatment', version: 2, first_id: '200000000000000000000001', parameters: [{name: 'time', range: {}}]}); done(); }); }); @@ -310,9 +312,10 @@ describe('/template', () => { req: {name: 'heat treatment3', parameters: [{name: 'material', range: {values: ['copper']}}]} }).end((err, res) => { if (err) return done(err); - should(res.body).have.only.keys('_id', 'name', 'version', 'parameters'); + should(res.body).have.only.keys('_id', 'name', 'version', 'first_id', 'parameters'); should(res.body).have.property('name', 'heat treatment3'); should(res.body).have.property('version', 1); + should(res.body._id).be.eql(res.body.first_id); should(res.body).have.property('parameters').have.lengthOf(1); should(res.body.parameters[0]).have.property('name', 'material'); should(res.body.parameters[0]).have.property('range'); @@ -336,6 +339,7 @@ describe('/template', () => { should(data.first_id.toString()).be.eql(data._id.toString()); should(data).have.property('name', 'heat aging'); should(data).have.property('version', 1); + should(res.body._id).be.eql(res.body.first_id); should(data).have.property('parameters').have.lengthOf(1); should(data.parameters[0]).have.property('name', 'time'); should(data.parameters[0]).have.property('range'); @@ -480,8 +484,9 @@ describe('/template', () => { const json = require('../test/db.json'); should(res.body).have.lengthOf(json.collections.measurement_templates.length); should(res.body).matchEach(measurement => { - should(measurement).have.only.keys('_id', 'name', 'version', 'parameters'); + should(measurement).have.only.keys('_id', 'name', 'version', 'first_id', 'parameters'); should(measurement).have.property('_id').be.type('string'); + should(measurement).have.property('first_id').be.type('string'); should(measurement).have.property('name').be.type('string'); should(measurement).have.property('version').be.type('number'); should(measurement.parameters).matchEach(number => { @@ -517,7 +522,7 @@ describe('/template', () => { url: '/template/measurement/300000000000000000000001', auth: {basic: 'janedoe'}, httpStatus: 200, - res: {_id: '300000000000000000000001', name: 'spectrum', version: 1, parameters: [{name: 'dpt', range: { type: 'array'}}, {name: 'device', range: {}}]} + res: {_id: '300000000000000000000001', name: 'spectrum', version: 1, first_id: '300000000000000000000001', parameters: [{name: 'dpt', range: { type: 'array'}}, {name: 'device', range: {}}]} }); }); it('rejects an API key', done => { @@ -553,7 +558,7 @@ describe('/template', () => { auth: {basic: 'admin'}, httpStatus: 200, req: {}, - res: {_id: '300000000000000000000001', name: 'spectrum', version: 1, parameters: [{name: 'dpt', range: { type: 'array'}}, {name: 'device', range: {}}]} + res: {_id: '300000000000000000000001', name: 'spectrum', version: 1, first_id: '300000000000000000000001', parameters: [{name: 'dpt', range: { type: 'array'}}, {name: 'device', range: {}}]} }); }); it('keeps unchanged properties', done => { @@ -563,7 +568,7 @@ describe('/template', () => { auth: {basic: 'admin'}, httpStatus: 200, req: {name: 'spectrum', parameters: [{name: 'dpt', range: { type: 'array'}}, {name: 'device', range: {}}]}, - res: {_id: '300000000000000000000001', name: 'spectrum', version: 1, parameters: [{name: 'dpt', range: {type: 'array'}}, {name: 'device', range: {}}]} + res: {_id: '300000000000000000000001', name: 'spectrum', version: 1, first_id: '300000000000000000000001', parameters: [{name: 'dpt', range: {type: 'array'}}, {name: 'device', range: {}}]} }); }); it('keeps only one unchanged property', done => { @@ -573,7 +578,7 @@ describe('/template', () => { auth: {basic: 'admin'}, httpStatus: 200, req: {name: 'spectrum'}, - res: {_id: '300000000000000000000001', name: 'spectrum', version: 1, parameters: [{name: 'dpt', range: {type: 'array'}}, {name: 'device', range: {}}]} + res: {_id: '300000000000000000000001', name: 'spectrum', version: 1, first_id: '300000000000000000000001', parameters: [{name: 'dpt', range: {type: 'array'}}, {name: 'device', range: {}}]} }); }); it('changes the given properties', done => { @@ -585,7 +590,7 @@ describe('/template', () => { req: {name: 'IR spectrum', parameters: [{name: 'data point table', range: {min: 0, max: 1000}}]} }).end((err, res) => { if (err) return done(err); - should(_.omit(res.body, '_id')).be.eql({name: 'IR spectrum', version: 2, parameters: [{name: 'data point table', range: {min: 0, max: 1000}}]}); + should(_.omit(res.body, '_id')).be.eql({name: 'IR spectrum', version: 2, first_id: '300000000000000000000001', parameters: [{name: 'data point table', range: {min: 0, max: 1000}}]}); TemplateMeasurementModel.findById(res.body._id).lean().exec((err, data:any) => { if (err) return done(err); should(data).have.only.keys('_id', 'first_id', 'name', 'version', 'parameters', '__v'); @@ -626,7 +631,7 @@ describe('/template', () => { req: {name: 'IR spectrum'}, }).end((err, res) => { if (err) return done(err); - should(_.omit(res.body, '_id')).be.eql({name: 'IR spectrum', version: 2, parameters: [{name: 'dpt', range: {type: 'array'}}, {name: 'device', range: {}}]}); + should(_.omit(res.body, '_id')).be.eql({name: 'IR spectrum', version: 2, first_id: '300000000000000000000001', parameters: [{name: 'dpt', range: {type: 'array'}}, {name: 'device', range: {}}]}); TemplateMeasurementModel.findById(res.body._id).lean().exec((err, data:any) => { if (err) return done(err); should(data).have.only.keys('_id', 'first_id', 'name', 'version', 'parameters', '__v'); @@ -652,7 +657,7 @@ describe('/template', () => { req: {parameters: [{name: 'dpt', range: {values: [1, 2, 5]}}]} }).end((err, res) => { if (err) return done(err); - should(_.omit(res.body, '_id')).be.eql({name: 'spectrum', version: 2, parameters: [{name: 'dpt', range: {values: [1, 2, 5]}}]}); + should(_.omit(res.body, '_id')).be.eql({name: 'spectrum', version: 2, first_id: '300000000000000000000001', parameters: [{name: 'dpt', range: {values: [1, 2, 5]}}]}); done(); }); }); @@ -665,7 +670,7 @@ describe('/template', () => { req: {parameters: [{name: 'dpt', range: {min: 0, max: 1000}}]} }).end((err, res) => { if (err) return done(err); - should(_.omit(res.body, '_id')).be.eql({name: 'spectrum', version: 2, parameters: [{name: 'dpt', range: {min: 0, max: 1000}}]}); + should(_.omit(res.body, '_id')).be.eql({name: 'spectrum', version: 2, first_id: '300000000000000000000001', parameters: [{name: 'dpt', range: {min: 0, max: 1000}}]}); done(); }); }); @@ -678,7 +683,7 @@ describe('/template', () => { req: {parameters: [{name: 'dpt2', range: {type: 'array'}}]} }).end((err, res) => { if (err) return done(err); - should(_.omit(res.body, '_id')).be.eql({name: 'spectrum', version: 2, parameters: [{name: 'dpt2', range: {type: 'array'}}]}); + should(_.omit(res.body, '_id')).be.eql({name: 'spectrum', version: 2, first_id: '300000000000000000000001', parameters: [{name: 'dpt2', range: {type: 'array'}}]}); done(); }); }); @@ -691,7 +696,7 @@ describe('/template', () => { req: {parameters: [{name: 'weight %', range: {}}]} }).end((err, res) => { if (err) return done(err); - should(_.omit(res.body, '_id')).be.eql({name: 'kf', version: 2, parameters: [{name: 'weight %', range: {}}]}); + should(_.omit(res.body, '_id')).be.eql({name: 'kf', version: 2, first_id: '300000000000000000000002', parameters: [{name: 'weight %', range: {}}]}); done(); }); }); @@ -761,9 +766,10 @@ describe('/template', () => { req: {name: 'vz', parameters: [{name: 'vz', range: {min: 1}}]} }).end((err, res) => { if (err) return done(err); - should(res.body).have.only.keys('_id', 'name', 'version', 'parameters'); + should(res.body).have.only.keys('_id', 'name', 'version', 'first_id', 'parameters'); should(res.body).have.property('name', 'vz'); should(res.body).have.property('version', 1); + should(res.body._id).be.eql(res.body.first_id); should(res.body).have.property('parameters').have.lengthOf(1); should(res.body.parameters[0]).have.property('name', 'vz'); should(res.body.parameters[0]).have.property('range'); @@ -911,7 +917,7 @@ describe('/template', () => { const json = require('../test/db.json'); should(res.body).have.lengthOf(json.collections.material_templates.length); should(res.body).matchEach(measurement => { - should(measurement).have.only.keys('_id', 'name', 'version', 'parameters'); + should(measurement).have.only.keys('_id', 'name', 'version', 'first_id', 'parameters'); should(measurement).have.property('_id').be.type('string'); should(measurement).have.property('name').be.type('string'); should(measurement).have.property('version').be.type('number'); diff --git a/src/routes/validate/template.ts b/src/routes/validate/template.ts index 0721bd7..aed8f68 100644 --- a/src/routes/validate/template.ts +++ b/src/routes/validate/template.ts @@ -65,6 +65,7 @@ export default class TemplateValidate { _id: IdValidate.get(), name: this.template.name, version: this.template.version, + first_id: IdValidate.get(), parameters: this.template.parameters }).validate(data, {stripUnknown: true}); return error !== undefined? null : value;