Skip to content

Commit b0a3620

Browse files
committed
Improved getOwners and developed relative tests. All tests pass #38
1 parent 73bc0de commit b0a3620

File tree

2 files changed

+66
-3
lines changed

2 files changed

+66
-3
lines changed

api/services/OperatorService.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
/* jshint esnext: true */
88
/* jshint node: true */
9-
/* globals _, sails , GroupService */
9+
/* globals _, sails , GroupService, DataType */
1010

1111
const BluebirdPromise = require('bluebird');
1212

@@ -17,8 +17,16 @@ const coroutines = {
1717
if (!datum || !datum.type) {
1818
return [];
1919
}
20-
let idProject = _.isObject(datum.type.project) ? datum.type.project.id : datum.type.project;
21-
// _.isObject(datum.type.project);
20+
21+
let idProject;
22+
if (!_.isObject(datum.type)) {
23+
let dataType = yield DataType.findOne(datum.type);
24+
idProject = dataType.project;
25+
}
26+
else {
27+
idProject = _.isObject(datum.type.project) ? datum.type.project.id : datum.type.project;
28+
}
29+
2230
let groups = yield GroupService.getGroupsByProject(idProject);
2331

2432
let operators = _.uniq(_.flatten(_.map(groups,'members')));
+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/* jshint node:true */
2+
/* jshint mocha: true */
3+
/* globals _, sails, fixtures, OperatorService, DataType*/
4+
"use strict";
5+
var chai = require("chai");
6+
var expect = chai.expect, sinon = require('sinon');
7+
8+
describe('OperatorService', function() {
9+
10+
describe('#getOwners', function() {
11+
12+
it("should return an empty array", function(done) {
13+
// var idProject = fixtures.project[0].id;
14+
15+
OperatorService.getOwners().then(function (err,res) {
16+
17+
expect(res).to.be.empty;
18+
done();
19+
return;
20+
});
21+
});
22+
23+
24+
it("should return an array containg all elegible owners", function(done) {
25+
let spy = sinon.spy(DataType, "findOne");
26+
var datum = _.cloneDeep(fixtures.data[0]);
27+
datum.type = _.cloneDeep(fixtures.datatype[2]);
28+
var expectedOperators = _.cloneDeep(fixtures.operator);
29+
30+
OperatorService.getOwners(datum).then(function (res) {
31+
sinon.assert.notCalled(spy);
32+
expect(res.length).to.eql(expectedOperators.length);
33+
34+
DataType.findOne.restore();
35+
done();
36+
return;
37+
});
38+
});
39+
40+
it("should return an array containg all elegible owners retrieving dataType ", function(done) {
41+
let spy = sinon.spy(DataType, "findOne");
42+
var datum = _.cloneDeep(fixtures.data[0]);
43+
var expectedOperators = _.cloneDeep(fixtures.operator);
44+
OperatorService.getOwners(datum).then(function (res) {
45+
sinon.assert.called(spy);
46+
expect(res.length).to.eql(expectedOperators.length);
47+
48+
DataType.findOne.restore();
49+
done();
50+
return;
51+
});
52+
});
53+
});
54+
55+
});

0 commit comments

Comments
 (0)