Skip to content

Commit

Permalink
add getByType and test
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean-PhilippeD committed Sep 25, 2016
1 parent 2e84047 commit 91ef1e4
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 1 deletion.
18 changes: 18 additions & 0 deletions api/core/devicetype/deviceType.getByType.js
Original file line number Diff line number Diff line change
@@ -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;
}

});
}
4 changes: 3 additions & 1 deletion api/core/devicetype/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
module.exports.getByDevice = require('./deviceType.getByDevice.js');
module.exports.getByType = require('./deviceType.getByType.js');
module.exports.getById = require('./deviceType.getById.js');
23 changes: 23 additions & 0 deletions test/unit/api/core/devicetype/deviceType.getByType.test.js
Original file line number Diff line number Diff line change
@@ -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);
});

});

});

});
22 changes: 22 additions & 0 deletions test/unit/api/validator/deviceTypeValidator.js
Original file line number Diff line number Diff line change
@@ -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');
}

0 comments on commit 91ef1e4

Please sign in to comment.