Skip to content

Commit 563552a

Browse files
committed
test: add test for correct multipleOf validation of fractional numbers
1 parent 1c9b832 commit 563552a

File tree

1 file changed

+79
-1
lines changed

1 file changed

+79
-1
lines changed

test/client.js

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ describe('RPCClient', function(){
3535
client.handle('Heartbeat', () => {
3636
return {currentTime: new Date().toISOString()};
3737
});
38+
client.handle('TestTenth', ({params}) => {
39+
return {val: params.val / 10};
40+
});
3841
if (extra.withClient) {
3942
extra.withClient(client);
4043
}
@@ -71,6 +74,37 @@ describe('RPCClient', function(){
7174
]);
7275
}
7376

77+
function getNumberTestValidator() {
78+
return createValidator('numbers1.0', [
79+
{
80+
"$schema": "http://json-schema.org/draft-07/schema",
81+
"$id": "urn:TestTenth.req",
82+
"type": "object",
83+
"properties": {
84+
"val": {
85+
"type": "number",
86+
"multipleOf": 0.1
87+
}
88+
},
89+
"additionalProperties": false,
90+
"required": ["val"]
91+
},
92+
{
93+
"$schema": "http://json-schema.org/draft-07/schema",
94+
"$id": "urn:TestTenth.conf",
95+
"type": "object",
96+
"properties": {
97+
"val": {
98+
"type": "number",
99+
"multipleOf": 0.01
100+
}
101+
},
102+
"additionalProperties": false,
103+
"required": ["val"]
104+
}
105+
]);
106+
}
107+
74108
describe('#constructor', function(){
75109

76110
it('should throw on missing identity', async () => {
@@ -1428,8 +1462,52 @@ describe('RPCClient', function(){
14281462
}
14291463
});
14301464

1465+
it("should not reject messages due to floating point imprecision in strictMode", async () => {
1466+
1467+
const { endpoint, close, server } = await createServer({
1468+
protocols: ['numbers1.0'],
1469+
});
1470+
1471+
const cli = new RPCClient({
1472+
endpoint,
1473+
identity: 'X',
1474+
protocols: ['numbers1.0'],
1475+
strictModeValidators: [getNumberTestValidator()],
1476+
strictMode: true,
1477+
});
1478+
1479+
try {
1480+
await cli.connect();
1481+
1482+
const [c1, c2, c3, c4, c5] = await Promise.allSettled([
1483+
cli.call('TestTenth', { val: 1 }),
1484+
cli.call('TestTenth', { val: 0.1 }),
1485+
cli.call('TestTenth', { val: 9.1 }),
1486+
cli.call('TestTenth', { val: 9.11 }),
1487+
cli.call('TestTenth', { val: 57.3 / 3 }),
1488+
]);
1489+
1490+
assert.equal(c1.status, 'fulfilled');
1491+
assert.equal(c1.value.val, 1/10);
1492+
assert.equal(c2.status, 'fulfilled');
1493+
assert.equal(c2.value.val, 0.1/10);
1494+
assert.equal(c3.status, 'fulfilled');
1495+
assert.equal(c3.value.val, 9.1/10);
1496+
assert.equal(c4.status, 'rejected');
1497+
assert.ok(c4.reason instanceof RPCOccurenceConstraintViolationError);
1498+
assert.equal(c4.reason.details.errors[0].keyword, 'multipleOf');
1499+
assert.equal(c5.status, 'fulfilled');
1500+
assert.equal(c5.value.val, 57.3/3/10);
1501+
1502+
} finally {
1503+
await cli.close();
1504+
close();
1505+
}
1506+
1507+
});
1508+
14311509
it("should validate calls using in-built validators", async () => {
1432-
1510+
14331511
const {endpoint, close, server} = await createServer({
14341512
protocols: ['ocpp1.6'],
14351513
strictMode: true,

0 commit comments

Comments
 (0)