Skip to content

Commit

Permalink
Adding type tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
jason-fox committed Feb 29, 2024
1 parent 4c2a93c commit 2046331
Show file tree
Hide file tree
Showing 10 changed files with 320 additions and 8 deletions.
2 changes: 0 additions & 2 deletions controllers/subscriptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,6 @@ async function createSubscription(req, res) {
const response = await Request.sendRequest('/subscriptions', options);

res.statusCode = response.statusCode;

console.log(response.headers);
if (response.headers.location) {
{
res.set(
Expand Down
10 changes: 10 additions & 0 deletions lib/ngsi-ld.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,28 +49,33 @@ function formatAttribute(attr, transformFlags = {}) {
// Other Native JSON Types
case 'boolean':
obj.value = !!attr.value;
obj.valueType = Config.getConfig().valueType ? attr.type : undefined;
break;
case 'float':
if (isNaN(attr.value)) {
obj = undefined;
} else {
obj.value = Number.parseFloat(attr.value);
obj.valueType = Config.getConfig().valueType ? attr.type : undefined;
}
break;
case 'integer':
if (isNaN(attr.value)) {
obj = undefined;
} else {
obj.value = Number.parseInt(attr.value);
obj.valueType = Config.getConfig().valueType ? attr.type : undefined;
}
break;
case 'number':
if (isNaN(attr.value)) {
obj = undefined;
} else if (isFloat(attr.value)) {
obj.value = Number.parseFloat(attr.value);
obj.valueType = Config.getConfig().valueType ? attr.type : undefined;
} else {
obj.value = Number.parseInt(attr.value);
obj.valueType = Config.getConfig().valueType ? attr.type : undefined;
}
break;

Expand Down Expand Up @@ -172,6 +177,9 @@ function formatAttribute(attr, transformFlags = {}) {
case 'unitCode':
obj.unitCode = attr.metadata[key].value;
break;
case 'objectType':
obj.objectType = attr.metadata[key].value;
break;
case 'dateCreated':
obj.createdAt = moment.tz(attr.metadata[key].value, 'Etc/UTC').toISOString();
break;
Expand Down Expand Up @@ -360,6 +368,8 @@ function normalizeAttribute(attr) {
case 'objectList':
case 'json':
case 'unitCode':
case 'valueType':
case 'objectType':
case 'observedAt':
obj[key] = attr[key];
break;
Expand Down
15 changes: 10 additions & 5 deletions lib/ngsi-v2.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,13 @@ function formatAttribute(attr, isMetadata = false) {
case 'property':
default:
obj = { type: 'Property', value: attr.value };

if (Config.getConfig().valueType && attr.valueType) {
obj.type = attr.valueType;
}
break;
}

if (Config.getConfig().valueType && attr.valueType) {
obj.type = attr.valueType;
}

obj.metadata = {};

Object.keys(attr).forEach(function (key) {
Expand All @@ -191,7 +191,12 @@ function formatAttribute(attr, isMetadata = false) {
type: 'Property',
value: attr[key]
};

break;
case 'objectType':
obj.metadata.objectType = {
type: 'Property',
value: attr[key]
};
break;
case 'metadata':
obj.metadata = attr[key];
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"lint:md": "remark -f '*.md'",
"lint:text": "textlint '*.md'",
"prettier": "prettier --config .prettierrc.json --write **/**/*.js **/*.js *.js",
"test:single": "nyc --reporter=text mocha --recursive 'test/**/*.js' --reporter spec --timeout 5000 --ui bdd --exit --color true -g 'options'",
"test:single": "nyc --reporter=text mocha --recursive 'test/**/*.js' --reporter spec --timeout 5000 --ui bdd --exit --color true -g 'valueType'",
"test:html": "nyc --reporter=html mocha -- --recursive 'test/**/*.js' --reporter spec --exit",
"test": "nyc --reporter=text mocha --recursive 'test/**/*.js' --reporter spec --timeout 5000 --ui bdd --exit --color true",
"test:coverage": "nyc --reporter=lcov mocha -- --recursive 'test/**/*.js' --reporter spec --exit",
Expand Down
25 changes: 25 additions & 0 deletions test/ngsi-ld/Entity-valueType-concise.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"id": "urn:ngsi-ld:TemperatureSensor:001",
"type": "TemperatureSensor",
"batteryLevel": {
"value": 0.8,
"unitCode": "C62",
"valueType": "Float"
},
"category": {
"vocab": "sensor"
},
"controlledAsset": {
"object": "urn:ngsi-ld:Building:barn002",
"objectType": "Building"
},
"temperature": {
"value": 25,
"valueType": "Integer",
"unitCode": "CEL"
},
"location": {
"type": "Point",
"coordinates": [13.3986, 52.5547]
}
}
31 changes: 31 additions & 0 deletions test/ngsi-ld/Entity-valueType-no-id.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"type": "TemperatureSensor",
"batteryLevel": {
"type": "Property",
"value": 0.8,
"unitCode": "C62",
"valueType": "Float"
},
"category": {
"type": "VocabularyProperty",
"vocab": "sensor"
},
"controlledAsset": {
"type": "Relationship",
"object": "urn:ngsi-ld:Building:barn002",
"objectType": "Building"
},
"temperature": {
"type": "Property",
"value": 25,
"unitCode": "CEL",
"valueType": "Integer"
},
"location": {
"type": "GeoProperty",
"value": {
"type": "Point",
"coordinates": [13.3986, 52.5547]
}
}
}
32 changes: 32 additions & 0 deletions test/ngsi-ld/Entity-valueType.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"id": "urn:ngsi-ld:TemperatureSensor:001",
"type": "TemperatureSensor",
"batteryLevel": {
"type": "Property",
"value": 0.8,
"unitCode": "C62",
"valueType": "Float"
},
"category": {
"type": "VocabularyProperty",
"vocab": "sensor"
},
"controlledAsset": {
"type": "Relationship",
"object": "urn:ngsi-ld:Building:barn002",
"objectType": "Building"
},
"temperature": {
"type": "Property",
"value": 25,
"unitCode": "CEL",
"valueType": "Integer"
},
"location": {
"type": "GeoProperty",
"value": {
"type": "Point",
"coordinates": [13.3986, 52.5547]
}
}
}
47 changes: 47 additions & 0 deletions test/ngsi-v2/Entity-valueType.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"id": "urn:ngsi-ld:TemperatureSensor:001",
"type": "TemperatureSensor",
"batteryLevel": {
"type": "Float",
"value": 0.8,
"metadata": {
"unitCode": {
"type": "Property",
"value": "C62"
}
}
},
"category": {
"type": "VocabularyProperty",
"value": "sensor",
"metadata": {}
},
"controlledAsset": {
"type": "Relationship",
"value": "urn:ngsi-ld:Building:barn002",
"metadata": {
"objectType": {
"type": "Property",
"value": "Building"
}
}
},
"temperature": {
"type": "Integer",
"value": 25,
"metadata": {
"unitCode": {
"type": "Property",
"value": "CEL"
}
}
},
"location": {
"type": "geo:json",
"value": {
"type": "Point",
"coordinates": [13.3986, 52.5547]
},
"metadata": {}
}
}
76 changes: 76 additions & 0 deletions test/unit-test/entities/create-entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ describe('Create Entity', function () {
});
});



describe('When a concise entity is created', function () {
beforeEach(function (done) {
options.json = utils.readExampleFile('./test/ngsi-ld/Entity-concise.json');
Expand Down Expand Up @@ -183,3 +185,77 @@ describe('Create Entity', function () {
});
});
});




describe('Create Entity with valueType', function () {
beforeEach((done) => {
nock.cleanAll();
config.valueType = true;
lepus.start(config, () => {
done();
});
});

afterEach((done) => {
config.valueType = false;
lepus.stop(function () {
done();
});
});

const options = {
method: 'POST',
url: LEPUS_URL + 'entities'
};
const ORION_ENDPOINT = '/v2/entities';

describe('When a normalized entity with valueType is created', function () {
beforeEach(function (done) {
options.json = utils.readExampleFile('./test/ngsi-ld/Entity-valueType.json');
contextBrokerMock = nock(V2_BROKER)
.post(ORION_ENDPOINT, utils.readExampleFile('./test/ngsi-v2/Entity-valueType.json'))
.reply(201);

done();
});
it('should return success', function (done) {
request(options, function (error, response, body) {
response.statusCode.should.equal(201);
done();
});
});

it('should forward an NGSI-v2 POST request', function (done) {
request(options, function (error, response, body) {
contextBrokerMock.done();
done();
});
});
});

describe('When a concise entity with valueType is created', function () {
beforeEach(function (done) {
options.json = utils.readExampleFile('./test/ngsi-ld/Entity-valueType-concise.json');
contextBrokerMock = nock(V2_BROKER)
.post(ORION_ENDPOINT, utils.readExampleFile('./test/ngsi-v2/Entity-valueType.json'))
.reply(201);

done();
});
it('should return success', function (done) {
request(options, function (error, response, body) {
response.statusCode.should.equal(201);
done();
});
});

it('should forward an NGSI-v2 POST request', function (done) {
request(options, function (error, response, body) {
contextBrokerMock.done();
done();
});
});
});
});
Loading

0 comments on commit 2046331

Please sign in to comment.