Archived
2

added first_id for template response

This commit is contained in:
VLE2FE 2020-07-23 15:25:45 +02:00
parent 3b08fb63c7
commit a8e8b28c11
3 changed files with 34 additions and 23 deletions

View File

@ -158,6 +158,10 @@ Template:
type: number type: number
readOnly: true readOnly: true
example: 1 example: 1
first_id:
readOnly: true
type: string
example: 5ea0450ed851c30a90e70894
parameters: parameters:
type: array type: array
items: items:

View File

@ -25,10 +25,11 @@ describe('/template', () => {
const json = require('../test/db.json'); const json = require('../test/db.json');
should(res.body).have.lengthOf(json.collections.condition_templates.length); should(res.body).have.lengthOf(json.collections.condition_templates.length);
should(res.body).matchEach(condition => { 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('_id').be.type('string');
should(condition).have.property('name').be.type('string'); should(condition).have.property('name').be.type('string');
should(condition).have.property('version').be.type('number'); should(condition).have.property('version').be.type('number');
should(condition).have.property('first_id').be.type('string');
should(condition.parameters).matchEach(number => { should(condition.parameters).matchEach(number => {
should(number).have.only.keys('name', 'range'); should(number).have.only.keys('name', 'range');
should(number).have.property('name').be.type('string'); should(number).have.property('name').be.type('string');
@ -62,7 +63,7 @@ describe('/template', () => {
url: '/template/condition/200000000000000000000001', url: '/template/condition/200000000000000000000001',
auth: {basic: 'janedoe'}, auth: {basic: 'janedoe'},
httpStatus: 200, 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 => { it('rejects an API key', done => {
@ -98,7 +99,7 @@ describe('/template', () => {
auth: {basic: 'admin'}, auth: {basic: 'admin'},
httpStatus: 200, httpStatus: 200,
req: {}, 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 => { it('keeps unchanged properties', done => {
@ -108,7 +109,7 @@ describe('/template', () => {
auth: {basic: 'admin'}, auth: {basic: 'admin'},
httpStatus: 200, httpStatus: 200,
req: {name: 'heat treatment', parameters: [{name: 'material', range: {values: ['copper', 'hot air']}}, {name: 'weeks', range: {min: 1, max: 10}}]}, 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 => { it('keeps only one unchanged property', done => {
@ -118,7 +119,7 @@ describe('/template', () => {
auth: {basic: 'admin'}, auth: {basic: 'admin'},
httpStatus: 200, httpStatus: 200,
req: {name: 'heat treatment'}, 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 => { it('changes the given properties', done => {
@ -136,6 +137,7 @@ describe('/template', () => {
should(data.first_id.toString()).be.eql('200000000000000000000001'); should(data.first_id.toString()).be.eql('200000000000000000000001');
should(data).have.property('name', 'heat aging'); should(data).have.property('name', 'heat aging');
should(data).have.property('version', 2); should(data).have.property('version', 2);
should(data.first_id.toString()).be.eql('200000000000000000000001');
should(data).have.property('parameters').have.lengthOf(1); should(data).have.property('parameters').have.lengthOf(1);
should(data.parameters[0]).have.property('name', 'time'); should(data.parameters[0]).have.property('name', 'time');
should(data.parameters[0]).have.property('range'); should(data.parameters[0]).have.property('range');
@ -191,7 +193,7 @@ describe('/template', () => {
req: {parameters: [{name: 'time', range: {values: [1, 2, 5]}}]} req: {parameters: [{name: 'time', range: {values: [1, 2, 5]}}]}
}).end((err, res) => { }).end((err, res) => {
if (err) return done(err); 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(); done();
}); });
}); });
@ -204,7 +206,7 @@ describe('/template', () => {
req: {parameters: [{name: 'time', range: {min: 1, max: 11}}]} req: {parameters: [{name: 'time', range: {min: 1, max: 11}}]}
}).end((err, res) => { }).end((err, res) => {
if (err) return done(err); 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(); done();
}); });
}); });
@ -217,7 +219,7 @@ describe('/template', () => {
req: {parameters: [{name: 'time', range: {type: 'array'}}]} req: {parameters: [{name: 'time', range: {type: 'array'}}]}
}).end((err, res) => { }).end((err, res) => {
if (err) return done(err); 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(); done();
}); });
}); });
@ -230,7 +232,7 @@ describe('/template', () => {
req: {parameters: [{name: 'time', range: {}}]} req: {parameters: [{name: 'time', range: {}}]}
}).end((err, res) => { }).end((err, res) => {
if (err) return done(err); 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(); done();
}); });
}); });
@ -310,9 +312,10 @@ describe('/template', () => {
req: {name: 'heat treatment3', parameters: [{name: 'material', range: {values: ['copper']}}]} req: {name: 'heat treatment3', parameters: [{name: 'material', range: {values: ['copper']}}]}
}).end((err, res) => { }).end((err, res) => {
if (err) return done(err); 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('name', 'heat treatment3');
should(res.body).have.property('version', 1); 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).have.property('parameters').have.lengthOf(1);
should(res.body.parameters[0]).have.property('name', 'material'); should(res.body.parameters[0]).have.property('name', 'material');
should(res.body.parameters[0]).have.property('range'); 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.first_id.toString()).be.eql(data._id.toString());
should(data).have.property('name', 'heat aging'); should(data).have.property('name', 'heat aging');
should(data).have.property('version', 1); 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).have.property('parameters').have.lengthOf(1);
should(data.parameters[0]).have.property('name', 'time'); should(data.parameters[0]).have.property('name', 'time');
should(data.parameters[0]).have.property('range'); should(data.parameters[0]).have.property('range');
@ -480,8 +484,9 @@ describe('/template', () => {
const json = require('../test/db.json'); const json = require('../test/db.json');
should(res.body).have.lengthOf(json.collections.measurement_templates.length); should(res.body).have.lengthOf(json.collections.measurement_templates.length);
should(res.body).matchEach(measurement => { 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('_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('name').be.type('string');
should(measurement).have.property('version').be.type('number'); should(measurement).have.property('version').be.type('number');
should(measurement.parameters).matchEach(number => { should(measurement.parameters).matchEach(number => {
@ -517,7 +522,7 @@ describe('/template', () => {
url: '/template/measurement/300000000000000000000001', url: '/template/measurement/300000000000000000000001',
auth: {basic: 'janedoe'}, auth: {basic: 'janedoe'},
httpStatus: 200, 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 => { it('rejects an API key', done => {
@ -553,7 +558,7 @@ describe('/template', () => {
auth: {basic: 'admin'}, auth: {basic: 'admin'},
httpStatus: 200, httpStatus: 200,
req: {}, 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 => { it('keeps unchanged properties', done => {
@ -563,7 +568,7 @@ describe('/template', () => {
auth: {basic: 'admin'}, auth: {basic: 'admin'},
httpStatus: 200, httpStatus: 200,
req: {name: 'spectrum', parameters: [{name: 'dpt', range: { type: 'array'}}, {name: 'device', range: {}}]}, 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 => { it('keeps only one unchanged property', done => {
@ -573,7 +578,7 @@ describe('/template', () => {
auth: {basic: 'admin'}, auth: {basic: 'admin'},
httpStatus: 200, httpStatus: 200,
req: {name: 'spectrum'}, 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 => { 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}}]} req: {name: 'IR spectrum', parameters: [{name: 'data point table', range: {min: 0, max: 1000}}]}
}).end((err, res) => { }).end((err, res) => {
if (err) return done(err); 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) => { TemplateMeasurementModel.findById(res.body._id).lean().exec((err, data:any) => {
if (err) return done(err); if (err) return done(err);
should(data).have.only.keys('_id', 'first_id', 'name', 'version', 'parameters', '__v'); should(data).have.only.keys('_id', 'first_id', 'name', 'version', 'parameters', '__v');
@ -626,7 +631,7 @@ describe('/template', () => {
req: {name: 'IR spectrum'}, req: {name: 'IR spectrum'},
}).end((err, res) => { }).end((err, res) => {
if (err) return done(err); 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) => { TemplateMeasurementModel.findById(res.body._id).lean().exec((err, data:any) => {
if (err) return done(err); if (err) return done(err);
should(data).have.only.keys('_id', 'first_id', 'name', 'version', 'parameters', '__v'); 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]}}]} req: {parameters: [{name: 'dpt', range: {values: [1, 2, 5]}}]}
}).end((err, res) => { }).end((err, res) => {
if (err) return done(err); 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(); done();
}); });
}); });
@ -665,7 +670,7 @@ describe('/template', () => {
req: {parameters: [{name: 'dpt', range: {min: 0, max: 1000}}]} req: {parameters: [{name: 'dpt', range: {min: 0, max: 1000}}]}
}).end((err, res) => { }).end((err, res) => {
if (err) return done(err); 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(); done();
}); });
}); });
@ -678,7 +683,7 @@ describe('/template', () => {
req: {parameters: [{name: 'dpt2', range: {type: 'array'}}]} req: {parameters: [{name: 'dpt2', range: {type: 'array'}}]}
}).end((err, res) => { }).end((err, res) => {
if (err) return done(err); 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(); done();
}); });
}); });
@ -691,7 +696,7 @@ describe('/template', () => {
req: {parameters: [{name: 'weight %', range: {}}]} req: {parameters: [{name: 'weight %', range: {}}]}
}).end((err, res) => { }).end((err, res) => {
if (err) return done(err); 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(); done();
}); });
}); });
@ -761,9 +766,10 @@ describe('/template', () => {
req: {name: 'vz', parameters: [{name: 'vz', range: {min: 1}}]} req: {name: 'vz', parameters: [{name: 'vz', range: {min: 1}}]}
}).end((err, res) => { }).end((err, res) => {
if (err) return done(err); 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('name', 'vz');
should(res.body).have.property('version', 1); 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).have.property('parameters').have.lengthOf(1);
should(res.body.parameters[0]).have.property('name', 'vz'); should(res.body.parameters[0]).have.property('name', 'vz');
should(res.body.parameters[0]).have.property('range'); should(res.body.parameters[0]).have.property('range');
@ -911,7 +917,7 @@ describe('/template', () => {
const json = require('../test/db.json'); const json = require('../test/db.json');
should(res.body).have.lengthOf(json.collections.material_templates.length); should(res.body).have.lengthOf(json.collections.material_templates.length);
should(res.body).matchEach(measurement => { 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('_id').be.type('string');
should(measurement).have.property('name').be.type('string'); should(measurement).have.property('name').be.type('string');
should(measurement).have.property('version').be.type('number'); should(measurement).have.property('version').be.type('number');

View File

@ -65,6 +65,7 @@ export default class TemplateValidate {
_id: IdValidate.get(), _id: IdValidate.get(),
name: this.template.name, name: this.template.name,
version: this.template.version, version: this.template.version,
first_id: IdValidate.get(),
parameters: this.template.parameters parameters: this.template.parameters
}).validate(data, {stripUnknown: true}); }).validate(data, {stripUnknown: true});
return error !== undefined? null : value; return error !== undefined? null : value;