From 91ef1e4180193d568095e9e0f2d53b514dd2760c Mon Sep 17 00:00:00 2001 From: Jean-PhilippeD Date: Sun, 25 Sep 2016 15:56:55 +0200 Subject: [PATCH] add getByType and test --- api/core/devicetype/deviceType.getByType.js | 18 +++++++++++++++ api/core/devicetype/index.js | 4 +++- .../devicetype/deviceType.getByType.test.js | 23 +++++++++++++++++++ .../unit/api/validator/deviceTypeValidator.js | 22 ++++++++++++++++++ 4 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 api/core/devicetype/deviceType.getByType.js create mode 100644 test/unit/api/core/devicetype/deviceType.getByType.test.js create mode 100644 test/unit/api/validator/deviceTypeValidator.js diff --git a/api/core/devicetype/deviceType.getByType.js b/api/core/devicetype/deviceType.getByType.js new file mode 100644 index 0000000000..3270022567 --- /dev/null +++ b/api/core/devicetype/deviceType.getByType.js @@ -0,0 +1,18 @@ +var queries = require('./deviceType.queries.js'); +var Promise = require('bluebird'); + +module.exports = function(type){ + + // get all deviceTypes for a given tag + return gladys.utils.sql(queries.getByType, [type]) + .then(function(deviceTypes){ + // if there is some deviceTypes for this tag + if(deviceTypes.length){ + // return deviceTypes + return deviceTypes; + } else { + return null; + } + + }); +} diff --git a/api/core/devicetype/index.js b/api/core/devicetype/index.js index e6cca5b69f..420d2478a6 100644 --- a/api/core/devicetype/index.js +++ b/api/core/devicetype/index.js @@ -6,4 +6,6 @@ module.exports.exec = require('./deviceType.exec.js'); module.exports.save = require('./deviceType.save.js'); module.exports.getAll = require('./deviceType.getAll.js'); module.exports.getByRoom = require('./deviceType.getByRoom.js'); -module.exports.getByDevice = require('./deviceType.getByDevice.js'); \ No newline at end of file +module.exports.getByDevice = require('./deviceType.getByDevice.js'); +module.exports.getByType = require('./deviceType.getByType.js'); +module.exports.getById = require('./deviceType.getById.js'); diff --git a/test/unit/api/core/devicetype/deviceType.getByType.test.js b/test/unit/api/core/devicetype/deviceType.getByType.test.js new file mode 100644 index 0000000000..54bca85eb1 --- /dev/null +++ b/test/unit/api/core/devicetype/deviceType.getByType.test.js @@ -0,0 +1,23 @@ +var should = require('should'); +var validateDeviceType = require('../../validator/deviceTypeValidator.js'); + +describe('DeviceType', function() { + + describe('getByType', function() { + + it('should return a deviceType', function (done) { + + var type = 'binary'; + + gladys.deviceType.getByType(type).then(function(type){ + validateDeviceType(type); + done(); + }).catch(function(err){ + done(err); + }); + + }); + + }); + +}); diff --git a/test/unit/api/validator/deviceTypeValidator.js b/test/unit/api/validator/deviceTypeValidator.js new file mode 100644 index 0000000000..5e3b4f4103 --- /dev/null +++ b/test/unit/api/validator/deviceTypeValidator.js @@ -0,0 +1,22 @@ +module.exports = validate; + +var should = require('should'); + +function validate(devicetype) { + if(devicetype instanceof Array) { + devicetype.forEach(validateDeviceType); + } elseĀ { + validateDeviceType(devicetype); + } +} + +function validateDeviceType(devicetype) { + device.should.be.instanceOf(Object); + device.should.have.property('id'); + device.should.have.property('type'); + device.should.have.property('tag'); + device.should.have.property('sensor'); + device.should.have.property('device'); + device.should.have.property('min'); + device.should.have.property('max'); +}