fixed sample requests for the restructured material
This commit is contained in:
@ -239,8 +239,11 @@ async function propertiesCheck (properties, param, res, next, checkVersion = tru
|
||||
}
|
||||
|
||||
// validate parameters
|
||||
const {error, value: ignore} = ParametersValidate.input(_.omit(properties, 'material_template'), materialData.parameters, param);
|
||||
const {error, value} = ParametersValidate.input(_.omit(properties, 'material_template'), materialData.parameters, param);
|
||||
if (error) {res400(error, res); return false;}
|
||||
Object.keys(value).forEach(key => {
|
||||
properties[key] = value[key];
|
||||
});
|
||||
return materialData;
|
||||
}
|
||||
|
||||
|
@ -200,7 +200,7 @@ describe('/sample', () => {
|
||||
}).end((err, res) => {
|
||||
if (err) return done(err);
|
||||
should(res.body[0]).have.property('number', 'Rng36');
|
||||
should(res.body[1]).have.property('number', '33');
|
||||
should(res.body[1]).have.property('number', '34');
|
||||
should(res.body[res.body.length - 1]).have.property('number', '1');
|
||||
done();
|
||||
});
|
||||
@ -214,7 +214,7 @@ describe('/sample', () => {
|
||||
}).end((err, res) => {
|
||||
if (err) return done(err);
|
||||
should(res.body[0]).have.property('_id', '400000000000000000000006');
|
||||
should(res.body[1]).have.property('_id', '400000000000000000000002');
|
||||
should(res.body[1]).have.property('_id', '400000000000000000000007');
|
||||
done();
|
||||
});
|
||||
});
|
||||
@ -226,7 +226,7 @@ describe('/sample', () => {
|
||||
httpStatus: 200
|
||||
}).end((err, res) => {
|
||||
if (err) return done(err);
|
||||
should(res.body[0]).have.property('_id', '400000000000000000000002');
|
||||
should(res.body[0]).have.property('_id', '400000000000000000000007');
|
||||
should(res.body[1]).have.property('_id', '400000000000000000000006');
|
||||
done();
|
||||
});
|
||||
@ -334,6 +334,38 @@ describe('/sample', () => {
|
||||
done();
|
||||
});
|
||||
});
|
||||
it('filters by a measurement properties property', done => {
|
||||
TestHelper.request(server, done, {
|
||||
method: 'get',
|
||||
url: '/samples?status=all&fields[]=number&fields[]=material.name&fields[]=material.properties.glass_fiber&filters[]=%7B%22mode%22%3A%22eq%22%2C%22field%22%3A%22material.properties.glass_fiber%22%2C%22values%22%3A%5B%2225%22%5D%7D',
|
||||
auth: {basic: 'janedoe'},
|
||||
httpStatus: 200
|
||||
}).end((err, res) => {
|
||||
if (err) return done(err);
|
||||
should(res.body).have.lengthOf(2);
|
||||
should(res.body).matchEach(sample => {
|
||||
should(sample.material.properties.glass_fiber).be.eql(25);
|
||||
});
|
||||
done();
|
||||
});
|
||||
});
|
||||
it('filters and sorts by a measurement properties property', done => {
|
||||
TestHelper.request(server, done, {
|
||||
method: 'get',
|
||||
url: '/samples?status=all&sort=material.properties.glass_fiber-desc&fields[]=number&fields[]=material.name&fields[]=material.properties.glass_fiber&filters[]=%7B%22mode%22%3A%22eq%22%2C%22field%22%3A%22material.properties.glass_fiber%22%2C%22values%22%3A%5B%2225%22%5D%7D',
|
||||
auth: {basic: 'janedoe'},
|
||||
httpStatus: 200
|
||||
}).end((err, res) => {
|
||||
if (err) return done(err);
|
||||
should(res.body).have.lengthOf(2);
|
||||
should(res.body[0].number).be.eql('Rng36');
|
||||
should(res.body[1].number).be.eql('1');
|
||||
should(res.body).matchEach(sample => {
|
||||
should(sample.material.properties.glass_fiber).be.eql(25);
|
||||
});
|
||||
done();
|
||||
});
|
||||
});
|
||||
it('filters multiple properties', done => {
|
||||
TestHelper.request(server, done, {
|
||||
method: 'get',
|
||||
@ -342,7 +374,7 @@ describe('/sample', () => {
|
||||
httpStatus: 200
|
||||
}).end((err, res) => {
|
||||
if (err) return done(err);
|
||||
should(res.body).have.lengthOf(3);
|
||||
should(res.body).have.lengthOf(4);
|
||||
should(res.body[0]).be.eql({number: '1', batch: ''});
|
||||
done();
|
||||
});
|
||||
@ -359,7 +391,7 @@ describe('/sample', () => {
|
||||
it('rejects an invalid filter mode', done => {
|
||||
TestHelper.request(server, done, {
|
||||
method: 'get',
|
||||
url: '/samples?status=all&fields[]=number&fields[]=material.glass_fiber&fields[]=batch&filters[]=%7B%22mode%22%3A%22xx%22%2C%22field%22%3A%22batch%22%2C%22values%22%3A%5B%221704-005%22%5D%7D',
|
||||
url: '/samples?status=all&fields[]=number&fields[]=batch&filters[]=%7B%22mode%22%3A%22xx%22%2C%22field%22%3A%22batch%22%2C%22values%22%3A%5B%221704-005%22%5D%7D',
|
||||
auth: {basic: 'janedoe'},
|
||||
httpStatus: 400,
|
||||
res: {status: 'Invalid body format', details: '"filters[0].mode" must be one of [eq, ne, lt, lte, gt, gte, in, nin]'}
|
||||
@ -407,6 +439,25 @@ describe('/sample', () => {
|
||||
res: [{number: '1', condition: {material: 'copper', weeks: 3, condition_template: '200000000000000000000001'}, color: 'black', material: {name: 'Schulamid 66 GF 25 H', supplier: 'Schulmann'}}]
|
||||
});
|
||||
});
|
||||
it('returns specified material properties fields', done => {
|
||||
TestHelper.request(server, done, {
|
||||
method: 'get',
|
||||
url: '/samples?status=all&fields[]=number&fields[]=material.properties.glass_fiber&fields[]=material.name',
|
||||
auth: {basic: 'janedoe'},
|
||||
httpStatus: 200
|
||||
}).end((err, res) => {
|
||||
if (err) return done(err);
|
||||
const json = require('../test/db.json');
|
||||
should(res.body).matchEach(sample => {
|
||||
const materialId = json.collections.samples.find(e => e.number === sample.number).material_id;
|
||||
const material = json.collections.materials.find(e => e._id.toString() == materialId);
|
||||
should(sample).have.only.keys('number', 'material');
|
||||
should(sample.material.name).be.eql(material.name);
|
||||
should(sample.material.properties.glass_fiber).be.eql(material.properties.glass_fiber);
|
||||
});
|
||||
done()
|
||||
});
|
||||
});
|
||||
it('rejects a from-id not in the database', done => {
|
||||
TestHelper.request(server, done, {
|
||||
method: 'get',
|
||||
|
@ -22,7 +22,6 @@ import csv from '../helpers/csv';
|
||||
const router = express.Router();
|
||||
|
||||
// TODO: check added filter
|
||||
// TODO: return total number of pages -> use facet
|
||||
// TODO: use query pointer
|
||||
// TODO: convert filter value to number according to table model
|
||||
// TODO: validation for filter parameters
|
||||
|
@ -907,7 +907,7 @@ describe('/template', () => {
|
||||
}).end((err, res) => {
|
||||
if (err) return done(err);
|
||||
const json = require('../test/db.json');
|
||||
should(res.body).have.lengthOf(json.collections.measurement_templates.length);
|
||||
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.property('_id').be.type('string');
|
||||
|
@ -4,6 +4,7 @@ import _ from 'lodash';
|
||||
import TemplateValidate from './validate/template';
|
||||
import ConditionTemplateModel from '../models/condition_template';
|
||||
import MeasurementTemplateModel from '../models/measurement_template';
|
||||
import MaterialTemplateModel from '../models/material_template';
|
||||
import res400 from './validate/res400';
|
||||
import IdValidate from './validate/id';
|
||||
import mongoose from "mongoose";
|
||||
@ -82,5 +83,9 @@ router.post('/template/:collection(measurement|condition|material)/new', async (
|
||||
module.exports = router;
|
||||
|
||||
function model (req) { // return right template model
|
||||
return req.params.collection === 'condition' ? ConditionTemplateModel : MeasurementTemplateModel;
|
||||
switch (req.params.collection) {
|
||||
case 'condition': return ConditionTemplateModel
|
||||
case 'measurement': return MeasurementTemplateModel
|
||||
case 'material': return MaterialTemplateModel
|
||||
}
|
||||
}
|
@ -18,7 +18,7 @@ export default class MaterialValidate { // validate input for material
|
||||
numbers: Joi.array()
|
||||
.items(
|
||||
Joi.string()
|
||||
.length(10)
|
||||
.max(64)
|
||||
)
|
||||
};
|
||||
|
||||
|
@ -10,6 +10,7 @@ export default class ParametersValidate {
|
||||
.valid(...parameter.range.values);
|
||||
}
|
||||
else if (parameter.range.hasOwnProperty('min') && parameter.range.hasOwnProperty('max')) {
|
||||
|
||||
joiObject[parameter.name] = Joi.number()
|
||||
.min(parameter.range.min)
|
||||
.max(parameter.range.max);
|
||||
|
@ -62,10 +62,8 @@ export default class SampleValidate {
|
||||
'material.name',
|
||||
'material.supplier',
|
||||
'material.group',
|
||||
'material.mineral',
|
||||
'material.glass_fiber',
|
||||
'material.carbon_fiber',
|
||||
'material.number',
|
||||
'material.properties.*',
|
||||
'measurements.(?!spectrum)*'
|
||||
];
|
||||
|
||||
@ -175,8 +173,8 @@ export default class SampleValidate {
|
||||
let validator;
|
||||
let field = data.filters[i].field
|
||||
if (/material\./.test(field)) { // select right validation model
|
||||
validator = MaterialValidate.outputV().append({number: Joi.string().max(128).allow('')});
|
||||
field = field.replace('material.', '');
|
||||
validator = MaterialValidate.outputV().append({number: Joi.string().max(128).allow(''), properties: Joi.alternatives().try(Joi.number(), Joi.string().max(128))});
|
||||
field = field.replace('material.', '').split('.')[0];
|
||||
}
|
||||
else if (/measurements\./.test(field)) {
|
||||
validator = Joi.object({
|
||||
@ -215,7 +213,7 @@ export default class SampleValidate {
|
||||
filters: Joi.array().items(Joi.object({
|
||||
mode: Joi.string().valid('eq', 'ne', 'lt', 'lte', 'gt', 'gte', 'in', 'nin'),
|
||||
field: Joi.string().pattern(new RegExp('^(' + this.fieldKeys.join('|').replace(/\./g, '\\.').replace(/\*/g, '.+') + ')$', 'm')).messages({'string.pattern.base': 'Invalid filter field name'}),
|
||||
values: Joi.array().items(Joi.alternatives().try(Joi.string().max(128), Joi.number(), Joi.boolean(), Joi.date().iso())).min(1)
|
||||
values: Joi.array().items(Joi.alternatives().try(Joi.string().max(128), Joi.number(), Joi.boolean(), Joi.date().iso(), Joi.object())).min(1)
|
||||
})).default([])
|
||||
}).with('to-page', 'page-size').validate(data);
|
||||
}
|
||||
|
@ -95,6 +95,19 @@
|
||||
"user_id": {"$oid":"000000000000000000000002"},
|
||||
"status": 0,
|
||||
"__v": 0
|
||||
},
|
||||
{
|
||||
"_id": {"$oid":"400000000000000000000007"},
|
||||
"number": "34",
|
||||
"type": "liquid",
|
||||
"color": "black",
|
||||
"batch": "",
|
||||
"condition": {},
|
||||
"material_id": {"$oid":"100000000000000000000009"},
|
||||
"note_id": null,
|
||||
"user_id": {"$oid":"000000000000000000000002"},
|
||||
"status": 0,
|
||||
"__v": 0
|
||||
}
|
||||
],
|
||||
"notes": [
|
||||
|
Reference in New Issue
Block a user